History log of /openbsd-current/sys/scsi/scsiconf.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.253 06-Apr-2022 krw

Avoid traversing SLIST twice to remove a link.

From millert@


# 1.252 06-Apr-2022 krw

Recognize LUN 0 device if SID_QUAL_LU_OFFLINE and T_NODEVICE are
true. This enables the discovery and probing of other LUNs the
target has to offer.

Add uk* to amd64 RAMDISK_CD so such devices are configured.

Allows bsd.rd as well as bsd[.mp] to boot on Oracle Cloud amd64
instances.

Issue reported by Ryan Kavanagh and Scott Nicholas via bugs@.

Original diff and much subsequent testing by Scott. Thanks!

ok dlg@ jmatthew@


Revision tags: OPENBSD_7_1_BASE
# 1.251 02-Apr-2022 krw

Add a paranoia/future proofing check for link->pool == NULL to
the SDEV_OWN_IOPL flag check.

Ambiguous/inconsistant code pointed out by Coverity #1515557.


# 1.250 02-Apr-2022 krw

Bring back r1.247, using scsi_detach_link() to release
a partially configured struct scsi_link.

Problematic code path found by jungle Boogie was plugged
by r1.249.


# 1.249 28-Mar-2022 krw

Only SLIST_REMOVE() a link when the link is on the list.

Avoids a potential panic.


# 1.248 24-Mar-2022 krw

Revert previous. Breaks probing native IDE devices.

Problem reported by jungle Boogie via bugs@


# 1.247 23-Mar-2022 krw

When configuring a scsi_link that has passed dev_probe() but
fallen at one of the subsequent hurdles of scsi_probe_link()
don't partially replicate scsi_discard_link(). Just
call scsi_discard_link(). It now handles such partially configured
scsi_link's.


# 1.246 22-Mar-2022 krw

Nuke increasingly pointless comment. Shorten a line and
compare pointer to NULL instead of 0.

No functional change.


# 1.245 21-Mar-2022 krw

Whitespace tweaks.


# 1.244 21-Mar-2022 krw

No point in calling scsi_link_shutdown() if link->pool is NULL.

Ditto config_detach() if link->device_softc is NULL.

Currently just some extra paranoia, but will allow simplification
of exit logic in scsi_probe_link() and other future uses of
scsi_detach_link() on partially configured links.

No intentional functional change.


# 1.243 03-Mar-2022 krw

r1.241 was the culprit. Unrevert r1.240.


# 1.242 03-Mar-2022 krw

Revert r1.241 and r1.240 which may have broken softraid.


# 1.241 02-Mar-2022 krw

Abstract the memory allocation, scsibus_softc data copying and
flag setting bits of creating a scsi_link into scsi_alloc_link().

Shrinks the bloated scsi_probe_link() a bit, makes it possible to
eventually create a useable scsi_link even when scsi_probe_link()
can't attach a device.

Developed from part of a diff submitted by Scott Nicholas via
tech@.


# 1.240 02-Mar-2022 krw

Move the code obtaining the LUN 0 scsi_link used to determine the
LUNs available to a target into scsi_get_target_luns(). Clearer
code and prep for future changes.

No functional change.

Extracted from a larger diff submitted by Scott Nicholas via
tech@.


# 1.239 28-Feb-2022 krw

Shuffle some SCSIDEBUG code to simplify code, tersify the
emitted verbiage, and show INQUIRY header & vendor info early so
humans can more easily determine what scsi_probe_link() will
do.

No functional change outside SCSIDEBUG.


# 1.238 24-Oct-2021 mpi

Constify struct cfattach.

ok visa@ a long time ago, ok krw@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.237 19-Nov-2020 krw

TL;DR -- don't configure devices that return insufficient INQUIRY data.

Treat INQUIRY data with fewer than SID_SCSI2_HDRLEN bytes as invalid.

Use only INQUIRY data returned by the device.

Get all available INQUIRY data (up to sizeof(struct scsi_inquiry_data))
even when SCSIDEBUG is not set.

Tweak returned INQUIRY data so additional_length field does not point
past end of returned data when available data is greater than
sizeof(struct scsi_inquiry_data).

Missing dmafree() spotted by gnezdo@. ok jmatthew@.


Revision tags: OPENBSD_6_8_BASE
# 1.236 18-Aug-2020 krw

Try to avoid a theoretical infinite loop while detaching all the scsi_link's on
the bus. Use SLIST_FOREACH_SAFE() rather than 'while (!SLIST_EMPTY())'' as there
is a condition which would cause scsi_detach_link() to return without removing
the scsi_link from the SLIST.


# 1.235 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.234 11-Aug-2020 krw

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

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


# 1.233 10-Aug-2020 krw

Minor code shuffle to get all *_activate(), *_probe() and *_detach() functions
nestled together. Rename scsibusprint() to scsibussubprint() since it is used
with scsibussubmatch().


# 1.232 09-Aug-2020 krw

More code shuffling. Fix a rename missed in previous.


# 1.231 09-Aug-2020 krw

Shuffle functions and declarations around to more logical grouping. Nuke some
leading whitespace. Rename some local functions.


# 1.230 08-Aug-2020 krw

scsi_link's are born knowing their bus. Use that instead of passing extra
scsibus_softc pointers around.


# 1.229 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.228 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.227 16-Jul-2020 krw

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


# 1.226 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.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.251 02-Apr-2022 krw

Add a paranoia/future proofing check for link->pool == NULL to
the SDEV_OWN_IOPL flag check.

Ambiguous/inconsistant code pointed out by Coverity #1515557.


# 1.250 02-Apr-2022 krw

Bring back r1.247, using scsi_detach_link() to release
a partially configured struct scsi_link.

Problematic code path found by jungle Boogie was plugged
by r1.249.


# 1.249 28-Mar-2022 krw

Only SLIST_REMOVE() a link when the link is on the list.

Avoids a potential panic.


# 1.248 24-Mar-2022 krw

Revert previous. Breaks probing native IDE devices.

Problem reported by jungle Boogie via bugs@


# 1.247 23-Mar-2022 krw

When configuring a scsi_link that has passed dev_probe() but
fallen at one of the subsequent hurdles of scsi_probe_link()
don't partially replicate scsi_discard_link(). Just
call scsi_discard_link(). It now handles such partially configured
scsi_link's.


# 1.246 22-Mar-2022 krw

Nuke increasingly pointless comment. Shorten a line and
compare pointer to NULL instead of 0.

No functional change.


# 1.245 21-Mar-2022 krw

Whitespace tweaks.


# 1.244 21-Mar-2022 krw

No point in calling scsi_link_shutdown() if link->pool is NULL.

Ditto config_detach() if link->device_softc is NULL.

Currently just some extra paranoia, but will allow simplification
of exit logic in scsi_probe_link() and other future uses of
scsi_detach_link() on partially configured links.

No intentional functional change.


# 1.243 03-Mar-2022 krw

r1.241 was the culprit. Unrevert r1.240.


# 1.242 03-Mar-2022 krw

Revert r1.241 and r1.240 which may have broken softraid.


# 1.241 02-Mar-2022 krw

Abstract the memory allocation, scsibus_softc data copying and
flag setting bits of creating a scsi_link into scsi_alloc_link().

Shrinks the bloated scsi_probe_link() a bit, makes it possible to
eventually create a useable scsi_link even when scsi_probe_link()
can't attach a device.

Developed from part of a diff submitted by Scott Nicholas via
tech@.


# 1.240 02-Mar-2022 krw

Move the code obtaining the LUN 0 scsi_link used to determine the
LUNs available to a target into scsi_get_target_luns(). Clearer
code and prep for future changes.

No functional change.

Extracted from a larger diff submitted by Scott Nicholas via
tech@.


# 1.239 28-Feb-2022 krw

Shuffle some SCSIDEBUG code to simplify code, tersify the
emitted verbiage, and show INQUIRY header & vendor info early so
humans can more easily determine what scsi_probe_link() will
do.

No functional change outside SCSIDEBUG.


# 1.238 24-Oct-2021 mpi

Constify struct cfattach.

ok visa@ a long time ago, ok krw@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.237 19-Nov-2020 krw

TL;DR -- don't configure devices that return insufficient INQUIRY data.

Treat INQUIRY data with fewer than SID_SCSI2_HDRLEN bytes as invalid.

Use only INQUIRY data returned by the device.

Get all available INQUIRY data (up to sizeof(struct scsi_inquiry_data))
even when SCSIDEBUG is not set.

Tweak returned INQUIRY data so additional_length field does not point
past end of returned data when available data is greater than
sizeof(struct scsi_inquiry_data).

Missing dmafree() spotted by gnezdo@. ok jmatthew@.


Revision tags: OPENBSD_6_8_BASE
# 1.236 18-Aug-2020 krw

Try to avoid a theoretical infinite loop while detaching all the scsi_link's on
the bus. Use SLIST_FOREACH_SAFE() rather than 'while (!SLIST_EMPTY())'' as there
is a condition which would cause scsi_detach_link() to return without removing
the scsi_link from the SLIST.


# 1.235 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.234 11-Aug-2020 krw

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

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


# 1.233 10-Aug-2020 krw

Minor code shuffle to get all *_activate(), *_probe() and *_detach() functions
nestled together. Rename scsibusprint() to scsibussubprint() since it is used
with scsibussubmatch().


# 1.232 09-Aug-2020 krw

More code shuffling. Fix a rename missed in previous.


# 1.231 09-Aug-2020 krw

Shuffle functions and declarations around to more logical grouping. Nuke some
leading whitespace. Rename some local functions.


# 1.230 08-Aug-2020 krw

scsi_link's are born knowing their bus. Use that instead of passing extra
scsibus_softc pointers around.


# 1.229 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.228 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.227 16-Jul-2020 krw

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


# 1.226 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.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.249 28-Mar-2022 krw

Only SLIST_REMOVE() a link when the link is on the list.

Avoids a potential panic.


# 1.248 24-Mar-2022 krw

Revert previous. Breaks probing native IDE devices.

Problem reported by jungle Boogie via bugs@


# 1.247 23-Mar-2022 krw

When configuring a scsi_link that has passed dev_probe() but
fallen at one of the subsequent hurdles of scsi_probe_link()
don't partially replicate scsi_discard_link(). Just
call scsi_discard_link(). It now handles such partially configured
scsi_link's.


# 1.246 22-Mar-2022 krw

Nuke increasingly pointless comment. Shorten a line and
compare pointer to NULL instead of 0.

No functional change.


# 1.245 21-Mar-2022 krw

Whitespace tweaks.


# 1.244 21-Mar-2022 krw

No point in calling scsi_link_shutdown() if link->pool is NULL.

Ditto config_detach() if link->device_softc is NULL.

Currently just some extra paranoia, but will allow simplification
of exit logic in scsi_probe_link() and other future uses of
scsi_detach_link() on partially configured links.

No intentional functional change.


# 1.243 03-Mar-2022 krw

r1.241 was the culprit. Unrevert r1.240.


# 1.242 03-Mar-2022 krw

Revert r1.241 and r1.240 which may have broken softraid.


# 1.241 02-Mar-2022 krw

Abstract the memory allocation, scsibus_softc data copying and
flag setting bits of creating a scsi_link into scsi_alloc_link().

Shrinks the bloated scsi_probe_link() a bit, makes it possible to
eventually create a useable scsi_link even when scsi_probe_link()
can't attach a device.

Developed from part of a diff submitted by Scott Nicholas via
tech@.


# 1.240 02-Mar-2022 krw

Move the code obtaining the LUN 0 scsi_link used to determine the
LUNs available to a target into scsi_get_target_luns(). Clearer
code and prep for future changes.

No functional change.

Extracted from a larger diff submitted by Scott Nicholas via
tech@.


# 1.239 28-Feb-2022 krw

Shuffle some SCSIDEBUG code to simplify code, tersify the
emitted verbiage, and show INQUIRY header & vendor info early so
humans can more easily determine what scsi_probe_link() will
do.

No functional change outside SCSIDEBUG.


# 1.238 24-Oct-2021 mpi

Constify struct cfattach.

ok visa@ a long time ago, ok krw@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.237 19-Nov-2020 krw

TL;DR -- don't configure devices that return insufficient INQUIRY data.

Treat INQUIRY data with fewer than SID_SCSI2_HDRLEN bytes as invalid.

Use only INQUIRY data returned by the device.

Get all available INQUIRY data (up to sizeof(struct scsi_inquiry_data))
even when SCSIDEBUG is not set.

Tweak returned INQUIRY data so additional_length field does not point
past end of returned data when available data is greater than
sizeof(struct scsi_inquiry_data).

Missing dmafree() spotted by gnezdo@. ok jmatthew@.


Revision tags: OPENBSD_6_8_BASE
# 1.236 18-Aug-2020 krw

Try to avoid a theoretical infinite loop while detaching all the scsi_link's on
the bus. Use SLIST_FOREACH_SAFE() rather than 'while (!SLIST_EMPTY())'' as there
is a condition which would cause scsi_detach_link() to return without removing
the scsi_link from the SLIST.


# 1.235 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.234 11-Aug-2020 krw

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

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


# 1.233 10-Aug-2020 krw

Minor code shuffle to get all *_activate(), *_probe() and *_detach() functions
nestled together. Rename scsibusprint() to scsibussubprint() since it is used
with scsibussubmatch().


# 1.232 09-Aug-2020 krw

More code shuffling. Fix a rename missed in previous.


# 1.231 09-Aug-2020 krw

Shuffle functions and declarations around to more logical grouping. Nuke some
leading whitespace. Rename some local functions.


# 1.230 08-Aug-2020 krw

scsi_link's are born knowing their bus. Use that instead of passing extra
scsibus_softc pointers around.


# 1.229 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.228 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.227 16-Jul-2020 krw

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


# 1.226 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.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.248 24-Mar-2022 krw

Revert previous. Breaks probing native IDE devices.

Problem reported by jungle Boogie via bugs@


# 1.247 23-Mar-2022 krw

When configuring a scsi_link that has passed dev_probe() but
fallen at one of the subsequent hurdles of scsi_probe_link()
don't partially replicate scsi_discard_link(). Just
call scsi_discard_link(). It now handles such partially configured
scsi_link's.


# 1.246 22-Mar-2022 krw

Nuke increasingly pointless comment. Shorten a line and
compare pointer to NULL instead of 0.

No functional change.


# 1.245 21-Mar-2022 krw

Whitespace tweaks.


# 1.244 21-Mar-2022 krw

No point in calling scsi_link_shutdown() if link->pool is NULL.

Ditto config_detach() if link->device_softc is NULL.

Currently just some extra paranoia, but will allow simplification
of exit logic in scsi_probe_link() and other future uses of
scsi_detach_link() on partially configured links.

No intentional functional change.


# 1.243 03-Mar-2022 krw

r1.241 was the culprit. Unrevert r1.240.


# 1.242 03-Mar-2022 krw

Revert r1.241 and r1.240 which may have broken softraid.


# 1.241 02-Mar-2022 krw

Abstract the memory allocation, scsibus_softc data copying and
flag setting bits of creating a scsi_link into scsi_alloc_link().

Shrinks the bloated scsi_probe_link() a bit, makes it possible to
eventually create a useable scsi_link even when scsi_probe_link()
can't attach a device.

Developed from part of a diff submitted by Scott Nicholas via
tech@.


# 1.240 02-Mar-2022 krw

Move the code obtaining the LUN 0 scsi_link used to determine the
LUNs available to a target into scsi_get_target_luns(). Clearer
code and prep for future changes.

No functional change.

Extracted from a larger diff submitted by Scott Nicholas via
tech@.


# 1.239 28-Feb-2022 krw

Shuffle some SCSIDEBUG code to simplify code, tersify the
emitted verbiage, and show INQUIRY header & vendor info early so
humans can more easily determine what scsi_probe_link() will
do.

No functional change outside SCSIDEBUG.


# 1.238 24-Oct-2021 mpi

Constify struct cfattach.

ok visa@ a long time ago, ok krw@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.237 19-Nov-2020 krw

TL;DR -- don't configure devices that return insufficient INQUIRY data.

Treat INQUIRY data with fewer than SID_SCSI2_HDRLEN bytes as invalid.

Use only INQUIRY data returned by the device.

Get all available INQUIRY data (up to sizeof(struct scsi_inquiry_data))
even when SCSIDEBUG is not set.

Tweak returned INQUIRY data so additional_length field does not point
past end of returned data when available data is greater than
sizeof(struct scsi_inquiry_data).

Missing dmafree() spotted by gnezdo@. ok jmatthew@.


Revision tags: OPENBSD_6_8_BASE
# 1.236 18-Aug-2020 krw

Try to avoid a theoretical infinite loop while detaching all the scsi_link's on
the bus. Use SLIST_FOREACH_SAFE() rather than 'while (!SLIST_EMPTY())'' as there
is a condition which would cause scsi_detach_link() to return without removing
the scsi_link from the SLIST.


# 1.235 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.234 11-Aug-2020 krw

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

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


# 1.233 10-Aug-2020 krw

Minor code shuffle to get all *_activate(), *_probe() and *_detach() functions
nestled together. Rename scsibusprint() to scsibussubprint() since it is used
with scsibussubmatch().


# 1.232 09-Aug-2020 krw

More code shuffling. Fix a rename missed in previous.


# 1.231 09-Aug-2020 krw

Shuffle functions and declarations around to more logical grouping. Nuke some
leading whitespace. Rename some local functions.


# 1.230 08-Aug-2020 krw

scsi_link's are born knowing their bus. Use that instead of passing extra
scsibus_softc pointers around.


# 1.229 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.228 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.227 16-Jul-2020 krw

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


# 1.226 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.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.246 22-Mar-2022 krw

Nuke increasingly pointless comment. Shorten a line and
compare pointer to NULL instead of 0.

No functional change.


# 1.245 21-Mar-2022 krw

Whitespace tweaks.


# 1.244 21-Mar-2022 krw

No point in calling scsi_link_shutdown() if link->pool is NULL.

Ditto config_detach() if link->device_softc is NULL.

Currently just some extra paranoia, but will allow simplification
of exit logic in scsi_probe_link() and other future uses of
scsi_detach_link() on partially configured links.

No intentional functional change.


# 1.243 03-Mar-2022 krw

r1.241 was the culprit. Unrevert r1.240.


# 1.242 03-Mar-2022 krw

Revert r1.241 and r1.240 which may have broken softraid.


# 1.241 02-Mar-2022 krw

Abstract the memory allocation, scsibus_softc data copying and
flag setting bits of creating a scsi_link into scsi_alloc_link().

Shrinks the bloated scsi_probe_link() a bit, makes it possible to
eventually create a useable scsi_link even when scsi_probe_link()
can't attach a device.

Developed from part of a diff submitted by Scott Nicholas via
tech@.


# 1.240 02-Mar-2022 krw

Move the code obtaining the LUN 0 scsi_link used to determine the
LUNs available to a target into scsi_get_target_luns(). Clearer
code and prep for future changes.

No functional change.

Extracted from a larger diff submitted by Scott Nicholas via
tech@.


# 1.239 28-Feb-2022 krw

Shuffle some SCSIDEBUG code to simplify code, tersify the
emitted verbiage, and show INQUIRY header & vendor info early so
humans can more easily determine what scsi_probe_link() will
do.

No functional change outside SCSIDEBUG.


# 1.238 24-Oct-2021 mpi

Constify struct cfattach.

ok visa@ a long time ago, ok krw@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.237 19-Nov-2020 krw

TL;DR -- don't configure devices that return insufficient INQUIRY data.

Treat INQUIRY data with fewer than SID_SCSI2_HDRLEN bytes as invalid.

Use only INQUIRY data returned by the device.

Get all available INQUIRY data (up to sizeof(struct scsi_inquiry_data))
even when SCSIDEBUG is not set.

Tweak returned INQUIRY data so additional_length field does not point
past end of returned data when available data is greater than
sizeof(struct scsi_inquiry_data).

Missing dmafree() spotted by gnezdo@. ok jmatthew@.


Revision tags: OPENBSD_6_8_BASE
# 1.236 18-Aug-2020 krw

Try to avoid a theoretical infinite loop while detaching all the scsi_link's on
the bus. Use SLIST_FOREACH_SAFE() rather than 'while (!SLIST_EMPTY())'' as there
is a condition which would cause scsi_detach_link() to return without removing
the scsi_link from the SLIST.


# 1.235 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.234 11-Aug-2020 krw

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

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


# 1.233 10-Aug-2020 krw

Minor code shuffle to get all *_activate(), *_probe() and *_detach() functions
nestled together. Rename scsibusprint() to scsibussubprint() since it is used
with scsibussubmatch().


# 1.232 09-Aug-2020 krw

More code shuffling. Fix a rename missed in previous.


# 1.231 09-Aug-2020 krw

Shuffle functions and declarations around to more logical grouping. Nuke some
leading whitespace. Rename some local functions.


# 1.230 08-Aug-2020 krw

scsi_link's are born knowing their bus. Use that instead of passing extra
scsibus_softc pointers around.


# 1.229 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.228 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.227 16-Jul-2020 krw

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


# 1.226 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.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.245 21-Mar-2022 krw

Whitespace tweaks.


# 1.244 21-Mar-2022 krw

No point in calling scsi_link_shutdown() if link->pool is NULL.

Ditto config_detach() if link->device_softc is NULL.

Currently just some extra paranoia, but will allow simplification
of exit logic in scsi_probe_link() and other future uses of
scsi_detach_link() on partially configured links.

No intentional functional change.


# 1.243 03-Mar-2022 krw

r1.241 was the culprit. Unrevert r1.240.


# 1.242 03-Mar-2022 krw

Revert r1.241 and r1.240 which may have broken softraid.


# 1.241 02-Mar-2022 krw

Abstract the memory allocation, scsibus_softc data copying and
flag setting bits of creating a scsi_link into scsi_alloc_link().

Shrinks the bloated scsi_probe_link() a bit, makes it possible to
eventually create a useable scsi_link even when scsi_probe_link()
can't attach a device.

Developed from part of a diff submitted by Scott Nicholas via
tech@.


# 1.240 02-Mar-2022 krw

Move the code obtaining the LUN 0 scsi_link used to determine the
LUNs available to a target into scsi_get_target_luns(). Clearer
code and prep for future changes.

No functional change.

Extracted from a larger diff submitted by Scott Nicholas via
tech@.


# 1.239 28-Feb-2022 krw

Shuffle some SCSIDEBUG code to simplify code, tersify the
emitted verbiage, and show INQUIRY header & vendor info early so
humans can more easily determine what scsi_probe_link() will
do.

No functional change outside SCSIDEBUG.


# 1.238 24-Oct-2021 mpi

Constify struct cfattach.

ok visa@ a long time ago, ok krw@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.237 19-Nov-2020 krw

TL;DR -- don't configure devices that return insufficient INQUIRY data.

Treat INQUIRY data with fewer than SID_SCSI2_HDRLEN bytes as invalid.

Use only INQUIRY data returned by the device.

Get all available INQUIRY data (up to sizeof(struct scsi_inquiry_data))
even when SCSIDEBUG is not set.

Tweak returned INQUIRY data so additional_length field does not point
past end of returned data when available data is greater than
sizeof(struct scsi_inquiry_data).

Missing dmafree() spotted by gnezdo@. ok jmatthew@.


Revision tags: OPENBSD_6_8_BASE
# 1.236 18-Aug-2020 krw

Try to avoid a theoretical infinite loop while detaching all the scsi_link's on
the bus. Use SLIST_FOREACH_SAFE() rather than 'while (!SLIST_EMPTY())'' as there
is a condition which would cause scsi_detach_link() to return without removing
the scsi_link from the SLIST.


# 1.235 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.234 11-Aug-2020 krw

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

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


# 1.233 10-Aug-2020 krw

Minor code shuffle to get all *_activate(), *_probe() and *_detach() functions
nestled together. Rename scsibusprint() to scsibussubprint() since it is used
with scsibussubmatch().


# 1.232 09-Aug-2020 krw

More code shuffling. Fix a rename missed in previous.


# 1.231 09-Aug-2020 krw

Shuffle functions and declarations around to more logical grouping. Nuke some
leading whitespace. Rename some local functions.


# 1.230 08-Aug-2020 krw

scsi_link's are born knowing their bus. Use that instead of passing extra
scsibus_softc pointers around.


# 1.229 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.228 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.227 16-Jul-2020 krw

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


# 1.226 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.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.245 21-Mar-2022 krw

Whitespace tweaks.


# 1.244 21-Mar-2022 krw

No point in calling scsi_link_shutdown() if link->pool is NULL.

Ditto config_detach() if link->device_softc is NULL.

Currently just some extra paranoia, but will allow simplification
of exit logic in scsi_probe_link() and other future uses of
scsi_detach_link() on partially configured links.

No intentional functional change.


# 1.243 03-Mar-2022 krw

r1.241 was the culprit. Unrevert r1.240.


# 1.242 03-Mar-2022 krw

Revert r1.241 and r1.240 which may have broken softraid.


# 1.241 02-Mar-2022 krw

Abstract the memory allocation, scsibus_softc data copying and
flag setting bits of creating a scsi_link into scsi_alloc_link().

Shrinks the bloated scsi_probe_link() a bit, makes it possible to
eventually create a useable scsi_link even when scsi_probe_link()
can't attach a device.

Developed from part of a diff submitted by Scott Nicholas via
tech@.


# 1.240 02-Mar-2022 krw

Move the code obtaining the LUN 0 scsi_link used to determine the
LUNs available to a target into scsi_get_target_luns(). Clearer
code and prep for future changes.

No functional change.

Extracted from a larger diff submitted by Scott Nicholas via
tech@.


# 1.239 28-Feb-2022 krw

Shuffle some SCSIDEBUG code to simplify code, tersify the
emitted verbiage, and show INQUIRY header & vendor info early so
humans can more easily determine what scsi_probe_link() will
do.

No functional change outside SCSIDEBUG.


# 1.238 24-Oct-2021 mpi

Constify struct cfattach.

ok visa@ a long time ago, ok krw@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.237 19-Nov-2020 krw

TL;DR -- don't configure devices that return insufficient INQUIRY data.

Treat INQUIRY data with fewer than SID_SCSI2_HDRLEN bytes as invalid.

Use only INQUIRY data returned by the device.

Get all available INQUIRY data (up to sizeof(struct scsi_inquiry_data))
even when SCSIDEBUG is not set.

Tweak returned INQUIRY data so additional_length field does not point
past end of returned data when available data is greater than
sizeof(struct scsi_inquiry_data).

Missing dmafree() spotted by gnezdo@. ok jmatthew@.


Revision tags: OPENBSD_6_8_BASE
# 1.236 18-Aug-2020 krw

Try to avoid a theoretical infinite loop while detaching all the scsi_link's on
the bus. Use SLIST_FOREACH_SAFE() rather than 'while (!SLIST_EMPTY())'' as there
is a condition which would cause scsi_detach_link() to return without removing
the scsi_link from the SLIST.


# 1.235 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.234 11-Aug-2020 krw

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

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


# 1.233 10-Aug-2020 krw

Minor code shuffle to get all *_activate(), *_probe() and *_detach() functions
nestled together. Rename scsibusprint() to scsibussubprint() since it is used
with scsibussubmatch().


# 1.232 09-Aug-2020 krw

More code shuffling. Fix a rename missed in previous.


# 1.231 09-Aug-2020 krw

Shuffle functions and declarations around to more logical grouping. Nuke some
leading whitespace. Rename some local functions.


# 1.230 08-Aug-2020 krw

scsi_link's are born knowing their bus. Use that instead of passing extra
scsibus_softc pointers around.


# 1.229 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.228 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.227 16-Jul-2020 krw

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


# 1.226 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.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.243 03-Mar-2022 krw

r1.241 was the culprit. Unrevert r1.240.


# 1.242 03-Mar-2022 krw

Revert r1.241 and r1.240 which may have broken softraid.


# 1.241 02-Mar-2022 krw

Abstract the memory allocation, scsibus_softc data copying and
flag setting bits of creating a scsi_link into scsi_alloc_link().

Shrinks the bloated scsi_probe_link() a bit, makes it possible to
eventually create a useable scsi_link even when scsi_probe_link()
can't attach a device.

Developed from part of a diff submitted by Scott Nicholas via
tech@.


# 1.240 02-Mar-2022 krw

Move the code obtaining the LUN 0 scsi_link used to determine the
LUNs available to a target into scsi_get_target_luns(). Clearer
code and prep for future changes.

No functional change.

Extracted from a larger diff submitted by Scott Nicholas via
tech@.


# 1.239 28-Feb-2022 krw

Shuffle some SCSIDEBUG code to simplify code, tersify the
emitted verbiage, and show INQUIRY header & vendor info early so
humans can more easily determine what scsi_probe_link() will
do.

No functional change outside SCSIDEBUG.


# 1.238 24-Oct-2021 mpi

Constify struct cfattach.

ok visa@ a long time ago, ok krw@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.237 19-Nov-2020 krw

TL;DR -- don't configure devices that return insufficient INQUIRY data.

Treat INQUIRY data with fewer than SID_SCSI2_HDRLEN bytes as invalid.

Use only INQUIRY data returned by the device.

Get all available INQUIRY data (up to sizeof(struct scsi_inquiry_data))
even when SCSIDEBUG is not set.

Tweak returned INQUIRY data so additional_length field does not point
past end of returned data when available data is greater than
sizeof(struct scsi_inquiry_data).

Missing dmafree() spotted by gnezdo@. ok jmatthew@.


Revision tags: OPENBSD_6_8_BASE
# 1.236 18-Aug-2020 krw

Try to avoid a theoretical infinite loop while detaching all the scsi_link's on
the bus. Use SLIST_FOREACH_SAFE() rather than 'while (!SLIST_EMPTY())'' as there
is a condition which would cause scsi_detach_link() to return without removing
the scsi_link from the SLIST.


# 1.235 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.234 11-Aug-2020 krw

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

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


# 1.233 10-Aug-2020 krw

Minor code shuffle to get all *_activate(), *_probe() and *_detach() functions
nestled together. Rename scsibusprint() to scsibussubprint() since it is used
with scsibussubmatch().


# 1.232 09-Aug-2020 krw

More code shuffling. Fix a rename missed in previous.


# 1.231 09-Aug-2020 krw

Shuffle functions and declarations around to more logical grouping. Nuke some
leading whitespace. Rename some local functions.


# 1.230 08-Aug-2020 krw

scsi_link's are born knowing their bus. Use that instead of passing extra
scsibus_softc pointers around.


# 1.229 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.228 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.227 16-Jul-2020 krw

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


# 1.226 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.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.241 02-Mar-2022 krw

Abstract the memory allocation, scsibus_softc data copying and
flag setting bits of creating a scsi_link into scsi_alloc_link().

Shrinks the bloated scsi_probe_link() a bit, makes it possible to
eventually create a useable scsi_link even when scsi_probe_link()
can't attach a device.

Developed from part of a diff submitted by Scott Nicholas via
tech@.


# 1.240 02-Mar-2022 krw

Move the code obtaining the LUN 0 scsi_link used to determine the
LUNs available to a target into scsi_get_target_luns(). Clearer
code and prep for future changes.

No functional change.

Extracted from a larger diff submitted by Scott Nicholas via
tech@.


# 1.239 28-Feb-2022 krw

Shuffle some SCSIDEBUG code to simplify code, tersify the
emitted verbiage, and show INQUIRY header & vendor info early so
humans can more easily determine what scsi_probe_link() will
do.

No functional change outside SCSIDEBUG.


# 1.238 24-Oct-2021 mpi

Constify struct cfattach.

ok visa@ a long time ago, ok krw@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.237 19-Nov-2020 krw

TL;DR -- don't configure devices that return insufficient INQUIRY data.

Treat INQUIRY data with fewer than SID_SCSI2_HDRLEN bytes as invalid.

Use only INQUIRY data returned by the device.

Get all available INQUIRY data (up to sizeof(struct scsi_inquiry_data))
even when SCSIDEBUG is not set.

Tweak returned INQUIRY data so additional_length field does not point
past end of returned data when available data is greater than
sizeof(struct scsi_inquiry_data).

Missing dmafree() spotted by gnezdo@. ok jmatthew@.


Revision tags: OPENBSD_6_8_BASE
# 1.236 18-Aug-2020 krw

Try to avoid a theoretical infinite loop while detaching all the scsi_link's on
the bus. Use SLIST_FOREACH_SAFE() rather than 'while (!SLIST_EMPTY())'' as there
is a condition which would cause scsi_detach_link() to return without removing
the scsi_link from the SLIST.


# 1.235 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.234 11-Aug-2020 krw

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

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


# 1.233 10-Aug-2020 krw

Minor code shuffle to get all *_activate(), *_probe() and *_detach() functions
nestled together. Rename scsibusprint() to scsibussubprint() since it is used
with scsibussubmatch().


# 1.232 09-Aug-2020 krw

More code shuffling. Fix a rename missed in previous.


# 1.231 09-Aug-2020 krw

Shuffle functions and declarations around to more logical grouping. Nuke some
leading whitespace. Rename some local functions.


# 1.230 08-Aug-2020 krw

scsi_link's are born knowing their bus. Use that instead of passing extra
scsibus_softc pointers around.


# 1.229 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.228 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.227 16-Jul-2020 krw

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


# 1.226 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.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.239 28-Feb-2022 krw

Shuffle some SCSIDEBUG code to simplify code, tersify the
emitted verbiage, and show INQUIRY header & vendor info early so
humans can more easily determine what scsi_probe_link() will
do.

No functional change outside SCSIDEBUG.


# 1.238 24-Oct-2021 mpi

Constify struct cfattach.

ok visa@ a long time ago, ok krw@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.237 19-Nov-2020 krw

TL;DR -- don't configure devices that return insufficient INQUIRY data.

Treat INQUIRY data with fewer than SID_SCSI2_HDRLEN bytes as invalid.

Use only INQUIRY data returned by the device.

Get all available INQUIRY data (up to sizeof(struct scsi_inquiry_data))
even when SCSIDEBUG is not set.

Tweak returned INQUIRY data so additional_length field does not point
past end of returned data when available data is greater than
sizeof(struct scsi_inquiry_data).

Missing dmafree() spotted by gnezdo@. ok jmatthew@.


Revision tags: OPENBSD_6_8_BASE
# 1.236 18-Aug-2020 krw

Try to avoid a theoretical infinite loop while detaching all the scsi_link's on
the bus. Use SLIST_FOREACH_SAFE() rather than 'while (!SLIST_EMPTY())'' as there
is a condition which would cause scsi_detach_link() to return without removing
the scsi_link from the SLIST.


# 1.235 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.234 11-Aug-2020 krw

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

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


# 1.233 10-Aug-2020 krw

Minor code shuffle to get all *_activate(), *_probe() and *_detach() functions
nestled together. Rename scsibusprint() to scsibussubprint() since it is used
with scsibussubmatch().


# 1.232 09-Aug-2020 krw

More code shuffling. Fix a rename missed in previous.


# 1.231 09-Aug-2020 krw

Shuffle functions and declarations around to more logical grouping. Nuke some
leading whitespace. Rename some local functions.


# 1.230 08-Aug-2020 krw

scsi_link's are born knowing their bus. Use that instead of passing extra
scsibus_softc pointers around.


# 1.229 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.228 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.227 16-Jul-2020 krw

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


# 1.226 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.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.238 24-Oct-2021 mpi

Constify struct cfattach.

ok visa@ a long time ago, ok krw@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.237 19-Nov-2020 krw

TL;DR -- don't configure devices that return insufficient INQUIRY data.

Treat INQUIRY data with fewer than SID_SCSI2_HDRLEN bytes as invalid.

Use only INQUIRY data returned by the device.

Get all available INQUIRY data (up to sizeof(struct scsi_inquiry_data))
even when SCSIDEBUG is not set.

Tweak returned INQUIRY data so additional_length field does not point
past end of returned data when available data is greater than
sizeof(struct scsi_inquiry_data).

Missing dmafree() spotted by gnezdo@. ok jmatthew@.


Revision tags: OPENBSD_6_8_BASE
# 1.236 18-Aug-2020 krw

Try to avoid a theoretical infinite loop while detaching all the scsi_link's on
the bus. Use SLIST_FOREACH_SAFE() rather than 'while (!SLIST_EMPTY())'' as there
is a condition which would cause scsi_detach_link() to return without removing
the scsi_link from the SLIST.


# 1.235 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.234 11-Aug-2020 krw

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

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


# 1.233 10-Aug-2020 krw

Minor code shuffle to get all *_activate(), *_probe() and *_detach() functions
nestled together. Rename scsibusprint() to scsibussubprint() since it is used
with scsibussubmatch().


# 1.232 09-Aug-2020 krw

More code shuffling. Fix a rename missed in previous.


# 1.231 09-Aug-2020 krw

Shuffle functions and declarations around to more logical grouping. Nuke some
leading whitespace. Rename some local functions.


# 1.230 08-Aug-2020 krw

scsi_link's are born knowing their bus. Use that instead of passing extra
scsibus_softc pointers around.


# 1.229 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.228 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.227 16-Jul-2020 krw

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


# 1.226 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.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.237 19-Nov-2020 krw

TL;DR -- don't configure devices that return insufficient INQUIRY data.

Treat INQUIRY data with fewer than SID_SCSI2_HDRLEN bytes as invalid.

Use only INQUIRY data returned by the device.

Get all available INQUIRY data (up to sizeof(struct scsi_inquiry_data))
even when SCSIDEBUG is not set.

Tweak returned INQUIRY data so additional_length field does not point
past end of returned data when available data is greater than
sizeof(struct scsi_inquiry_data).

Missing dmafree() spotted by gnezdo@. ok jmatthew@.


Revision tags: OPENBSD_6_8_BASE
# 1.236 18-Aug-2020 krw

Try to avoid a theoretical infinite loop while detaching all the scsi_link's on
the bus. Use SLIST_FOREACH_SAFE() rather than 'while (!SLIST_EMPTY())'' as there
is a condition which would cause scsi_detach_link() to return without removing
the scsi_link from the SLIST.


# 1.235 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.234 11-Aug-2020 krw

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

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


# 1.233 10-Aug-2020 krw

Minor code shuffle to get all *_activate(), *_probe() and *_detach() functions
nestled together. Rename scsibusprint() to scsibussubprint() since it is used
with scsibussubmatch().


# 1.232 09-Aug-2020 krw

More code shuffling. Fix a rename missed in previous.


# 1.231 09-Aug-2020 krw

Shuffle functions and declarations around to more logical grouping. Nuke some
leading whitespace. Rename some local functions.


# 1.230 08-Aug-2020 krw

scsi_link's are born knowing their bus. Use that instead of passing extra
scsibus_softc pointers around.


# 1.229 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.228 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.227 16-Jul-2020 krw

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


# 1.226 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.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.236 18-Aug-2020 krw

Try to avoid a theoretical infinite loop while detaching all the scsi_link's on
the bus. Use SLIST_FOREACH_SAFE() rather than 'while (!SLIST_EMPTY())'' as there
is a condition which would cause scsi_detach_link() to return without removing
the scsi_link from the SLIST.


# 1.235 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.234 11-Aug-2020 krw

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

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


# 1.233 10-Aug-2020 krw

Minor code shuffle to get all *_activate(), *_probe() and *_detach() functions
nestled together. Rename scsibusprint() to scsibussubprint() since it is used
with scsibussubmatch().


# 1.232 09-Aug-2020 krw

More code shuffling. Fix a rename missed in previous.


# 1.231 09-Aug-2020 krw

Shuffle functions and declarations around to more logical grouping. Nuke some
leading whitespace. Rename some local functions.


# 1.230 08-Aug-2020 krw

scsi_link's are born knowing their bus. Use that instead of passing extra
scsibus_softc pointers around.


# 1.229 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.228 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.227 16-Jul-2020 krw

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


# 1.226 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.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.235 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.234 11-Aug-2020 krw

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

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


# 1.233 10-Aug-2020 krw

Minor code shuffle to get all *_activate(), *_probe() and *_detach() functions
nestled together. Rename scsibusprint() to scsibussubprint() since it is used
with scsibussubmatch().


# 1.232 09-Aug-2020 krw

More code shuffling. Fix a rename missed in previous.


# 1.231 09-Aug-2020 krw

Shuffle functions and declarations around to more logical grouping. Nuke some
leading whitespace. Rename some local functions.


# 1.230 08-Aug-2020 krw

scsi_link's are born knowing their bus. Use that instead of passing extra
scsibus_softc pointers around.


# 1.229 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.228 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.227 16-Jul-2020 krw

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


# 1.226 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.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.234 11-Aug-2020 krw

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

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


# 1.233 10-Aug-2020 krw

Minor code shuffle to get all *_activate(), *_probe() and *_detach() functions
nestled together. Rename scsibusprint() to scsibussubprint() since it is used
with scsibussubmatch().


# 1.232 09-Aug-2020 krw

More code shuffling. Fix a rename missed in previous.


# 1.231 09-Aug-2020 krw

Shuffle functions and declarations around to more logical grouping. Nuke some
leading whitespace. Rename some local functions.


# 1.230 08-Aug-2020 krw

scsi_link's are born knowing their bus. Use that instead of passing extra
scsibus_softc pointers around.


# 1.229 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.228 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.227 16-Jul-2020 krw

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


# 1.226 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.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.233 10-Aug-2020 krw

Minor code shuffle to get all *_activate(), *_probe() and *_detach() functions
nestled together. Rename scsibusprint() to scsibussubprint() since it is used
with scsibussubmatch().


# 1.232 09-Aug-2020 krw

More code shuffling. Fix a rename missed in previous.


# 1.231 09-Aug-2020 krw

Shuffle functions and declarations around to more logical grouping. Nuke some
leading whitespace. Rename some local functions.


# 1.230 08-Aug-2020 krw

scsi_link's are born knowing their bus. Use that instead of passing extra
scsibus_softc pointers around.


# 1.229 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.228 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.227 16-Jul-2020 krw

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


# 1.226 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.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.231 09-Aug-2020 krw

Shuffle functions and declarations around to more logical grouping. Nuke some
leading whitespace. Rename some local functions.


# 1.230 08-Aug-2020 krw

scsi_link's are born knowing their bus. Use that instead of passing extra
scsibus_softc pointers around.


# 1.229 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.228 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.227 16-Jul-2020 krw

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


# 1.226 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.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.229 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.228 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.227 16-Jul-2020 krw

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


# 1.226 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.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.227 16-Jul-2020 krw

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


# 1.226 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.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.225 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.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.224 30-Jun-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.223 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.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.222 08-Dec-2019 krw

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


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.221 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.220 23-Nov-2019 krw

Consistently use !ISSET() to check for unset flags.


# 1.219 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

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


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

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

No functional change.

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


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

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

ok krw@


# 1.177 03-Jul-2011 matthew

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

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


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

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


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

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


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

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

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

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


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


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

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


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

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

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

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

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


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

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


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


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

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

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


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

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


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

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


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.218 09-Nov-2019 krw

Make sure that SDEV_NOSYNC abd SDEV_NOWIDE quirks are not
inadvertantly set on devices for which they are irrelevant or
incorrect. Lets these device operate at full speed.

ok sthen@ deraadt@


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

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


# 1.216 27-Sep-2019 krw

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


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

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


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

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


# 1.199 14-Aug-2019 krw

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


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


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

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.177 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


# 1.140 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.


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


Revision tags: OPENBSD_3_4_BASE
# 1.73 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


Revision tags: OPENBSD_3_1_BASE
# 1.67 14-Mar-2002 millert

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.217 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.216 27-Sep-2019 krw

Shuffle and consolidate SCSIDEBUG declarations into fewer sections in
more intuitive locations.


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


# 1.199 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.196 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.177 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


# 1.140 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.


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


Revision tags: OPENBSD_3_4_BASE
# 1.73 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


Revision tags: OPENBSD_3_1_BASE
# 1.67 14-Mar-2002 millert

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.215 24-Sep-2019 krw

Use consistent names for unused flags/quirks when printing same under
SCSIDEBUG.


# 1.214 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


# 1.199 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.196 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.177 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


# 1.140 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.


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


Revision tags: OPENBSD_3_4_BASE
# 1.73 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


Revision tags: OPENBSD_3_1_BASE
# 1.67 14-Mar-2002 millert

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.214 23-Sep-2019 krw

When printing the scsi_link info under SCSIDEBUG show state, luns,
openings, flags and quirks.


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


# 1.199 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.196 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.177 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


# 1.140 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.


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


Revision tags: OPENBSD_3_4_BASE
# 1.73 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


Revision tags: OPENBSD_3_1_BASE
# 1.67 14-Mar-2002 millert

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.213 16-Sep-2019 krw

Update list of device types with combination of FreeBSD and
SPC-5. Add/Fix comments.


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


# 1.199 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.196 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.177 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


# 1.140 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.


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


Revision tags: OPENBSD_3_4_BASE
# 1.73 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


Revision tags: OPENBSD_3_1_BASE
# 1.67 14-Mar-2002 millert

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.212 03-Sep-2019 krw

Shorten normal dmesg attach verbiage and expand SCSIDEBUG verbiage.

ok deraadt@


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


# 1.199 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.196 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.177 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


# 1.140 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.


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


Revision tags: OPENBSD_3_4_BASE
# 1.73 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


Revision tags: OPENBSD_3_1_BASE
# 1.67 14-Mar-2002 millert

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.211 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.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


# 1.199 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.196 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.177 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


# 1.140 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.


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


Revision tags: OPENBSD_3_4_BASE
# 1.73 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


Revision tags: OPENBSD_3_1_BASE
# 1.67 14-Mar-2002 millert

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.210 30-Aug-2019 krw

Rectify error made in 2006. SPC-2 == SCSI-3, not SCSI-2!!

Some cd/sd/safte/ses devices will now be correctly identified as
SCSI-3 and gain all the advantages associated with that lofty
status. e.g. READ CAP 16, REPORT LUNS.

ok deraadt@


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


# 1.199 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.196 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.177 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


# 1.140 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.


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


Revision tags: OPENBSD_3_4_BASE
# 1.73 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


Revision tags: OPENBSD_3_1_BASE
# 1.67 14-Mar-2002 millert

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.209 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.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


# 1.199 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.196 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.177 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


# 1.140 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.


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


Revision tags: OPENBSD_3_4_BASE
# 1.73 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


Revision tags: OPENBSD_3_1_BASE
# 1.67 14-Mar-2002 millert

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.208 27-Aug-2019 krw

Refactor probing logic to mirror detach logic. i.e. put smarts in
scsi_probe() and make scsi_probe_bus(), scsi_probe_target() and
scsi_probe_lun() simple wrappers around scsi_probe().

Abstract the determination of which luns to probe into a separate
function. Thus eliminating the need to remove/add lun 0 link while
probing devices modern enough to support REPORTLUNS. Which means the
lun 0 link is no longer in different positions in the scsi_link list
for such devices compared to older devices which are blindly probed
until an invalid LUN is encountered.


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


# 1.199 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.196 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.177 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


# 1.140 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.


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


Revision tags: OPENBSD_3_4_BASE
# 1.73 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


Revision tags: OPENBSD_3_1_BASE
# 1.67 14-Mar-2002 millert

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.207 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.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


# 1.199 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.196 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.177 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


# 1.140 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.


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


Revision tags: OPENBSD_3_4_BASE
# 1.73 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


Revision tags: OPENBSD_3_1_BASE
# 1.67 14-Mar-2002 millert

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.206 22-Aug-2019 krw

T10/BSR INCITS 503 (SPC-5) is apparently a thing. Update
version_to_spc() to map the formerly reserved value 0x07 in the
INQUIRY version field to 5 (a.k.a. SPC-5), instead of 0 (a.k.a. device
does not claim support for any SPC version).

Tweak comment for 0x03 mapping to note it means compliance to SPC, not
SPC-3. Tweak comment for 0x06 mappoing to specify the ANSI INCITS
513-2005 that documents SPC-4.


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


# 1.199 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.196 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.177 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


# 1.140 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.


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


Revision tags: OPENBSD_3_4_BASE
# 1.73 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


Revision tags: OPENBSD_3_1_BASE
# 1.67 14-Mar-2002 millert

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.205 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.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


# 1.199 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.196 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.177 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


# 1.140 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.


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


Revision tags: OPENBSD_3_4_BASE
# 1.73 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


Revision tags: OPENBSD_3_1_BASE
# 1.67 14-Mar-2002 millert

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.204 18-Aug-2019 krw

Every "goto bad" in scsi_probedev() deserves a SC_DEBUG().


# 1.203 18-Aug-2019 krw

Rename 'link' to 'link0' as it refers to target 0 only.


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


# 1.199 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.196 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.177 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


# 1.140 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.


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


Revision tags: OPENBSD_3_4_BASE
# 1.73 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


Revision tags: OPENBSD_3_1_BASE
# 1.67 14-Mar-2002 millert

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.202 18-Aug-2019 krw

When activating or detaching a target don't search the scsi_link SLIST
for each target:lun. Just travese the SLIST once taking care of relevant
scsi_link's as they are encountered.

ok jmatthew@


# 1.201 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.200 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


# 1.199 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.196 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.177 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


# 1.140 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.


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


Revision tags: OPENBSD_3_4_BASE
# 1.73 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


Revision tags: OPENBSD_3_1_BASE
# 1.67 14-Mar-2002 millert

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.199 14-Aug-2019 krw

scsi_[add|remove]_link() are local functions so move their
declarations.


# 1.198 14-Aug-2019 krw

Whitespace nit. Add {} around body of SLIST_FOREACH().


# 1.197 14-Aug-2019 krw

Tweak some comments.


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.196 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.177 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


# 1.140 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.


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


Revision tags: OPENBSD_3_4_BASE
# 1.73 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


Revision tags: OPENBSD_3_1_BASE
# 1.67 14-Mar-2002 millert

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


Revision tags: OPENBSD_6_2_BASE
# 1.196 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_0_BASE OPENBSD_6_1_BASE
# 1.195 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.194 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_9_BASE
# 1.193 23-Aug-2015 tedu

add some sizes to free. looked over by deraadt


Revision tags: OPENBSD_5_8_BASE
# 1.192 07-Jun-2015 krw

More damned eye searing whitespace.


# 1.191 14-Mar-2015 jsg

Remove some includes include-what-you-use claims don't
have any direct symbols used. Tested for indirect use by compiling
amd64/i386/sparc64 kernels.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.190 11-Feb-2015 dlg

we dont need sys/lock.h because we dont use lockmgr, but we do need
sys/atomic.h for atomic_setbits_int.


# 1.189 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_6_BASE
# 1.188 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.187 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.186 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.185 06-Dec-2013 deraadt

Add a DVACT_WAKEUP op to the *_activate() API. This is called after the
kernel resumes normal (non-cold, able to run processes, etc) operation.
Previously we were relying on specific DVACT_RESUME op's in drivers
creating callback/threads themselves, but that has become too common,
indicating the need for a built-in mechanism.
ok dlg kettenis, tested by a sufficient amount of people


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.184 16-Oct-2012 jsg

#if SCSIDEBUG -> #ifdef SCSIDEBUG
matches the rest of the scsi code.


# 1.183 08-Oct-2012 deraadt

Revamp the sequences for suspend/hibernate -> resume so that the code
paths are reflexive. It is now possible to fail part-way through a
suspend sequence, and recover along the resume code path.
Split DVACT_SUSPEND by adding a new DVACT_POWERDOWN method is used
after hibernate (and suspend too) to finish the job. Some drivers
must be converted at the same time to use this instead of shutdown hooks
(the others will follow at a later time)
ok kettenis mlarkin


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.182 22-Sep-2011 jsing

Fix order of arguments passed to malloc(9) - type first then flags.


# 1.181 02-Sep-2011 dlg

generate a devid from vpd page 80 if vpd page 83 doesnt exist or work.

ok krw@


Revision tags: OPENBSD_5_0_BASE
# 1.180 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.179 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.178 05-Jul-2011 matthew

Garbage collect SDEV_S_WAITING and scsi_link->scsibus now that nothing
needs either of them.

ok krw@


# 1.177 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.176 21-Jun-2011 matthew

scsi_link's scsibus field is redundant with bus->sc_dev.dv_unit, so
remove two of the remaining three uses of it. (softraid(4) still uses
it at the moment, so the field and its assignment in scsibusattach()
stay for now...)

ok krw@; feedback and "tenteiramen rejid"(!?) miod@


# 1.175 04-May-2011 sthen

When printing scsi device ids, skip leading blanks and collapse multiple
whitespace into one. Written after Mitja showed a particularly unwieldy
attach line:

sd0 at scsibus0 targ 2 lun 0: <ATA, HTS721010G9SA00, MCZI> SCSI3 0/direct fixed t10.ATA_____HTS721010G9SA00_______________________________blahblahblah

ok/incorporating a suggestion from matthew@, krw@ likes it, dlg@ doesn't
feel strongly either way.


# 1.174 29-Apr-2011 dlg

zero out a scsi_links node_wwn and port_wwn fields after initialising it
by copying the adapters scsi_link. this way devices wont inherit the
adapters addresses on fc fabrics.


# 1.173 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.172 06-Apr-2011 dlg

unconditionally print scsi device ids instead of just when mpath is
enabled so people can get used to it.

ok deraadt@


# 1.171 05-Apr-2011 dlg

do inquiries against dmaable memory while probing devices.

found by marco@
ok and tweaks deraadt@ krw@


# 1.170 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@


# 1.169 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.168 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.167 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.166 08-Sep-2010 dlg

activate hooks should return a value.

all from deraadt@
tested by me with hotplugged disks on mpi(4)


# 1.165 02-Sep-2010 dlg

the page_length field in the vpd page header is 2 bytes, not 1.

ok krw@ marco@ matthew@


# 1.164 31-Aug-2010 deraadt

Add DVACT_QUIECE support. This is called before splhigh() and before
DVACT_SUSPEND, therefore DVACT_QUIECE can do standard sleeping operations
to get ready.
Discussed quite a while back with kettenis and jakemsr, oga suddenly needed
it as well and wrote half of it, so it was time to finish it.
proofread by miod.


# 1.163 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.162 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.161 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.160 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.159 30-Jun-2010 deraadt

for scsibus, silence the activate function when unknown events are given.
they are supposed to do, or be silent.
ok mlarkin


# 1.158 30-Jun-2010 kettenis

Flush cache before suspend.

ok krw@, marco@


# 1.157 23-Apr-2010 deraadt

Merge the only relevant (for now) parts of simplelock.h into lock.h
since it is time to start transitioning away from the no-op behaviour.
ok oga kettenis


# 1.156 17-Apr-2010 dlg

use the iopools mutex to protect the semaphore wrapping the openings
runqueue. less is more sometimes.


# 1.155 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@


Revision tags: OPENBSD_4_7_BASE
# 1.154 01-Jan-2010 miod

If you want to use atomic ops, you need to #include the proper files instead
of relying upon other headers bringing it in for you.


# 1.153 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.152 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.151 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.150 10-Nov-2009 dlg

dont compare devids when we dont have a devid to compare with.
DEVID_CMP now evaluates to false if the devids are NULL.

some stupid devices dont understand luns, so we have code that
detects when the device at lun 0 also appears at luns 1, 2, 3, and
so on. this check is short circuited if the devices report different
devids. no devids isnt the same as different devids though.

found by okan@ on ciss (which currently ignores luns).
tested by krw@ marco@ johan@ okan@
ok krw@ marco@


# 1.149 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.148 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.147 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.146 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.145 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.144 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


# 1.143 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.142 10-Aug-2009 dlg

if mpath steals a link, print out where the link was stolen so dmesg still
shows the physical topology of your system.


# 1.141 10-Aug-2009 dlg

pull the printing out of scsibusprint so it can be used against scsi_link
structures by things other than autoconf.


# 1.140 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.


# 1.139 08-Aug-2009 dlg

if the adapters wwn fields are set, print them out when attaching scsibus.

we need this to get some clue as to which ports are which on an fc fabric.

requested by and ok deraadt@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.138 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.137 16-Feb-2009 krw

Don't try to SCSIDEBUG targets or luns >31 since we only have 32 bits to
use to identify devices of interest.

ok deraadt@


# 1.136 16-Feb-2009 dlg

on some buses (eg sas and fc fabrics) the initiator id doesnt mean
anything. we represent that in the midlayre by moving the initiator id out
of the buswidth. let's not print it in that case.

ok deraadt@ kettenis@ krw@ marco@


Revision tags: OPENBSD_4_4_BASE
# 1.135 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.134 22-Jul-2008 dlg

tweak comment to reflect the new reality after my last change.


# 1.133 21-Jul-2008 dlg

when probing a device the midlayer queries its inquiry data and
keeps it on the stack till we attach a driver to it. then it copies
the inquiry data int the scsi_link struct.

this diff uses the scsi_link struct instead of the stack for that data,
which makes the inq data for users of the scsi_link struct available much
earlier during device probe.

review and feedback from both krw@ and marco@


# 1.132 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.131 26-May-2008 kettenis

Print SCSI initiator ID such that it is easier to spot configuration
problems.

ok krw@, marco@, deraadt@


# 1.130 24-Apr-2008 krw

Say 'ATAPI' rather than 'SCSIn' for ATAPI devices found on (pseudo)
SCSI buses like atapiscsi. This more accurately describes the
commands that will be used on the device.

ok dlg@


Revision tags: OPENBSD_4_3_BASE
# 1.129 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.128 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@


# 1.127 06-Nov-2007 krw

Fix SDF_DIRTY handling, eliminate useless SDF_FLUSHING. The sd_flush()
called from the last sdclose() on a device will now reset SDF_DIRTY
after submitting the SYNCHRONIZE CACHE command. sddone() need not
worry about SDF_DIRTY since it was never called for the SYNCHRONIZE
CACHE command anyway.

This eliminates a spurious SYNCHRONIZE CACHE command being issued for
every sd device from sd_shutdown().

ok dlg@


# 1.126 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.125 08-May-2007 deraadt

all scsidebug_*-using code is under #ifdef, so the variables themselves should be too


Revision tags: OPENBSD_4_1_BASE
# 1.124 29-Dec-2006 pedro

Avoid void * arithmetic, okay deraadt@, suggestions from millert@


# 1.123 28-Nov-2006 dlg

give scsi controllers a real attach args to fill in when attaching scsibus.

ok miod@ marco@ deraadt@


# 1.122 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.121 27-Nov-2006 dlg

add bio code to do hotplug of devices on the scsibus.

thumbs up deraadt@


# 1.120 27-Nov-2006 dlg

hook scsibus up to bio.

ok deraadt@ krw@ an earlier diff was ok marco@ too


# 1.119 27-Nov-2006 dlg

if there are no luns on a target, then say there were no devices,
otherwise return any error we find during detach of the luns.


# 1.118 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.117 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.116 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.115 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


# 1.114 01-Oct-2006 dlg

whitespace tweaks


# 1.113 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@


# 1.112 21-Sep-2006 dlg

when we probe and find devices on the scsibus, we allocate a scsi_link
struct for it and keep it in the midlayer. however, this struct was never
free'd on detach.

since we only do hotplugging of controllers (and the scsibus and devices
get hotplugged as a matter of course), we now walk the list of scsi_link
structs and free them on detach of scsibus.

ok marco@


Revision tags: OPENBSD_4_0_BASE
# 1.111 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.110 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.109 22-Jul-2006 krw

Allocate enough, and only enough, scsi_link pointers for the number of
LUNs the driver says targets could have. Don't unconditionally
allocate 8. USB and ATAPI devices have fewer. Fibre Channel devices
can have more.


# 1.108 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.107 14-Jul-2006 krw

Don't keep a special copy of the INQUIRY data for LUN 0 anymore. There
is now a copy in the scsi_link structure so just use that one.

'looks reasonable' beck@ ok dlg@


# 1.106 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.105 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.104 11-Jul-2006 dlg

knf and ansi. no binary change.


# 1.103 11-Jul-2006 dlg

remove an if 0 chunk thats been with us forever, but never used and never
will be.


# 1.102 13-May-2006 krw

And the fallout from Manuel Pata's USB reader rumbles on ...

Fix the display of the device info for umass devices at lun 0 by
passing the correct inquiry data to config_attach. i.e. not the
inquiry data for lun 1, which we gratuitously probe to prevent USB
card readers from 'helpfully' lying about who is where, but the
inquiry data for lun 0 we have saved in sc_link->inqdata.


# 1.101 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.100 21-Jan-2006 miod

Invoke disk_detach() and related cleanup work in detach(), rather than
zeroref() - just to be on the safe side, should we mess up our ref count.


# 1.99 18-Jan-2006 krw

Don't index before the start of the sc_link array if scsi_probe_bus()
is called with a target of -1 and a valid lun. Spotted by Miod.

ok miod@


# 1.98 13-Nov-2005 krw

Use SCSI_DELAY only once. Document it. Default to no delay.

Fixes two second system 'freeze' when umass device plugged in. Speeds
up boot by not waiting for a minimum of 2 seconds at each scsi bus.

ok jmc@ pedro@ deraadt@


# 1.97 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.96 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.95 07-May-2005 krw

Eliminate 'mode sense (n) returned nonsense' and 'could not mode sense
(4/5)' messages in favour of a single SC_DEBUG() message about the mode
sense error. Less useless verbiage.

As this eliminates the only SDEV_NOMODESENSE use in scsi/*, eliminate
all quirks table entries that used only SDEV_NOMODESENSE. Iomega Zip
tested by miod@ to ensure this did not break something.

Finally, only fake a geometry if scsi_size() can determine a disk size
to fake from.


# 1.94 28-Apr-2005 krw

Some really braindead usb devices such as x-in-1 card reader/writers
try to help equally braindead os's by presenting any inserted media as
LUN 0 until another LUN is used in a command. Trick them by issuing a
gratuitous/harmless INQUIRY to LUN 1 after issuing the LUN 0 INQUIRY
but before any other command. Only umass scsi devices with >1 lun are
affected.

Fixes dlg's reader/writer for one.

Lots of diagnosis and testing by dlg@, ok dlg@, ok marco@.


# 1.93 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@.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.92 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@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.91 09-May-2004 krw

Remove some #ifndef __OpenBSD__/#endif sections that were intended to
make sync'ing with NetBSD easier. NetBSD has moved on, most of these
sections have not.

ok marco@ deraadt@ grange@ csapuntz@


# 1.90 07-May-2004 krw

Set value for rslt before trying to display it. 'Bad LUN ...' messsage
will now display correct error value.

Spotted in SCSIDEBUG output from Adrian Close's bizarre USB device.

ok millert@ deraadt@ marco@.


# 1.89 07-May-2004 miod

Remove mvme88k workaround for ssh(4) bugs.


# 1.88 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.87 10-Mar-2004 krw

branches: 1.87.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.86 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.85 07-Feb-2004 krw

If scsi_probe_bus() is called with a particular lun, ensure that lun 0
information is available to make the new lun validation logic work.
i.e. don't find phantom luns just because the user asks about them.

Also ensure the lun value given does not exceed the maximum valid lun
for a bus, rather than assuming the maximum valid lun is 7.

ok tdeval@ deraadt@.


# 1.84 30-Jan-2004 tdeval

Backout until we have a better implementation...


# 1.83 29-Jan-2004 tdeval

"And you definitely don't want to use p_priority. Use PRIBIO"
From art@


# 1.82 29-Jan-2004 tdeval

Avoid an annoying freeze during attach of live "scsibus" devices.
looks ok deraadt@, mickey@, krw@ and art@(but I hate it)


# 1.81 25-Jan-2004 krw

Allow restriction of SCSIDEBUG output to particular scsi buses in
addition to device targets and luns.

ok deraadt@.


# 1.80 24-Jan-2004 deraadt

ugly #ifdef to be deleted later
must still do TUR on mvme68k & mvme88k ssh(4) driver
ok miod krw


# 1.79 23-Jan-2004 krw

Don't probe impossible luns. If lun 0 is non-existant, or if the device shows
it doesn't grok luns then stop probing.

Speeds up the probe of an empty scsi bus by approx. 30 seconds.

From Marco Peereboom.

Tested by Marco Peereboom, millert@, miod@, Diana Eichart, and a host of
anonymous snapshot users.

ok deraadt@.


# 1.78 17-Jan-2004 krw

Use SC_DEBUG() to display debug messages. Makes SCSIDEBUG output better.

ok tdeval@.


# 1.77 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@.


# 1.76 07-Jan-2004 krw

Some code cleanup and fixes inspired by NetBSD changes from mycroft@
and pointed out by Nate@. Fixes some <, , > displays for devices that
were not filling the INQUIRY data in correctly. Silences INQUIRY
commands that fail during probe. Treats SID_QUAL_LU_OFFLINE results
the same as SID_QUAL_BAD_LU. Eliminate special treatment for DEC TK30
and DEC TK50 devices.

ok tdeval@.


# 1.75 27-Oct-2003 mickey

atlas does indeed support tagging and only the siop was broken


# 1.74 30-Sep-2003 mickey

quantum atlas iv 9 wls lies about tags


Revision tags: OPENBSD_3_4_BASE
# 1.73 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: UBC_SYNC_A
# 1.72 17-May-2003 nate

dale's camera has a usb quirk now


# 1.71 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.70 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.69 04-Sep-2002 tdeval

Write sentences.


# 1.68 04-Sep-2002 tdeval

Add support for RBC (simplified direct) devices.
ok costa@, krw@


Revision tags: OPENBSD_3_1_BASE
# 1.67 14-Mar-2002 millert

First round of __P removal in sys


# 1.66 09-Mar-2002 krw

Be less parochial and remember that others may need to use quirks!

Just set the SDEV_NOTAGS, SDEV_NOWIDE, SDEV_NOSYNC bits in
quirks. DON'T zero all other bits that may have already been set.

Noted (and fix tested) by lebel@.


# 1.65 28-Feb-2002 krw

Start quirks off with NOWIDE, NOSYNC and NOTAGS and remove the
restrictions as the results of the INQUIRY command and the quirks
table indicate.

This should (for drivers that pay attention) make for more successful
communication with devices having quirks, by using the lowest common
denominator until more information is available.

Issue a second TEST_UNIT_READY command after the INQUIRY command has
been processed to allow drivers waiting for valid quirks data to set
sync/wide/tags asap. Rework a little bit of the logic to ensure that
negotiation messages produced by the TEST_UNIT_READY command do not
get mixed up with attachment messages. This has the side benefit of
putting the negotiation messages before the device description, where
they have usually been displayed in the past.

If a driver is examining and using quirks data before the INQUIRY
command is processed, and not renegotiating after the INQUIRY command,
it may now end up with async 8 bit, non-tagged transfers. Before it
would have ended up with possibly unusable transfer parameters when
talking to a device with quirks.

ok costa@


# 1.64 16-Feb-2002 millert

Disable tagged queueing for HP C3725S and IBM DCAS drives where
is is broken.


Revision tags: UBC_BASE
# 1.63 02-Nov-2001 millert

branches: 1.63.2;
In scsi_strvis(), collapse adjacent whitespace/NUL chars to a single
space to get the most info with the least amount of wasted space.
OK krw@, niklas@


Revision tags: OPENBSD_3_0_BASE
# 1.62 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.61 26-Aug-2001 millert

Don't restrict MICROP 4421-07 quirk to a specific firmware revision


# 1.60 25-Aug-2001 fgsch

Change scsi_[free|get]_xs to use pool(9); art@ krw@ miod@ ok.


# 1.59 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.58 24-Jun-2001 mickey

cold is in systm now


# 1.57 22-Jun-2001 deraadt

KNF


# 1.56 24-May-2001 angelos

Check malloc() return value, from tedu@heorot.stanford.edu


Revision tags: OPENBSD_2_9_BASE
# 1.55 22-Jan-2001 csapuntz

ATAPI CD-ROMs BCD-16X and BCD-24X have troubles starting and stopping their disks


# 1.54 23-Nov-2000 deraadt

fix lun support, not as nice as i would like


# 1.53 20-Nov-2000 deraadt

limit luns on usb


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.52 18-Apr-2000 csapuntz

sd and scsibus detach

cdlock/cdunlock now through disk_lock/disk_unlock


# 1.51 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


# 1.50 21-Feb-2000 mjacob

add T_ENCLOSURE name and NOLUN Photon SENA devices


Revision tags: SMP_BASE
# 1.49 31-Dec-1999 millert

branches: 1.49.2;
Add SDEV_NOLUNS quirk for NEC CD-ROM DRIVE:501


# 1.48 16-Dec-1999 mjacob

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.


# 1.47 11-Dec-1999 csapuntz

LS-120's do support mode sense.


Revision tags: kame_19991208
# 1.46 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.45 24-Jul-1999 deraadt

oops, LS-120 entry munged


# 1.44 24-Jul-1999 deraadt

SDEV_NOMODESENSE on LS-120 VER5 00


# 1.43 20-Jul-1999 csapuntz

Make acd redundant.

Mostly based on NetBSD-current


Revision tags: OPENBSD_2_5_BASE
# 1.42 24-Feb-1999 downsj

Zip250 doesn't do modesense, either.


# 1.41 28-Nov-1998 downsj

Add another Sony CD-ROM, wvdputte@reptile.rug.ac.be


# 1.40 11-Nov-1998 deraadt

some scsi devices use 0xff as string terminator in inquiry strings; soren@t.dk


Revision tags: OPENBSD_2_4_BASE
# 1.39 19-Jul-1998 downsj

Don't bother trying to use luns on any CyberDrv devices.


# 1.38 26-Jun-1998 deraadt

no luns on the ricoh scanner


# 1.37 05-May-1998 deraadt

more lun flakes; rh@vip.at, simonb@telstra.com.au


# 1.36 25-Apr-1998 deraadt

some exceptions from netbsd


Revision tags: OPENBSD_2_3_BASE
# 1.35 18-Mar-1998 deraadt

more SDEV_AUTOSAVE devices


# 1.34 16-Feb-1998 deraadt

jaz drives do not do SDEV_NOTAGS


# 1.33 12-Jan-1998 kstailey

Obsure, old NEC SCSI semi-disk. A big blob of RAM with a SCSI disk interface.


Revision tags: OPENBSD_2_2_BASE
# 1.32 30-Sep-1997 millert

Quirk for Cipher ST150S tape drive, jbernard@tater.mines.edu


# 1.31 30-Sep-1997 millert

Quirks for revs 015 and 016 of the hitachi dk515. jbernard@tater.mines.edu


# 1.30 11-Sep-1997 deraadt

another bad lun handler; pk@netbsd


# 1.29 25-Jul-1997 mickey

more quirks from netbsd


Revision tags: OPENBSD_2_1_BASE
# 1.28 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.27 02-Apr-1997 deraadt

most 1.3X versions of MEDIAVIS CDR-H93MV have problems; koji@math.human.nagoya-u.ac.jp


# 1.26 13-Mar-1997 briggs

UMAX SuperVista S-12 also needs a NOLUNS quirk.


# 1.25 27-Feb-1997 tholo

Add quirk for a scanner


# 1.24 24-Feb-1997 jkatz

Adds support for DEC SCSI tape drives used in Vaxen that can also be used
in PC's. from port-vax@netbsd.org


# 1.23 18-Jan-1997 niklas

Boundary error (s/<=/</)


# 1.22 16-Jan-1997 kstailey

prevent scsiconf.c:110: warning: unused variable `l'
also, #ifdef 0 -> #ifndef __OpenBSD__


# 1.21 16-Jan-1997 maja

Added scsiprint from NetBSD, needed by new driver for VAX. -moj


# 1.20 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.19 28-Nov-1996 niklas

Make SCSI debugging more dynamic, more targets and luns can be
debugged simultaneously and which ones, as well as the verbosity, can be
determined at runtime.


# 1.18 25-Nov-1996 millert

Oops, we don't have SDEV_NOSTARTUNIT. Remove TEAC scsi floppy quirk for now.


# 1.17 25-Nov-1996 millert

Add some quirky devices from NetBSD.


# 1.16 23-Nov-1996 kstailey

added const to second parameter of cfprint_t routines


# 1.15 20-Oct-1996 millert

Add quirk entries for 2 optical drives, NetBSD PR #2861


Revision tags: OPENBSD_2_0_BASE
# 1.14 30-Aug-1996 downsj

Add an IBM quirk.


# 1.13 15-Aug-1996 shawn

for NEC 210 CD-ROM drivers


# 1.12 24-Jul-1996 deraadt

for sun-modified maxtor XT-8760S drives; from ivanenko@ctpa03.mit.edu


# 1.11 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.10 06-May-1996 deraadt

shinaken cd has lun problem


# 1.9 02-May-1996 deraadt

no sys/cpu.h, fix bugs in ch


# 1.8 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.7 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.6 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.5 12-Jan-1996 deraadt

no luns on Tandberg 3600 w/ fake Archive Viper emulation roms; from
raeburn@raeburn.org; netbsd pr#1934


# 1.4 10-Jan-1996 briggs

Save inquiry flags in sc_link so low-level drivers can use it.


# 1.3 01-Jan-1996 deraadt

lun problem on Chinon CDS-525; from k125374@cs.tut.fi; netbsd pr#1686.


# 1.2 14-Dec-1995 deraadt

from netbsd:
add a bunch of rogues
Trim NULs, in addition to spaces, in scsi_strvis().


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision