History log of /openbsd-current/sys/scsi/scsiconf.h
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.202 10-May-2023 krw

Add flag SDEV_UFI so umass_scsi_attach() can provide information
sufficient to get sdgetdisklabel() to correctly set d_type to
DTYPE_FLOPPY in the default disklabel.

installboot(8) in particular likes to know it is dealing with a
floppy.

ok miod@


Revision tags: OPENBSD_7_2_BASE OPENBSD_7_3_BASE
# 1.201 16-Apr-2022 naddy

constify SCSI adapter entry points

ok krw@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE OPENBSD_7_1_BASE
# 1.200 14-Oct-2020 krw

Introduce scsi_copy_internal_data() to copy 'faked' data from a driver to a
scsi_xfer. Will replace various equivalent functions/hand-rolled chunks, none of
which were setting xs->resid.


Revision tags: OPENBSD_6_8_BASE
# 1.199 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.198 30-Aug-2020 krw

Compactify SDEV_/ADEV_ flags & quirks to eliminate the gaps in bit use and thus
allow shrinking the names arrays.


# 1.197 28-Aug-2020 krw

Nuke CDF_ANCIENT, SDF_ANCIENT SDEV_ONLYBIG and bogus check of SID_RelAdr in
favour of simply using the device's claimed SCSI level of support. Except of
course for ATAPI/USB devices which often don't claim anything. Keep assuming
they are at least SCSI-2. Use consistant tests in
sdminphys/cdminphys/sdstart/cdstart.


# 1.196 26-Aug-2020 krw

Remove needless uses of SDEV_ONLYBIG. It was always set when SDEV_UMASS was set,
and was always checked in concert with SDEV_ATAPI. Just rely on SDEV_ATAPI and
SDEV_UMASS in all but the one place sd(4) where SDEV_ONLYBIG is set
independently of SDEV_ATAPI/_UMASS.

ok jmatthew@


# 1.195 16-Aug-2020 krw

Don't include scsi_debug.h via scsiconf.h. Nobody but scsi/* should be using the
contents thereof.

Compile tests by martjn@ (alpha), visa@ (sgi) jmatthew@ (sparc64) aoyama@
(luna88k)


# 1.194 14-Aug-2020 krw

Redistribute and tweak the scsi_[attach|probe|detach]_[bus|target|lun]() code to
make the three variants more similar and easier to understand. Ensures
consistent error checks and eliminates pointless adapter_buswidth checks when
processing the list of scsi_links.


# 1.193 20-Jul-2020 krw

Move remaining scsi bus initialization info from "prototype scsi link"
fields to struct scsibus_attach_args. Nuke the struct scsi_link *
(saa_sc_link) in scaibus_attach_args.

Explicitly initialize each field in scsibus_attach_args variables.


# 1.192 19-Jul-2020 krw

Move the adapter related items (luns, adapter, adapter_target,
adapter_buswidth, adapter_softc) from struct scsi_link to struct
scsibus_attach_args.

Additional compile tests by jmatthew@ (sparc64) and aoyam@ (luna88k).


# 1.191 16-Jul-2020 krw

Beef up struct scsibus_softc to hold the information needed to
initialize the scsi_link's on the bus. After sucking this information
out of the "prototype" link provided by the scsibus_attach_arg, no
need to keep a pointer to that prototype.


# 1.190 05-Jul-2020 krw

Nuke struct scsi_link's "scsibus" member. The two drivers using it
(ahc(4) and qlw(4)) can just compare the values of the "bus" member
directly.

A slightly different path to the same result that matthew@ traversed
in his work culminating in scsiconf.h r1.146.


# 1.189 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.


# 1.188 30-Jun-2020 krw

Garbage collect SDEV_S_WAITING, last used in scsi_base.c r1.167 in
2010.

Part of matthew@'s reverted scsiconf.h r1.146.


# 1.187 27-Jun-2020 krw

Garbage collect SDEV_2NDBUS. Only used in isp(4) which was removed in
2015 in favour of ql*(4).


# 1.186 24-Jun-2020 krw

Adapters that do not appear as a target on the SCSI bus must set
'adapter_target' to a value greater than or equal to
'adapater_buswidth' to allow all possible targets to be probed.

Add SDEV_NO_ADAPTER_TARGET (0xffff), a value guaranteed to
be greater than or equal to the u_int16_t 'adapter_buswidth'.

ok dlg@ as part of a larger diff.


Revision tags: OPENBSD_6_7_BASE
# 1.185 06-Feb-2020 krw

Try to reduce the lying, hyperbolic or obsolete commentary on
the relationships between various scsi structs.


# 1.184 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.183 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.182 08-Dec-2019 krw

Various cleanup tweaks. 'return' is not a function. KNF. Whitespace,
Comment fixes.


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.201 16-Apr-2022 naddy

constify SCSI adapter entry points

ok krw@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE OPENBSD_7_1_BASE
# 1.200 14-Oct-2020 krw

Introduce scsi_copy_internal_data() to copy 'faked' data from a driver to a
scsi_xfer. Will replace various equivalent functions/hand-rolled chunks, none of
which were setting xs->resid.


Revision tags: OPENBSD_6_8_BASE
# 1.199 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.198 30-Aug-2020 krw

Compactify SDEV_/ADEV_ flags & quirks to eliminate the gaps in bit use and thus
allow shrinking the names arrays.


# 1.197 28-Aug-2020 krw

Nuke CDF_ANCIENT, SDF_ANCIENT SDEV_ONLYBIG and bogus check of SID_RelAdr in
favour of simply using the device's claimed SCSI level of support. Except of
course for ATAPI/USB devices which often don't claim anything. Keep assuming
they are at least SCSI-2. Use consistant tests in
sdminphys/cdminphys/sdstart/cdstart.


# 1.196 26-Aug-2020 krw

Remove needless uses of SDEV_ONLYBIG. It was always set when SDEV_UMASS was set,
and was always checked in concert with SDEV_ATAPI. Just rely on SDEV_ATAPI and
SDEV_UMASS in all but the one place sd(4) where SDEV_ONLYBIG is set
independently of SDEV_ATAPI/_UMASS.

ok jmatthew@


# 1.195 16-Aug-2020 krw

Don't include scsi_debug.h via scsiconf.h. Nobody but scsi/* should be using the
contents thereof.

Compile tests by martjn@ (alpha), visa@ (sgi) jmatthew@ (sparc64) aoyama@
(luna88k)


# 1.194 14-Aug-2020 krw

Redistribute and tweak the scsi_[attach|probe|detach]_[bus|target|lun]() code to
make the three variants more similar and easier to understand. Ensures
consistent error checks and eliminates pointless adapter_buswidth checks when
processing the list of scsi_links.


# 1.193 20-Jul-2020 krw

Move remaining scsi bus initialization info from "prototype scsi link"
fields to struct scsibus_attach_args. Nuke the struct scsi_link *
(saa_sc_link) in scaibus_attach_args.

Explicitly initialize each field in scsibus_attach_args variables.


# 1.192 19-Jul-2020 krw

Move the adapter related items (luns, adapter, adapter_target,
adapter_buswidth, adapter_softc) from struct scsi_link to struct
scsibus_attach_args.

Additional compile tests by jmatthew@ (sparc64) and aoyam@ (luna88k).


# 1.191 16-Jul-2020 krw

Beef up struct scsibus_softc to hold the information needed to
initialize the scsi_link's on the bus. After sucking this information
out of the "prototype" link provided by the scsibus_attach_arg, no
need to keep a pointer to that prototype.


# 1.190 05-Jul-2020 krw

Nuke struct scsi_link's "scsibus" member. The two drivers using it
(ahc(4) and qlw(4)) can just compare the values of the "bus" member
directly.

A slightly different path to the same result that matthew@ traversed
in his work culminating in scsiconf.h r1.146.


# 1.189 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.


# 1.188 30-Jun-2020 krw

Garbage collect SDEV_S_WAITING, last used in scsi_base.c r1.167 in
2010.

Part of matthew@'s reverted scsiconf.h r1.146.


# 1.187 27-Jun-2020 krw

Garbage collect SDEV_2NDBUS. Only used in isp(4) which was removed in
2015 in favour of ql*(4).


# 1.186 24-Jun-2020 krw

Adapters that do not appear as a target on the SCSI bus must set
'adapter_target' to a value greater than or equal to
'adapater_buswidth' to allow all possible targets to be probed.

Add SDEV_NO_ADAPTER_TARGET (0xffff), a value guaranteed to
be greater than or equal to the u_int16_t 'adapter_buswidth'.

ok dlg@ as part of a larger diff.


Revision tags: OPENBSD_6_7_BASE
# 1.185 06-Feb-2020 krw

Try to reduce the lying, hyperbolic or obsolete commentary on
the relationships between various scsi structs.


# 1.184 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.183 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.182 08-Dec-2019 krw

Various cleanup tweaks. 'return' is not a function. KNF. Whitespace,
Comment fixes.


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.200 14-Oct-2020 krw

Introduce scsi_copy_internal_data() to copy 'faked' data from a driver to a
scsi_xfer. Will replace various equivalent functions/hand-rolled chunks, none of
which were setting xs->resid.


Revision tags: OPENBSD_6_8_BASE
# 1.199 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.198 30-Aug-2020 krw

Compactify SDEV_/ADEV_ flags & quirks to eliminate the gaps in bit use and thus
allow shrinking the names arrays.


# 1.197 28-Aug-2020 krw

Nuke CDF_ANCIENT, SDF_ANCIENT SDEV_ONLYBIG and bogus check of SID_RelAdr in
favour of simply using the device's claimed SCSI level of support. Except of
course for ATAPI/USB devices which often don't claim anything. Keep assuming
they are at least SCSI-2. Use consistant tests in
sdminphys/cdminphys/sdstart/cdstart.


# 1.196 26-Aug-2020 krw

Remove needless uses of SDEV_ONLYBIG. It was always set when SDEV_UMASS was set,
and was always checked in concert with SDEV_ATAPI. Just rely on SDEV_ATAPI and
SDEV_UMASS in all but the one place sd(4) where SDEV_ONLYBIG is set
independently of SDEV_ATAPI/_UMASS.

ok jmatthew@


# 1.195 16-Aug-2020 krw

Don't include scsi_debug.h via scsiconf.h. Nobody but scsi/* should be using the
contents thereof.

Compile tests by martjn@ (alpha), visa@ (sgi) jmatthew@ (sparc64) aoyama@
(luna88k)


# 1.194 14-Aug-2020 krw

Redistribute and tweak the scsi_[attach|probe|detach]_[bus|target|lun]() code to
make the three variants more similar and easier to understand. Ensures
consistent error checks and eliminates pointless adapter_buswidth checks when
processing the list of scsi_links.


# 1.193 20-Jul-2020 krw

Move remaining scsi bus initialization info from "prototype scsi link"
fields to struct scsibus_attach_args. Nuke the struct scsi_link *
(saa_sc_link) in scaibus_attach_args.

Explicitly initialize each field in scsibus_attach_args variables.


# 1.192 19-Jul-2020 krw

Move the adapter related items (luns, adapter, adapter_target,
adapter_buswidth, adapter_softc) from struct scsi_link to struct
scsibus_attach_args.

Additional compile tests by jmatthew@ (sparc64) and aoyam@ (luna88k).


# 1.191 16-Jul-2020 krw

Beef up struct scsibus_softc to hold the information needed to
initialize the scsi_link's on the bus. After sucking this information
out of the "prototype" link provided by the scsibus_attach_arg, no
need to keep a pointer to that prototype.


# 1.190 05-Jul-2020 krw

Nuke struct scsi_link's "scsibus" member. The two drivers using it
(ahc(4) and qlw(4)) can just compare the values of the "bus" member
directly.

A slightly different path to the same result that matthew@ traversed
in his work culminating in scsiconf.h r1.146.


# 1.189 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.


# 1.188 30-Jun-2020 krw

Garbage collect SDEV_S_WAITING, last used in scsi_base.c r1.167 in
2010.

Part of matthew@'s reverted scsiconf.h r1.146.


# 1.187 27-Jun-2020 krw

Garbage collect SDEV_2NDBUS. Only used in isp(4) which was removed in
2015 in favour of ql*(4).


# 1.186 24-Jun-2020 krw

Adapters that do not appear as a target on the SCSI bus must set
'adapter_target' to a value greater than or equal to
'adapater_buswidth' to allow all possible targets to be probed.

Add SDEV_NO_ADAPTER_TARGET (0xffff), a value guaranteed to
be greater than or equal to the u_int16_t 'adapter_buswidth'.

ok dlg@ as part of a larger diff.


Revision tags: OPENBSD_6_7_BASE
# 1.185 06-Feb-2020 krw

Try to reduce the lying, hyperbolic or obsolete commentary on
the relationships between various scsi structs.


# 1.184 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.183 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.182 08-Dec-2019 krw

Various cleanup tweaks. 'return' is not a function. KNF. Whitespace,
Comment fixes.


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.199 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.198 30-Aug-2020 krw

Compactify SDEV_/ADEV_ flags & quirks to eliminate the gaps in bit use and thus
allow shrinking the names arrays.


# 1.197 28-Aug-2020 krw

Nuke CDF_ANCIENT, SDF_ANCIENT SDEV_ONLYBIG and bogus check of SID_RelAdr in
favour of simply using the device's claimed SCSI level of support. Except of
course for ATAPI/USB devices which often don't claim anything. Keep assuming
they are at least SCSI-2. Use consistant tests in
sdminphys/cdminphys/sdstart/cdstart.


# 1.196 26-Aug-2020 krw

Remove needless uses of SDEV_ONLYBIG. It was always set when SDEV_UMASS was set,
and was always checked in concert with SDEV_ATAPI. Just rely on SDEV_ATAPI and
SDEV_UMASS in all but the one place sd(4) where SDEV_ONLYBIG is set
independently of SDEV_ATAPI/_UMASS.

ok jmatthew@


# 1.195 16-Aug-2020 krw

Don't include scsi_debug.h via scsiconf.h. Nobody but scsi/* should be using the
contents thereof.

Compile tests by martjn@ (alpha), visa@ (sgi) jmatthew@ (sparc64) aoyama@
(luna88k)


# 1.194 14-Aug-2020 krw

Redistribute and tweak the scsi_[attach|probe|detach]_[bus|target|lun]() code to
make the three variants more similar and easier to understand. Ensures
consistent error checks and eliminates pointless adapter_buswidth checks when
processing the list of scsi_links.


# 1.193 20-Jul-2020 krw

Move remaining scsi bus initialization info from "prototype scsi link"
fields to struct scsibus_attach_args. Nuke the struct scsi_link *
(saa_sc_link) in scaibus_attach_args.

Explicitly initialize each field in scsibus_attach_args variables.


# 1.192 19-Jul-2020 krw

Move the adapter related items (luns, adapter, adapter_target,
adapter_buswidth, adapter_softc) from struct scsi_link to struct
scsibus_attach_args.

Additional compile tests by jmatthew@ (sparc64) and aoyam@ (luna88k).


# 1.191 16-Jul-2020 krw

Beef up struct scsibus_softc to hold the information needed to
initialize the scsi_link's on the bus. After sucking this information
out of the "prototype" link provided by the scsibus_attach_arg, no
need to keep a pointer to that prototype.


# 1.190 05-Jul-2020 krw

Nuke struct scsi_link's "scsibus" member. The two drivers using it
(ahc(4) and qlw(4)) can just compare the values of the "bus" member
directly.

A slightly different path to the same result that matthew@ traversed
in his work culminating in scsiconf.h r1.146.


# 1.189 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.


# 1.188 30-Jun-2020 krw

Garbage collect SDEV_S_WAITING, last used in scsi_base.c r1.167 in
2010.

Part of matthew@'s reverted scsiconf.h r1.146.


# 1.187 27-Jun-2020 krw

Garbage collect SDEV_2NDBUS. Only used in isp(4) which was removed in
2015 in favour of ql*(4).


# 1.186 24-Jun-2020 krw

Adapters that do not appear as a target on the SCSI bus must set
'adapter_target' to a value greater than or equal to
'adapater_buswidth' to allow all possible targets to be probed.

Add SDEV_NO_ADAPTER_TARGET (0xffff), a value guaranteed to
be greater than or equal to the u_int16_t 'adapter_buswidth'.

ok dlg@ as part of a larger diff.


Revision tags: OPENBSD_6_7_BASE
# 1.185 06-Feb-2020 krw

Try to reduce the lying, hyperbolic or obsolete commentary on
the relationships between various scsi structs.


# 1.184 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.183 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.182 08-Dec-2019 krw

Various cleanup tweaks. 'return' is not a function. KNF. Whitespace,
Comment fixes.


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.198 30-Aug-2020 krw

Compactify SDEV_/ADEV_ flags & quirks to eliminate the gaps in bit use and thus
allow shrinking the names arrays.


# 1.197 28-Aug-2020 krw

Nuke CDF_ANCIENT, SDF_ANCIENT SDEV_ONLYBIG and bogus check of SID_RelAdr in
favour of simply using the device's claimed SCSI level of support. Except of
course for ATAPI/USB devices which often don't claim anything. Keep assuming
they are at least SCSI-2. Use consistant tests in
sdminphys/cdminphys/sdstart/cdstart.


# 1.196 26-Aug-2020 krw

Remove needless uses of SDEV_ONLYBIG. It was always set when SDEV_UMASS was set,
and was always checked in concert with SDEV_ATAPI. Just rely on SDEV_ATAPI and
SDEV_UMASS in all but the one place sd(4) where SDEV_ONLYBIG is set
independently of SDEV_ATAPI/_UMASS.

ok jmatthew@


# 1.195 16-Aug-2020 krw

Don't include scsi_debug.h via scsiconf.h. Nobody but scsi/* should be using the
contents thereof.

Compile tests by martjn@ (alpha), visa@ (sgi) jmatthew@ (sparc64) aoyama@
(luna88k)


# 1.194 14-Aug-2020 krw

Redistribute and tweak the scsi_[attach|probe|detach]_[bus|target|lun]() code to
make the three variants more similar and easier to understand. Ensures
consistent error checks and eliminates pointless adapter_buswidth checks when
processing the list of scsi_links.


# 1.193 20-Jul-2020 krw

Move remaining scsi bus initialization info from "prototype scsi link"
fields to struct scsibus_attach_args. Nuke the struct scsi_link *
(saa_sc_link) in scaibus_attach_args.

Explicitly initialize each field in scsibus_attach_args variables.


# 1.192 19-Jul-2020 krw

Move the adapter related items (luns, adapter, adapter_target,
adapter_buswidth, adapter_softc) from struct scsi_link to struct
scsibus_attach_args.

Additional compile tests by jmatthew@ (sparc64) and aoyam@ (luna88k).


# 1.191 16-Jul-2020 krw

Beef up struct scsibus_softc to hold the information needed to
initialize the scsi_link's on the bus. After sucking this information
out of the "prototype" link provided by the scsibus_attach_arg, no
need to keep a pointer to that prototype.


# 1.190 05-Jul-2020 krw

Nuke struct scsi_link's "scsibus" member. The two drivers using it
(ahc(4) and qlw(4)) can just compare the values of the "bus" member
directly.

A slightly different path to the same result that matthew@ traversed
in his work culminating in scsiconf.h r1.146.


# 1.189 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.


# 1.188 30-Jun-2020 krw

Garbage collect SDEV_S_WAITING, last used in scsi_base.c r1.167 in
2010.

Part of matthew@'s reverted scsiconf.h r1.146.


# 1.187 27-Jun-2020 krw

Garbage collect SDEV_2NDBUS. Only used in isp(4) which was removed in
2015 in favour of ql*(4).


# 1.186 24-Jun-2020 krw

Adapters that do not appear as a target on the SCSI bus must set
'adapter_target' to a value greater than or equal to
'adapater_buswidth' to allow all possible targets to be probed.

Add SDEV_NO_ADAPTER_TARGET (0xffff), a value guaranteed to
be greater than or equal to the u_int16_t 'adapter_buswidth'.

ok dlg@ as part of a larger diff.


Revision tags: OPENBSD_6_7_BASE
# 1.185 06-Feb-2020 krw

Try to reduce the lying, hyperbolic or obsolete commentary on
the relationships between various scsi structs.


# 1.184 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.183 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.182 08-Dec-2019 krw

Various cleanup tweaks. 'return' is not a function. KNF. Whitespace,
Comment fixes.


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.197 28-Aug-2020 krw

Nuke CDF_ANCIENT, SDF_ANCIENT SDEV_ONLYBIG and bogus check of SID_RelAdr in
favour of simply using the device's claimed SCSI level of support. Except of
course for ATAPI/USB devices which often don't claim anything. Keep assuming
they are at least SCSI-2. Use consistant tests in
sdminphys/cdminphys/sdstart/cdstart.


# 1.196 26-Aug-2020 krw

Remove needless uses of SDEV_ONLYBIG. It was always set when SDEV_UMASS was set,
and was always checked in concert with SDEV_ATAPI. Just rely on SDEV_ATAPI and
SDEV_UMASS in all but the one place sd(4) where SDEV_ONLYBIG is set
independently of SDEV_ATAPI/_UMASS.

ok jmatthew@


# 1.195 16-Aug-2020 krw

Don't include scsi_debug.h via scsiconf.h. Nobody but scsi/* should be using the
contents thereof.

Compile tests by martjn@ (alpha), visa@ (sgi) jmatthew@ (sparc64) aoyama@
(luna88k)


# 1.194 14-Aug-2020 krw

Redistribute and tweak the scsi_[attach|probe|detach]_[bus|target|lun]() code to
make the three variants more similar and easier to understand. Ensures
consistent error checks and eliminates pointless adapter_buswidth checks when
processing the list of scsi_links.


# 1.193 20-Jul-2020 krw

Move remaining scsi bus initialization info from "prototype scsi link"
fields to struct scsibus_attach_args. Nuke the struct scsi_link *
(saa_sc_link) in scaibus_attach_args.

Explicitly initialize each field in scsibus_attach_args variables.


# 1.192 19-Jul-2020 krw

Move the adapter related items (luns, adapter, adapter_target,
adapter_buswidth, adapter_softc) from struct scsi_link to struct
scsibus_attach_args.

Additional compile tests by jmatthew@ (sparc64) and aoyam@ (luna88k).


# 1.191 16-Jul-2020 krw

Beef up struct scsibus_softc to hold the information needed to
initialize the scsi_link's on the bus. After sucking this information
out of the "prototype" link provided by the scsibus_attach_arg, no
need to keep a pointer to that prototype.


# 1.190 05-Jul-2020 krw

Nuke struct scsi_link's "scsibus" member. The two drivers using it
(ahc(4) and qlw(4)) can just compare the values of the "bus" member
directly.

A slightly different path to the same result that matthew@ traversed
in his work culminating in scsiconf.h r1.146.


# 1.189 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.


# 1.188 30-Jun-2020 krw

Garbage collect SDEV_S_WAITING, last used in scsi_base.c r1.167 in
2010.

Part of matthew@'s reverted scsiconf.h r1.146.


# 1.187 27-Jun-2020 krw

Garbage collect SDEV_2NDBUS. Only used in isp(4) which was removed in
2015 in favour of ql*(4).


# 1.186 24-Jun-2020 krw

Adapters that do not appear as a target on the SCSI bus must set
'adapter_target' to a value greater than or equal to
'adapater_buswidth' to allow all possible targets to be probed.

Add SDEV_NO_ADAPTER_TARGET (0xffff), a value guaranteed to
be greater than or equal to the u_int16_t 'adapter_buswidth'.

ok dlg@ as part of a larger diff.


Revision tags: OPENBSD_6_7_BASE
# 1.185 06-Feb-2020 krw

Try to reduce the lying, hyperbolic or obsolete commentary on
the relationships between various scsi structs.


# 1.184 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.183 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.182 08-Dec-2019 krw

Various cleanup tweaks. 'return' is not a function. KNF. Whitespace,
Comment fixes.


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.196 26-Aug-2020 krw

Remove needless uses of SDEV_ONLYBIG. It was always set when SDEV_UMASS was set,
and was always checked in concert with SDEV_ATAPI. Just rely on SDEV_ATAPI and
SDEV_UMASS in all but the one place sd(4) where SDEV_ONLYBIG is set
independently of SDEV_ATAPI/_UMASS.

ok jmatthew@


# 1.195 16-Aug-2020 krw

Don't include scsi_debug.h via scsiconf.h. Nobody but scsi/* should be using the
contents thereof.

Compile tests by martjn@ (alpha), visa@ (sgi) jmatthew@ (sparc64) aoyama@
(luna88k)


# 1.194 14-Aug-2020 krw

Redistribute and tweak the scsi_[attach|probe|detach]_[bus|target|lun]() code to
make the three variants more similar and easier to understand. Ensures
consistent error checks and eliminates pointless adapter_buswidth checks when
processing the list of scsi_links.


# 1.193 20-Jul-2020 krw

Move remaining scsi bus initialization info from "prototype scsi link"
fields to struct scsibus_attach_args. Nuke the struct scsi_link *
(saa_sc_link) in scaibus_attach_args.

Explicitly initialize each field in scsibus_attach_args variables.


# 1.192 19-Jul-2020 krw

Move the adapter related items (luns, adapter, adapter_target,
adapter_buswidth, adapter_softc) from struct scsi_link to struct
scsibus_attach_args.

Additional compile tests by jmatthew@ (sparc64) and aoyam@ (luna88k).


# 1.191 16-Jul-2020 krw

Beef up struct scsibus_softc to hold the information needed to
initialize the scsi_link's on the bus. After sucking this information
out of the "prototype" link provided by the scsibus_attach_arg, no
need to keep a pointer to that prototype.


# 1.190 05-Jul-2020 krw

Nuke struct scsi_link's "scsibus" member. The two drivers using it
(ahc(4) and qlw(4)) can just compare the values of the "bus" member
directly.

A slightly different path to the same result that matthew@ traversed
in his work culminating in scsiconf.h r1.146.


# 1.189 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.


# 1.188 30-Jun-2020 krw

Garbage collect SDEV_S_WAITING, last used in scsi_base.c r1.167 in
2010.

Part of matthew@'s reverted scsiconf.h r1.146.


# 1.187 27-Jun-2020 krw

Garbage collect SDEV_2NDBUS. Only used in isp(4) which was removed in
2015 in favour of ql*(4).


# 1.186 24-Jun-2020 krw

Adapters that do not appear as a target on the SCSI bus must set
'adapter_target' to a value greater than or equal to
'adapater_buswidth' to allow all possible targets to be probed.

Add SDEV_NO_ADAPTER_TARGET (0xffff), a value guaranteed to
be greater than or equal to the u_int16_t 'adapter_buswidth'.

ok dlg@ as part of a larger diff.


Revision tags: OPENBSD_6_7_BASE
# 1.185 06-Feb-2020 krw

Try to reduce the lying, hyperbolic or obsolete commentary on
the relationships between various scsi structs.


# 1.184 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.183 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.182 08-Dec-2019 krw

Various cleanup tweaks. 'return' is not a function. KNF. Whitespace,
Comment fixes.


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.195 16-Aug-2020 krw

Don't include scsi_debug.h via scsiconf.h. Nobody but scsi/* should be using the
contents thereof.

Compile tests by martjn@ (alpha), visa@ (sgi) jmatthew@ (sparc64) aoyama@
(luna88k)


# 1.194 14-Aug-2020 krw

Redistribute and tweak the scsi_[attach|probe|detach]_[bus|target|lun]() code to
make the three variants more similar and easier to understand. Ensures
consistent error checks and eliminates pointless adapter_buswidth checks when
processing the list of scsi_links.


# 1.193 20-Jul-2020 krw

Move remaining scsi bus initialization info from "prototype scsi link"
fields to struct scsibus_attach_args. Nuke the struct scsi_link *
(saa_sc_link) in scaibus_attach_args.

Explicitly initialize each field in scsibus_attach_args variables.


# 1.192 19-Jul-2020 krw

Move the adapter related items (luns, adapter, adapter_target,
adapter_buswidth, adapter_softc) from struct scsi_link to struct
scsibus_attach_args.

Additional compile tests by jmatthew@ (sparc64) and aoyam@ (luna88k).


# 1.191 16-Jul-2020 krw

Beef up struct scsibus_softc to hold the information needed to
initialize the scsi_link's on the bus. After sucking this information
out of the "prototype" link provided by the scsibus_attach_arg, no
need to keep a pointer to that prototype.


# 1.190 05-Jul-2020 krw

Nuke struct scsi_link's "scsibus" member. The two drivers using it
(ahc(4) and qlw(4)) can just compare the values of the "bus" member
directly.

A slightly different path to the same result that matthew@ traversed
in his work culminating in scsiconf.h r1.146.


# 1.189 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.


# 1.188 30-Jun-2020 krw

Garbage collect SDEV_S_WAITING, last used in scsi_base.c r1.167 in
2010.

Part of matthew@'s reverted scsiconf.h r1.146.


# 1.187 27-Jun-2020 krw

Garbage collect SDEV_2NDBUS. Only used in isp(4) which was removed in
2015 in favour of ql*(4).


# 1.186 24-Jun-2020 krw

Adapters that do not appear as a target on the SCSI bus must set
'adapter_target' to a value greater than or equal to
'adapater_buswidth' to allow all possible targets to be probed.

Add SDEV_NO_ADAPTER_TARGET (0xffff), a value guaranteed to
be greater than or equal to the u_int16_t 'adapter_buswidth'.

ok dlg@ as part of a larger diff.


Revision tags: OPENBSD_6_7_BASE
# 1.185 06-Feb-2020 krw

Try to reduce the lying, hyperbolic or obsolete commentary on
the relationships between various scsi structs.


# 1.184 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.183 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.182 08-Dec-2019 krw

Various cleanup tweaks. 'return' is not a function. KNF. Whitespace,
Comment fixes.


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.194 14-Aug-2020 krw

Redistribute and tweak the scsi_[attach|probe|detach]_[bus|target|lun]() code to
make the three variants more similar and easier to understand. Ensures
consistent error checks and eliminates pointless adapter_buswidth checks when
processing the list of scsi_links.


# 1.193 20-Jul-2020 krw

Move remaining scsi bus initialization info from "prototype scsi link"
fields to struct scsibus_attach_args. Nuke the struct scsi_link *
(saa_sc_link) in scaibus_attach_args.

Explicitly initialize each field in scsibus_attach_args variables.


# 1.192 19-Jul-2020 krw

Move the adapter related items (luns, adapter, adapter_target,
adapter_buswidth, adapter_softc) from struct scsi_link to struct
scsibus_attach_args.

Additional compile tests by jmatthew@ (sparc64) and aoyam@ (luna88k).


# 1.191 16-Jul-2020 krw

Beef up struct scsibus_softc to hold the information needed to
initialize the scsi_link's on the bus. After sucking this information
out of the "prototype" link provided by the scsibus_attach_arg, no
need to keep a pointer to that prototype.


# 1.190 05-Jul-2020 krw

Nuke struct scsi_link's "scsibus" member. The two drivers using it
(ahc(4) and qlw(4)) can just compare the values of the "bus" member
directly.

A slightly different path to the same result that matthew@ traversed
in his work culminating in scsiconf.h r1.146.


# 1.189 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.


# 1.188 30-Jun-2020 krw

Garbage collect SDEV_S_WAITING, last used in scsi_base.c r1.167 in
2010.

Part of matthew@'s reverted scsiconf.h r1.146.


# 1.187 27-Jun-2020 krw

Garbage collect SDEV_2NDBUS. Only used in isp(4) which was removed in
2015 in favour of ql*(4).


# 1.186 24-Jun-2020 krw

Adapters that do not appear as a target on the SCSI bus must set
'adapter_target' to a value greater than or equal to
'adapater_buswidth' to allow all possible targets to be probed.

Add SDEV_NO_ADAPTER_TARGET (0xffff), a value guaranteed to
be greater than or equal to the u_int16_t 'adapter_buswidth'.

ok dlg@ as part of a larger diff.


Revision tags: OPENBSD_6_7_BASE
# 1.185 06-Feb-2020 krw

Try to reduce the lying, hyperbolic or obsolete commentary on
the relationships between various scsi structs.


# 1.184 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.183 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.182 08-Dec-2019 krw

Various cleanup tweaks. 'return' is not a function. KNF. Whitespace,
Comment fixes.


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.193 20-Jul-2020 krw

Move remaining scsi bus initialization info from "prototype scsi link"
fields to struct scsibus_attach_args. Nuke the struct scsi_link *
(saa_sc_link) in scaibus_attach_args.

Explicitly initialize each field in scsibus_attach_args variables.


# 1.192 19-Jul-2020 krw

Move the adapter related items (luns, adapter, adapter_target,
adapter_buswidth, adapter_softc) from struct scsi_link to struct
scsibus_attach_args.

Additional compile tests by jmatthew@ (sparc64) and aoyam@ (luna88k).


# 1.191 16-Jul-2020 krw

Beef up struct scsibus_softc to hold the information needed to
initialize the scsi_link's on the bus. After sucking this information
out of the "prototype" link provided by the scsibus_attach_arg, no
need to keep a pointer to that prototype.


# 1.190 05-Jul-2020 krw

Nuke struct scsi_link's "scsibus" member. The two drivers using it
(ahc(4) and qlw(4)) can just compare the values of the "bus" member
directly.

A slightly different path to the same result that matthew@ traversed
in his work culminating in scsiconf.h r1.146.


# 1.189 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.


# 1.188 30-Jun-2020 krw

Garbage collect SDEV_S_WAITING, last used in scsi_base.c r1.167 in
2010.

Part of matthew@'s reverted scsiconf.h r1.146.


# 1.187 27-Jun-2020 krw

Garbage collect SDEV_2NDBUS. Only used in isp(4) which was removed in
2015 in favour of ql*(4).


# 1.186 24-Jun-2020 krw

Adapters that do not appear as a target on the SCSI bus must set
'adapter_target' to a value greater than or equal to
'adapater_buswidth' to allow all possible targets to be probed.

Add SDEV_NO_ADAPTER_TARGET (0xffff), a value guaranteed to
be greater than or equal to the u_int16_t 'adapter_buswidth'.

ok dlg@ as part of a larger diff.


Revision tags: OPENBSD_6_7_BASE
# 1.185 06-Feb-2020 krw

Try to reduce the lying, hyperbolic or obsolete commentary on
the relationships between various scsi structs.


# 1.184 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.183 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.182 08-Dec-2019 krw

Various cleanup tweaks. 'return' is not a function. KNF. Whitespace,
Comment fixes.


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.191 16-Jul-2020 krw

Beef up struct scsibus_softc to hold the information needed to
initialize the scsi_link's on the bus. After sucking this information
out of the "prototype" link provided by the scsibus_attach_arg, no
need to keep a pointer to that prototype.


# 1.190 05-Jul-2020 krw

Nuke struct scsi_link's "scsibus" member. The two drivers using it
(ahc(4) and qlw(4)) can just compare the values of the "bus" member
directly.

A slightly different path to the same result that matthew@ traversed
in his work culminating in scsiconf.h r1.146.


# 1.189 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.


# 1.188 30-Jun-2020 krw

Garbage collect SDEV_S_WAITING, last used in scsi_base.c r1.167 in
2010.

Part of matthew@'s reverted scsiconf.h r1.146.


# 1.187 27-Jun-2020 krw

Garbage collect SDEV_2NDBUS. Only used in isp(4) which was removed in
2015 in favour of ql*(4).


# 1.186 24-Jun-2020 krw

Adapters that do not appear as a target on the SCSI bus must set
'adapter_target' to a value greater than or equal to
'adapater_buswidth' to allow all possible targets to be probed.

Add SDEV_NO_ADAPTER_TARGET (0xffff), a value guaranteed to
be greater than or equal to the u_int16_t 'adapter_buswidth'.

ok dlg@ as part of a larger diff.


Revision tags: OPENBSD_6_7_BASE
# 1.185 06-Feb-2020 krw

Try to reduce the lying, hyperbolic or obsolete commentary on
the relationships between various scsi structs.


# 1.184 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.183 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.182 08-Dec-2019 krw

Various cleanup tweaks. 'return' is not a function. KNF. Whitespace,
Comment fixes.


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.190 05-Jul-2020 krw

Nuke struct scsi_link's "scsibus" member. The two drivers using it
(ahc(4) and qlw(4)) can just compare the values of the "bus" member
directly.

A slightly different path to the same result that matthew@ traversed
in his work culminating in scsiconf.h r1.146.


# 1.189 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.


# 1.188 30-Jun-2020 krw

Garbage collect SDEV_S_WAITING, last used in scsi_base.c r1.167 in
2010.

Part of matthew@'s reverted scsiconf.h r1.146.


# 1.187 27-Jun-2020 krw

Garbage collect SDEV_2NDBUS. Only used in isp(4) which was removed in
2015 in favour of ql*(4).


# 1.186 24-Jun-2020 krw

Adapters that do not appear as a target on the SCSI bus must set
'adapter_target' to a value greater than or equal to
'adapater_buswidth' to allow all possible targets to be probed.

Add SDEV_NO_ADAPTER_TARGET (0xffff), a value guaranteed to
be greater than or equal to the u_int16_t 'adapter_buswidth'.

ok dlg@ as part of a larger diff.


Revision tags: OPENBSD_6_7_BASE
# 1.185 06-Feb-2020 krw

Try to reduce the lying, hyperbolic or obsolete commentary on
the relationships between various scsi structs.


# 1.184 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.183 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.182 08-Dec-2019 krw

Various cleanup tweaks. 'return' is not a function. KNF. Whitespace,
Comment fixes.


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.189 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.


# 1.188 30-Jun-2020 krw

Garbage collect SDEV_S_WAITING, last used in scsi_base.c r1.167 in
2010.

Part of matthew@'s reverted scsiconf.h r1.146.


# 1.187 27-Jun-2020 krw

Garbage collect SDEV_2NDBUS. Only used in isp(4) which was removed in
2015 in favour of ql*(4).


# 1.186 24-Jun-2020 krw

Adapters that do not appear as a target on the SCSI bus must set
'adapter_target' to a value greater than or equal to
'adapater_buswidth' to allow all possible targets to be probed.

Add SDEV_NO_ADAPTER_TARGET (0xffff), a value guaranteed to
be greater than or equal to the u_int16_t 'adapter_buswidth'.

ok dlg@ as part of a larger diff.


Revision tags: OPENBSD_6_7_BASE
# 1.185 06-Feb-2020 krw

Try to reduce the lying, hyperbolic or obsolete commentary on
the relationships between various scsi structs.


# 1.184 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.183 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.182 08-Dec-2019 krw

Various cleanup tweaks. 'return' is not a function. KNF. Whitespace,
Comment fixes.


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.187 27-Jun-2020 krw

Garbage collect SDEV_2NDBUS. Only used in isp(4) which was removed in
2015 in favour of ql*(4).


# 1.186 24-Jun-2020 krw

Adapters that do not appear as a target on the SCSI bus must set
'adapter_target' to a value greater than or equal to
'adapater_buswidth' to allow all possible targets to be probed.

Add SDEV_NO_ADAPTER_TARGET (0xffff), a value guaranteed to
be greater than or equal to the u_int16_t 'adapter_buswidth'.

ok dlg@ as part of a larger diff.


Revision tags: OPENBSD_6_7_BASE
# 1.185 06-Feb-2020 krw

Try to reduce the lying, hyperbolic or obsolete commentary on
the relationships between various scsi structs.


# 1.184 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.183 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.182 08-Dec-2019 krw

Various cleanup tweaks. 'return' is not a function. KNF. Whitespace,
Comment fixes.


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.186 24-Jun-2020 krw

Adapters that do not appear as a target on the SCSI bus must set
'adapter_target' to a value greater than or equal to
'adapater_buswidth' to allow all possible targets to be probed.

Add SDEV_NO_ADAPTER_TARGET (0xffff), a value guaranteed to
be greater than or equal to the u_int16_t 'adapter_buswidth'.

ok dlg@ as part of a larger diff.


Revision tags: OPENBSD_6_7_BASE
# 1.185 06-Feb-2020 krw

Try to reduce the lying, hyperbolic or obsolete commentary on
the relationships between various scsi structs.


# 1.184 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.183 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.182 08-Dec-2019 krw

Various cleanup tweaks. 'return' is not a function. KNF. Whitespace,
Comment fixes.


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.185 06-Feb-2020 krw

Try to reduce the lying, hyperbolic or obsolete commentary on
the relationships between various scsi structs.


# 1.184 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.183 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.182 08-Dec-2019 krw

Various cleanup tweaks. 'return' is not a function. KNF. Whitespace,
Comment fixes.


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.184 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.183 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.182 08-Dec-2019 krw

Various cleanup tweaks. 'return' is not a function. KNF. Whitespace,
Comment fixes.


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.183 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.182 08-Dec-2019 krw

Various cleanup tweaks. 'return' is not a function. KNF. Whitespace,
Comment fixes.


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.182 08-Dec-2019 krw

Various cleanup tweaks. 'return' is not a function. KNF. Whitespace,
Comment fixes.


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.181 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.180 05-Dec-2019 krw

Move prototypes for scsi_mode_sense(), scsi_mode_sense_big(),
scsi_mode_sense_page() and scsi_mode_sense_big_page() into
scsi_base.c.

They are just internal helper functions for scsi_do_mode_sense().


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.179 03-Dec-2019 krw

Check for expected mode sense page code as well as expected mode page length when
constructing the pointer to the page data. Remove now unneeded DISK_PGCODE().

Usual misc whitespace/modernization tweaks to functions being modified.


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.178 28-Nov-2019 krw

Abstract mode sense block descriptor parsing into a separate function.


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.177 25-Nov-2019 krw

Move struct scsi_read_cap_data and struct scsi_read_cap_data_16 to
scsi_all.h.

Add scsi_read_cap_10() and scsi_read_cap_16() functions to
scsi_base.c, i.e. move logic to do actual READ_CAPACITY commands out
of sd_read_cap() and sd_read_cap_16().

This will allow the READ_CAPACITY code to be reused by cd(4).

Return -1 for errors where the error code is just discarded, reducing
ENOMEM, ENXIO, EIO uses.

No intentional functional change.


Revision tags: OPENBSD_6_6_BASE
# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.176 27-Sep-2019 krw

Use consistent idiom/naming convention for the the #include
guards. Spelunkers using grep are easily confused.


# 1.175 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.174 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.173 19-Sep-2019 krw

Introduce and use convenience SID_REPOONSE_FORMAT() define.


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.172 01-Sep-2019 krw

Adopt the SCSI versioning #define's from FreeBSD. Eliminate the
now unneeded version_to_spc() mapping array, a duplicate #define
and a couple of magic numbers. Toss in some comments for future
generations of spelunkers.

Makes it possible to check for specific SPC versions when new
features or eliminated features require such a check.

No intentional functional change.


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.171 28-Aug-2019 krw

Introduce SCSI0(), SCSI2() and SCSI3() defines to clarify (some) uses
of SCSISPC() when checking the values of the INQUIRY version field.


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.170 24-Aug-2019 krw

Simply logic of detaching things. scsi_detach_bus() folded into
scsi_detach(), scsi_detach_target() and scsi_detach_lun() become
simple wrappers of scsi_detach() invocations.

No intentional functional change.


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.169 20-Aug-2019 krw

scsi_probe_bus() always returns 0. Nobody but scsi_probe() even
pretended to care. So just make in a void, and explicitly return 0 in
the appropriate case in scsi_probe().


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.168 18-Aug-2019 krw

sc_buswidth field in struct scsi_link is redundant. Just use
adapter_link->adapter_buswidth, which supplied the value for
sc_buswidth and is never changed.


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.167 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


Revision tags: OPENBSD_6_2_BASE
# 1.166 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@


# 1.165 19-May-2017 sf

Add some comments about the assumptions of the scsi mid layer

ok krw@


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.164 10-Mar-2016 krw

Enforce some naming sanity. Stop using 'sc_link' to mean two different
things by renaming the field 'SLIST_HEAD(, scsi_link) sc_link' to
'sc_link_list' in struct scsibus_softc. Use 'sb' as the short name
for scsibus_softc variables.

Impetus from & ok bluhm@


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

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.162 09-Sep-2014 dlg

remove the scsi_task() wrapper around workq_add_task now that nothing
uses it. dont need to include workq.h anymore here either now.


Revision tags: OPENBSD_5_6_BASE
# 1.161 22-Apr-2014 dlg

factor out the code that figures out whether you're probing or detaching
a whole bus, a target, or a specific lun on a target from the bioctl
and scsi_req paths.

i want to reuse this factored code for something claudio wants.


Revision tags: OPENBSD_5_5_BASE
# 1.160 31-Jan-2014 dlg

if a device doesnt have device ids or serial numbers, try using node_wwn to
generate a devid. if its an fc device this is good enough.


# 1.159 27-Jan-2014 dlg

poison the io "allocated" by the default pool allocator so any attempt to
use it should cause a fault.

based on discussion with miod@


# 1.158 18-Jan-2014 dlg

rename scsi_ioh_runqueue to scsi_iopool_run, and make it available
outside scsi_base.c.

this will allow adapters to restrict access to iopool resources
based on some state, and then kick the pending requests on the pool
when the state comes good again.

ive been avoiding this for a long time, but it is the least worst
way to deal with some uses of XS_NO_CCB.

discussion with kettenis@ helped me decide this was right.


# 1.157 27-Sep-2013 krw

scsi_size() is now used only by cd(4). So move it from scsi_base.c
to cd.c and call it cd_size(), like sd_size() lives in sd.c.

Tweak some daddr_t variables to u_int64_t on the way, when they are
for disk sector numbers, not 512-byte block numbers.


# 1.156 29-Aug-2013 dlg

rename scsi_sem_{enter,leave} to scsi_pending_{start,finish}. these are
the wrappers around handling of pending work, theyre not semaphores.

names from tedu@
ok krw@ guenther@


# 1.155 26-Aug-2013 dlg

make scsi_{xsh,ioh}_{add,del} return whether they moved something
on or off the queues so things calling them can tell if something
is or isnt going to happen.


# 1.154 25-Aug-2013 dlg

move the prototypes of scsi_sem_{enter,leave} into a header so things
other than scsi_base.c can use them.


Revision tags: OPENBSD_5_4_BASE
# 1.153 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.152 28-Mar-2013 deraadt

do not include machine/cpu.h from a .c file; it is the responsibility of
.h files to pull it in, if needed
ok tedu


# 1.151 04-Mar-2013 dlg

replace the LIST_ENTRY in scsi_xfer with a SIMPLEQ_ENTRY. the things that
use it want something they can add to the end of they do horrible things
to be able to do that with the LIST_ENTRY.

this makes those horrible things a bit less horrible, and makes it easier
to use lists of scsi_xfers in other things.


Revision tags: OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.150 01-Jul-2012 krw

Nuke unused _[23]ltol() and _lto[23]l() inline functions. Move
_4ltol() and _lto4l() to bha, the only place they were used.

ok dlg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.149 17-Jul-2011 matthew

Backout a bunch of my SCSI commits from c2k11. At least one of these
is causing problems when trying to boot sparc64 from an isp(4).

Verified to fix the sparc64/isp(4) regression by krw@; ok deraadt@


# 1.148 09-Jul-2011 matthew

Add SCSI_NO_ADAPTER_TARGET as a value for adapters to set
adapter_target to if their adapter isn't addressable on the bus.

ok dlg@, krw@


# 1.147 06-Jul-2011 matthew

Add {sc,saa}_{targets,luns} to scsibus_softc and scsibus_attach_args.
These will be used to replace scsi_link's adapter_buswidth and luns
fields, but for now we stay compatible with existing SCSI adapter
driver conventions while I update them to set the scsibus_attach_args
fields directly.

ok dlg@


# 1.146 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.145 15-Jun-2011 dlg

factor the common bits out of code that builds scsi inquiry commands into
scsi_init_inquiry(). cut the compiled INQUIRY code over to it.

ok and tweaks from krw@
ok matthew@


# 1.144 06-Apr-2011 dlg

add a new "serial" devid type for scsi devices. add code to usb that fakes
it up by using the usb devices iSerial thing.

ok deraadt@


# 1.143 05-Apr-2011 dlg

move forward with scsi multipathing.

the big change is how paths between mpath capable devices and the
kernel are managed.

originally the midlayer would steal the links to the devices and
hide them behind mpath. all the changes an adapter made to a link
(eg activate or detach), the midlayer had to test if it was an mpath
link and then call special mpath code to handle it.

the original code also assumed that all paths behaved the same, but
the reality is that different devices have different command sets
and behaviours. figuring out which behaviour to pick and prioritising
them is basically the same job autoconf does with match and attach.

rather than special casing mpath in the midlayer and reimplimenting
autoconf, this turns paths into actual device drivers with match
and attach routines. after they figure out if the path is active,
they then give it to mpath(4) to use as a backend.

i have written drivers for symmetric access devices (sym(4)) where
all paths to the same logical unit are as good as each other,
lsi/engenio arrays (rdac(4), and emc arrays (emc(4)).

the rdac and emc drivers only detect active paths at attach time,
the do not cope if the controller changes state unless you unplug
the path and plug it in again to retest the active state. they also
do not have support for directing array failover.

operating and hoplugging has been tested with mpii(4), fc and sas
mpi(4), and iscsi via vscsi (claudio did this too).

ok krw@ deraadt@


Revision tags: OPENBSD_4_9_BASE
# 1.142 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.141 12-Oct-2010 krw

Force openings to 1 for devices that can't do tagged i/o, i.e. more
than 1 i/o active at once. This reduces the chances that concurrent
i/o's for such devices will confuse the device or the adapter code.
It also eliminates a reason for adapter code to maintain its own
queues.

Tweak all drivers that fake INQUIRY results to set the SID_CmdQue
flag, thus continuing to claim to be able to do tagged i/o.

Positive feedback from matthew@ and marco@ for an earlier version.

ok dlg@


# 1.140 20-Sep-2010 dlg

bring back scsi_base.c r1.195 and scsiconf.h r1.138 now that
src/sys/dev/ata/atascsi.c r1.92 solves the problem that my scsi_link
change tickled.

tested by josh elsasser who reported the problem in pr6470
deraadt@ is letting it in again so it can get widespread testing


# 1.139 17-Sep-2010 dlg

revert scsi_base.c r1.195 and scsiconf.h r1.138 as it screws up with some
disks in atascsi.

as reported in pr6470


# 1.138 14-Sep-2010 dlg

the openings member of scsi_link is the maximum number of commands that a
scsi device can have in flight. instead of counting users of openings on
the bus by taking away from the openings value, count the number of
pending commands on the bus in a new pending variable.

this lets us know how many outstanding commands there are. we can then use
that to make sure that all commands a device has generated get completed
before detaching the device. this helps avoid resource leaks and use after
frees.

tested by me on pci ehci/umass, fc mpi, and sas mpi.
tested by jakemsr on cardbus ohci umass.
it found issues in sas mpi which were fixed as a result of this diff.
ok krw@


# 1.137 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.136 25-Aug-2010 dlg

add scsi_iopool_destroy and scsi_link_shutdown. when a link or
device are going away, this will walk the pool and link queues and
wake up processes that are sleeping while waiting for an io or xs.
they will return NULL to the scsi_{xs,io}_get callers, which should
then check if they device is still alive. all other handlers that
are registered on the queues should be removed by their owners
before the destroy/shutdown funcs are called.

lots of help and discussion with matthew@
ok matthew@


Revision tags: OPENBSD_4_8_BASE
# 1.135 27-Jul-2010 matthew

Add scsi_cmd_rw_decode() for decoding any SCSI READ or WRITE command,
and update atascsi(4) to make use of it. (Other HBAs will be updated
post-release.) Should allow for use of SATA drives with >2^32 LBAs.

ok deraadt@, dlg@, krw@


# 1.134 27-Jul-2010 dlg

the queue entry and state variables in the xsh and ioh structs are part
of a separate struct which the ioh struct includes for no good reason
anymore. just put the vars directly in the ioh.

this removes this useless abstraction.

ok krw@ matthew@


# 1.133 24-Jul-2010 matthew

Get rid of scsi_deinit(), and change scsi_init() back to a one-time
initialization strategy, rather than pretending to do user reference
counting. Previously, we would re-initialize the SCSI pool(9)s, which
had the fun consequence of causing sysctl(kern.pool.npools) to
infinite loop at IPL_VM.

ok krw@


# 1.132 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.131 13-Jul-2010 krw

Correct comment. scsi_sense_data is now 18 bytes, not 32.


# 1.130 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.129 01-Jul-2010 matthew

Change scsibus(4)'s scsi_link array to an SLIST to save memory on
sparsely populated buses.

ok dlg@, krw@


# 1.128 27-Jun-2010 matthew

"Believe it or not," nothing uses scsi_xfer's req_sense_length field
in any meaningful way, so just get rid of it.

ok krw@, dlg@


# 1.127 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.126 14-Jun-2010 thib

remove the scsi_buf_* functions as these have been
replaced by BUFQ's.

OK krw@, dlg@


# 1.125 24-May-2010 dlg

removed prototypes for functions that were removed.

patch from matthew dempsky


# 1.124 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.123 16-Apr-2010 dlg

whitespace fix


# 1.122 16-Apr-2010 dlg

fix an fatal flaw with iopools.

an xshandler gets put on a series of lists as it allocates different
resources, and uses the same tailq entry on each of these lists as
its only supposed to be on one of them at a time. however, it was
possible for the xshandler to be added to both at the same time,
therefore corrupting the lists and leading to a panic.

this diff moves from using separate flags for each queue an xshandler
could be on to having a single state variable that shows which one
it is on (or not on). this prevents an xshandler on the io runqueue
from being added to the openings runqueue, which in turn prevents
the list corruption.

some operations have been reordered to avoid races and complexity
in this little state machine.


# 1.121 06-Apr-2010 dlg

implement a new mechanism for allocating resources on the bus.

instead of optimistically trying to use a resource by executing an
xs and then failing when there's no room for it, this puts things
that want to use the hardware on a runqueue. as resources become
available on the bus then consumers on the runqueue are popped off
and guaranteed access to the resource.

the resources are generally "ccbs" in adapter drivers, so this
abstracts a way for the midlayer to get access to them into something
called iopools.

it also provides a callback api for consumers of resources to use:
the scsi_ioh api for things that want direct access to the ccbs,
and the scsi_xsh api for things that want to issue a scsi_xfer on
the bus. these apis have been modelled on the timeout api.

scsi_xs_get and therefore scsi_scs_cmd have been cut over to using these
apis internally, so if they are allowed to sleep then can wait on the
runqueue for a resource to become available and therefore guarantee that
when executed on an adapter providing an iopool that they will succeed.

ok krw@ beck@ marco@
tested by many including krw@ beck@ mk@ okan@ todd@


# 1.120 23-Mar-2010 krw

Change the scsi_cmd function member of scsi_adapter from int to
void. Use XS_NO_CCB error in the scsi command (xs) to report the
NO_CCB condition. Eliminates all SUCCESSFULLY_QUEUED and COMPLETE
confusion and untangles the midlayer from the adapter a bit more.

Eyes and some fixes by miod@

There may be some compile issues on little used (i.e. I don't have
any) drivers but the change is mechanical and thus easy to remedy.

ok dlg@


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

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

ok dlg@


# 1.118 14-Jan-2010 krw

Tweak names a bit. show_mem -> scsi_show_mem, show_scsi_xs ->
scsi_xs_show.

No functional change.


# 1.117 04-Jan-2010 dlg

introduce scsi_xs_sync. this will synchronously execute a scsi_xfer
and do the appropriate sleeps, retries, error processing, and finally
returns an errno style value to the caller.

this cuts scsi_scsi_cmd, the ioctl code, sd_flush, and scsi_inquiry over
to scsi_xs_sync.

ok krw@


# 1.116 01-Jan-2010 dlg

split the flags used in a scsi_link structure to represent its state at
runtime out into a separate state variable. only operate on the state bits
with atomic ops. introduce the DYING state so things that sleep can figure
out if they should keep going or not.


# 1.115 06-Dec-2009 krw

Nuke SCSI_URGENT after removing its only use, in ncr53c9x.c. That use
was to try to play dangerous games with tagged queuing.

ok marco@


# 1.114 06-Dec-2009 dlg

SCSI_USER is deprecated. the ioctl handlers arent special with regard to
the midlayer.


# 1.113 01-Dec-2009 dlg

properly handle all xs states that can be returned by an adapter in sd.

requested by krw@ after spending a week munging through my code.


# 1.112 01-Dec-2009 dlg

put the midlayer changes back in.

the two issues affecting it last time are gone. the first, mishandling of
TRY_AGAIN_LATER is not relevant now that krw got rid of TRY_AGAIN_LATER.
the second, the misbehaving IBM disk was found to be a problem with siop
using ordered tags on most ops combined with the speed of the new code.

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.111 22-Nov-2009 krw

Remove only use in the tree of ESCAPE_NOT_SUPPORTED. Which, ironically,
wasn't supported. Nuke #define of ESCAPE_NOT_SUPPORTED at the same
time.

ok miod@


# 1.110 22-Nov-2009 krw

Nuke TRY_AGAIN_LATER now that no driver returns it.

ok marco@


# 1.109 12-Nov-2009 dlg

revert midlayer back to it was before i put my big rewrite in. this is
causing a weird problems on an alpha and also appears responsible for
isp(4) weirdness i havent had a chance to examine yet.

sigh, this makes me sad.


# 1.108 10-Nov-2009 dlg

fix the NO_CCB handling i broke when working on simplifying the midlayer.

marco@ discovered my rewrite retried commands rejected by an adapter
from a timeout, which was trivially starved by normal io going to
disks. this diff allows an xs to be marked as XS_NO_CCB, which will
cause it to be returned to the device driver to be retried as part
of the normal io queue.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.107 10-Nov-2009 dlg

backout the backout marco did of my code because of the NO_CCB breakage.
the fix for the NO_CCB breakage will follow shortly.

tested by krw@ marco@ johan@
ok krw@ marco@


# 1.106 05-Nov-2009 marco

The big diff dlg committed to the midlayer breaks NO_CCB and
TRY_AGAIN_LATER. NO_CCB is a timer based mechanism that can trivially
be made to fail by running IO to two or more disks simultaneously. The
TRY_AGAIN_LATER thing is more subtle because it now is a permanent
failure instead of transient however this is much harder to hit because
something must have gone wrong before it hits.

ok deraadt krw miod


# 1.105 23-Oct-2009 dlg

if you're attempting to detach multiple devices (eg, many targets,
many luns, or the entire bus), dont report ENXIO as an error to the
caller. this broke autoconf when it tried to forcefully remove a
bus such as umass and it thought there was a failure.

this introduces a way for scsi hbas to call activate/deactivate on
a device based on its target/lun address via a call to scsi_activate().
they can then schedule the actual detach/attach in a thread later via
scsi_req_probe/detach.

the mpi changes tweak the sas event handling code to use these apis
to properly handle attaches and detaches of disks. event handling
is still disabled till i can make it less chatty.

umass breakage reported by form@


# 1.104 22-Oct-2009 dlg

devices below the scsibus should all be detached via scsi_detach_lun.
scsibusdetach wasnt doign it properly, so we would be leaking on detach in
some cases.

now, with the introduction of mpath, the scsi_link structures can
represent a path to a mpath node as well as normal devices. this
intercepts the device activate entrypoints and sends them to mpath
if it it in use rather than assuming a device is always there. the
scsibusdetach change ensures that detach always ends up handling
the mpath node case too.

hotplug bus functionality (eg, usb) tested by form@


# 1.103 20-Oct-2009 dlg

"active" is an unused member of the scsi_link structure. i couldnt find any
uses of it in our tree.

ok krw@ deraadt@


# 1.102 14-Oct-2009 dlg

rework how devids are handled in the midlayer and mpath.

previously a devid was a structure containing its type, length, and
a pointer to the actual devid value. this has been changed so a
devid is a header followed immediately by the memory making up the
id value. this allows the header and its value to be allocated
together.

devids are now reference counted, so multiple things (eg, the mpath
node handlers and the various scsi_link structures) can share the
same allocation safely. this also frees devids when scsi_links go
away, which was previously not done.

if mpath is enabled, then print the devids out as part of the devices
attach line.


# 1.101 14-Sep-2009 dlg

rework the scsi midlayer to start addressing some problems i have
with it which became extremely annoying with what mpath wants to
do.

the major change is a new interface for submitting scsi commands.
previously the only way for drivers like sd, cd, st, etc to push
commands onto the hardware was via scsi_scsi_cmd(). the problem
with scsi_scsi_cmd is that it doesnt tell the caller if the command
failed, was queued, or completed unless you shoved a buf down with
it. this is important for mpath which wants to know what the physical
path to the device did so it can report it back to the midlayer
which called it.

this provides a new api which lets drivers like cd/sd/st/mpath etc
allocate an xs, fill it in, and provide a completion routine which
the midlayer will call with the state of the command when it is
finished with it. the caller is then responsible for freeing the
xs.

from the hba side of thing, the return code from the scsi_cmd
entrypoint is largely ignored now, and it is now always the
responsibility of the hba driver to call scsi_done when it has
completed the io, rather than returning COMPLETE and expecting the
midlayer to do it for you.

i have emulated scsi_scsi_cmd on top of this new api so existing
users of it will continue to work. sd(4) has been reworked to use
the new api directly to both demonstrate its use and test that the
new api actually does work.

this diff was mostly written in a day at f2k9. thanks to miod for poking
through hba drivers to help mitigate against fallout from the change to
the COMPLETE semantic. this has been reviewed by krw who didnt spot
anything wrong.

thanks to dave del debbio for testing.

ok deraadt@


# 1.100 13-Aug-2009 dlg

provide an api for an interrupt (or something like it) to notify the
midlayer that a scsi device has appeared or dissapeared. the midlayer will
queue an event and run it in the system workq (which has process context)
to handle the attach or detach.


# 1.99 09-Aug-2009 dlg

add mpath(4), a driver that steals paths to scsi devices if it
thinks they could be available via multiple paths. those stolen
devices are then made available via mpath(4).

this is the minimum amount of code to implement the stealing. it
is generally broken and very brittle, so it is currently disabled.

it is going in so i can work on it in the tree.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.98 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.97 10-Nov-2008 deraadt

clean namespace a fair bit; wrap kernel-use-only structures and gunk in
#ifdef _KERNEL


# 1.96 09-Nov-2008 deraadt

workaround lack of struct proc. this whole mess must be cleaned out.
userland processes should not see all the kernel components.


# 1.95 12-Sep-2008 miod

SCSI_DATA_UIO is never used. Code which checks for it is either dead or
commented out, remove it. Unifdef TFS while there.

ok marco@ krw@


Revision tags: OPENBSD_4_4_BASE
# 1.94 22-Jul-2008 dlg

implement the fetching of a scsi devices "devid". recent hardware provides
a vpd page that uniquely identifies a device no matter what bus topology or
addressing was used to find it.

we have a workaround for old school scsi devices that do not differentiate
between luns. if the inq data for high luns is the same as the inq data
for lun 0, we assume it is one of these buggy devices.

the problem with this is that things like SANs present multiple
volumes as luns and they all have the same inq data. if you wanted
to present more than one volume to openbsd you would only ever see
the first one.

devices give us a mechanism to differentiate between luns, so now
i do get all my volumes attached in openbsde.

review and feedback by krw@ marco@ testing by todd@


# 1.93 21-Jun-2008 krw

fgsch@ points out my last commit changed the retry limit for CD capacity
commands. Bump limit back to the standard 4, and at the same time eliminate
SDRETRIES and CDRETRIES (both defined to 4) in favour of a new define,
SCSI_RETRIES, also defined to 4.


# 1.92 14-Jun-2008 krw

Nuke ADEV_NOTUR, always issue TEST UNIT READY to clear out power-up
errors before issuing INQUIRY. Fixes Sony YE-Data floppy drive and
probably other devices at the cost of possibly breaking some 10 year
old CD-ROM drives. Un-special cases mvme68k which was forcing these
initial TURs.

Now wait for the inevitable weird USB device that breaks to surface.

ok marco@ deraadt@


# 1.91 13-Jun-2008 krw

Merge show_scsi_xs() and show_scsi_cmd() and move invocation so the
debug output shows the xs/command before it's issued rather than
displaying it on command completion. Some commands don't come back
and it would be nice to see their details.

While here nuke invocations of scsi_show* in umass. If you want SCSI
debug output use SCSI debug options.

Only affects SCSI debug output.

ok marco@


Revision tags: OPENBSD_4_3_BASE
# 1.90 29-Dec-2007 dlg

the scsi layer always had function pointers for asking the hba about a
device before issuing scsi commands to it, but it was never implemented,
never used, and no hba actually filled them in. i came along and added
another two function pointers for the same thing.

this cleans up the extra pointers.

ok krw@ marco@ miod@ deraadt@


# 1.89 26-Nov-2007 dlg

let scsibus ask the adapter about a device before probing it. also allow
the adapter to be notified when a device goes away so it can free any state
it maintains about that device.

ok deraadt@ marco@


# 1.88 25-Nov-2007 dlg

dont use the adapter_softc member of scsi_link as a softc anymore. the
"adapter_softc" is simply a way for the adapter to determine what scsibus
it is now dealing with, not a pointer back to the adapters device struct.

ok deraadt@ marco@


Revision tags: OPENBSD_4_2_BASE
# 1.87 23-Jun-2007 krw

Implement disk sizes > 2^32-1. Code modelled on NetBSD.

Tested, tweaked and ok otto@


# 1.86 31-May-2007 tedu

ckuethe found i missed a file in the workq update. my bad


# 1.85 31-May-2007 dlg

remove the scsi task thread, and replace it with the system workq.

"just :wq and do it" tedu@


# 1.84 10-Apr-2007 miod

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


# 1.83 03-Apr-2007 dlg

add a wrapper for querying vpd inquiry pages


Revision tags: OPENBSD_4_1_BASE
# 1.82 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.81 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.80 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.79 28-Nov-2006 dlg

remove a comment that was describing a behaviour that was changed over nine
years ago.


# 1.78 26-Nov-2006 dlg

no argument names in prototypes pls


# 1.77 26-Nov-2006 dlg

provide scsi_detach_bus, _target, and _lun to wrap up config_detach for
scsi devices. the midlayer keeps some state for each device that is
attached which needs to be cleaned up on detach, hence this wrapper.


# 1.76 21-Oct-2006 dlg

rework the bus scanning code by splitting it out into separate functions
for walking the bus and targets, and probing the luns. this removes the
need to use magic numbers to wildcard each of these, which in turn makes
the code a lot easier to read. as a bonus we get some more space to work in
(80 chars isnt that much somtimes).

note that this code wont probe high luns if lun 0 doesnt exist.

ok krw@


# 1.75 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.74 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.73 22-Sep-2006 dlg

implement a kernel thread that can be used by the midlayer or scsi drivers
when they need a process context to do something. the most obvious task
that springs to mind is attaches and detaches of devices on scsibus.

ok krw@ marco@ deraadt@


Revision tags: OPENBSD_4_0_BASE
# 1.72 29-Jul-2006 krw

The version field of scsi_inquiry_data is not a simple numeric value
that specifies the version of SCSI being supported. Even the ANSI part
that we use is complex. 4 means 2, 5 means 3 and 6 means 4. Translate
and use the value correctly. Fixes SCSI5 and SCSI6 in dmesg. And
properly protects SCSI2 devices from getting SCSI3 commands.

"seems like an elegant solution to me" millert@ ok dlg@ marco@


# 1.71 23-Jul-2006 krw

Use REPORT LUNS to get the list of LUNs to probe. If such a list is
obtained probe the LUNs given without checking for duplicate INQUIRY
data.

For non-USB, non-ATAPI, devices claiming to be SCSI-3 compliant. And
the target must have something attached at LUN 0.

If REPORT LUNS can't be used or isn't supported, the old scan process
is used.

Fixes Fibre Channel and SCSI enclosure devices that provide identical
INQUIRY data for all LUNs and were thus being misprobed as having
only LUN 0.

Tested by Bob Kitella, dlg@, beck@. Suggestions from deraadt@.

ok dlg@ beck@


# 1.70 22-Jul-2006 krw

Nuke SCSIFORCELUN* and friends. These were introduced as a safety
valve in case our duplicate LUN checks had to be circumvented. Since
no one has found a need for them, and they were just one more place
trying to shift a bit 255 places to the left could be induced, remove
them.

"i don't think any options like that are worthwhile" deraadt@


# 1.69 13-Jul-2006 krw

Eliminate scsi_link field 'scsi_version' and just use the INQUIRY data
stored in scsi_link. That's where the value came from anyway. Move 'luns'
field to where 'scsi_version' used to be to preserve alignment.

ok dlg@


# 1.68 11-Jul-2006 dlg

the scsi_link structure contained a copy of the inquiry flags and the whole
inquiry. this removes the flags member and makes all its users refer to the
whole inquiry now.

ok miod@ krw@


# 1.67 06-Jul-2006 dlg

add a flag to sc_link to be used by a hba to mark the target as virtual.

grudging ok from deraadt@ so i can move forward. this should be revisited
one day though.


# 1.66 20-Jun-2006 deraadt

pack wwn fields into struct better; ok marco krw


# 1.65 31-May-2006 jason

add a place to store the PortWWN and NodeWWN for fibre channel stuffs.


# 1.64 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.63 02-Dec-2005 marco

Remove one tab that cause a line to overflow. No functional change.


# 1.62 13-Nov-2005 krw

Use scsi_delay() instead of delay() when waiting for scsi disk to
become ready. Make verbiage SC_DEBUG().

ok dlg@


# 1.61 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.60 24-Jun-2005 krw

Fix botch introduced in r1.12. reduced_geometry.sectors is a 5 byte
field, with reduced_geometry.sectors[0] not being sectors/track. Add
_5btol() to correctly parse entire field. From NetBSD.


# 1.59 23-Jun-2005 krw

Don't discard the blocksize information provided by the READ CAPACITY
command. Return it in a new parameter to scsi_size.


# 1.58 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.57 03-Jun-2005 krw

Tweak scsi_do_mode_sense(). NetBSD was right and I was wrong. Sigh.
Add 'big' parameter to report which mode sense header type is being
returned. Eliminate icky pointer arithmetic, since it won't work if
only block descriptors are returned by the device.

No functional change.


# 1.56 03-Jun-2005 krw

Cache a copy of the INQUIRY data obtained during device attachment in
the scsi_link structure. This is a more general solution than the
current inconsistant copying of fields into _softc structures. The
redundant fields in _softc's will be cleaned up later. The device
field will be used immediately to finish up the new mode sense code.

ok marco@


# 1.55 28-May-2005 krw

Add _lto8b() and _8btol() to decode nblocks field of
scsi_mode_blk_desc_big. Will be needed to move st to new mode sense
function.

Be a bit more careful with types of parameters to scsi_do_mode_sense,
using u_int32_t instead of int.

No functional change.


# 1.54 25-May-2005 krw

With PQUIK_NOMODESENSE gone, SDEV_NOMODESENSE can go too.


# 1.53 25-May-2005 krw

Introduce safer, more general mode sense capability. Transparently use
both 10 byte and 6 byte MODE SENSE commands and just return error
checked values.

Convert sd_scsi.c to use new mechanism for non-optical drives. USB
umass devices will now display actual mode sense info if it is
available via a 10 byte MODE SENSE. Which may mean 0 heads, etc. is
shown until cosmetics are finalized.

ok marco@ 'that is pretty cool' deraadt@


# 1.52 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.51 27-Apr-2005 krw

Add SDEV_UMASS flag, analogous to SDEV_ATAPI, and use it to force a
full LUN scan on UMASS SCSI targets. UMASS provides reliable max lun
information so we shouldn't waste time. Fixes many x-in-1 card
reader/writers that report identical INQUIRY information for every
slot they provide.

Lots of diagnosis and testing by dlg@, ok dlg@, 'I can live with this'
marco@.


# 1.50 06-Apr-2005 krw

De-register scsi.


# 1.49 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.48 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@.


Revision tags: OPENBSD_3_6_BASE
# 1.47 31-Jul-2004 krw

Remove scsi_change_def() and related command structure. Its only use
in the tree is #ifdef'd out and is fundamentally wrong anyway - it
tries to force *all* devices to SCSI-2. Also recent SCSI specs seem to
have marked the command as obsolete. Bonus - eliminates another
undocumented option (SCSI_2_DEF).

Found in a list of unused kernel functions posted to tech-kern@netbsd
by Krister Walfridsson in 2002.

ok marco@ tdeval@


# 1.46 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.45 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.44 09-May-2004 krw

Eliminate verbosity parameter to scsi_print_sense and the associated
chunk of code. It was never executed unless you were debugging a
mvmex8k 'vs' device.

ok miod@ (mvmex8k bits) marco@ deraadt@


# 1.43 18-Apr-2004 krw

Restore siop's (and possibly others) ability to negotiate tags/wide/sync by
reverting to a single (short) INQUIRY command during probe. Compensate in siop
by trying PPR on all targets on SCSI-3 buses and falling back to WDTR/SDTR if
PPR rejected.

Problem found by mickey@. Tested on a wide variety of devices by Marco.

ok marco@ deraadt@.


Revision tags: OPENBSD_3_5_BASE
# 1.42 10-Mar-2004 krw

branches: 1.42.2;
Simplify new LUN scanning logic, add diagnostic messages for all
instances of bad LUNs and add SCSIFORCELUN_BUSES and
SCSIFORCELUN_TARGETS options.

ok miod@ deraadt@. Tested in the Marco Peereboom torture chamber.


# 1.41 21-Feb-2004 krw

Eliminate the quirks SDEV_NOSTARTUNIT, UMASS_QUIRK_NO_START_STOP, and
UMASS_QUIRK_FORCE_SHORT_INQUIRY. Fixes a bunch of USB devices. Based
on work by Mycroft in NetBSD.

ok tdeval@ deraadt@.


# 1.40 14-Jan-2004 krw

Nuke SDEV_NOLUNS, SDEV_FORCELUNS, and PQUIRK_FORCELUNS quirks. Also
moreluns field in scsi_link structure. Instead, treat an INQUIRY
result that duplicates the INQUIRY result of LUN 0 as proof the LUN
does not exist. Compensate for lack of SDEV_NOLUNS where necessary by
setting sc_link->luns to 1, which has the same effect. From Marco
Peereboom.

Don't issue Test Unit Ready command before INQUIRY command - not
necessary and potentially harmful to devices with ADEV_NOTUR quirk
since quirks have not been set yet. From mycroft@NetBSD

ok deraadt@, mvme* changes by miod@.


Revision tags: OPENBSD_3_4_BASE
# 1.39 18-May-2003 mickey

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


Revision tags: UBC_SYNC_A
# 1.38 16-May-2003 krw

Provide most if not all the support required for the usb changes Nate
is trying to bring in.

1) Change name of SDEV_NOCDB6 to SDEV_ONLYBIG to align it with the
same quirk in NetBSD, and make it more clear what it is trying to do.
i.e. force the use of READ_BIG/WRITE_BIG commands, not suppress all
use of 6 byte CDB's.

2) Check SDEV_ONLYBIG in cd.c as well as sd.c. i.e. both places where
a choice is made to use the 6 or 10 byte versions of READ/WRITE.

3) Actually make use of the ADEV_NOTUR (No TEST UNIT READY) quirk to
suppress the emission of TEST UNIT READY commands.

4) Add some explanatory comments from NetBSD to scsiconf.h so that the
use of the quirks is made clear.

ok miod@ tdeval@ nate@


Revision tags: OPENBSD_3_3_BASE
# 1.37 20-Feb-2003 krw

Eliminate unused parameter to scsi_free_xs().

ok deraadt@ tdeval@


# 1.36 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.35 03-Aug-2002 pvalchev

spell 'debugging' correctly with double 'g' (in comments)


# 1.34 27-May-2002 tdeval

punctuation


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

First round of __P removal in sys


# 1.32 07-Jan-2002 mickey

pass the proc* down the ioctl chain to controller; mjacob@ csapuntz@ ok


Revision tags: UBC_BASE
# 1.31 26-Oct-2001 krw

branches: 1.31.2;
Remove unused #define INUSE. It was rendered superfluous by the
move to pools in SCSI layer.


Revision tags: OPENBSD_3_0_BASE
# 1.30 08-Oct-2001 drahn

Add a new quirk type, SDEV_NOCDB6, some USB devices like ATAPI
do not support 6 byte CDBs.
This quirk is used for OLYMPUS USB cameras.
Loosely based on code in FreeBSD.
ok costa@


# 1.29 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.28 18-Aug-2001 krw

Make siop pay attention to quirks table. This not only eliminates the
ugly INQUIRY snooping but avoids adding even uglier #ifdef's to turn
off stuff, e.g. tagged queuing.

Add two disk drives now known to lie about supporting tagged queuing
to quirks table. One from millert@ (<MICROP, 4421-07 0329SJ, 0329>)
and one from Hakan Olsson (<SEAGATE, ST150176LW, 0002>).

Add field 'inquiry_flags2' to struct scsi_link to hold flags2 field
from struct scsi_inquiry_data. These flags relate to SCSI-3 specific
features.

Clean up some logic, eliminating need for TARF_PPR flag.


# 1.27 22-Jun-2001 deraadt

KNF


# 1.26 30-Apr-2001 csapuntz

Pass IOCTL to adapter device if not understood by SCSI layer.


Revision tags: OPENBSD_2_9_BASE
# 1.25 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.24 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_8_BASE
# 1.23 06-Jul-2000 mjacob

Add in a timeout structure to the scsi_xfer structure so that HBA's can
watchdog per command. Reserve 4 bits of the flags field for the HBA's
private use (e.g., for saying whether the commands in the middle of
being watchdogged....).


Revision tags: OPENBSD_2_7_BASE
# 1.22 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: SMP_BASE
# 1.21 16-Dec-1999 mjacob

branches: 1.21.2;
Split SDEV_NOSYNCWIDE into SDEV_NOSYNC and SDEV_NOWIDE (as is done
in NetBSD). Look at Inquiry data during probing to further set quirks
based upon device capabilities. Thanks to Todd.Miller@courtesan.com for
doing the grunt work and encouraging this to get done fully.


Revision tags: kame_19991208
# 1.20 22-Nov-1999 mjacob

In order to support Fibre Channel fabric fatten scsi target id's to 16 bits.
Also, to support at least first level SCSI-3 hierarchical luns, fatten luns
to 16 bits too.


Revision tags: OPENBSD_2_6_BASE
# 1.19 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.


# 1.18 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


# 1.17 18-Jul-1999 csapuntz

Added SDEV_NOSENSE for ATAPI devices without request sense.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.16 04-Aug-1998 millert

make resid size_t


Revision tags: OPENBSD_2_3_BASE
# 1.15 16-Feb-1998 deraadt

add SDEV_NOTAGS


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

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


# 1.13 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.


Revision tags: OPENBSD_2_1_BASE
# 1.12 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.11 18-Jan-1997 briggs

scsi->moreluns is a bitfield based on target--should be u_int16_t, now.


# 1.10 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.9 15-Jan-1997 deraadt

sc_link.adapter_buswidth, set to 16 if wide scsi. if 0 it gets converted
to 8 internally so that drivers do not need to init it for regular scsi :-)


# 1.8 31-Oct-1996 niklas

$OpenBSD RCSIDs + comment fix in sd.c


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

limit ioctl priviledges


# 1.6 10-Jun-1996 downsj

Several changes:
* Implemented NetBSD PR#2529, adding ZIP 100.
* Added MTIOCTOP support to acd, cd, and sd.
* Implemented eject on close for acd, cd, and sd.

`mt -f /dev/rcd0d offline' now ejects a mounted {acd|cd|sd} when it is
unmounted.


# 1.5 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.4 20-Feb-1996 briggs

Sync. with NetBSD:
- scsi prototypes.
- Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a but. Needs more work.
ss.c:
- Truncate to the window size in ssminphys(), not ssread().
- Missed some prototyping foo.
- Minor tweak; make sure window size is 0 on close.
- Change variable name to avoid GCC warning.
- Handle EOF a little differently.


# 1.3 14-Jan-1996 briggs

From NetBSD: (required for last change to scsi_base.c in case we get
"command aborted" status)
Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.


# 1.2 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision