Home | History | Annotate | only in /src/sys/dev/scsipi
History log of /src/sys/dev/scsipi
RevisionDateAuthorComments
 1.6 11-Dec-2005  christos merge ktrace-lwp.
 1.5 21-Feb-2005  thorpej Part 1 of a cleanup pass over the SCSI subsystem. The aim is to name
everything "scsi_*", since we really are talking about the SCSI command
set, ATAPI transport not withstanding. Improve the names of many structures,
and prepend "SCSI_" onto all SCSI command opcodes. Place items described
by the SCSI Primary Commands document into scsi_spc.h.
 1.4 31-May-2001  tron branches: 1.4.22; 1.4.30; 1.4.32;
Install "dev/scsipi/scsipi_cd.h" again because it is e.g. used by the
CD player include in version 2.x of the "kdemultimedia" package.
 1.3 11-Apr-2001  jdolecek Only install headers which are actually used by our userland. This
saves about 2.2MB under /usr/include/dev/. Discussed on tech-kern@
recently.

I HOPE to get the list right. The headers I left in are ones
used for MI tools and those whose usage I discovered by grep over tree sources.
Feel free to put needed includes back in if you encounter anything which
should not be removed from lists.
 1.2 20-Feb-2000  mjacob branches: 1.2.6;
add ses.h to list of includes
 1.1 12-Jun-1998  cgd branches: 1.1.14;
Rework the way kernel include files are installed. In the new method,
as with user-land programs, include files are installed by each directory
in the tree that has includes to install. (This allows more flexibility
as to what gets installed, makes 'partial installs' easier, and gives us
more options as to which machines' includes get installed at any given
time.) The old SYS_INCLUDES={symlinks,copies} behaviours are _both_
still supported, though at least one bug in the 'symlinks' case is
fixed by this change. Include files can't be build before installation,
so directories that have includes as targets (e.g. dev/pci) have to move
those targets into a different Makefile.
 1.1.14.2 21-Apr-2001  bouyer Sync with HEAD
 1.1.14.1 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.2.6.1 21-Jun-2001  nathanw Catch up to -current.
 1.4.32.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.4.30.1 29-Apr-2005  kent sync with -current
 1.4.22.1 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.4 14-May-2001  bouyer Use SCSI/ATAPI common definition for MODE_{SELECT,SENSE}{,_BIG}. Define
functions to send theses commands in scsipi_base.c and use them instead
of ad-hoc commands setups.
 1.3 13-Feb-1998  enami branches: 1.3.26;
Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.2 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.1 15-Jan-1998  cgd add support for an ATAPI attachment for 'sd'.
fix 'cd' driver's NCD_SCSI bogosity (was using testing wrong macro!)
clean up in various ways:
* make common atapi_mode_{sense,select}() functions.
* put ATAPI data structures in more sensible headers, split up by
device type.
* include headers a bit more carefully.
* pass flags to attachment-specific cd functions, and use them.
* get rid of SCSI bits in scsipi_base.h's scsipi_make_xs(), move
them into the correct place in scsi_base.c.
* fix minor typo in struct name in scsipiconf.h (which was apparently
never used except in a #define later in the same file).
* use __attribute__ to force 4-byte alignment for xs command store,
so that architectures trying to bus_space_write_multi_N() (where
N > 1) that data to a controller won't lose.
* clean up a few comments in typos, and make a few #defines easier to
understand/maintain.
* rename cd_link.h to cdvar.h (via repository copy). This is exactly
what a 'var' file is supposed to be.
 1.3.26.1 28-Feb-2002  nathanw Catch up to -current.
 1.30 03-Feb-2019  mrg - add or adjust /* FALLTHROUGH */ where appropriate
- add __unreachable() after functions that can return but won't in
this case, and thus can't be marked __dead easily
 1.29 18-Oct-2014  snj branches: 1.29.20;
src is too big these days to tolerate superfluous apostrophes. It's
"its", people!
 1.28 28-Apr-2008  martin branches: 1.28.44;
Remove clause 3 and 4 from TNF licenses
 1.27 05-Apr-2008  cegger branches: 1.27.2; 1.27.4;
use aprint_*_dev and device_xname
 1.26 11-Dec-2005  christos branches: 1.26.70;
merge ktrace-lwp.
 1.25 29-May-2005  christos - Sprinkle const
- Avoid variable shadowing.
- Eliminate some caddr_t abuse.
 1.24 21-Feb-2005  thorpej Part 1 of a cleanup pass over the SCSI subsystem. The aim is to name
everything "scsi_*", since we really are talking about the SCSI command
set, ATAPI transport not withstanding. Improve the names of many structures,
and prepend "SCSI_" onto all SCSI command opcodes. Place items described
by the SCSI Primary Commands document into scsi_spc.h.
 1.23 17-Sep-2004  mycroft branches: 1.23.4; 1.23.6;
Change the way bustype_cmd is used. Rather than having it be responsible for
calling scsipi_make_xs() and scsipi_execute_xs(), instead push these into
scsipi_command. Make bustype_cmd and PHOLD/PRELE be called from
scsipi_execute_xs(). This allows us to create a xfer structure -- possibly on
the stack -- and call scsipi_execute_xs() directly.
 1.22 09-Sep-2004  bouyer Make the xxstart() functions reentrant again, as some drivers HBA can call
scsipi_done() from their scsipi_request().
For this, add a struct scsipi_xfer * argument to scsipi_command().
If not NULL scsipi_command() will use this to enqueue this xfer, otherwise
it'll try to allocate a new one. This scsipi_xfer has to be allocated
and initialised by scsipi_make_xs() or equivalent.
In xxstart(), allocate a scsipi_xfer using scsipi_make_xs(), and if not NULL,
dequeue the buffer before calling scsipi_command(). This makes sure that
scsipi_command() will not fail, and also makes sure that xxstart() won't
be called again between the BUFQ_PEEK() and BUFQ_GET().

Fix "dequeued wrong buf" panics reported by Juergen Hannken-Illjes in
private mail and Andreas Wrede on current-users@.
Thanks to Jason Thorpe and Chuck Silver for review, and Andreas Wrede for
testing the patch.
 1.21 27-Aug-2004  bouyer Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
Havard Eidnes's analysis of this problem is that the scsipi_xfer pool is
competing for resources with other pools, including the the inode and vnode
pools which can grow quite large.

*_scsipi_cmd(): don't biodone the buffer if scsipi_make_xs() fails, let the
caller deal with the problem
start function of block devices drivers: dequeue the buffer after the
scsipi_command() call. If scsipi_command() fails with ENOMEM don't dequeue
the buffer, and schedule a callout to call the start function after
some delay.
scsipi_init(): prime the scsipi_xfer_pool with one page. This ensure that
there is always some scsipi_xfer to play with. If scsipi_command() fails
because of pool_get(), we're sure there will be resources available later,
when the pending commands have completed.

Reviewed by Jason Thorpe and Havard Eidnes.
Todo: remove the "unable to allocate scsipi_xfer" and "not queued, error %d"
printfs, but I choose to keep them for now, to help make sure the code does
what it should.
 1.20 21-Aug-2004  thorpej Use ANSI function decls and make use of static.
 1.19 10-Mar-2004  bouyer branches: 1.19.2;
Decrease xs_retries before retrying aborted commands, and report EIO if it
reaches 0. Avoids looping on aborded command in some special cases.
 1.18 15-Nov-2001  lukem branches: 1.18.16;
don't need <sys/types.h> when including <sys/param.h>
 1.17 13-Nov-2001  lukem add RCSIDs
 1.16 14-May-2001  bouyer branches: 1.16.2;
Use SCSI/ATAPI common definition for MODE_{SELECT,SENSE}{,_BIG}. Define
functions to send theses commands in scsipi_base.c and use them instead
of ad-hoc commands setups.
 1.15 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.14 30-Sep-1999  thorpej branches: 1.14.2; 1.14.14;
Cleanup the scsipi_xfer flags:
- `flags' is now gone, replaced with `xs_control' and `xs_status'.
- Massive cleanup of the control flags. Now we explicitly say that
a job is to complete asynchronously, rather than relying on side-effects,
and use a new flag to now that device discovery is being performed.
- Do SCSI device discovery interrupt-driven.
 1.13 28-Aug-1999  thorpej If scsipi_make_xs() fails, make sure to flag the buffer as error'd,
if provided, and biodone() it. Callers of scsipi_command() expect
this to be done.
 1.12 25-Jun-1999  thorpej Go to splbio() when calling scsipi_free_xs(), just like the SCSI counterpart.
Fixes PR #7853, Paul Dokas <dokas@cs.umn.edu>.
 1.11 17-Nov-1998  bouyer branches: 1.11.4; 1.11.6; 1.11.8;
In atapi_wdc.c, issue a 'REQUEST SENSE' command when appropriate.
Return XS_SENSE when the full sense info has been retrieved, or
XS_SHORTSENSE if only the sense key was available (from the error register)
Make atapi_interpret_sense() deal with this, and call scsipi_interpret_sense()
for XS_SENSE. (XXX sd_interpret_sense() and the ioctl code needs to be made
aware of XS_SHORTSENSE too ! sense hanlding for these is now less broken for
devices that support 'REQUEST SENSE')
All the ATAPI devices I have access to seems to honnor the SENSE_REQUEST
command, but I suspect some ATAPI devices will not (althouh it's mandatory).
The code should be able to deal with this, but is untested ...
 1.10 12-Oct-1998  bouyer Merge bouyer-ide
 1.9 15-Aug-1998  mycroft branches: 1.9.2;
Assign my copyrights to TNF.
 1.8 15-Jul-1998  mjacob part of fix for kern/3835: use of enumerated returns from target sense handlers
 1.7 11-Jul-1998  mjacob Clarify that Sense Key 1 is a "RECOVERED ERROR"- not just an error.
 1.6 01-Jul-1998  mjacob Use new Sense Key defines (for clarity)
 1.5 15-Jan-1998  cgd add support for an ATAPI attachment for 'sd'.
fix 'cd' driver's NCD_SCSI bogosity (was using testing wrong macro!)
clean up in various ways:
* make common atapi_mode_{sense,select}() functions.
* put ATAPI data structures in more sensible headers, split up by
device type.
* include headers a bit more carefully.
* pass flags to attachment-specific cd functions, and use them.
* get rid of SCSI bits in scsipi_base.h's scsipi_make_xs(), move
them into the correct place in scsi_base.c.
* fix minor typo in struct name in scsipiconf.h (which was apparently
never used except in a #define later in the same file).
* use __attribute__ to force 4-byte alignment for xs command store,
so that architectures trying to bus_space_write_multi_N() (where
N > 1) that data to a controller won't lose.
* clean up a few comments in typos, and make a few #defines easier to
understand/maintain.
* rename cd_link.h to cdvar.h (via repository copy). This is exactly
what a 'var' file is supposed to be.
 1.4 30-Dec-1997  is EACCESS->EROFS, for the write on readonly medium error.
 1.3 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.2 27-Aug-1997  bouyer branches: 1.2.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.1 01-Jul-1997  bouyer branches: 1.1.2;
file atapi_base.c was initially added on branch bouyer-scsipi.
 1.1.2.2 01-Jul-1997  thorpej Purely cosmetic formatting changes: some spaces -> tabs, indenting
consistency in prototypes.
 1.1.2.1 01-Jul-1997  bouyer New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
scsibus at aha
sd at scsibus
cd at scsibus
atapibus at wdc
cd at atapibus
cd has bus-specific config and function front-end in scsicd.c and atapicd.c
The call to theses functions from cd.c is conditionned to
NSCSICD and NATAPICD (all defined in cd.h by config).
 1.2.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.1 27-Aug-1997  thorpej file atapi_base.c was added on branch marc-pcmcia on 1997-08-27 23:32:53 +0000
 1.9.2.2 02-Oct-1998  bouyer When printing the error message, also print the channel with the contrller
name and drive number.
 1.9.2.1 15-Aug-1998  bouyer file atapi_base.c was added on branch bouyer-ide on 1998-10-02 18:12:49 +0000
 1.11.8.1 30-Nov-1999  itojun bring in latest KAME (as of 19991130, KAME/NetBSD141) into kame branch
just for reference purposes.
This commit includes 1.4 -> 1.4.1 sync for kame branch.

The branch does not compile at all (due to the lack of ALTQ and some other
source code). Please do not try to modify the branch, this is just for
referenre purposes.

synchronization to latest KAME will take place on HEAD branch soon.
 1.11.6.1 01-Jul-1999  thorpej Sync w/ -current.
 1.11.4.1 25-Jun-1999  perry pullup 1.11->1.12 (thorpej): Go to splbio() when calling scsipi_free_xs()
 1.14.14.3 08-Jan-2002  nathanw Catch up to -current.
 1.14.14.2 14-Nov-2001  nathanw Catch up to -current.
 1.14.14.1 21-Jun-2001  nathanw Catch up to -current.
 1.14.2.2 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.14.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.16.2.1 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.18.16.7 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.18.16.6 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.18.16.5 21-Sep-2004  skrll Fix the sync with head I botched.
 1.18.16.4 18-Sep-2004  skrll Sync with HEAD.
 1.18.16.3 03-Sep-2004  skrll Sync with HEAD
 1.18.16.2 25-Aug-2004  skrll Sync with HEAD.
 1.18.16.1 03-Aug-2004  skrll Sync with HEAD
 1.19.2.1 11-Sep-2004  he Pull up revisions 1.21-1.22 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.23.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.23.4.1 29-Apr-2005  kent sync with -current
 1.26.70.1 02-Jun-2008  mjf Sync with HEAD.
 1.27.4.1 16-May-2008  yamt sync with head.
 1.27.2.1 18-May-2008  yamt sync with head.
 1.28.44.1 03-Dec-2017  jdolecek update from HEAD
 1.29.20.1 10-Jun-2019  christos Sync with HEAD
 1.11 07-Sep-2003  mycroft Set PQUIRK_ONLYBIG in the wdc-atapi frontend, obviating the need to ever test
the "bus type" for this.

Merge all the code in the SCSI and ATAPI backends for "cd" devices. All of
the mode page handling and whatnot is general to SCSI MMC devices, and should
never have been separated to begin with. This fixes a variety of problems,
and adds load/unload support for SCSI-attached devices.
 1.10 14-May-2001  bouyer branches: 1.10.22;
Use SCSI/ATAPI common definition for MODE_{SELECT,SENSE}{,_BIG}. Define
functions to send theses commands in scsipi_base.c and use them instead
of ad-hoc commands setups.
 1.9 13-Jul-1998  thorpej branches: 1.9.26;
Those tabs sure are excitable!!
 1.8 13-Jul-1998  hpeyerl Add support for ATA CD changer devices like the NEC CDR-251.
Evidence of managerial coding removed by Victor T. Cleaner (thorpej)
 1.7 13-Feb-1998  enami Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.6 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.5 15-Jan-1998  cgd add support for an ATAPI attachment for 'sd'.
fix 'cd' driver's NCD_SCSI bogosity (was using testing wrong macro!)
clean up in various ways:
* make common atapi_mode_{sense,select}() functions.
* put ATAPI data structures in more sensible headers, split up by
device type.
* include headers a bit more carefully.
* pass flags to attachment-specific cd functions, and use them.
* get rid of SCSI bits in scsipi_base.h's scsipi_make_xs(), move
them into the correct place in scsi_base.c.
* fix minor typo in struct name in scsipiconf.h (which was apparently
never used except in a #define later in the same file).
* use __attribute__ to force 4-byte alignment for xs command store,
so that architectures trying to bus_space_write_multi_N() (where
N > 1) that data to a controller won't lose.
* clean up a few comments in typos, and make a few #defines easier to
understand/maintain.
* rename cd_link.h to cdvar.h (via repository copy). This is exactly
what a 'var' file is supposed to be.
 1.4 03-Oct-1997  enami Don't define AUDIO_PLAY twice (accidently add in last commit).
 1.3 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.2 27-Aug-1997  bouyer branches: 1.2.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.1 01-Jul-1997  bouyer branches: 1.1.2;
file atapi_cd.h was initially added on branch bouyer-scsipi.
 1.1.2.2 01-Jul-1997  thorpej Purely cosmetic formatting changes: some spaces -> tabs, indenting
consistency in prototypes.
 1.1.2.1 01-Jul-1997  bouyer New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
scsibus at aha
sd at scsibus
cd at scsibus
atapibus at wdc
cd at atapibus
cd has bus-specific config and function front-end in scsicd.c and atapicd.c
The call to theses functions from cd.c is conditionned to
NSCSICD and NATAPICD (all defined in cd.h by config).
 1.2.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.1 27-Aug-1997  thorpej file atapi_cd.h was added on branch marc-pcmcia on 1997-08-27 23:32:54 +0000
 1.9.26.1 21-Jun-2001  nathanw Catch up to -current.
 1.10.22.1 03-Aug-2004  skrll Sync with HEAD
 1.6 12-Oct-2003  bouyer Remove this file; ATAPI_READ_FORMAT_CAPACITIES is the same as
READ_FORMAT_CAPACITIES; atapi_flex_geometry_page should probably be the same as
scsi_disk's page_flex_geometry.
 1.5 17-Sep-2003  mycroft Remove PQUIRK_BYTE5_ZERO.
 1.4 14-May-2001  bouyer branches: 1.4.22;
Use SCSI/ATAPI common definition for MODE_{SELECT,SENSE}{,_BIG}. Define
functions to send theses commands in scsipi_base.c and use them instead
of ad-hoc commands setups.
 1.3 13-Feb-1998  enami branches: 1.3.26;
Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.2 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.1 15-Jan-1998  cgd add support for an ATAPI attachment for 'sd'.
fix 'cd' driver's NCD_SCSI bogosity (was using testing wrong macro!)
clean up in various ways:
* make common atapi_mode_{sense,select}() functions.
* put ATAPI data structures in more sensible headers, split up by
device type.
* include headers a bit more carefully.
* pass flags to attachment-specific cd functions, and use them.
* get rid of SCSI bits in scsipi_base.h's scsipi_make_xs(), move
them into the correct place in scsi_base.c.
* fix minor typo in struct name in scsipiconf.h (which was apparently
never used except in a #define later in the same file).
* use __attribute__ to force 4-byte alignment for xs command store,
so that architectures trying to bus_space_write_multi_N() (where
N > 1) that data to a controller won't lose.
* clean up a few comments in typos, and make a few #defines easier to
understand/maintain.
* rename cd_link.h to cdvar.h (via repository copy). This is exactly
what a 'var' file is supposed to be.
 1.3.26.1 21-Jun-2001  nathanw Catch up to -current.
 1.4.22.1 03-Aug-2004  skrll Sync with HEAD
 1.6 11-Dec-2005  christos merge ktrace-lwp.
 1.5 21-Feb-2005  thorpej Part 1 of a cleanup pass over the SCSI subsystem. The aim is to name
everything "scsi_*", since we really are talking about the SCSI command
set, ATAPI transport not withstanding. Improve the names of many structures,
and prepend "SCSI_" onto all SCSI command opcodes. Place items described
by the SCSI Primary Commands document into scsi_spc.h.
 1.4 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.3 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.2 01-Dec-2001  bouyer branches: 1.2.16; 1.2.24; 1.2.26;
Various quirks for the ATAPI OnStream DI-30, mostly from the FreeBSD driver.
Many thanks to Chris Pinnock for giving me remote access to his hardware.
 1.1 18-Jun-2001  bouyer branches: 1.1.2; 1.1.4;
Snapshot of ATAPI tapes support. Known to be able to read/write to tape with:
st0 at atapibus0 drive 1: <Seagate STT8000A, , 5.51> type 1 sequential
removable
Major changes may still happen in order to properly support other ATAPI
tape drives.
 1.1.4.1 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.1.2.3 08-Jan-2002  nathanw Catch up to -current.
 1.1.2.2 21-Jun-2001  nathanw Catch up to -current.
 1.1.2.1 18-Jun-2001  nathanw file atapi_tape.h was added on branch nathanw_sa on 2001-06-21 20:05:40 +0000
 1.2.26.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.2.24.1 29-Apr-2005  kent sync with -current
 1.2.16.2 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.2.16.1 04-Feb-2005  skrll Sync with HEAD.
 1.141 05-Oct-2021  rin PR kern/56403

Fix kernel freeze for wdc(4) variants with ATAC_CAP_NOIRQ:

(1) Change ata_xfer_ops:c_poll from void to int function. When it returns
ATAPOLL_AGAIN, let ata_xfer_start() iterate itself again.

(2) Let wdc_ata_bio_poll() return ATAPOLL_AGAIN until ATA_ITSDONE is
achieved.

A similar change has been made for mvsata(4) (see mvsata_bio_poll()),
and no functional changes for other devices.

This is how the drivers worked before jdolecek-ncq branch was merged.

Note that this changes are less likely to cause infinite recursion:

(1) wdc_ata_bio_intr() called from wdc_ata_bio_poll() asserts ATA_ITSDONE
in its error handling paths via wdc_ata_bio_done().

(2) Return value from c_start (= wdc_ata_bio_start()) is checked in
ata_xfer_start().

Therefore, errors encountered in ata_xfer_ops:c_poll and c_start routines
terminate the recursion for wdc(4). The situation is similar for mvsata(4).

Still, there is a possibility where ata_xfer_start() takes long time to
finish a normal operation. This can result in a delayed response for lower
priority interrupts. But, I've never observed such a situation, even when
heavy thrashing takes place for swap partition in wd(4).

"Go ahead" by jdolecek@.
 1.140 07-Aug-2021  thorpej Merge thorpej-cfargs2.
 1.139 24-Apr-2021  thorpej branches: 1.139.8;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).
 1.138 13-Apr-2020  jdolecek branches: 1.138.4;
fix use-after-free for ata xfer on bio submission found by KASAN

driver ata_bio hooks read parts of the xfer after ata_exec_xfer()
call in order to determine return value, change so that the hook
doesn't return any value - callers do not care already,
as all I/O requests are asynchronous

this problem was uncovered by recent change for wd(4) to not hold
wd mutex during ata_bio call, the interrupt for the xfer might
thus actually fire immediately

adjust also ata_exec_command driver hooks similarily - remove all
completion and waiting logic from drivers, upper layer ata code
using AT_WAIT/AT_POLL changed to call ata_wait_cmd() itself

PR kern/55169 by Nick Hudson
 1.137 04-Apr-2020  jdolecek branches: 1.137.2;
fix deadlock in wdcwait() when xfer timeout happens while the atabus
thread sleeps in wdcwait() - check current lwp rather than relying
on global ATACH_TH_RUN channel flag

should fix the hang part of the problem reported in
http://mail-index.netbsd.org/netbsd-users/2020/03/12/msg024249.html

thanks to Paul Ripke for providing extensive debugging info
 1.136 19-Feb-2020  riastradh C99 initializers for scsipi_bustype. No functional change intended.
 1.135 12-Feb-2020  chs the number of possible ATAPI devices on an ATA bus is not always 2,
it is however many devices the underlying ATA bus can have (eg. 1 for SATA),
so initialize the scsipi chan_ntargets from the ATA ch_ndrives.
this fixes a memory read overrun detected by KASAN.
discussed with mlelstv@ and jdolecek@
 1.134 10-Nov-2019  chs branches: 1.134.2;
in many device attach paths, allocate memory with M_WAITOK instead of M_NOWAIT
and remove code to handle failures that can no longer happen.
 1.133 12-Nov-2018  jdolecek branches: 1.133.4;
pass correct status + error to *_atapi_phase_complete(), so that
the function is actually able to recognize when there was an error;
tested via reading a cd0 device in QEMU with ejected cdrom

bug was introduced with jdolecek-ncq branch

fixes PR kern/53724 by Andreas Gustafsson
 1.132 12-Nov-2018  jdolecek hold channel lock during whole ata_dmaerr()/ata_downgrade_mode() -
according to code inspection this is safe, none of the set_modes
hooks execute anything which would be taking the lock

adresses PR kern/53714 by Andreas Gustafsson
 1.131 22-Oct-2018  jdolecek Merge jdolecek-ncqfixes branch

- ata_xfer's are dynamicall allocated as needed using a pool, no longer
limited to number of possible openings supported by controller; dump
and recovery paths use dedicated pre-allocated storage
- moved callouts and condvars from ata_xfer to queue or channel, so that
ata_xfer does not need special initialization
- slot allocation now done when xfer is being activated, uncoupled
from memory allocation; active slots are no longer tracked by controller
code
- channel and drive reset is done always via the atabus thread, and
now executes with channel locked the whole time
- NCQ recovery moved to shared function, and run via the thread also
- added some workarounds for buggy error recovery AHCI emulation in QEMU
and Parallels

designed to primarily fix kern/52614, but might also help with kern/47041
and kern/53183
 1.130 01-Sep-2018  mlelstv Make wdcdebug_atapi_mask configurable like the other debug variables.
 1.129 17-Oct-2017  jdolecek branches: 1.129.2; 1.129.4; 1.129.6;
reintroduce ATACH_IRQ_WAIT flag for attachments using wdcintr(), only
process the interrupt when the flag is set - this fixes spurious interrupt
during post-reset drive setup in wdc_ata_bio_start(), and wdc_atapi_start()

while those functions set WDCTL_IDS, this seems to be ignored by certain
(maybe all) PCI-IDE controllers; usually the implicit KERNEL_LOCK() would
prevent the interrupt anyway, but not when the start routine is started
from the atabus thread, which doesn't take it

fixes 'panic: wdc_ata_bio_intr: bad state' reported on current-users
by Chavdar Ivanov
 1.128 10-Oct-2017  jdolecek revert the logic in wdc_atapi_intr() for wdc_wait_for_unbusy() to what it
was before NCQ merge; it got broken during the efford to remove ch_status
and ch_error on the branch

fixes atapi timeouts in vbox and with real harware reported separately
by Abhinav Upadhyay, Pault Goyette, Chavdar Ivanov, and Rares
Aioanei; with a bit of luck it could also fix PR kern/52605 and/or PR
kern/52606 by Martin Husemann
 1.127 08-Oct-2017  christos don't initialize tfd twice.
 1.126 08-Oct-2017  christos initialize tfd
 1.125 08-Oct-2017  joerg Initialize tfd even when not waiting for DSC.
 1.124 07-Oct-2017  jdolecek Merge support for SATA NCQ (Native Command Queueing) from jdolecek-ncq branch

ATA subsystem was changed to support several outstanding commands, and use
NCQ xfers if supported by both the controller and the disk, including NCQ
error recovery. Set NCQ high priority for BPRIO_TIMECRITICAL xfers
if supported. Added FUA support.

Done some work towards MP-safe, all ATA code tsleep()/wakeup() replaced
by condvars, and switched most code from spl* to mutexes (separate
wd(4) and ata channel lock).

Introduced new option WD_CHAOS_MONKEY to facilitate testing of error
handling, fixed several uncovered issues. Also fixed several problems
with kernel dump to wd(4) disk.

Tested with ahcisata(4), mvsata(4), siisata(4), piixide(4) on amd64,
with and without port multiplier, both disk and ATAPI devices; other
drivers and archs mechanically adjusted and compile-tested. NCQ is
supported for ahcisata(4) and siisata(4) for any controller, for
mvsata(4) only Gen IIe ones for now. Also enabled ATAPI support in
mvsata(4).

Thanks to Matt Thomas for initial ATA infrastructure patch, and
Jonathan A.Kollasch for siisata(4) NCQ changes and general testing.

Also fixes PR kern/43169 (wd(4)); and PR kern/11811, PR kern/47041,
PR kern/51979 (kernel dump)
 1.123 20-Nov-2016  mlelstv branches: 1.123.4;
Make scsipi framework MPSAFE.

Data structures are now protected by a per-adapter mutex at IPL_BIO
that is created by the scsibus or atapibus instance when the adapter
is configured.
The enable reference counter and the channel freeze counter which are
currently used by HBA code before the adapter is configured, are made
atomic.
The target drivers are now all tagged as D_MPSAFE.

Almost all HBA drivers still require the kernel lock to present,
so all callbacks into HBA code are still protected by kernel lock
unless the driver is tagged as SCSIPI_ADAPT_MPSAFE.

TODO: refactor sd and cd to use dksubr.
 1.122 02-May-2016  christos branches: 1.122.2;
move scsipi_strvis -> libkern:strnvisx()
change the prototype to match userland
fix sizes of strings passed to it
 1.121 20-Mar-2016  tsutsui Pass a correct residual size to wdcbit_bucket() that drains extra bytes.

Seems broken (but almost harmless?) since rev 1.101 committed ~10 years ago:
http://mail-index.netbsd.org/source-changes/2006/09/07/0043.html
 1.120 20-Mar-2016  tsutsui Remove extra spaces.
 1.119 31-Jul-2012  bouyer branches: 1.119.2; 1.119.16;
Move a check for the drive type to the right place. This bug would cause
ATA drives to not be detected when the PATA (or emulated PATA) bus is shared
with an ATAPI device.
Should fix the problem reported by various peoples.
Thanks to Onno van der Linden and vanoven@free.fr for pointing out the
code in error.
 1.118 31-Jul-2012  bouyer Apply back changes that were reverted on Jul 24 and Jul 26 (general ata/wdc
cleanup and SATA PMP support), now that I'm back to fix the fallouts.
 1.117 26-Jul-2012  jakllsch Revert, with intention of restoring in a less invasive way, the SATA Port
Multiplier code.

ok christos@
 1.116 24-Jul-2012  jakllsch Revert dsl@'s changes of Sun, 15 Jul 2012 10:55:35 +0000 and
Sun, 15 Jul 2012 10:56:50 +0000, excepting the kernel version bump.
First step in reverting regressions to ata(4) subsystem during the addition of
port multiplier support.
 1.115 15-Jul-2012  dsl Some namespace protection (and add greppablity).
Prefix the DRIVE_ and DRIVET_ constants from atavar.h with ATA_.
Don't use an enum for drive_type - you don't know how big it will be.
Move driver_type to avoid implicit structure padding (esp on arm).
This change is purely lexical and mechanical.

Update to 6.99.9 - this wasn't done when the SATA PMP changes
were made - I'm sure they warranted a bump.
 1.114 02-Jul-2012  bouyer Add sata Port MultiPlier (PMP) support to the ata bus layer,
as described in
http://mail-index.netbsd.org/tech-kern/2012/06/23/msg013442.html
PMP support in integrated to the atabus layer.
struct ata_channel's ch_drive[] is not dynamically allocated, and ch_ndrive
(renamed to ch_ndrives) closely reflects the size of the ch_drive[] array.
Add helper functions atabus_alloc_drives() and atabus_free_drives()
to manage ch_drive[]/ch_ndrives.
Add wdc_maxdrives to struct wdc_softc so that bus front-end can specify
how much drive they really support (master/slave or single).
ata_reset_drive() callback gains a uint32_t *sigp argument which,
when not NULL, will contain the signature of the device being reset.
While there, some cosmetic changes:
- added a drive_type enum to ata_drive_datas, and stop encoding the
probed drive type in drive_flags (we were out of drive flags anyway).
- rename DRIVE_ATAPIST to DRIVE_ATAPIDSCW to better reflect what this
really is
- remove ata_channel->ata_drives, it's redundant with the pointer in
ata_drive_datas
- factor out the interpretation of SATA signatures in sata_interpet_sig()

propagate these changes to the ATA HBA drivers, and add support for PMP
to ahcisata(4) and siisata(4).

Thanks to:
- Protocase (http://www.protocase.com/) which provided a system
with lots of controllers, SATA PMP and drive slots
- Conservation Genomics Laboratory, Department of Biology, New Mexico State
University for hosting the above system
- Brook Milligan, who set up remote access and has been very responsive
when SATA cable move was needed
 1.113 20-Apr-2012  bouyer Add a bustype_async_event_xfer_mode() callback to scsipi_bustype (which can
be NULL), so that transport-specific details of transfer mode setting/printing
can be handled more easily.
Move scsipi_async_event_xfer_mode() and scsipi_print_xfer_mode() to
scsi_base.c and split in parallel scsi and FC/SAS parts.
size of struct scsipi_bustype has changed, welcome to 6.99.5
 1.112 24-May-2011  joerg branches: 1.112.4; 1.112.8;
Fix obvious condition snafu
 1.111 28-Mar-2010  snj branches: 1.111.2;
Spell "enough" properly.
 1.110 19-Oct-2009  bouyer branches: 1.110.2; 1.110.4;
Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.109 16-Nov-2008  bouyer cpu_intr_p() doesn't account for software interrupts (e.g. callouts) so
we can't use it here. Rssurect ATACH_TH_RUN, backing out
src/sys/dev/ata/ata.c 1.101
src/sys/dev/ata/ata_wdc.c 1.90
src/sys/dev/ata/atavar.h 1.77
src/sys/dev/ic/wdc.c 1.255
src/sys/dev/scsipi/atapi_wdc.c 1.108
Should fix kern/39927 and kern/39725.
 1.108 02-Oct-2008  bouyer branches: 1.108.2; 1.108.4;
Kill ATACH_TH_RUN and use cpu_intr_p() instead.
 1.107 24-Mar-2008  cube branches: 1.107.4; 1.107.6; 1.107.10;
Split device_t and softc for atapibus(4).
 1.106 18-Mar-2008  cube Split device_t and softc for ATA devices, as well as wd(4). Other
cosmetic changes where appropriate.
 1.105 19-Oct-2007  ad branches: 1.105.12; 1.105.16;
machine/{bus,cpu,intr}.h -> sys/{bus,cpu,intr}.h
 1.104 23-Jan-2007  reinoud branches: 1.104.6; 1.104.18; 1.104.20; 1.104.24;
Fix for atapi devices that have 32 bit databusses selected. The devices
won't error out but will return garbage data. Implemented by generalising
the odd-check for PIO selection to check for 4 byte alignment in offset and
size.

Note that this check could be relaxed by checking if the ATAPI/ide bus
width is 16 for the device but its unknown to me if that information is
available here.
 1.103 01-Oct-2006  reinoud branches: 1.103.2;
In gcc4 structures are differently aligned compared to gcc3. Lots of
structures can thus be misaligned for DMA when declared on stack.

The current atapi code accepts the misaligned dma's only screw's them up.
This small patch disables DMA for the rare transactions that are
misaligned. Note that all bulk-transfers are aligned due to page sizes.

See PR kern/34689
 1.102 30-Sep-2006  itohy A little effort against kernel bloat....
Exclude ATA DMA support if no ATA DMA capable drivers are compiled in.
 1.101 07-Sep-2006  itohy branches: 1.101.2; 1.101.4;
Add PIOBM (busmastering transfer using ATA PIO mode) support.
The PIOBM is used by only one driver (will be added later,
stay tuned) and intruduce an attribute "ata_piobm" so that
it will be conditionally compiled in.
The "ata_dma" (busmastering transfer using ATA DMA mode) and
"ata_udma" (busmastering transfer using ATA Ultra DMA mode)
attributes are also added for consistency, but unused for now.
 1.100 27-Jun-2006  tron Make this build with GCC 4.x.
 1.99 21-May-2006  christos branches: 1.99.2;
void casts to functions whose return values are ignored.
 1.98 20-Feb-2006  thorpej branches: 1.98.2; 1.98.6; 1.98.8;
Use device_is_active() rather than testing dv_flags for DVF_ACTIVE
directly.
 1.97 11-Dec-2005  christos branches: 1.97.2; 1.97.4; 1.97.6;
merge ktrace-lwp.
 1.96 07-Jul-2005  drochner tighten the autoconf constraints by passing the atapi/ata/ata_hl attribute
to config_found()
(after some cleanup we might be able to kill SCSIPI_BUSTYPE_*)
 1.95 06-Jul-2005  thorpej branches: 1.95.2;
- When starting an ATA or ATAPI transfer, handle the case where (*dma_init)()
returns EINVAL, indicating that DMA cannot be done for this transfer.
Fall back to PIO in this case.
- Add a geodeide_dma_init() routine that checks to make sure that transfers
start on a 16 byte boundary, returning EINVAL if not. Works around a chip
bug that causes a hard system hang.

Problem reported and patch tested by Erik Fair.
 1.94 07-Jun-2005  peter Fix typos.
 1.93 29-May-2005  christos - Sprinkle const
- Avoid variable shadowing.
- Eliminate some caddr_t abuse.
 1.92 27-Feb-2005  perry branches: 1.92.2;
nuke trailing whitespace
 1.91 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.90 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.89 21-Aug-2004  thorpej branches: 1.89.4; 1.89.6;
atastart() (called only at splbio(), and from interrupts) can change
drive_flags, to make sure all drive_flags manipulations are done at
splbio().
 1.88 20-Aug-2004  thorpej Move most of wdc_softc into a new atac_softc structure that contains
info common to all types of ATA controllers.
 1.87 14-Aug-2004  thorpej - Split the register handles out of struct wdc_channel into a separate
wdc_regs structure, and array of which (indexed per channel) is pointed
to by struct wdc_softc.
- Move the resulting wdc_channel structure to atavar.h and rename it to
ata_channel. Rename the corresponding flags.
- Add a "ch_ndrive" member to struct ata_channel, which indicates the
maximum number of drives that can be present on the channel. For now,
this is always 2. Add an ATA_MAXDRIVES constant that places an upper
limit on this value, also currently 2.
 1.86 13-Aug-2004  thorpej WDCDEBUG -> ATADEBUG.
 1.85 13-Aug-2004  thorpej Don't bother with bits that tell of the presence of optional callbacks;
just check the function pointers for NULL.
 1.84 13-Aug-2004  thorpej Move wdcstart() to ata.c and rename it to atastart().
 1.83 13-Aug-2004  thorpej Move wdc_exec_xfer() to ata.c and rename it ata_exec_xfer().
 1.82 12-Aug-2004  thorpej Move wdc_addref() and wdc_delref() to ata.c and rename them to
ata_*.
 1.81 12-Aug-2004  thorpej - Move wdc_xfer_pool, wdc_get_xfer(), wdc_free_xfer() to ata.c, and rename
to ata_*.
- Use a static initializer for the ata_xfer_pool.
 1.80 12-Aug-2004  thorpej Move wdc_probe_caps() to ata.c and rename it ata_probe_caps().
 1.79 12-Aug-2004  thorpej Rename some constants:
WDC_COMPLETE -> ATACMD_COMPLETE
WDC_QUEUED -> ATACMD_QUEUED
WDC_TRY_AGAIN -> ATACMD_TRY_AGAIN
 1.78 12-Aug-2004  thorpej Rename "struct wdc_command" to "struct ata_command".
 1.77 11-Aug-2004  mycroft Make datain_pio and dataout_pio function pointers in wdc_softc, which can
be overridden by the backend if desired. Add experimental code to wdc_pcmcia
to use this in memory-mapped mode, disabled by default.
 1.76 11-Aug-2004  mycroft Change the behavior when a transfer mode setting is rejected. For PIO, fall
back to mode 3. For DMA, fall back to mode 0. These are the minimums for
ATAPI. For UDMA, fail.
 1.75 11-Aug-2004  mycroft Add two helper functions -- wdc_datain_pio() and wdc_dataout_pio() -- which
encapsulate the logic for the various methods of transferring data. Use these
throughout.
 1.74 04-Aug-2004  bouyer Change wdc_kill_pending() to take a struct ata_drive_datas * as argument,
and kill only pending requests for this drive.
Implement a DRIVE_WAITDRAIN flag, which will cause the active command to
be killed once complete.
Other minor fixes.
Now it's possible to detach a ATA or ATAPI device from ioctl even when
a dd on the raw char partition is running.
 1.73 04-Aug-2004  bouyer Add
struct ata_xfer *active_xfer
to ata_queue. Now the active xfer isn't the head of the queue any more,
this makes a few things easier (this will also help for tagged queuing
support).
Remove the WDCF_ACTIVE flag, test active_xfer != NULL instead.
clean up wdc_free_xfer() and kill_xfer().
Clean up wdc_reset_channel(), and make it issue a ATAPI_SOFT_RESET if the
active command is ATAPI.
In wdc_atapi_get_params(), use AT_WAIT | AT_POLL for ATAPI_SOFT_RESET,
so that we'll use tsleep() instead of delay().
In wdc_atapi_start(), call wdc_dmawait() at the right place.
 1.72 01-Aug-2004  bouyer Implement an atabus control device, and define some ATA bus control
IOCTLS. Implement ATABUSIORESET, which will reset the given ATA bus.
 1.71 31-Jul-2004  bouyer Implement asynchronous channel reset.
Use this to reset the channel before doing a dump, instead of the hack in
wdc_exec_xfer() based on C_POLL. This hack was causing problems on
controllers with a shared queue, because we now can have C_POLL set during
concurent channels probes (problem found and analysed on sparc64 by
Martin Husemann).
This should even make core dumps marginally more reliable on ATA drives.
 1.70 08-May-2004  bouyer Add a delay(10) after re-enabling interrupts in the control register.
Some controllers/drives (e.g. SataLink 3114 with WD Raptor) require
it. Should fix kern/23808 by Chris Gilbert, patch suplied by Chris Gilbert
on tech-kern, extended to all places enabling interrupts by me.
 1.69 03-Feb-2004  bouyer branches: 1.69.2;
Clear drive_flags when wdc_atapi_get_params() fails. Avoid a panic later
when probing a slave-only bus in some cases.
 1.68 03-Jan-2004  thorpej More structure member namespace cleanup: thread -> ch_thread
 1.67 03-Jan-2004  thorpej More wdc_channel structure member namespace cleanup:
- channel -> ch_channel
- wdc -> ch_wdc
 1.66 03-Jan-2004  thorpej Rename "struct channel_softc" to "struct wdc_channel".
 1.65 01-Jan-2004  thorpej Tidy this file up.
 1.64 01-Jan-2004  thorpej Prepend "wdc_" to wait_for_drq, wait_for_unbusy, and wait_for_ready.
 1.63 01-Jan-2004  thorpej Rename:
- wdc_xfer to ata_xfer
- channel_queue to ata_queue
and move them to <dev/ata/atavar.h> so they can be used by non-wdc ATA
controllers. Clean up the member names of these structures while at it.
 1.62 27-Nov-2003  fvdl There are some cards that map the ATA control and IDE DMA registers
in a different fashion. Individually, they have the same functionality,
but their layout is different. An example of such a chipset is
the Promise 203xx.

To be able to deal with this, transform the cmd and dma bus_space handles
into an array of handles, each seperately created with bus_space_subregion.
The code generated by using the extra indirection shouldn't change much,
since the extra indirection is negated by having the offset calculation
already done in bus_space_subregion. E.g.

bus_space_write_4(tag, handle, offset, value)

becomes

bus_space_write_4(tag, handles[offset], 0, value)

Reviewed by Manuel Bouyer. Tested on wdc_isa, wdc_pcmcia, viaide, piixide (i386)
and on cmdide (sparc64).
 1.61 29-Oct-2003  bouyer freese->freeze, as pointed out by Frederick Bruck.
 1.60 15-Oct-2003  bouyer Clear chp->ch_drive[i].drive_flags if we didn't find a driver for the
drive.
 1.59 08-Oct-2003  bouyer Make the ATA mid-layer appears as atabus, as proposed in
http://mail-index.netbsd.org/tech-kern/2003/09/25/0006.html
This adds a device (atabus) between IDE controllers and wd or atapibus, to
have each ATA channel show up in the device tree. Later there will be atabus
devices in /dev, so that we can do IOCTL on them.
Each atabus has its own kernel thread, to handle operations that needs polling,
e.g. reset and others.

Device probing on each bus it defered to the atabus thread creation.
This allows to do the reset and basic device probes in parallel, which reduce
boot time on systems with several pciide controllers.
 1.58 05-Oct-2003  bouyer Remove references to University of California from my copyright notices.
 1.57 19-Sep-2003  mycroft 1) Use config_interrupts() to attach IDE and ATAPI drives. This eliminates
most polling.
2) Clean up some goofiness in pciide -- get rid of the whole "candisable" path
(it's gratuitous) and simplify the code by calling pciide_map_compat_intr(),
*_set_modes() and wdc_print_modes() from central locations.
3) Add a register writability and register ghost test to eliminate phantom
drives more quickly.
 1.56 07-Sep-2003  mycroft Set PQUIRK_ONLYBIG in the wdc-atapi frontend, obviating the need to ever test
the "bus type" for this.

Merge all the code in the SCSI and ATAPI backends for "cd" devices. All of
the mode page handling and whatnot is general to SCSI MMC devices, and should
never have been separated to begin with. This fixes a variety of problems,
and adds load/unload support for SCSI-attached devices.
 1.55 27-Sep-2002  provos branches: 1.55.6;
remove trailing \n in panic(). approved perry.
 1.54 26-Jul-2002  wiz enouth -> enough.
 1.53 22-Jul-2002  bouyer Do PIO transfers for len < 4 bytes. From Matthias Drochner in private mail.
 1.52 18-May-2002  bouyer branches: 1.52.2;
If the data len if not a multiple of 2 bytes, force PIO mode. DMA can't
deal with such xfers, and can wedge the system with some controllers.
It's a bug to request such xfers for ATAPI, but as the request may come
from userland we have to protect against it.
 1.51 23-Apr-2002  bouyer branches: 1.51.2;
More copyright fixes, pointed out by Thomas. Thanks !
 1.50 05-Apr-2002  bouyer Implement mstohz() as discussed on tech-kern, and use it in SCSI drivers
to convert xs->timeout to callout() parameter.
 1.49 31-Mar-2002  martin Don't clear the adapt structure of the attaching channel - this nukes
all function pointers passed in from the adapter driver.

This partly fixes PR 13480, i.e. the FREECOM CD driver works now in pcmcia
adapters.

The remaining issue (timing problems with slow cards and cardbus bridges)
is probably the cause of several other PRs too.
 1.48 27-Jan-2002  bouyer IF we change the ATA modes, also reset the device, so that we can retry
the command with the new mode.
 1.47 03-Dec-2001  bouyer Update my copyrigth.
 1.46 02-Dec-2001  bouyer Clean up attach of wd/atapibus:
kill ata_atapi_attach. Change atapibus to use a struct scsipi_channel instead
of ata_atapi_attach as attach arch. Create a ata_device, compatible with
scsipi_channel, to attach wd.
 1.45 01-Dec-2001  bouyer For ATAPI tape drives, poll for DSC (using a callout), to get the real
status of the command, and make sure the drive is ready for the next one.
 1.44 13-Nov-2001  lukem add RCSIDs
 1.43 27-Jun-2001  bouyer branches: 1.43.2;
Some ATAPI CD occasionally reports "ATA illegal indication length" for
a request sense command, but the sense is properly transfered anyway.
Ignore the error for this case.
 1.42 13-Jun-2001  bjh21 Add explicit support for IDE and SCSI adaptors which don't support interrupts.
On such adaptors, all transfers are done in polling mode.

OK'ed by Manuel on tech-kern.
 1.41 15-May-2001  lukem delint: can't have an empty case item at the end of a switch; need a ; at least
 1.40 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.39 22-Jan-2001  mycroft branches: 1.39.2;
Force certain commands (i.e. DVD authentication) to PIO mode. They are
explicitly not supported in DMA mode, and generally return an `illegal field
in CDB' error.
This should have been done ages ago.
 1.38 03-Aug-2000  bouyer If the SET_FEATURE for piomode is rejected, fallback to mode 0 instead of
reporting an error.
This fixes the "piomode error (0x4)" problem with some ATAPI ZIP drives
reported on port-i386.
 1.37 28-Jun-2000  mrg remove include of <vm/vm.h>
 1.36 12-Jun-2000  bouyer branches: 1.36.2;
Add a callback (*irqack), for controllers that need special action to ack
the interrupt once it has been ack'd on the drive.
 1.35 15-May-2000  bouyer branches: 1.35.2;
Sync copyrigth notice
 1.34 02-Apr-2000  augustss Let the device thet gets an atapibus attached specify how to kill pending
transfers by giving a function pointer.
The old method always called wdc specific code.
 1.33 01-Apr-2000  bouyer - DMA code cleanup: pciide_dma_finish() doesn't stop/unload the current DMA op
if an IRQ was not detected, unless the force flag was given. Use this to
detect if the IRQ was for us (closer to shared IRQ for controllers which
don't have their own IRQ handler in pciide.c) and to poll for DMA xfer.
Also makes the timeout recovery code simpler.
- ATAPI cleanup: don't call controller-specific functions from atapiconf.c
(wdc_*), so that it's possible to attach an atapibus to something else
than a wdc/pciide (Hi Lennart :).
Overload struct scsi_adapter with struct atapi_adapter, defined
as struct scsi_adapter + atapi-specific callbacks. scsipi_link still points
to an scsi_adapter, atapi code casts it to atapi_adapter if needed.
Move atapi_softc to atapiconf.h so that it can be used by the underlying
controller code (e.g. atapi_wdc.c).
Add an atapi-specific callback *atapi_probedev(), which probe a drive
in a controller-specific way, allocate the sc_link and fills in the
ataparams if needed. It then calls atapi_probedev() (from atapiconf.c)
to do the generic initialisations and attach the device.
- While I'm there merge and centralise the state definitions in atavar.h.
It should now be possible to use a common ata/atapi routine to set the
drive's modes (will do later).
 1.32 23-Mar-2000  thorpej New callout mechanism with two major improvements over the old
timeout()/untimeout() API:
- Clients supply callout handle storage, thus eliminating problems of
resource allocation.
- Insertion and removal of callouts is constant time, important as
this facility is used quite a lot in the kernel.

The old timeout()/untimeout() API has been removed from the kernel.
 1.31 20-Mar-2000  enami Cancel requested command if controller isn't active.
 1.30 17-Jan-2000  bouyer Improve the downgrade logic:
- If UDMA 2 is failing try UDMA 1 first, it helps in some cases
- downgrade if we get an error in the first 4000 xfers, or if we get
4 errors in 4000 xfers if the first 4000 went without troubles.

While I'm there commit a local change I have since some time to get my CD
probed: issue a "blanck" IDENTIFY before the one used to detect slave ghosts,
with my drive the first IDENTIFY following a controller reset fails with an
aborted command ...
 1.29 04-Nov-1999  bouyer Pass xfer->c_bcount instead of sc_xfer->datalen to the device, so that
it gets the rigth len for a request sense (from OpenBSD).
 1.28 20-Oct-1999  enami Cancel active transfers on aic/wdc detach.
Also makes LS-120 drive works for me again.
 1.27 30-Sep-1999  thorpej branches: 1.27.2; 1.27.4; 1.27.6;
Cleanup the scsipi_xfer flags:
- `flags' is now gone, replaced with `xs_control' and `xs_status'.
- Massive cleanup of the control flags. Now we explicitly say that
a job is to complete asynchronously, rather than relying on side-effects,
and use a new flag to now that device discovery is being performed.
- Do SCSI device discovery interrupt-driven.
 1.26 23-Sep-1999  enami Allow to detach wdc, atapibus, wd and cd.
 1.25 09-Aug-1999  bouyer Add a missing 'return' in previous commit.
Don't use C_NEEDDONE anymore (it's safe to always call scsipi_done()).
 1.24 06-Aug-1999  bouyer - Add some debug printf (WDCPROBE) in _wdcreset_wait(), I've needed these
2 times in the past
- Set up timeout per xfer instead of per interrupt. This helps with
PIO transfer (we would call timeout()/untimout() several times for a
transfer).
- If we missed an interrupt for a PIO transfer, reset and restart it
immedialy, don't try to recover and continue. If we missed an interrupt we
may have lost a read/write cycle on the IDE bus. If this happens 1) we
corrupt data and 2) we enter an interrupt loop at the end of the xfer, as
the drive has some more data to read/write, but the host thinks the xfer is
done.
This last change fix the (or at last some of the) 'lookup after lost interrupt'
some peoples have been experiencing.
 1.23 05-May-1999  perry Add patch from Manuel Bouyer -- his mail message said:

By code inspection I found 2 bugs in the ATAPI code, one may be the cause
of your problem: A counter is not reset when issuing the request_sense
command. This is c_skip, the counter used to track the offset in the data
buffer when a data tranfer needs multiple phases. The effect of this is that
the sense data transfered may be stored outside the sense buffer (sense buffer
+ some, potentially several KB, offset). This can only occur in PIO mode,
DMA is not affected.
This doesn't occur in "normal" use because when reading a data CD, either
the sense is issued for a non-data command (in which case c_skip stays to
0), or an error occured and no data has been transfered, and c_skip is still 0.
I can't see a case where a data READ/WRITE could cause a sense tranfer without
error.

The second problem is that b_resid can be set to a false value (resulting of
the sense tranfer and not the data transfer). Again this is not a problem with
usual data tranfers because both values ends up being 0 when no error occurs.
 1.22 15-Apr-1999  bouyer If setting to pio mode < 3 fails, try to set pio mode 3 before returning
EIO. The spec says ATAPI devices should support "PIO 3 or better".
They are supposed to support less as well. Setting the device to a highter
mode than the controller shoul'nt be a problem, and this is likely what
happens with legaty ISA controllers.
Solve problem reported by Ruey-Shyang Guo.
 1.21 08-Apr-1999  bouyer Fix a long-standing bug with the ACAP_* flags:
- the cap field is a u_int8_t, so none of the defined flags would fit in.
Looks like nobody had a drive using 16 bytes commands.
- the ACAP_DRQ_* flags are all wrong. Just remove them and use the definitions
from ata/atareg.h, there's no need to duplicate theses. The effect of this
was that we were always polling for the command phase, even for drives
with interrupt DRQ. This didn't break until the code was changed to support
shared interrupts.

Should fix the lookup problems or 'boot hangs' reported by some users, and
kern/7111.
 1.20 01-Apr-1999  bouyer branches: 1.20.2;
- change the interrupt routines to take a 3rd arguments, set to 1 if we
are called from the interrupt or timeout handler, 0 otherwise.
- use this to know if we can busy-wait for wait_for_unbusy or wait_for_ready
This fixes a bug where CDs withot the DRQ_INTR capability would not busy-wait
for the CMDOUT phase.
While I'm there change 2 delay() to DELAY() for consistency, and
garbage-collect some old code from wdcintr() which has been ifdef'd out
for some time now.
 1.19 25-Mar-1999  bouyer Avoid busy-waiting when possible if we are not polling (real IRQ or timeout
callback). Shared PCI IRQ should now work (but still untested).
 1.18 21-Feb-1999  hubertf Only define WDCDEBUG if not already define (e.g. by kernel option)
 1.17 15-Feb-1999  bouyer If the REQUEST_SENSE command fails, issue a reset instead of XS_DRIVER_STUFFUP.
If REQUEST_SENSE succeeds but doens't return any data, return XS_SHORTSENSE
as the sense field will just contain a copy or the error register.
 1.16 02-Feb-1999  bouyer For IDE/ATAPI protocols botches, reset the bus and return XS_RESET
instead of XS_DRIVER_STUFFUP.
 1.15 29-Jan-1999  bouyer Cosmetic changes, from Soren S. Jorvan.
 1.14 09-Jan-1999  thorpej Make this compile again after the void * arith changes.
 1.13 08-Jan-1999  augustss Avoid arithmetic on `void *' since that's not ANSI C.
 1.12 17-Dec-1998  bouyer Add a new quirk flags, "ADEV_NOSENSE", for devices that don't
handle properly the request sense command. Add <FX320S, , q01> as being
sense-unfriendly in the quirk table.
 1.11 16-Dec-1998  bouyer Keep track of DMA errors, and downgrade the transert mode (UDMA ->DMA,
DMA->PIO) in case of 2 consecutive errors. Don't downgrade if the
PIO/DMA/UDMA modes were forced by a config flag.
 1.10 21-Nov-1998  drochner in wdc_softc: access the per-channel data via a pointer array instead of
an array of fixed-sized channel_softc elements. This way IDE controllers
which more than 1 channel (pciide) can extend the channel data easily
for private needs.
To avoid the double dereference at runtime, change the argument of
wdcstart() to the channel data pointer instead of the array index.
 1.9 19-Nov-1998  thorpej Adapt to the new scsipi_adapter interface.
 1.8 17-Nov-1998  bouyer In atapi_wdc.c, issue a 'REQUEST SENSE' command when appropriate.
Return XS_SENSE when the full sense info has been retrieved, or
XS_SHORTSENSE if only the sense key was available (from the error register)
Make atapi_interpret_sense() deal with this, and call scsipi_interpret_sense()
for XS_SENSE. (XXX sd_interpret_sense() and the ioctl code needs to be made
aware of XS_SHORTSENSE too ! sense hanlding for these is now less broken for
devices that support 'REQUEST SENSE')
All the ATAPI devices I have access to seems to honnor the SENSE_REQUEST
command, but I suspect some ATAPI devices will not (althouh it's mandatory).
The code should be able to deal with this, but is untested ...
 1.7 19-Oct-1998  bouyer Move the "warning: bcount value is %d after io" message in a WDCDEBUG_PRINT.
Ifdef DIAGNOSTIC, print the above message only if bcount is < 0.
 1.6 13-Oct-1998  bouyer Add a new drive flag, DRIVE_MODE, set when the drive properly reported its
PIO/DMA modes. Don't try a SET_FEATURE if it didn't report its mode.
 1.5 13-Oct-1998  bouyer In debug message, always print controller:channel:drive
 1.4 13-Oct-1998  bouyer Properly separate DEBUF_XFERS and DEBUG_FUNCS: DEBUF_XFERS is for data
transfers (may be called often), where DEBUG_FUNCS is for setup functions
(not used for normal contitions).
 1.3 13-Oct-1998  bouyer pciide.c: don't define WDCDEBUG, so it compiles on alpha. Correct a bogon
in the printing of DMA mode (piix3/4 only)
others: set the debug_mask to 0, so that debug messages are turned off by
default but can be easily turned on.
Reset drive_flags to 0 for unconfigured devices, so that they are ignored
later. For configured devices, reset state to 0 after probe/attach.
 1.2 12-Oct-1998  bouyer Merge bouyer-ide
 1.1 04-Jun-1998  bouyer branches: 1.1.2;
file atapi_wdc.c was initially added on branch bouyer-ide.
 1.1.2.13 12-Oct-1998  bouyer Sync with HEAD.
 1.1.2.12 05-Oct-1998  bouyer Rename WDC_CAPABILITY_PIO to WDC_CAPABILITY_MODE, as it's used to tell wether
the controller's driver can tell which timing mode it uses. Check this
before setting DMA modes too.
This allow the generic DMA code to work again.
 1.1.2.11 04-Oct-1998  bouyer New flag for wdc_xfer: C_DMA, set when a cmd will use DMA. This reduce some
if() in ata_wdc.c and atapi_wdc.c from 3 tests to one.
 1.1.2.10 02-Oct-1998  bouyer Cleanup a few error message, remove some dead code. Re-add a reset at end of
attach, unless we have WDC_NO_EXTRA_RESETS.
Add support for data32iot/data32ioh from -current: each controller pass
WDC_CAPABILITY_DATA32 if they can do 32bit, and WDC_CAPABILITY_DATA16 if they
can do 16bit. For controller that support both, the usual autodetect
mechanism is still used.
 1.1.2.9 02-Oct-1998  bouyer Use channel provided by your caller, not xfer->channel which is unitialized
at this point.
 1.1.2.8 20-Sep-1998  bouyer Init up xfer->drive in wdc_ata_bio(), or bad things will happen for drive 1.
No need to init xfer->channel, it's done in wdc_exec_xfer().
 1.1.2.7 20-Sep-1998  bouyer - always call config_found() from wdcattach(), instead of printing our own
the "not configured" message ourselve. When no atapibus is configured,
use a fake ata_atapi_attach, else call wdc_atapibus_attach(). This way we
don't have to include the whole atapi support in an ata-only config.
- the dma_* function pointers take a 'int flags' as last argument, instead
of an 'int read'. The 2 valid flags are: WDC_DMA_READ (to select read/write)
and WDC_DMA_POLL, to signal interrupt-less mode (for core dumps).
- Reworked wdcprobe() so look more like the old one. A status of 0xff is
interpreted as "no drive" (freebsd does this); this this speed up
the probe for non-IDE machines (a reset timeout has been bumped to 31s,
to match the specs). The probe set ups the drive flags to either ATA or
ATAPI (depend on register signature) or OLD (if register signature don't
match, but a working drive appears to be there). Later the ATA code
will look at the OLD flag to decide wether there is no drive, or an old
(ST506) one if WDC_IDENTIFY fails.
- For known PCI chips, don't use the wdcprobe() heuristic to find wether
a channel is enabled/disabled; use the chip's specific registers for this.
- Clear the command queue when we have a polled command. This allows dumps to
work even if a command was being handled by the controller at panic time.
- Increase the number of available DMA segments by one, as user requests may
not be aligned on a page boundary.
 1.1.2.6 13-Aug-1998  bouyer - sync with HEAD
- better error detection and report for wd drives
- better wddump() support
- wdcwait() now takes a timout argument, so that we can honnor the
timeout argument of scsipi cmds.
 1.1.2.5 23-Jun-1998  leo Add necessary cruft to handle swapped byte-lanes on the data-register. It
basically boils down to the fact that there are 2 new options:
WDC_CAPABILITY_{ATA,ATAPI}_NOSTREAM with which you are able to control
the usage bus_space_read_multi_X() vs. bus_space_read_multi_stream_X().
 1.1.2.4 19-Jun-1998  leo Use the stream functions where appropriate.
 1.1.2.3 19-Jun-1998  leo Fix hw-locking.
 1.1.2.2 11-Jun-1998  bouyer pciide: when setting up DMA mode, also enable fast timings for PIO modes
if possible. For ATAPI devices, commands are always send via PIO.
atapi: matain a drive state (like wd drives) and use it to set up the
timings if needed.
 1.1.2.1 04-Jun-1998  bouyer Commit changes to the IDE system in a branch. This allows a better separation
between higth-level and low-level (i.e. registers read/write) and generalize
the queue for all commands. This also add supports for IDE DMA.
 1.20.2.7 14-Aug-2000  he Pull up revision 1.38 (requested by bouyer):
If SET_FEATURE for piomode is rejected, fall back to mode 0
instead of giving up. Works around "piomode error (0x4)" problem
with some ATAPI ZIP drives.
 1.20.2.6 07-Jul-2000  he Apply patch (requested by bouyer):
Add support for the following PCIIDE controllers:
o AMD 756
o CMD PCI0648 and PCI0649
o Hightpoint HPT366
o OPTi 82c621 (and a few of its derivatives)
o Promise Ultra/33 and Ultra/66
o Intel 82801 (ICH/ICH0)
Also fix PR#10437 (detect more ATAPI devices).
 1.20.2.5 23-Jan-2000  he Pull up revision 1.30 (via patch, requested by bouyer):
Improve the downgrade logic to work better in some cases (especially
for Acer Labs M5229 controllers with Ultra-DMA/66 disks).
 1.20.2.4 28-Aug-1999  he Pull up revision 1.22:
Retry PIO mode 3 if PIO mode < 3 fails.
 1.20.2.3 25-Aug-1999  he Apply patch, pulling up 1.23->1.24 + "return" part of 1.24->1.25:
Add some debug printfs, change timeout handling to be more efficient
and less error-prone, and fix the "lockup after lost interrupt"
problem some users have been reporting. (bouyer)
 1.20.2.2 05-May-1999  perry branches: 1.20.2.2.2;
pullup 1.22->1.23 (fvdl)
 1.20.2.1 08-Apr-1999  bouyer Pull up atapi_wdc.c rev 1.21 and scsipiconf.h rev 1.30:
Fix botch with atapi capability flags, preventing 16 bytes commands to be ever
sent, and interrupt DRQ capable devices to effectively use interrupts for
the CMD_OUT phase. Didn't break until the code was changed to handle shared
interrupts (and I guess nobody has a device that requires 16 bytes commands
yet). Should fix the hangs reported by some users at first access of an ATAPI
device.
 1.20.2.2.2.1 21-Jun-1999  thorpej Sync w/ -current.
 1.27.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.27.4.1 15-Nov-1999  fvdl Sync with -current
 1.27.2.7 11-Feb-2001  bouyer Sync with HEAD.
 1.27.2.6 15-Jan-2001  bouyer Let the mid-layer issue the request sense command.
 1.27.2.5 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.27.2.4 20-Oct-1999  thorpej Fix merge botch.
 1.27.2.3 20-Oct-1999  thorpej Sync w/ trunk.
 1.27.2.2 20-Oct-1999  thorpej Eliminate sleeping in adapter routines. It is not legal as the queue may
be run from interrupt context.
 1.27.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.35.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.36.2.3 09-Feb-2002  he Pull up revision 1.48 (requested by bouyer):
If we change the ATA modes, also reset the device, so that we
can retry the command with the new mode. Fixes PR#15331.
 1.36.2.2 27-Dec-2001  he Pull up revision 1.39 (requested by mycroft):
Make DVD commands work on drives configured for DMA.
 1.36.2.1 04-Aug-2000  bouyer pull up 1.37->1.38 (approved by thorpej):
If the SET_FEATURE for piomode is rejected, fallback to mode 0 instead of
reporting an error. This fixes the "piomode error (0x4)" problem with some
ATAPI ZIP drives reported on port-i386.
 1.39.2.9 18-Oct-2002  nathanw Catch up to -current.
 1.39.2.8 01-Aug-2002  nathanw Catch up to -current.
 1.39.2.7 20-Jun-2002  nathanw Catch up to -current.
 1.39.2.6 17-Apr-2002  nathanw Catch up to -current.
 1.39.2.5 28-Feb-2002  nathanw Catch up to -current.
 1.39.2.4 08-Jan-2002  nathanw Catch up to -current.
 1.39.2.3 14-Nov-2001  nathanw Catch up to -current.
 1.39.2.2 24-Aug-2001  nathanw Catch up with -current.
 1.39.2.1 21-Jun-2001  nathanw Catch up to -current.
 1.43.2.5 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.43.2.4 06-Sep-2002  jdolecek sync kqueue branch with HEAD
 1.43.2.3 23-Jun-2002  jdolecek catch up with -current on kqueue branch
 1.43.2.2 11-Feb-2002  jdolecek Sync w/ -current.
 1.43.2.1 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.51.2.2 29-Aug-2002  gehenna catch up with -current.
 1.51.2.1 30-May-2002  gehenna Catch up with -current.
 1.52.2.1 01-Nov-2002  tron Pull up revision 1.53 (requested by bouyer in ticket #733):
Do PIO transfers for len < 4 bytes. From Matthias Drochner in private mail.
 1.55.6.7 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.55.6.6 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.55.6.5 21-Sep-2004  skrll Fix the sync with head I botched.
 1.55.6.4 18-Sep-2004  skrll Sync with HEAD.
 1.55.6.3 25-Aug-2004  skrll Sync with HEAD.
 1.55.6.2 12-Aug-2004  skrll Sync with HEAD.
 1.55.6.1 03-Aug-2004  skrll Sync with HEAD
 1.69.2.2 11-Aug-2004  jmc branches: 1.69.2.2.2;
Pullup rev 1.71 (requested by bouyer in ticket #733)

Implement asynchronous channel reset.
Use this to reset the channel before doing a dump, instead of the hack in
wdc_exec_xfer() based on C_POLL. This hack was causing problems on
controllers with a shared queue, because we now can have C_POLL set during
concurent channels probes
 1.69.2.1 29-May-2004  tron Pull up revision 1.70 (requested by bouyer in ticket #396):
Add a delay(10) after re-enabling interrupts in the control register.
Some controllers/drives (e.g. SataLink 3114 with WD Raptor) require
it. Should fix kern/23808 by Chris Gilbert, patch suplied by Chris Gilbert
on tech-kern, extended to all places enabling interrupts by me.
 1.69.2.2.2.1 18-Jul-2005  riz Pull up revision 1.95 (requested by thorpej in ticket #2072):
- When starting an ATA or ATAPI transfer, handle the case where
(*dma_init)()
returns EINVAL, indicating that DMA cannot be done for this
transfer.
Fall back to PIO in this case.
- Add a geodeide_dma_init() routine that checks to make sure that
transfers
start on a 16 byte boundary, returning EINVAL if not. Works
around a chip
bug that causes a hard system hang.
Problem reported and patch tested by Erik Fair.
 1.89.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.89.4.1 29-Apr-2005  kent sync with -current
 1.92.2.1 06-Jul-2005  tron Pull up revision 1.95 (requested by thorpej in ticket #553):
- When starting an ATA or ATAPI transfer, handle the case where (*dma_init)()
returns EINVAL, indicating that DMA cannot be done for this transfer.
Fall back to PIO in this case.
- Add a geodeide_dma_init() routine that checks to make sure that transfers
start on a 16 byte boundary, returning EINVAL if not. Works around a chip
bug that causes a hard system hang.
Problem reported and patch tested by Erik Fair.
 1.95.2.5 24-Mar-2008  yamt sync with head.
 1.95.2.4 27-Oct-2007  yamt sync with head.
 1.95.2.3 26-Feb-2007  yamt sync with head.
 1.95.2.2 30-Dec-2006  yamt sync with head.
 1.95.2.1 21-Jun-2006  yamt sync with head.
 1.97.6.2 01-Jun-2006  kardel Sync with head.
 1.97.6.1 22-Apr-2006  simonb Sync with head.
 1.97.4.1 09-Sep-2006  rpaulo sync with head
 1.97.2.1 01-Mar-2006  yamt sync with head.
 1.98.8.1 19-Jun-2006  chap Sync with head.
 1.98.6.1 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.98.2.3 14-Sep-2006  yamt sync with head.
 1.98.2.2 11-Aug-2006  yamt sync with head
 1.98.2.1 24-May-2006  yamt sync with head.
 1.99.2.1 13-Jul-2006  gdamore Merge from HEAD.
 1.101.4.1 22-Oct-2006  yamt sync with head
 1.101.2.2 01-Feb-2007  ad Sync with head.
 1.101.2.1 18-Nov-2006  ad Sync with head.
 1.103.2.1 29-Jan-2007  bouyer Pull up following revision(s) (requested by reinoud in ticket #388):
sys/dev/scsipi/atapi_wdc.c: revision 1.104
Fix for atapi devices that have 32 bit databusses selected. The devices
won't error out but will return garbage data. Implemented by generalising
the odd-check for PIO selection to check for 4 byte alignment in offset and
size.
Note that this check could be relaxed by checking if the ATAPI/ide bus
width is 16 for the device but its unknown to me if that information is
available here.
 1.104.24.1 25-Oct-2007  bouyer Sync with HEAD.
 1.104.20.2 23-Mar-2008  matt sync with HEAD
 1.104.20.1 06-Nov-2007  matt sync with HEAD
 1.104.18.1 26-Oct-2007  joerg Sync with HEAD.

Follow the merge of pmap.c on i386 and amd64 and move
pmap_init_tmp_pgtbl into arch/x86/x86/pmap.c. Modify the ACPI wakeup
code to restore CR4 before jumping back into kernel space as the large
page option might cover that.
 1.104.6.1 23-Oct-2007  ad Sync with head.
 1.105.16.3 17-Jan-2009  mjf Sync with HEAD.
 1.105.16.2 05-Oct-2008  mjf Sync with HEAD.
 1.105.16.1 03-Apr-2008  mjf Sync with HEAD.
 1.105.12.1 24-Mar-2008  keiichi sync with head.
 1.107.10.2 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.107.10.1 19-Oct-2008  haad Sync with HEAD.
 1.107.6.1 10-Oct-2008  skrll Sync with HEAD.
 1.107.4.2 11-Aug-2010  yamt sync with head.
 1.107.4.1 11-Mar-2010  yamt sync with head
 1.108.4.1 20-Nov-2008  snj Pull up following revision(s) (requested by bouyer in ticket #67):
sys/dev/ic/wdc.c: revision 1.256
sys/dev/ata/ata.c: revision 1.102
sys/dev/scsipi/atapi_wdc.c: revision 1.109
sys/dev/ata/atavar.h: revision 1.78
sys/dev/ata/ata_wdc.c: revision 1.91
cpu_intr_p() doesn't account for software interrupts (e.g. callouts) so
we can't use it here. Rssurect ATACH_TH_RUN, backing out
src/sys/dev/ata/ata.c 1.101
src/sys/dev/ata/ata_wdc.c 1.90
src/sys/dev/ata/atavar.h 1.77
src/sys/dev/ic/wdc.c 1.255
src/sys/dev/scsipi/atapi_wdc.c 1.108
Should fix kern/39927 and kern/39725.
 1.108.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.110.4.2 31-May-2011  rmind sync with head
 1.110.4.1 30-May-2010  rmind sync with head
 1.110.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.111.2.1 06-Jun-2011  jruoho Sync with HEAD.
 1.112.8.1 29-Apr-2012  mrg sync to latest -current.
 1.112.4.2 30-Oct-2012  yamt sync with head
 1.112.4.1 23-May-2012  yamt sync with head.
 1.119.16.3 05-Dec-2016  skrll Sync with HEAD
 1.119.16.2 29-May-2016  skrll Sync with HEAD
 1.119.16.1 22-Apr-2016  skrll Sync with HEAD
 1.119.2.1 03-Dec-2017  jdolecek update from HEAD
 1.122.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.123.4.14 30-Sep-2017  jdolecek must drop channel lock before calling ata_dmaerr() to avoid 'locking against
myself' in case of consecutive errors from ata_reset_channel() called via
ata_downgrade_mode()
 1.123.4.13 10-Sep-2017  jdolecek refactor code so that xfer c_start() hook is called with channel mutex held,
and hence the controller submit code no longer relies on spl

tested all the affected drivers - wdc (via piixide), ahci, mvsata, siisata,
both disk and atapi I/O
 1.123.4.12 12-Aug-2017  jdolecek convert the atabus thread to use the channel lock and a condvar, adjust
code which sets the relevant channel flags to take the lock while doing so
 1.123.4.11 12-Aug-2017  jdolecek remove all logic around ATACH_IRQ_WAIT and channel-global ch_error/ch_status,
so that there is less hidden state shared by commands; primary intent is
to make the NCQ and non-NCQ paths more similar, and remove possibility
of incorrect handling for the NCQ commands

tested both disk and ATAPI - piixide(4) on QEMU, and siisata(4),
ahcisata(4), mvsata(4) on real hw
 1.123.4.10 29-Jul-2017  jdolecek do not use freed xfer in error message in wdc_atapi_get_params()
 1.123.4.9 27-Jun-2017  jdolecek attend error paths, more strict asserts and code consistency

- atastart() and ata_kill_pending() now KASSERT() that all xfers on queue
have same channel
- inactive xfers are killed via new reason KILL_GONE_INACTIVE, controller
code must not call any resource deactivation in that case
- c_intr() must call ata_waitdrain_xfer_check() as first thing, and must not
further touch any xfer structures on exit path; any resource cleanup
is supposed to be done in c_kill_xfer()
- c_kill_xfer() should never call atastart()
- ata_waitdrain_check() removed, replaced by ata_waitdrain_xfer_check()
- ATA_DRIVE_WAITDRAIN handling converted to use condvar
- removed unused ata_c callback
 1.123.4.8 21-Jun-2017  jdolecek two last forgotten on-stack xfers replaced with using the queue xfer -
ata_get_params() and ata_set_mode()

fix wdc_atapi_get_params() to free the xfer used for soft reset before
calling ata_get_params() - it's now necessary, as ata_get_params() doesn't
invent a private xfer own any more
 1.123.4.7 20-Jun-2017  jdolecek remove ata queue downsizing - every device, attached to the same channel,
uses slots according to it's own limits

wdc code changed to expect maximum one active xfer, and not check number
of openings in the channel; this is to facilitate using wdc functions
for e.g. handling of atapi commands for drivers which support both ATAPI
and NCQ
 1.123.4.6 19-Jun-2017  jdolecek add ata_channel lock, use it to protect queue manipulation (only that for now);
add ata_channel_detach() to destroy the locks

change ata_get_xfer() so that it can wait for xfer, convert all on-stack
xfer code to use the blocking variant

fix siisata_reset_drive() to use polled reset and not try ata_activate_xfer(),
convert drive probe code also over from slot0 XXX to ata_get_xfer()

drive reset and PMP now works on siisata(4) too; changes tested also
on piixide(4), ahci(4), mvsata(4)
 1.123.4.5 16-Jun-2017  jdolecek adjust reset channel and dump paths
- channel reset now always kills active transfer, even on dump path, but
now doesn't touch the queued waiting transfers; also kill_xfer hook is
always called, so that HBA can free any private xfer resources and thus
the dump request has chance to work
- kill_xfer routines now always call ata_deactivate_xfer(); added KASSERT()s
to ata_free_xfer() to expect deactivated xfer
- when called during channel reset before dump, ata_kill_active() drops
any queued waiting transfers without processing
- do not (re)queue any transfers in wddone() when dumping
- kill AT_RST_NOCMD flag

This should also hopefully fix the 'polled command has been queued' panic
as reported in:
PR kern/11811 by John Hawkinson
PR kern/47041 by Taylor R Campbell
PR kern/51979 by Martin Husemann

dump tested working with piixide(4) and ahci(4). mvsata(4) dump times out,
but otherwise tested working, will be fixed separately. siisata(4) mechanically
changed and not tested.
 1.123.4.4 19-Apr-2017  jdolecek adjust ata code to support more than one active command, including the
timeout handling, add support for NCQ commands

move probe for NCQ and number of tags to middle layer, negotiate mutual
support between drive and controller

implement NCQ support in ahci(4)
 1.123.4.3 15-Apr-2017  jdolecek pass also ata_command via ata_xfer, callers of ata_exec_command() is now
responsible for allocation/disposal of the structure

change code to allocate ata_xfer for commands on stack same way as previously
the ata_command were, using c_slot 0; adjust asserts so that it would allow
several xfers with same c_slot, as long as only one such transfer is active
at a time
 1.123.4.2 15-Apr-2017  jdolecek make ata_xfer's allocated as part of ata_queue and make it include ata_bio;
they are pre-allocated on attach and ata_get_xfer() now never sleep, drop the
pool

modify wd(4) to file the bio requests using the xfers and hence
make it possible to have more than one active I/O request in flight;
ata_bio callback doesn't need to allocate any memory any more,
require it to never return ATACMD_TRY_AGAIN

move lp, badsect, multi from ata_bio to ata_drive_datas, as they are per-drive,
not per transfer

drop unused drv atac_claim_hw/atac_free_hw hooks, and also drop again ata_bio
c_hwslot
 1.123.4.1 10-Apr-2017  jdolecek ATA infrastructure improvements to eventually support more outstanding
commands

patch by Matt Thomas
 1.129.6.4 22-Sep-2018  jdolecek separate ata_xfer slot allocation and the memory allocation, so that
there can be more queued xfers than number of supported slots by controller,
and use a pool instead of custom pre-allocation

primarily to help PR kern/52614

remove no longer needed custom wd(4) logic for flush cache

switch also wd(4) trim/suspend/setcache/wdioctlstrategy to sleep waiting
for the memory, they are all called from process context and this
avoids spurious failures
 1.129.6.3 17-Sep-2018  jdolecek move ATAPI-only members of ata_xfer to an union struct to further save space
 1.129.6.2 17-Sep-2018  jdolecek move low-level protocol handlers hooks from ata_xfer to separate struct,
initialized statically

primarily to reduce ata_xfer struct size, but also improves readibility,
and enforces consistency
 1.129.6.1 31-Aug-2018  jdolecek refactor ata_xfer to be just dumb structure; move all callouts/condvars out

retry callout to wd(4); reset callout and the active/cmd finish condvars
to channel queue; change code using the condvars so it works if there
are multiple waiters

simplify the async wait code for cmds, replace ata_wait_xfer()/ata_wake_xfer()
with ata_wait_cmd()

fix the callout_invoking/ack race handling code for timeouts to
actually have chance to work; change mvsata(4) to use generic timeout func

towards resolution of kern/52614
 1.129.4.4 21-Apr-2020  martin Sync with HEAD
 1.129.4.3 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.129.4.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.129.4.1 10-Jun-2019  christos Sync with HEAD
 1.129.2.2 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.129.2.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.133.4.1 30-Dec-2022  martin Pull up following revision(s) (requested by tsutsui in ticket #1557):

sys/dev/ic/ahcisata_core.c: revision 1.83
sys/dev/ic/ahcisata_core.c: revision 1.102
sys/dev/ata/ata.c: revision 1.164
sys/dev/ata/ata_wdc.c: revision 1.115
sys/dev/ata/ata_recovery.c: revision 1.4
sys/dev/ic/siisata.c: revision 1.42
sys/dev/ic/wdc.c: revision 1.308
sys/dev/ic/mvsata.c: revision 1.56
sys/dev/scsipi/atapi_wdc.c: revision 1.138
sys/dev/ic/siisata.c: revision 1.49
sys/dev/ata/atavar.h: revision 1.105
sys/dev/ata/wd.c: revision 1.460
sys/dev/ata/ata.c: revision 1.155
sys/dev/ata/wd.c: revision 1.462
sys/dev/ata/atavar.h: revision 1.109
sys/dev/ata/satapmp_subr.c: revision 1.16
sys/dev/ic/wdc.c: revision 1.299
sys/dev/ic/ahcisata_core.c: revision 1.93
sys/dev/ata/ata_wdc.c: revision 1.120
sys/dev/ic/wdcvar.h: revision 1.100
sys/dev/scsipi/atapi_wdc.c: revision 1.141
sys/dev/ic/mvsata.c: revision 1.61
sys/dev/usb/umass_isdata.c (apply patch)

drop wd lock in wdstart1() before calling the ata_bio hook; when called
from ata thread context, that can still need to sleep for wdc attachments
in wdcwait()

fix use-after-free for ata xfer on bio submission found by KASAN
driver ata_bio hooks read parts of the xfer after ata_exec_xfer()
call in order to determine return value, change so that the hook
doesn't return any value - callers do not care already,
as all I/O requests are asynchronous

this problem was uncovered by recent change for wd(4) to not hold
wd mutex during ata_bio call, the interrupt for the xfer might
thus actually fire immediately

adjust also ata_exec_command driver hooks similarily - remove all
completion and waiting logic from drivers, upper layer ata code
using AT_WAIT/AT_POLL changed to call ata_wait_cmd() itself
PR kern/55169 by Nick Hudson

Function declaration formating whitespace consistency. NFCI.

PR kern/56403
Fix kernel freeze for wdc(4) variants with ATAC_CAP_NOIRQ:

(1) Change ata_xfer_ops:c_poll from void to int function. When it returns
ATAPOLL_AGAIN, let ata_xfer_start() iterate itself again.
(2) Let wdc_ata_bio_poll() return ATAPOLL_AGAIN until ATA_ITSDONE is
achieved.

A similar change has been made for mvsata(4) (see mvsata_bio_poll()),
and no functional changes for other devices.

This is how the drivers worked before jdolecek-ncq branch was merged.
Note that this changes are less likely to cause infinite recursion:

(1) wdc_ata_bio_intr() called from wdc_ata_bio_poll() asserts ATA_ITSDONE
in its error handling paths via wdc_ata_bio_done().
(2) Return value from c_start (= wdc_ata_bio_start()) is checked in
ata_xfer_start().

Therefore, errors encountered in ata_xfer_ops:c_poll and c_start routines
terminate the recursion for wdc(4). The situation is similar for mvsata(4).

Still, there is a possibility where ata_xfer_start() takes long time to
finish a normal operation. This can result in a delayed response for lower
priority interrupts. But, I've never observed such a situation, even when
heavy thrashing takes place for swap partition in wd(4).
"Go ahead" by jdolecek@.
 1.134.2.1 29-Feb-2020  ad Sync with head.
 1.137.2.1 20-Apr-2020  bouyer Sync with HEAD
 1.138.4.1 02-Apr-2021  thorpej config_found_ia() -> config_found() w/ CFARG_IATTR.
 1.139.8.1 04-Aug-2021  thorpej Adapt to CFARGS().
 1.1 01-Jul-1997  bouyer branches: 1.1.2;
file atapicd.c was initially added on branch bouyer-scsipi.
 1.1.2.2 01-Jul-1997  bouyer Moved atapicd and scsicd to cd_scsi and cd_atapi, to be concistent with
other bus front-end naming conventions (Suggested by Mike Long)
 1.1.2.1 01-Jul-1997  bouyer New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
scsibus at aha
sd at scsibus
cd at scsibus
atapibus at wdc
cd at atapibus
cd has bus-specific config and function front-end in scsicd.c and atapicd.c
The call to theses functions from cd.c is conditionned to
NSCSICD and NATAPICD (all defined in cd.h by config).
 1.95 09-Apr-2022  riastradh sys: Use membar_release/acquire around reference drop.

This just goes through my recent reference count membar audit and
changes membar_exit to membar_release and membar_enter to
membar_acquire -- this should make everything cheaper on most CPUs
without hurting correctness, because membar_acquire is generally
cheaper than membar_enter.
 1.94 12-Mar-2022  riastradh sys: Membar audit around reference count releases.

If two threads are using an object that is freed when the reference
count goes to zero, we need to ensure that all memory operations
related to the object happen before freeing the object.

Using an atomic_dec_uint_nv(&refcnt) == 0 ensures that only one
thread takes responsibility for freeing, but it's not enough to
ensure that the other thread's memory operations happen before the
freeing.

Consider:

Thread A Thread B
obj->foo = 42; obj->baz = 73;
mumble(&obj->bar); grumble(&obj->quux);
/* membar_exit(); */ /* membar_exit(); */
atomic_dec -- not last atomic_dec -- last
/* membar_enter(); */
KASSERT(invariant(obj->foo,
obj->bar));
free_stuff(obj);

The memory barriers ensure that

obj->foo = 42;
mumble(&obj->bar);

in thread A happens before

KASSERT(invariant(obj->foo, obj->bar));
free_stuff(obj);

in thread B. Without them, this ordering is not guaranteed.

So in general it is necessary to do

membar_exit();
if (atomic_dec_uint_nv(&obj->refcnt) != 0)
return;
membar_enter();

to release a reference, for the `last one out hit the lights' style
of reference counting. (This is in contrast to the style where one
thread blocks new references and then waits under a lock for existing
ones to drain with a condvar -- no membar needed thanks to mutex(9).)

I searched for atomic_dec to find all these. Obviously we ought to
have a better abstraction for this because there's so much copypasta.
This is a stop-gap measure to fix actual bugs until we have that. It
would be nice if an abstraction could gracefully handle the different
styles of reference counting in use -- some years ago I drafted an
API for this, but making it cover everything got a little out of hand
(particularly with struct vnode::v_usecount) and I ended up setting
it aside to work on psref/localcount instead for better scalability.

I got bored of adding #ifdef __HAVE_ATOMIC_AS_MEMBAR everywhere, so I
only put it on things that look performance-critical on 5sec review.
We should really adopt membar_enter_preatomic/membar_exit_postatomic
or something (except they are applicable only to atomic r/m/w, not to
atomic_load/store_*, making the naming annoying) and get rid of all
the ifdefs.
 1.93 07-Aug-2021  thorpej Merge thorpej-cfargs2.
 1.92 24-Apr-2021  thorpej branches: 1.92.8;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).
 1.91 17-Jun-2017  mlelstv branches: 1.91.22;
The atapibus detach path did hold the channel mutex while calling into autoconf,
which would trigger a panic when unplugging a USB ATAPI CDROM.

Align detach code for scsibus and atapibus to fix this.

Also avoid races when detaching devices by replacing callout_stop with
callout_halt.
 1.90 29-Nov-2016  mlelstv branches: 1.90.8;
reference count adapter mutex possibly shared by multiple channels.

fix error in atapibusdetach, when a child device cannot be detached,
keep atapibus instance alive.
 1.89 20-Nov-2016  mlelstv Make scsipi framework MPSAFE.

Data structures are now protected by a per-adapter mutex at IPL_BIO
that is created by the scsibus or atapibus instance when the adapter
is configured.
The enable reference counter and the channel freeze counter which are
currently used by HBA code before the adapter is configured, are made
atomic.
The target drivers are now all tagged as D_MPSAFE.

Almost all HBA drivers still require the kernel lock to present,
so all callbacks into HBA code are still protected by kernel lock
unless the driver is tagged as SCSIPI_ADAPT_MPSAFE.

TODO: refactor sd and cd to use dksubr.
 1.88 13-Mar-2016  tsutsui branches: 1.88.2;
Sync with scsiconf.c. (use aprint_normal(9) for "not configured")
 1.87 05-Mar-2014  skrll branches: 1.87.6;
Don't probe beyond chan_ntargets in atapi_probe_bus in the "all" case.

PR/48626: ahci_atapi_probe_device panic with kmemguard
 1.86 24-Jun-2012  riastradh branches: 1.86.2; 1.86.4;
Take the kernel lock in atapibusdetach just like atapibuschilddet.

Fixes kassert in scsipi_lookup_periph when I press the power button
on one of my laptops (and maybe another one) to power it off.

ok mrg
 1.85 19-Apr-2012  bouyer Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.

This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.

In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.

Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.84 06-Apr-2012  chs take the kernel lock during detach of atapibus as well.
 1.83 07-Jun-2010  pgoyette branches: 1.83.8; 1.83.12; 1.83.14;
Update scsiverbose module to use module_autoload() rather than module_load().
Load the module right before each attempt to use its features, and let the
module subsystem handle unloading.
 1.82 30-May-2010  pgoyette Extract SCSIVERBOSE into a kernel module. The module can be builtin
by defining 'options SCSIVERBOSE' in the kernel config file (no change
from current behavior), or it can be loaded at boot time on those
architectures that support the boot loader's "load" command.

The module is built for all architectures, whether or not SCSI or
atapi support exists.
 1.81 12-Nov-2009  dyoung Remove superfluous activation hooks.
 1.80 19-Oct-2009  bouyer Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.79 12-May-2009  cegger struct cfdata * -> cfdata_t, no functional changes intended.
 1.78 07-Apr-2009  dyoung Detach atapibus(4), scsibus(4), cd(4), and sd(4) during shutdown.
Destroy sd->sc_callout in sddetach(). Delete some dead code in
cddetach().
 1.77 24-Mar-2008  cube branches: 1.77.4; 1.77.12; 1.77.18;
Split device_t and softc for atapibus(4).
 1.76 08-Mar-2008  yamt fix a double-free bug introduced by the following change. PR/38179.

revision 1.75
date: 2008/01/29 17:26:57; author: dyoung; state: Exp; lines: +34 -15
Use device_t. Add a handler for child detachment. Now I can detach
cd0 at atapibus0 without getting a panic when atapibus0 detaches,
later.
 1.75 29-Jan-2008  dyoung branches: 1.75.2; 1.75.6;
Use device_t. Add a handler for child detachment. Now I can detach
cd0 at atapibus0 without getting a panic when atapibus0 detaches,
later.
 1.74 09-Dec-2007  jmcneill Merge jmcneill-pm branch.
 1.73 01-Dec-2007  jmcneill branches: 1.73.2; 1.73.4;
aprintify
 1.72 16-Nov-2006  christos branches: 1.72.22; 1.72.24; 1.72.30;
__unused removal on arguments; approved by core.
 1.71 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.70 30-Mar-2006  thorpej branches: 1.70.8; 1.70.10;
Use device_private().
 1.69 11-Dec-2005  christos branches: 1.69.4; 1.69.6; 1.69.8; 1.69.10; 1.69.12;
merge ktrace-lwp.
 1.68 26-Aug-2005  drochner s/locdesc_t/int/g
 1.67 29-May-2005  christos branches: 1.67.2;
- Sprinkle const
- Avoid variable shadowing.
- Eliminate some caddr_t abuse.
 1.66 13-Sep-2004  drochner a round of autoconf cleanup:
-convert submatch() style functions (passed to config_search() or
config_found_sm()) to the locator passing variants
-pass interface attributes in some cases
-make submatch() functions look uniformly as far as possible
-avoid macros which just hide cfdata members, and reduce dependencies
on "locators.h"
 1.65 21-Aug-2004  thorpej Const poison scsipi_dtype().
 1.64 21-Aug-2004  thorpej Use ANSI function decls and make use of static.
 1.63 17-Oct-2003  mycroft Remove the Fuji quirk from here, too.
 1.62 08-Oct-2003  bouyer Make the ATA mid-layer appears as atabus, as proposed in
http://mail-index.netbsd.org/tech-kern/2003/09/25/0006.html
This adds a device (atabus) between IDE controllers and wd or atapibus, to
have each ATA channel show up in the device tree. Later there will be atabus
devices in /dev, so that we can do IOCTL on them.
Each atabus has its own kernel thread, to handle operations that needs polling,
e.g. reset and others.

Device probing on each bus it defered to the atabus thread creation.
This allows to do the reset and basic device probes in parallel, which reduce
boot time on systems with several pciide controllers.
 1.61 18-Sep-2003  mycroft Merge the geometry and cache handling code for all direct access and optical
devices, as it's general to all SCSI MMC devices. In the process, remove
PQUIRK_NO_FLEX_PAGE.
 1.60 17-Sep-2003  mycroft Remove PQUIRK_BYTE5_ZERO.
 1.59 09-Sep-2003  mycroft Exorcise PQUIRK_NODOORLOCK.
 1.58 08-Sep-2003  mycroft Do a START UNIT only if the TEST UNIT READY reports that the device is not
ready. This avoids gratuitously starting the motor on floppy and CD-ROM
drives, and eliminates the need for the audio playing test in cdopen().

Therefore, also remove PQUIRK_NOSTARTUNIT.
 1.57 03-Apr-2003  erh branches: 1.57.2;
Add a quirk to allow my NEO Jukebox to work again.
 1.56 19-Feb-2003  hannken Add PQUIRK_NO_FLEX_PAGE for Fujitsu MO MCJ3230AP.
This drive returns bogus geometry (0 heads, 0 sectors).
 1.55 01-Jan-2003  thorpej Use aprint_normal() in cfprint routines.
 1.54 04-Oct-2002  soren As in scsiconf.c, don't print the numberic device type in the attach message.
 1.53 02-Oct-2002  thorpej Add trailing ; to CFATTACH_DECL.
 1.52 30-Sep-2002  thorpej Use CFATTACH_DECL().
 1.51 27-Sep-2002  thorpej Declare all cfattach structures const.
 1.50 27-Sep-2002  thorpej Introduce a new routine, config_match(), which invokes the
cfattach->ca_match function in behalf of the caller. Use it
rather than invoking cfattach->ca_match directly.
 1.49 19-Sep-2002  jmc Force the initial probes to happen within the newly forked off kthread.
This eliminates problems where the underlying interrupt handler isn't the
specific layer calling scsipi_complete() for a given scsi transaction.
This avoids deadlocks where the kthread that called the autoconf routines
to configure a scsibus shouldn't be the one put to sleep waiting on a
scsipi_complete (only the scsibus's kthread should be doing that).

To avoid jitter this will force the scsibus's to probe in the order they
run through autoconf (so machines with multiple bus's don't move sd* devices
around on every reboot).
 1.48 01-Apr-2002  bouyer Add a chan_name to struct scsipi_channel, holding the channel's name.
Set this to dv_xname for scsibus and atapibus.
Set the name of the kernel thread to chan_name instead of controller's
name:channel number (so that we can use this name for controller-specific
threads).
 1.47 22-Dec-2001  gehenna The FujiFilm USB storage drive doesn't have a flex geom page and
doesn't know REQUEST SENSE.
 1.46 03-Dec-2001  bouyer Update my copyrigth.
 1.45 02-Dec-2001  bouyer No need to include atavar.h here.
 1.44 02-Dec-2001  bouyer Clean up attach of wd/atapibus:
kill ata_atapi_attach. Change atapibus to use a struct scsipi_channel instead
of ata_atapi_attach as attach arch. Create a ata_device, compatible with
scsipi_channel, to attach wd.
 1.43 15-Nov-2001  lukem don't need <sys/types.h> when including <sys/param.h>
 1.42 13-Nov-2001  lukem add RCSIDs
 1.41 13-Sep-2001  enami Pass the correct pointer to atapibusprint().
 1.40 14-May-2001  bouyer branches: 1.40.2; 1.40.4;
Use SCSI/ATAPI common definition for MODE_{SELECT,SENSE}{,_BIG}. Define
functions to send theses commands in scsipi_base.c and use them instead
of ad-hoc commands setups.
 1.39 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.38 01-Apr-2001  augustss The ZiO! MMC adapter doesn't have a flex geometry page.
 1.37 20-Mar-2001  augustss Add quirks for Panasonic MultiMediaCard adapter (attaches via USB).
 1.36 18-Jan-2001  jdolecek branches: 1.36.2;
constify
 1.35 02-Apr-2000  augustss Let the device thet gets an atapibus attached specify how to kill pending
transfers by giving a function pointer.
The old method always called wdc specific code.
 1.34 01-Apr-2000  bouyer - DMA code cleanup: pciide_dma_finish() doesn't stop/unload the current DMA op
if an IRQ was not detected, unless the force flag was given. Use this to
detect if the IRQ was for us (closer to shared IRQ for controllers which
don't have their own IRQ handler in pciide.c) and to poll for DMA xfer.
Also makes the timeout recovery code simpler.
- ATAPI cleanup: don't call controller-specific functions from atapiconf.c
(wdc_*), so that it's possible to attach an atapibus to something else
than a wdc/pciide (Hi Lennart :).
Overload struct scsi_adapter with struct atapi_adapter, defined
as struct scsi_adapter + atapi-specific callbacks. scsipi_link still points
to an scsi_adapter, atapi code casts it to atapi_adapter if needed.
Move atapi_softc to atapiconf.h so that it can be used by the underlying
controller code (e.g. atapi_wdc.c).
Add an atapi-specific callback *atapi_probedev(), which probe a drive
in a controller-specific way, allocate the sc_link and fills in the
ataparams if needed. It then calls atapi_probedev() (from atapiconf.c)
to do the generic initialisations and attach the device.
- While I'm there merge and centralise the state definitions in atavar.h.
It should now be possible to use a common ata/atapi routine to set the
drive's modes (will do later).
 1.33 28-Mar-2000  augustss Change a printf() to a panic() since the kernel is going to die on the next
line anyway.
 1.32 17-Mar-2000  soren atapiprint() does not exist.
 1.31 28-Feb-2000  jdolecek Add ADEV_NOSENSE quirk for HITACHI CDR-7730.

Patch sent by Hume Smith in kern/9489.
 1.30 20-Jan-2000  mjacob Nobody said no to adding a pointer to original scsi inquiry data
to the scsibus attach args. Make sure it's nulled for ATAPI. Also,
for scsiconf.c, modify SENA's quirk entry.
 1.29 20-Oct-1999  enami Cancel active transfers on aic/wdc detach.
Also makes LS-120 drive works for me again.
 1.28 30-Sep-1999  thorpej branches: 1.28.2; 1.28.4; 1.28.6;
Cleanup the scsipi_xfer flags:
- `flags' is now gone, replaced with `xs_control' and `xs_status'.
- Massive cleanup of the control flags. Now we explicitly say that
a job is to complete asynchronously, rather than relying on side-effects,
and use a new flag to now that device discovery is being performed.
- Do SCSI device discovery interrupt-driven.
 1.27 23-Sep-1999  enami Allow to detach wdc, atapibus, wd and cd.
 1.26 08-Jul-1999  bouyer Add another CD to the quirk table.
 1.25 15-Feb-1999  bouyer branches: 1.25.2; 1.25.4; 1.25.6;
Revert to 1.23, this was not supposed to be checked in yet.
 1.24 15-Feb-1999  bouyer For CDIOCCLOSE, ignore media changes, as some drive seems to return this
info here.
 1.23 26-Jan-1999  bouyer Another CD drive that doesn't support REQUEST SENSE, from Patrick Welche.
 1.22 21-Jan-1999  bouyer Add a mitsumi CD that can't handle REQUEST SENSE command. From
Johan Danielsson <joda@pdc.kth.se>.
 1.21 19-Jan-1999  bouyer Move test for SDEV_NOSTARTUNIT quirk from sd.c to scsipi_start().
Add a SDEV_NOSTARTUNIT quirk entry for BCD-16X 1997-04-25", "", "VER 2.2"
CD-rom (from Michael Santos).
 1.20 19-Jan-1999  bouyer "Memorex CRW-2642" CD-R can't properly handle REQUEST SENSE command.
Add it to the quick table, per PR kern/6844.
 1.19 21-Dec-1998  leo Change quirk entry: "FX320S", "", " q01" to: "FX320S", "", "q01". This
space does seem to matter...
 1.18 17-Dec-1998  bouyer Add a new quirk flags, "ADEV_NOSENSE", for devices that don't
handle properly the request sense command. Add <FX320S, , q01> as being
sense-unfriendly in the quirk table.
 1.17 16-Dec-1998  bouyer Leave some room for the string-terminating char in temp. buffers.
Pointed out by Scott Presnell (thanks !).
 1.16 08-Dec-1998  thorpej When allocating a device's scsipi_link, initialize the pending_xfers
queue.
 1.15 19-Nov-1998  thorpej Add a reference to the adapter before probing the bus, and delete it
once we are done probing.
 1.14 13-Oct-1998  enami Make this file compile again with -DATAPI_DEBUG_PROBE.
 1.13 12-Oct-1998  bouyer Merge bouyer-ide
 1.12 31-Aug-1998  cgd kill the last remnants of __BROKEN_INDIRECT_CONFIG. (only the pica port
used it, and it's non-working and apparently slated for replacement.)
 1.11 05-Aug-1998  drochner Improve generation of default disklabels:
-store printable product ID in cd's and sd's softc, use it as "typename"
-for this, add a "destination buffer length" argument to scsipi_strvis()
-return ATAPI device type for ATAPI devices
 1.10 31-Jul-1998  thorpej Use the pool allocator for scsipi_xfer structures.
 1.9 15-Jan-1998  cgd branches: 1.9.2; 1.9.4;
add support for an ATAPI attachment for 'sd'.
fix 'cd' driver's NCD_SCSI bogosity (was using testing wrong macro!)
clean up in various ways:
* make common atapi_mode_{sense,select}() functions.
* put ATAPI data structures in more sensible headers, split up by
device type.
* include headers a bit more carefully.
* pass flags to attachment-specific cd functions, and use them.
* get rid of SCSI bits in scsipi_base.h's scsipi_make_xs(), move
them into the correct place in scsi_base.c.
* fix minor typo in struct name in scsipiconf.h (which was apparently
never used except in a #define later in the same file).
* use __attribute__ to force 4-byte alignment for xs command store,
so that architectures trying to bus_space_write_multi_N() (where
N > 1) that data to a controller won't lose.
* clean up a few comments in typos, and make a few #defines easier to
understand/maintain.
* rename cd_link.h to cdvar.h (via repository copy). This is exactly
what a 'var' file is supposed to be.
 1.8 12-Jan-1998  thorpej Adjust for changes to config.
 1.7 06-Jan-1998  bouyer Add a few quirk entries from OpenBSD.
 1.6 05-Nov-1997  bouyer Add a quirk for a sanyo CD (From OpenBSD).
 1.5 31-Oct-1997  bouyer Add NOCAPACITY quirk entry for MATSHITA CR-574, rev 1.02.
 1.4 01-Oct-1997  enami branches: 1.4.2;
Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.3 28-Aug-1997  bouyer Added a quirk entry for the SANYO CRD-254P (from OpenBSD), and
one for the MATSHITA CR-574 (from Josef Wulf <wulf@ping.net.au> via OpenBSD).
 1.2 27-Aug-1997  bouyer branches: 1.2.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.1 01-Jul-1997  bouyer branches: 1.1.2;
file atapiconf.c was initially added on branch bouyer-scsipi.
 1.1.2.3 17-Jul-1997  bouyer Use "locator.h".
 1.1.2.2 01-Jul-1997  thorpej Purely cosmetic formatting changes: spaces -> tabs in a few places, and
a general KNF sweep.
 1.1.2.1 01-Jul-1997  bouyer New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
scsibus at aha
sd at scsibus
cd at scsibus
atapibus at wdc
cd at atapibus
cd has bus-specific config and function front-end in scsicd.c and atapicd.c
The call to theses functions from cd.c is conditionned to
NSCSICD and NATAPICD (all defined in cd.h by config).
 1.2.2.4 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.3 01-Sep-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.1 27-Aug-1997  thorpej file atapiconf.c was added on branch marc-pcmcia on 1997-08-27 23:32:56 +0000
 1.4.2.3 05-Feb-1999  cgd pull up rev 1.7 from trunk (bouyer)
 1.4.2.2 06-Nov-1997  mellon Pull rev 1.6 up from trunk (bouyer)
 1.4.2.1 31-Oct-1997  mellon Pull rev 1.5 up from trunk (bouyer)
 1.9.4.1 08-Aug-1998  eeh Revert cdevsw mmap routines to return int.
 1.9.2.4 20-Sep-1998  bouyer Remove a stale 'XXX'.
 1.9.2.3 11-Sep-1998  bouyer Sync with HEAD.
 1.9.2.2 13-Aug-1998  bouyer - sync with HEAD
- better error detection and report for wd drives
- better wddump() support
- wdcwait() now takes a timout argument, so that we can honnor the
timeout argument of scsipi cmds.
 1.9.2.1 04-Jun-1998  bouyer Commit changes to the IDE system in a branch. This allows a better separation
between higth-level and low-level (i.e. registers read/write) and generalize
the queue for all commands. This also add supports for IDE DMA.
 1.25.6.1 30-Nov-1999  itojun bring in latest KAME (as of 19991130, KAME/NetBSD141) into kame branch
just for reference purposes.
This commit includes 1.4 -> 1.4.1 sync for kame branch.

The branch does not compile at all (due to the lack of ALTQ and some other
source code). Please do not try to modify the branch, this is just for
referenre purposes.

synchronization to latest KAME will take place on HEAD branch soon.
 1.25.4.1 02-Aug-1999  thorpej Update from trunk.
 1.25.2.2 14-Dec-2000  he Pull up revision 1.31 (requested by bouyer):
Add a quirk entry for Hitachi CDR-7730. Fixes PR#11654.
 1.25.2.1 08-Jul-1999  perry pullup 1.25->1.26 (bouyer); last minute but it looked okay
 1.28.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.28.4.1 15-Nov-1999  fvdl Sync with -current
 1.28.2.10 21-Apr-2001  bouyer Sync with HEAD
 1.28.2.9 27-Mar-2001  bouyer Sync with HEAD.
 1.28.2.8 18-Feb-2001  bouyer Move atapiprint() to ic/wdc.c so that we can compile a kernel with IDE
controller but without atapibus.
 1.28.2.7 11-Feb-2001  bouyer Sync with HEAD.
 1.28.2.6 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.28.2.5 04-Feb-2000  thorpej Make sure the channel's completion thread exits when the channel
is detached, and completely encapsulate the way periph's are stored
by the channel.
 1.28.2.4 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.28.2.3 20-Oct-1999  thorpej Sync w/ trunk.
 1.28.2.2 20-Oct-1999  enami Fix compilation error when ATAPI_DEBUG_PROBE is defined.
 1.28.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.36.2.8 03-Jan-2003  thorpej Sync with HEAD.
 1.36.2.7 18-Oct-2002  nathanw Catch up to -current.
 1.36.2.6 17-Apr-2002  nathanw Catch up to -current.
 1.36.2.5 08-Jan-2002  nathanw Catch up to -current.
 1.36.2.4 14-Nov-2001  nathanw Catch up to -current.
 1.36.2.3 21-Sep-2001  nathanw Catch up to -current.
 1.36.2.2 21-Jun-2001  nathanw Catch up to -current.
 1.36.2.1 09-Apr-2001  nathanw Catch up with -current.
 1.40.4.1 01-Oct-2001  fvdl Catch up with -current.
 1.40.2.3 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.40.2.2 23-Jun-2002  jdolecek catch up with -current on kqueue branch
 1.40.2.1 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.57.2.5 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.57.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.57.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.57.2.2 25-Aug-2004  skrll Sync with HEAD.
 1.57.2.1 03-Aug-2004  skrll Sync with HEAD
 1.67.2.6 17-Mar-2008  yamt sync with head.
 1.67.2.5 04-Feb-2008  yamt sync with head.
 1.67.2.4 21-Jan-2008  yamt sync with head
 1.67.2.3 07-Dec-2007  yamt sync with head
 1.67.2.2 30-Dec-2006  yamt sync with head.
 1.67.2.1 21-Jun-2006  yamt sync with head.
 1.69.12.1 31-Mar-2006  tron Merge 2006-03-31 NetBSD-current into the "peter-altq" branch.
 1.69.10.1 19-Apr-2006  elad sync with head.
 1.69.8.1 01-Apr-2006  yamt sync with head.
 1.69.6.1 22-Apr-2006  simonb Sync with head.
 1.69.4.1 09-Sep-2006  rpaulo sync with head
 1.70.10.2 10-Dec-2006  yamt sync with head.
 1.70.10.1 22-Oct-2006  yamt sync with head
 1.70.8.1 18-Nov-2006  ad Sync with head.
 1.72.30.3 18-Feb-2008  mjf Sync with HEAD.
 1.72.30.2 27-Dec-2007  mjf Sync with HEAD.
 1.72.30.1 08-Dec-2007  mjf Sync with HEAD.
 1.72.24.2 23-Mar-2008  matt sync with HEAD
 1.72.24.1 09-Jan-2008  matt sync with HEAD
 1.72.22.3 08-Dec-2007  jmcneill Rename pnp(9) -> pmf(9), as requested by many.
 1.72.22.2 01-Dec-2007  jmcneill Sync with HEAD.
 1.72.22.1 12-Nov-2007  joerg Add null handlers for atapibus, it doesn't seem to need any magic.
 1.73.4.1 11-Dec-2007  yamt sync with head.
 1.73.2.1 26-Dec-2007  ad Sync with head.
 1.75.6.1 03-Apr-2008  mjf Sync with HEAD.
 1.75.2.1 24-Mar-2008  keiichi sync with head.
 1.77.18.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.77.12.1 28-Apr-2009  skrll Sync with HEAD.
 1.77.4.3 11-Mar-2010  yamt sync with head
 1.77.4.2 16-May-2009  yamt sync with head
 1.77.4.1 04-May-2009  yamt sync with head.
 1.83.14.3 02-Jul-2012  jdc Pull up revision 1.86 (requested by riastradh in ticket #373).

Take the kernel lock in atapibusdetach just like atapibuschilddet.

Fixes kassert in scsipi_lookup_periph when I press the power button
on one of my laptops (and maybe another one) to power it off.

ok mrg
 1.83.14.2 23-Apr-2012  riz Pull up following revision(s) (requested by bouyer in ticket #192):
sys/dev/scsipi/cd.c: revision 1.307
sys/dev/scsipi/scsiconf.c: revision 1.266
sys/dev/scsipi/sd.c: revision 1.298
sys/dev/scsipi/st_scsi.c: revision 1.35
sys/dev/scsipi/atapiconf.c: revision 1.85
sys/dev/scsipi/scsipiconf.h: revision 1.120
sys/dev/usb/umass_scsipi.c: revision 1.44
sys/dev/scsipi/scsiconf.h: revision 1.57
sys/dev/scsipi/st_atapi.c: revision 1.29
sys/dev/scsipi/scsipi_base.c: revision 1.158
sys/dev/scsipi/st.c: revision 1.221
sys/dev/scsipi/scsipi_ioctl.c: revision 1.67
Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.
This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.
In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.
Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.83.14.1 09-Apr-2012  riz Pull up following revision(s) (requested by chs in ticket #166):
sys/dev/scsipi/atapiconf.c: revision 1.84
take the kernel lock during detach of atapibus as well.
 1.83.12.1 29-Apr-2012  mrg sync to latest -current.
 1.83.8.4 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.83.8.3 30-Oct-2012  yamt sync with head
 1.83.8.2 23-May-2012  yamt sync with head.
 1.83.8.1 17-Apr-2012  yamt sync with head
 1.86.4.1 18-May-2014  rmind sync with head
 1.86.2.2 03-Dec-2017  jdolecek update from HEAD
 1.86.2.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.87.6.3 28-Aug-2017  skrll Sync with HEAD
 1.87.6.2 05-Dec-2016  skrll Sync with HEAD
 1.87.6.1 19-Mar-2016  skrll Sync with HEAD
 1.88.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.90.8.1 21-Jun-2017  snj Pull up following revision(s) (requested by mlelstv in ticket #53):
sys/dev/scsipi/atapiconf.c: revision 1.91
sys/dev/scsipi/cd.c: revision 1.341
sys/dev/scsipi/scsi_base.c: revision 1.92
sys/dev/scsipi/scsiconf.c: revision 1.280
sys/dev/scsipi/scsipi_base.c: revisions 1.176, 1.177
sys/dev/scsipi/sd.c: revision 1.325
sys/dev/scsipi/ss.c: revision 1.89
sys/dev/scsipi/st.c: revision 1.231
The atapibus detach path did hold the channel mutex while calling into autoconf,
which would trigger a panic when unplugging a USB ATAPI CDROM.
Align detach code for scsibus and atapibus to fix this.
Also avoid races when detaching devices by replacing callout_stop with
callout_halt.
--
pass config_detach error to caller.
 1.91.22.3 03-Apr-2021  thorpej Give config_attach() the tagged variadic argument treatment and
mechanically convert all call sites.
 1.91.22.2 21-Mar-2021  thorpej CFARG_IATTR usage audit:

If a device carries only one interface attribute, there is no need
to specify it when calling config_search(); that specification is
meant only to disambiguate which interface attribute (which is a
proxy for "what kind of attach args are being used") is having
children attached. cfparent_match() will take care of ensuring that
any potential children can attach to one of the parent's iterface
attributes, and if the parent only carries one, no disambiguation is
necessary.
 1.91.22.1 20-Mar-2021  thorpej The proliferation if config_search_*() and config_found_*() combinations
is a little absurd, so begin to tidy this up:

- Introduce a new cfarg_t enumerated type, that defines the types of
tag-value variadic arguments that can be passed to the various
config_*() functions (CFARG_SUBMATCH, CFARG_IATTR, and CFARG_LOCATORS,
for now, plus a CFARG_EOL sentinel).
- Collapse config_search_*() into config_search() that takes these
variadic arguments.
- Convert all call sites of config_search_*() to the new signature.
Noticed several incorrect usages along the way, which will be
audited in a future commit.
 1.92.8.1 04-Aug-2021  thorpej Adapt to CFARGS().
 1.23 19-Oct-2009  bouyer Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.22 24-Mar-2008  cube branches: 1.22.4;
Split device_t and softc for atapibus(4).
 1.21 23-Nov-2006  he branches: 1.21.48;
This file needs to know the size of "struct device", so include
<sys/device.h> here. This should allow the sparc GENERIC_SUN4U and
sparc64 kernels to build sata_subr.c again.
 1.20 11-Dec-2005  christos branches: 1.20.20; 1.20.22;
merge ktrace-lwp.
 1.19 17-Sep-2004  mycroft branches: 1.19.12;
Change the way bustype_cmd is used. Rather than having it be responsible for
calling scsipi_make_xs() and scsipi_execute_xs(), instead push these into
scsipi_command. Make bustype_cmd and PHOLD/PRELE be called from
scsipi_execute_xs(). This allows us to create a xfer structure -- possibly on
the stack -- and call scsipi_execute_xs() directly.
 1.18 09-Sep-2004  bouyer Make the xxstart() functions reentrant again, as some drivers HBA can call
scsipi_done() from their scsipi_request().
For this, add a struct scsipi_xfer * argument to scsipi_command().
If not NULL scsipi_command() will use this to enqueue this xfer, otherwise
it'll try to allocate a new one. This scsipi_xfer has to be allocated
and initialised by scsipi_make_xs() or equivalent.
In xxstart(), allocate a scsipi_xfer using scsipi_make_xs(), and if not NULL,
dequeue the buffer before calling scsipi_command(). This makes sure that
scsipi_command() will not fail, and also makes sure that xxstart() won't
be called again between the BUFQ_PEEK() and BUFQ_GET().

Fix "dequeued wrong buf" panics reported by Juergen Hannken-Illjes in
private mail and Andreas Wrede on current-users@.
Thanks to Jason Thorpe and Chuck Silver for review, and Andreas Wrede for
testing the patch.
 1.17 21-Aug-2004  thorpej De-__P.
 1.16 12-Aug-2004  thorpej Protect against multiple inclusion.
 1.15 04-Aug-2004  bouyer Remove an unused prototype.
 1.14 03-Dec-2001  bouyer branches: 1.14.16; 1.14.18;
Update my copyrigth.
 1.13 02-Dec-2001  bouyer Clean up attach of wd/atapibus:
kill ata_atapi_attach. Change atapibus to use a struct scsipi_channel instead
of ata_atapi_attach as attach arch. Create a ata_device, compatible with
scsipi_channel, to attach wd.
 1.12 14-May-2001  bouyer branches: 1.12.2;
Use SCSI/ATAPI common definition for MODE_{SELECT,SENSE}{,_BIG}. Define
functions to send theses commands in scsipi_base.c and use them instead
of ad-hoc commands setups.
 1.11 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.10 02-Apr-2000  augustss branches: 1.10.6;
Let the device thet gets an atapibus attached specify how to kill pending
transfers by giving a function pointer.
The old method always called wdc specific code.
 1.9 01-Apr-2000  bouyer - DMA code cleanup: pciide_dma_finish() doesn't stop/unload the current DMA op
if an IRQ was not detected, unless the force flag was given. Use this to
detect if the IRQ was for us (closer to shared IRQ for controllers which
don't have their own IRQ handler in pciide.c) and to poll for DMA xfer.
Also makes the timeout recovery code simpler.
- ATAPI cleanup: don't call controller-specific functions from atapiconf.c
(wdc_*), so that it's possible to attach an atapibus to something else
than a wdc/pciide (Hi Lennart :).
Overload struct scsi_adapter with struct atapi_adapter, defined
as struct scsi_adapter + atapi-specific callbacks. scsipi_link still points
to an scsi_adapter, atapi code casts it to atapi_adapter if needed.
Move atapi_softc to atapiconf.h so that it can be used by the underlying
controller code (e.g. atapi_wdc.c).
Add an atapi-specific callback *atapi_probedev(), which probe a drive
in a controller-specific way, allocate the sc_link and fills in the
ataparams if needed. It then calls atapi_probedev() (from atapiconf.c)
to do the generic initialisations and attach the device.
- While I'm there merge and centralise the state definitions in atavar.h.
It should now be possible to use a common ata/atapi routine to set the
drive's modes (will do later).
 1.8 20-Oct-1999  enami Cancel active transfers on aic/wdc detach.
Also makes LS-120 drive works for me again.
 1.7 12-Oct-1998  bouyer branches: 1.7.6; 1.7.12; 1.7.14; 1.7.16;
Merge bouyer-ide
 1.6 13-Feb-1998  enami branches: 1.6.2;
Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.5 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.4 15-Jan-1998  cgd add support for an ATAPI attachment for 'sd'.
fix 'cd' driver's NCD_SCSI bogosity (was using testing wrong macro!)
clean up in various ways:
* make common atapi_mode_{sense,select}() functions.
* put ATAPI data structures in more sensible headers, split up by
device type.
* include headers a bit more carefully.
* pass flags to attachment-specific cd functions, and use them.
* get rid of SCSI bits in scsipi_base.h's scsipi_make_xs(), move
them into the correct place in scsi_base.c.
* fix minor typo in struct name in scsipiconf.h (which was apparently
never used except in a #define later in the same file).
* use __attribute__ to force 4-byte alignment for xs command store,
so that architectures trying to bus_space_write_multi_N() (where
N > 1) that data to a controller won't lose.
* clean up a few comments in typos, and make a few #defines easier to
understand/maintain.
* rename cd_link.h to cdvar.h (via repository copy). This is exactly
what a 'var' file is supposed to be.
 1.3 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.2 27-Aug-1997  bouyer branches: 1.2.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.1 01-Jul-1997  bouyer branches: 1.1.2;
file atapiconf.h was initially added on branch bouyer-scsipi.
 1.1.2.2 01-Jul-1997  thorpej Purely cosmetic formatting changes: some spaces -> tabs, indenting
consistency in prototypes.
 1.1.2.1 01-Jul-1997  bouyer New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
scsibus at aha
sd at scsibus
cd at scsibus
atapibus at wdc
cd at atapibus
cd has bus-specific config and function front-end in scsicd.c and atapicd.c
The call to theses functions from cd.c is conditionned to
NSCSICD and NATAPICD (all defined in cd.h by config).
 1.2.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.1 27-Aug-1997  thorpej file atapiconf.h was added on branch marc-pcmcia on 1997-08-27 23:32:57 +0000
 1.6.2.2 11-Jun-1998  bouyer pciide: when setting up DMA mode, also enable fast timings for PIO modes
if possible. For ATAPI devices, commands are always send via PIO.
atapi: matain a drive state (like wd drives) and use it to set up the
timings if needed.
 1.6.2.1 04-Jun-1998  bouyer Commit changes to the IDE system in a branch. This allows a better separation
between higth-level and low-level (i.e. registers read/write) and generalize
the queue for all commands. This also add supports for IDE DMA.
 1.7.16.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.7.14.1 15-Nov-1999  fvdl Sync with -current
 1.7.12.4 21-Apr-2001  bouyer Get rid of references to sc_link/scsipi_link in debug or comments.
 1.7.12.3 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.7.12.2 20-Oct-1999  thorpej Sync w/ trunk.
 1.7.12.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.7.6.1 07-Jul-2000  he Apply patch (requested by bouyer):
Add support for the following PCIIDE controllers:
o AMD 756
o CMD PCI0648 and PCI0649
o Hightpoint HPT366
o OPTi 82c621 (and a few of its derivatives)
o Promise Ultra/33 and Ultra/66
o Intel 82801 (ICH/ICH0)
Also fix PR#10437 (detect more ATAPI devices).
 1.10.6.2 08-Jan-2002  nathanw Catch up to -current.
 1.10.6.1 21-Jun-2001  nathanw Catch up to -current.
 1.12.2.1 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.14.18.1 11-Sep-2004  he Pull up revision 1.18 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.14.16.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.14.16.3 18-Sep-2004  skrll Sync with HEAD.
 1.14.16.2 25-Aug-2004  skrll Sync with HEAD.
 1.14.16.1 12-Aug-2004  skrll Sync with HEAD.
 1.19.12.1 30-Dec-2006  yamt sync with head.
 1.20.22.1 10-Dec-2006  yamt sync with head.
 1.20.20.1 12-Jan-2007  ad Sync with head.
 1.21.48.1 03-Apr-2008  mjf Sync with HEAD.
 1.22.4.1 11-Mar-2010  yamt sync with head
 1.356 01-Dec-2024  andvar s/assised/assisted/ in comment.
 1.355 22-Jun-2024  palle branches: 1.355.2;
Add quirk for devices that does not handle READ_DISCINFO
 1.354 26-Jun-2022  andvar s/Ramdom/Random/ in comments.
 1.353 12-Oct-2021  andvar fix various typos, mainly in comments.
 1.352 21-Aug-2021  andvar fix mainly same typos as in my previous commit but outside sys/dev/dm.
 1.351 16-Apr-2021  reinoud Limit buffer size for device capabilities requests as a work-around for PR
kern/56109.
 1.350 10-Feb-2021  christos branches: 1.350.2;
PR/55986: Ryo Onodera: DK_BUSY must have mask as second argument. Make cd.c
consistent by also using __BIT()
 1.349 26-Oct-2020  mlelstv branches: 1.349.2;
Avoid buffer overflow when copying from bounce buffer.
Fixes PR 54810

Don't use uninitialized pointer in split bounce buffer case and
free a partially allocated bounce buffer on error.
 1.348 29-Sep-2020  msaitoh s/settng/setting/
 1.347 29-Sep-2020  msaitoh s/implicitely/implicitly/
 1.346 29-Sep-2020  msaitoh s/occurence/occurrence/
 1.345 29-Sep-2020  msaitoh s/parition/partition/
 1.344 13-Apr-2020  maxv constify
 1.343 27-Mar-2020  mlelstv branches: 1.343.2;
Allow open of RAWPART even when no medium is loaded.
Keep errors silent if no medium is loaded.

Fixes PR kern/55104
 1.342 03-Sep-2018  riastradh branches: 1.342.4;
Rename min/max -> uimin/uimax for better honesty.

These functions are defined on unsigned int. The generic name
min/max should not silently truncate to 32 bits on 64-bit systems.
This is purely a name change -- no functional change intended.

HOWEVER! Some subsystems have

#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))

even though our standard name for that is MIN/MAX. Although these
may invite multiple evaluation bugs, these do _not_ cause integer
truncation.

To avoid `fixing' these cases, I first changed the name in libkern,
and then compile-tested every file where min/max occurred in order to
confirm that it failed -- and thus confirm that nothing shadowed
min/max -- before changing it.

I have left a handful of bootloaders that are too annoying to
compile-test, and some dead code:

cobalt ews4800mips hp300 hppa ia64 luna68k vax
acorn32/if_ie.c (not included in any kernels)
macppc/if_gm.c (superseded by gem(4))

It should be easy to fix the fallout once identified -- this way of
doing things fails safe, and the goal here, after all, is to _avoid_
silent integer truncations, not introduce them.

Maybe one day we can reintroduce min/max as type-generic things that
never silently truncate. But we should avoid doing that for a while,
so that existing code has a chance to be detected by the compiler for
conversion to uimin/uimax without changing the semantics until we can
properly audit it all. (Who knows, maybe in some cases integer
truncation is actually intended!)
 1.341 17-Jun-2017  mlelstv branches: 1.341.4; 1.341.6;
The atapibus detach path did hold the channel mutex while calling into autoconf,
which would trigger a panic when unplugging a USB ATAPI CDROM.

Align detach code for scsibus and atapibus to fix this.

Also avoid races when detaching devices by replacing callout_stop with
callout_halt.
 1.340 08-Apr-2017  mlelstv branches: 1.340.4; 1.340.6;
ignore a failed scspi_prevent when accessing the RAW_PART as before.
 1.339 05-Jan-2017  mlelstv branches: 1.339.2;
Fix memory leak.

Found by maxv@
 1.338 21-Dec-2016  mlelstv really keep request queued after resource shortage
 1.337 21-Dec-2016  mlelstv cdrestart takes a softc parameter, not a periph.
 1.336 16-Dec-2016  mlelstv add comment about "missing" dk_start.
 1.335 10-Dec-2016  mlelstv appease gcc.
 1.334 10-Dec-2016  mlelstv Refactored sd and cd to use common disk subroutines.
 1.333 20-Nov-2016  mlelstv Make scsipi framework MPSAFE.

Data structures are now protected by a per-adapter mutex at IPL_BIO
that is created by the scsibus or atapibus instance when the adapter
is configured.
The enable reference counter and the channel freeze counter which are
currently used by HBA code before the adapter is configured, are made
atomic.
The target drivers are now all tagged as D_MPSAFE.

Almost all HBA drivers still require the kernel lock to present,
so all callbacks into HBA code are still protected by kernel lock
unless the driver is tagged as SCSIPI_ADAPT_MPSAFE.

TODO: refactor sd and cd to use dksubr.
 1.332 20-Nov-2016  pgoyette Avoid calling bufq_free() from critical code sections.
 1.331 15-May-2016  reinoud branches: 1.331.2;
Use _align(2) workaround for PR kern/51141. This fixes odd CD length
reporting.
 1.330 26-Apr-2015  mlelstv Use C99-style initializers for struct dkdriver.
 1.329 13-Apr-2015  riastradh Convert sys/dev to use <sys/rndsource.h>.
 1.328 02-Jan-2015  christos We have three sets of DTYPE_ constants in the kernel:
altq Drop Type
disklabel Disk Type
file Descriptor Type
(not to mention constants that contain the string DTYPE).
Let's make them two, by changing the disklabel one to be DisK TYPE since the
other disklabel constants seem to do that. Not many userland programs use
these constants (and the ones that they do are mostly in ifdefs). They will
be fixed shortly.
 1.327 31-Dec-2014  christos make more drivers use disk_ioctl, and add a dev parameter to it so that
we can merge the "easy" disklabel ioctls to it. Ultimately all this will
go do dk_ioctl once all the drivers have been converted.
 1.326 31-Dec-2014  mlelstv forgot this one. disk_set_info includes the functionality of disk_blocksize.
 1.325 18-Oct-2014  snj branches: 1.325.2;
src is too big these days to tolerate superfluous apostrophes. It's
"its", people!
 1.324 03-Oct-2014  justin Use uint32_t for blocksize as per other uses and to fix warnings
 1.323 10-Aug-2014  tls Merge tls-earlyentropy branch into HEAD.
 1.322 25-Jul-2014  dholland Add d_discard to all struct cdevsw instances I could find.

All have been set to "nodiscard"; some should get a real implementation.
 1.321 25-Jul-2014  dholland Add d_discard to all struct bdevsw instances I could find.

I've set them all to nodiscard. Some of them (wd, dk, vnd, ld,
raidframe, maybe cgd) should be implemented for real.
 1.320 12-Jul-2014  hannken Prevent detaching an open device unless forced.
 1.319 18-Apr-2014  martin Fix PR kern/48550 by aligning the single instance of scsipi_read_cd_cap_data
that we found misaligned in the wild so far properly for the ahcisata
driver. Also point at PR kern/48754 for the real issue.
 1.318 19-Mar-2014  martin branches: 1.318.2;
PR kern/48550: additional initialization and sanity checking on the reported
blocksize of the medium.
 1.317 16-Mar-2014  dholland Change (mostly mechanically) every cdevsw/bdevsw I can find to use
designated initializers.

I have not built every extant kernel so I have probably broken at
least one build; however I've also found and fixed some wrong
cdevsw/bdevsw entries so even if so I think we come out ahead.
 1.316 25-Oct-2013  martin Turn a few __unused into __diagused
 1.315 19-Oct-2013  martin Mark a potentially unused variable
 1.314 28-Sep-2013  skrll Don't return EPASSTHROUGH when succeeding in ODIOCGDINFO or ODIOCGDEFLABEL
 1.313 12-Sep-2013  martin Remove unused variables
 1.312 02-Jul-2013  reinoud Small patch to clarify last_lba and now using size confusingly. No functional
change.
 1.311 29-May-2013  christos branches: 1.311.2;
phase 1 of disk geometry cleanup:
- centralize the geometry -> plist code so that we don't have
n useless copies of it.
 1.310 15-Mar-2013  martin Prompted by PR kern/47646, zero the value buffer before running the
GET_CONFIGURATION command and check for an unexpectedly large feature
length answer afterwards.
 1.309 06-May-2012  martin branches: 1.309.2;
When ejecting a medium, invalidate the in core disklabel - it is not
meaningfull anymore. This makes the following cdclose() use silent
mode and finally fixes PR kern/43785.
 1.308 06-May-2012  martin In cdopen: make the test for media presence always silent. In all cases
requiring a message, we will most likely get that from the spinup attempt
anyway.
This avoids the spurious "Check Condition on CDB, Not Ready, Medium Not
Present, Tray Closed" messages at boot/shutdown time.
 1.307 19-Apr-2012  bouyer Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.

This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.

In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.

Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.306 25-Feb-2012  shattered Fix typos in comments.

OK by wiz@
 1.305 02-Feb-2012  tls branches: 1.305.2;
Entropy-pool implementation move and cleanup.

1) Move core entropy-pool code and source/sink/sample management code
to sys/kern from sys/dev.

2) Remove use of NRND as test for presence of entropy-pool code throughout
source tree.

3) Remove use of RND_ENABLED in device drivers as microoptimization to
avoid expensive operations on disabled entropy sources; make the
rnd_add calls do this directly so all callers benefit.

4) Fix bug in recent rnd_add_data()/rnd_add_uint32() changes that might
have lead to slight entropy overestimation for some sources.

5) Add new source types for environmental sensors, power sensors, VM
system events, and skew between clocks, with a sample implementation
for each.

ok releng to go in before the branch due to the difficulty of later
pullup (widespread #ifdef removal and moved files). Tested with release
builds on amd64 and evbarm and live testing on amd64.
 1.304 25-Nov-2011  joerg branches: 1.304.2;
Replace memset with incorrect size argument with M_ZERO.
 1.303 20-May-2011  reinoud branches: 1.303.4;
Fix size reporting for DVD+R/DL and BluRay's; the value was trunced due to a
32 bit trunc due to a lacking type cast. The number of sectors for such media
can be more than 1<<32-1.
 1.302 04-Apr-2010  martin branches: 1.302.2;
cd_size: if we fake a size (and I realy have no idea why this would be a
good idea), at least set up all values to the fake values, as the caller
expects.
Should fix PR kern/39904, though if noone can find out why the fake value
would be needed, we should change it to just return 0 as suggested in the PR.
 1.301 23-Mar-2010  martin If we find a device in non-2048 byte/block mode and manage to switch
it over, we need to adjust the capacity - so just read it again.
 1.300 23-Mar-2010  martin Make cdclose() silent, if we only have the raw partition open (e.g. when
probing for media and not finding any)
 1.299 22-Mar-2010  martin Add a DIOCTUR (test unit ready) ioctl, to allow userland to easily
(and silently) check for presence of media. While this would be easy
to implement via SCIOCCOMMAND, our kernel has quirks for this
command, which otherwise would not be readily available to userland
applications.
 1.298 11-Mar-2010  mrg branches: 1.298.2;
various aprint_* fixes.
 1.297 06-Jan-2010  martin branches: 1.297.2;
Some usb devices come with an internal emulated umass CD drive (containing
windows drivers). I have such a device that has 0 features - avoid wrappig
features_len to a very big unsiged 32bit number in this case.
 1.296 06-Dec-2009  dyoung Delete do-nothing device-activation hooks.
 1.295 21-Oct-2009  rmind Remove uarea swap-out functionality:

- Addresses the issue described in PR/38828.
- Some simplification in threading and sleepq subsystems.
- Eliminates pmap_collect() and, as a side note, allows pmap optimisations.
- Eliminates XS_CTL_DATA_ONSTACK in scsipi code.
- Avoids few scans on LWP list and thus potentially long holds of proc_lock.
- Cuts ~1.5k lines of code. Reduces amd64 kernel size by ~4k.
- Removes __SWAP_BROKEN cases.

Tested on x86, mips, acorn32 (thanks <mpumford>) and partly tested on
acorn26 (thanks to <bjh21>).

Discussed on <tech-kern>, reviewed by <ad>.
 1.294 26-Jul-2009  reinoud Significantly bump time for SCSI track/disc closure. DVD-RW when used
sequentially can significantly take more time.
 1.293 05-Jun-2009  haad Add support for DIOCGDISKINFO to disk like device drivers. Change
partutil.c::getdiskinfo to use it to get disk geometry info.
Use DIOCGWEDGEINFO ioctl to get information about partition size, if disk
driver doesn't support it use old DIOCGDINFO. This patch adds support for
wedge like devices(lvm logical volumes, ZFS zvol partitions) to newfs and
other tools.

No objections on tech-userlevel@.
 1.292 07-Apr-2009  dyoung Detach atapibus(4), scsibus(4), cd(4), and sd(4) during shutdown.
Destroy sd->sc_callout in sddetach(). Delete some dead code in
cddetach().
 1.291 01-Apr-2009  reinoud Implement MMC_TRACKINFO_DATA and MMC_TRACKINFO_AUDIO detection for cdrom and
dvdrom devices. These two compatibility codes were forgotten when those were
introduced.
 1.290 18-Mar-2009  cegger bzero -> memset
 1.289 17-Mar-2009  reinoud Fix long standing problem with CD/DVD writing with `dd'. If done this way the
device will remain in a wait-for-more-stuff-to-be-written state delaying other
SCSI calls, esp. some read-size or read-layout calls, until its told there is
no more comming.

To fix this, on last close explicitly flush the caches releasing the device
from this mode and allowing other SCSI commands to reach it.

This fixes PR 40528
 1.288 14-Mar-2009  ad 'boot -z' bogons
 1.287 21-Jan-2009  cegger branches: 1.287.2;
buildfix: re-adapt to major()/minor() returning a 32bit value.
 1.286 13-Jan-2009  yamt g/c BUFQ_FOO() macros and use bufq_foo() directly.
 1.285 11-Jan-2009  cegger make this compile
 1.284 30-Dec-2008  reinoud Add ISO partition detection enabling auto-detection of iso9660 and UDF
partitions on optical media like CD/DVD/BD but also on all other media if
there is no NetBSD disklabel or MBR label.

Also fix cd's readdisklabel arguments so the ioctl's arrive at the right
device (!) and update its default label to make more sense.
 1.283 05-Sep-2008  gmcgarry branches: 1.283.2; 1.283.4;
Remove unreachable code introduced by rev 1.224. Fixes PR#38973.
 1.282 12-Jun-2008  cegger branches: 1.282.2;
use device_lookup_private to get softc
 1.281 12-May-2008  jnemeth branches: 1.281.2;
add support for drvctl properties
 1.280 12-May-2008  tron Fix SSP kernel builds.
 1.279 08-May-2008  reinoud Implement write support for the MMC framework. This consists of a setting
up of write parameters call and a call to perform a series of operations on
these devices.

Note that the MMC framework interface is still not exposed to userland
unless the expose_mmc flag is set. This is to prevent applications to build
on a possibly still moving target. This flag will eventually be removed.
 1.278 06-May-2008  yamt branches: 1.278.2;
getiobuf(false, NULL) -> getiobuf(NULL, false)
 1.277 02-May-2008  reinoud Extend the MMC framework to also create sensible reports on audio discs.
 1.276 02-May-2008  reinoud Fixup CD and DVD drives that appear to be on crack when reporting features.
They state f.e. that a recordable CD-R is rewritable or completely forget
that the fixed packet size formatted CD-RW is strict overwrite rewritable
and not randomly rewritable.
 1.275 02-May-2008  reinoud Make the SCSI sense code "Logical Unit Not Ready, Operation In Progress"
(Sense code 0x04, 0x07) not a fatal error anymore but retry command later.
This sense code can be returned on a CD-MRW write that encounters a bad
block resulting in the drive being busy relocating it. During that time it
can return this sense code to indicate its busy for a while.

Also reduce waiting time for the other "Logical Unix Not Ready, Long Write
In Progress" to half a second.
 1.274 28-Apr-2008  martin Remove clause 3 and 4 from TNF licenses
 1.273 24-Mar-2008  cube branches: 1.273.2; 1.273.4;
Split device_t for cd(4).
 1.272 02-Jan-2008  ad branches: 1.272.6;
Merge vmlocking2 to head.
 1.271 09-Dec-2007  jmcneill branches: 1.271.2;
Merge jmcneill-pm branch.
 1.270 27-Nov-2007  reinoud branches: 1.270.2; 1.270.4;
Pullup fixes from UDF write development project. This patch fixes a
conceptional bug in mmc_discinfo, improves DVD+R feature detection and
prevents corruption of mmc_trackinfo readout on some devices that return
short trackinfo structures.
 1.269 08-Oct-2007  ad branches: 1.269.4;
Merge disk init changes from the vmlocking branch. These seperate init /
destroy of 'struct disk' from attach / detach.
 1.268 04-Aug-2007  rumble branches: 1.268.2; 1.268.4; 1.268.6;
Be sure not to make requests over MAXPHYS when employing bounce buffers.
It was previously possible to round up one sector length if the start block
were not properly aligned.

Resolves PR kern/36716. This may also fix the following reported issues
on port-sgimips:
http://mail-index.netbsd.org/port-sgimips/2005/09/12/0000.html
http://mail-index.netbsd.org/port-sgimips/2005/09/14/0000.html

While here, be sure to put back allocated bufs, which previously appear
to have been leaked. Further, remove the dead write bounce code which never
got executed anyway.

No complaints on tech-kern.
 1.267 29-Jul-2007  ad branches: 1.267.4;
It's not a good idea for device drivers to modify b_flags, as they don't
need to understand the locking around that field. Instead of setting
B_ERROR, set b_error instead. b_error is 'owned' by whoever completes
the I/O request.
 1.266 21-Jul-2007  ad Replace some uses of lockmgr().
 1.265 09-Jul-2007  ad branches: 1.265.2;
Merge some of the less invasive changes from the vmlocking branch:

- kthread, callout, devsw API changes
- select()/poll() improvements
- miscellaneous MT safety improvements
 1.264 30-Jun-2007  dsl The cd ioctl definitions are not condusive to being called from within
the kernel (eg from withthe linux compat code).
Add CDIOCREADSUBCHANNEL_BUF and CDIOREADTOCENTRIES_BUF which are like the
existing ioctls withoutthe _BUF, except that the data area immediately
follows the control structure (and is fixed length).
Modify scsipi/cd.c so that it only ever has one 'struct cd_formatted_toc'
allocated on the stack (maybe this info ought to be in the softc)
structure since even one 800 byte on-stack buffer isn't overly friendly.
cdplay(1) still seems able to read the track-list of an ATAPI cd.
 1.263 30-Jun-2007  dsl Remove a load of unnecessary casts now that 'addr' is 'void *'.
 1.262 04-Mar-2007  christos branches: 1.262.2; 1.262.4;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.261 14-Jan-2007  martin branches: 1.261.2;
The previous fix for PR 34202 was wrong (some sizeof used the union, not
the command block) - but instead of fixing this, we can now revert it
completley: the real fix has been applied to ../ic/wdc.c in rev. 1.244,
removing the alignement constraints.
 1.260 01-Dec-2006  martin branches: 1.260.2;
Fix PR kern/34202 differently, by aligning the variables "the traditional
way".
 1.259 25-Nov-2006  scw - Call disk_blocksize(9) when we determine the physical block size of the
media. (Will this ever be anything other than 2048?)
- bounds_check_with_label() has been fixed, so put the correct partition
size in the default disklabel.
 1.258 16-Nov-2006  christos __unused removal on arguments; approved by core.
 1.257 14-Nov-2006  reinoud Implement/add bufq strategy setting by dkctl for the CD class devices.

Though its hardly advisable to change the CD strategy setting to anything
other than `disksort' or `prioscan' it ought to be possible.
 1.256 28-Oct-2006  reinoud Implement another class of `unit not ready' sense handling that is not
fatal.. A `long write in progress' is a retry again later command that is
issued when a device returns immediately after a write request but needs
some time before it can handle read requests.
 1.255 27-Oct-2006  christos - don't allocate huge arrays on the stack
- no bogus ; after }; in block statements!
 1.254 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.253 10-Oct-2006  riz Also print blksize with %u - this is what I get for eyeballing
a patch instead of applying it directly. PR#33966.
 1.252 10-Oct-2006  riz Print an unsigned parameter with %lu, not %ld. From David A. Holland
in PR#33966.
 1.251 08-Oct-2006  mlelstv Use last track info only if it gives a sane value. Fixes PR#34688.
 1.250 08-Sep-2006  reinoud branches: 1.250.2;
Oeps! I overlooked the size argument of the SCSI call. It ought to be equal
to the number of bytes we are reading: READ_DISCINFO_BIGSIZE

sorry.
 1.249 07-Sep-2006  reinoud The size of struct scsipi_read_discinfo_data is not even due to the data[1]
pseudo array in the structure. The scsi command issued used
sizeof(scsipi_read_discinfo_data) but included thus the last byte that
wasn't going to be inspected anyway. Using the constant
READ_DISCINFO_BIGSIZE fixes the bug.

Even though SCSI adapters *should* accept odd lengths, the Sun U10
(sparc64's) crashes in bus_space_read_multi_stream_2(). That explains the
crash.

NOTE: somewhere in NetBSD/sparc64's atapibus support there is the
assumption on the even size; this needs to be fixed! (Hi Sparc64 portmaster
:-) )
 1.248 01-Sep-2006  matt branches: 1.248.2;
Use an unsigned int for the blksize on read_cd_capacity.
 1.247 31-Aug-2006  reinoud Fix panic on reading/writing to a (raw) CD device and encountering an
error. It now correctly sets bp->b_resid to the full size of the buffer.
The failed SCSI read/write command allways contains the complete buffer.

When encountering a read (or write) error, the scsipi stack sets the
xs->resid to zero since the command has been accepted by the drive. The
scsi command set does not allow a partial read or write to be performed and
will signal a success or an error.
 1.246 31-Aug-2006  reinoud Remove two spurious empty lines in functions
 1.245 31-Aug-2006  reinoud Fix fencing bug on CD disk size; the comment was wrong, its the *length* of
the device and since we count from zero its equal to the number of sectors
and thus one higher than the last sector.

This fixes the read/write problems on the (raw) device where the last
sector was not readable/writable.
 1.244 28-Aug-2006  christos Add missing initializer.
 1.243 10-Aug-2006  reinoud Fix incorrect `loast possible lba' reporting, remove some empty lines and
add mmc classification for some still rare HD DVD device types.
 1.242 30-Mar-2006  thorpej branches: 1.242.6;
Use device_private().
 1.241 28-Mar-2006  thorpej Use device_unit().
 1.240 08-Feb-2006  reinoud branches: 1.240.2; 1.240.4; 1.240.6;
Add name of the processed feature like the others have.
 1.239 04-Feb-2006  reinoud Add support for SCSI MMC feature 0x0024: hardware assisted defect
management to be passed in mmc_discinfo's device capabilities.

When a device reports support for this the hardware will automatically
remap sectors on read- or write-errors. CD-MRW, DVD+MRW, DVD-RAM and BR-RE
support these.
 1.238 02-Feb-2006  reinoud branches: 1.238.2;
Adding two Ioctl's to scsipi's cd.c to abstract SCSI MMC devices. The
ioctl's and their structures are currenly hidden from inclusion for normal
userland applications to allow the MMC abstraction interface to mature
first.

Its useage is mainly for applications dedicated to handling and processing
SCSI MMC compliant devices and their media. Examples of these are CDs, DVDs
and comparable optical devices but also some magnetic swapable devices that
present themselves as a SCSI MMC compliant device.

Its current use is currently the to be committed in-kernel UDF filingsystem
and its userland tools.
 1.237 07-Jan-2006  christos branches: 1.237.2;
fix typo.
 1.236 07-Jan-2006  yamt remove B_EINTR as it isn't used anymore.
 1.235 04-Jan-2006  yamt - add simple functions to allocate/free a buffer for i/o.
- make bufpool static.
 1.234 21-Dec-2005  reinoud branches: 1.234.2;
Improve disc size reporting. The current discsize thats reported is the
current recorded extent and not the complete maximum extent of the disc.

An empty CD/DVD disc is seen as having an invalid length and given 800 Mb
size. This makes it possible to write to a CD-R using `dd' as some tend to
do.

However when an extent is recorded the initial recorded length is seen as
valid and is thus set as absolute upperbound to read and write actions
preventing extention of the disc.

Committed after consultation on tech-kern with a positive advice by Manuel
Bouyer.
 1.233 16-Dec-2005  christos delete extraneous verbiage.
 1.232 15-Dec-2005  reinoud Clean up cdgetdisklabel. It was jumping over a return...
 1.231 11-Dec-2005  christos merge ktrace-lwp.
 1.230 11-Dec-2005  reinoud Fix illogical use of extra variable. It was mainly to get a better
readability in KNF. This solution gives better readability without the
extra variable.
 1.229 10-Dec-2005  reinoud Second stage of read TOC modifications. Introducing the various forms of
TOC reading. Note that the external interfaces haven't changed. Only the
formatted toc is requestable.

The read msinfo command hasn't been changed in this patch though it could
become more clever using the CD_TOC_MSINFO format.
 1.228 15-Oct-2005  yamt - change the way to specify a bufq strategy. (by string rather than by number)
- rather than embedding bufq_state in driver softc,
have a pointer to the former.
- move bufq related functions from kern/subr_disk.c to kern/subr_bufq.c.
- rename method to strategy for consistency.
- move some definitions which don't need to be exposed to the rest of kernel
from sys/bufq.h to sys/bufq_impl.h.
(is it better to move it to kern/ or somewhere?)
- fix some obvious breakage in dev/qbus/ts.c. (not tested)
 1.227 06-Sep-2005  reinoud Cleanup and fix cd_size() function; it was returning wrong results.
Also factor out read_cd_capacity()
 1.226 05-Sep-2005  reinoud Implement DIOCCACHESYNC for SCSI/ATAPI cd/mmc recordable devices found by
scsipi/cd.c by issueing the SYNCHRONISE CACHES scsi/atapi call as defined
per MMC standard.
 1.225 28-Aug-2005  reinoud Document executed SCSI commands with the IOCTL's to prevent confusion.
 1.224 06-Jul-2005  bouyer Always allow open() on the raw partition to succeed, even if any of the
SCSI command in cdopen() fails. Fix PR kern/30288 by Pavel Cahyna.
 1.223 29-May-2005  christos branches: 1.223.2;
- Sprinkle const
- Avoid variable shadowing.
- Eliminate some caddr_t abuse.
 1.222 25-Apr-2005  drochner fix more SSD_RCODE_VALID misuse introduced in cleanup
 1.221 31-Mar-2005  yamt introduce a function to drain bufq and use it where appropriate.
 1.220 27-Feb-2005  perry branches: 1.220.2;
nuke trailing whitespace
 1.219 21-Feb-2005  thorpej Part 1 of a cleanup pass over the SCSI subsystem. The aim is to name
everything "scsi_*", since we really are talking about the SCSI command
set, ATAPI transport not withstanding. Improve the names of many structures,
and prepend "SCSI_" onto all SCSI command opcodes. Place items described
by the SCSI Primary Commands document into scsi_spc.h.
 1.218 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.217 31-Jan-2005  reinoud Fix LP64 problems introduced by my u_long->uint32_t conversion.
 1.216 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.215 31-Jan-2005  reinoud As part of cleaning up sys/scsipi, replace all u_char by uint8_t and
replace all `short' with int16_t.
 1.214 30-Jan-2005  reinoud As in revision 1.213, not all information is returned in read-TOC format 0. In order to add a new generic read-toc ioctl, i renamed the origional to _f0 to indicate its only format 0. The routines can then be changed one at a time to the new generic calling. This patch fixes two calls i'd overseen.... silly me forgot to recompile :(
 1.213 30-Jan-2005  reinoud The code asumes all programs want to know is TOC response format 0. Support
for TOC response format 1 and 2 are mandatory on CD/DVD too and provide
more information.

Next an IOCTL needs to be implemented that can read all TOC formats in a
generic way. This is pending.
 1.212 07-Dec-2004  thorpej branches: 1.212.2; 1.212.4;
USe more appropriate macro/struct names for READ/WRITE (6) and
READ/WRITE (10).
 1.211 28-Oct-2004  yamt move buffer queue related stuffs from buf.h to their own header, bufq.h.
 1.210 26-Sep-2004  dogcow Fix debug message output args to match yamt's src/sys/sys/buf.h changes.
 1.209 18-Sep-2004  mycroft Standardize some variable names and the calling pattern for scsipi_command().
Use void pointer casts.
 1.208 17-Sep-2004  mycroft Remove the "xfer" argument to scsipi_command().
 1.207 17-Sep-2004  mycroft In places where we've already called scsipi_make_xs(), call scsipi_execute_xs()
directly rather than going through scsipi_command().
 1.206 17-Sep-2004  mycroft Do not manipulate xs->bp in "generic" code -- do it only in the psw_done
routine. As part of this, pass down our pre-parsed error code -- though this
interface will probably change later to accomodate better error handling.
 1.205 09-Sep-2004  bouyer Make the xxstart() functions reentrant again, as some drivers HBA can call
scsipi_done() from their scsipi_request().
For this, add a struct scsipi_xfer * argument to scsipi_command().
If not NULL scsipi_command() will use this to enqueue this xfer, otherwise
it'll try to allocate a new one. This scsipi_xfer has to be allocated
and initialised by scsipi_make_xs() or equivalent.
In xxstart(), allocate a scsipi_xfer using scsipi_make_xs(), and if not NULL,
dequeue the buffer before calling scsipi_command(). This makes sure that
scsipi_command() will not fail, and also makes sure that xxstart() won't
be called again between the BUFQ_PEEK() and BUFQ_GET().

Fix "dequeued wrong buf" panics reported by Juergen Hannken-Illjes in
private mail and Andreas Wrede on current-users@.
Thanks to Jason Thorpe and Chuck Silver for review, and Andreas Wrede for
testing the patch.
 1.204 06-Sep-2004  bouyer Fix comment: xxstart() can also be called from xxrestart()
 1.203 27-Aug-2004  bouyer Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
Havard Eidnes's analysis of this problem is that the scsipi_xfer pool is
competing for resources with other pools, including the the inode and vnode
pools which can grow quite large.

*_scsipi_cmd(): don't biodone the buffer if scsipi_make_xs() fails, let the
caller deal with the problem
start function of block devices drivers: dequeue the buffer after the
scsipi_command() call. If scsipi_command() fails with ENOMEM don't dequeue
the buffer, and schedule a callout to call the start function after
some delay.
scsipi_init(): prime the scsipi_xfer_pool with one page. This ensure that
there is always some scsipi_xfer to play with. If scsipi_command() fails
because of pool_get(), we're sure there will be resources available later,
when the pending commands have completed.

Reviewed by Jason Thorpe and Havard Eidnes.
Todo: remove the "unable to allocate scsipi_xfer" and "not queued, error %d"
printfs, but I choose to keep them for now, to help make sure the code does
what it should.
 1.202 21-Aug-2004  thorpej Use ANSI function decls and make use of static.
 1.201 24-Apr-2004  pk Some older devices do not understand the `disable block descriptor' bit in
the mode sense request. So fall back on mode sense data including a block
descriptor section.

See also sd.c rev. 1.214. Again: should we bother trying DBD at all?
 1.200 22-Feb-2004  enami branches: 1.200.2;
Backout an obvious bug introduced in rev. 1.145.
 1.199 10-Jan-2004  yamt store a i/o priority hint in struct buf for buffer queue discipline.
 1.198 10-Nov-2003  wiz Spell address with two d's. Inspired by similar changes in OpenBSD,
originating from Jonathon Gray and forwarded by jmc@openbsd.
 1.197 18-Sep-2003  mycroft Merge the geometry and cache handling code for all direct access and optical
devices, as it's general to all SCSI MMC devices. In the process, remove
PQUIRK_NO_FLEX_PAGE.
 1.196 13-Sep-2003  mycroft Don't be silent if there is no media present.
 1.195 13-Sep-2003  mycroft Make sure the "raw partition" can always be opened again.
 1.194 09-Sep-2003  mycroft In the test for whether to start the unit, I used the wrong error code.
ENODEV is only returned when we get "medium not present," which we can fail
immediately on. All other "not ready" cases return EIO.
 1.193 08-Sep-2003  mycroft Oops. Fix a typo in cd_mode_select().
 1.192 08-Sep-2003  mycroft Do a START UNIT only if the TEST UNIT READY reports that the device is not
ready. This avoids gratuitously starting the motor on floppy and CD-ROM
drives, and eliminates the need for the audio playing test in cdopen().

Therefore, also remove PQUIRK_NOSTARTUNIT.
 1.191 08-Sep-2003  mycroft Attempt to deal with Martin's weirdass Sun drive by setting the mode sense
allocation length a little more precisely -- add the space for the header in
cd_mode_sense(). Also delete the XS_CTL_SILENT, since we really do want to
see errors.

Lastly, add a similar wrapper for mode select, simplifying the callers
slightly.
 1.190 08-Sep-2003  mycroft Eliminate the separate ATAPI and SCSI attachments for "cd".
 1.189 08-Sep-2003  mycroft Nuke some printf()s.
 1.188 07-Sep-2003  mycroft Set PQUIRK_ONLYBIG in the wdc-atapi frontend, obviating the need to ever test
the "bus type" for this.

Merge all the code in the SCSI and ATAPI backends for "cd" devices. All of
the mode page handling and whatnot is general to SCSI MMC devices, and should
never have been separated to begin with. This fixes a variety of problems,
and adds load/unload support for SCSI-attached devices.
 1.187 18-Jul-2003  wiz Add a cast to avoid an integer overflow.
Fixes playing (at least some) DVDs.
Patch from Tsubai Masanari (tsubai at iri.co.jp) -- thanks a lot!
 1.186 10-Jul-2003  martin If a CD drive reports != 2048 byte block size, try to change it into
2048 byte mode. Fixes PR kern/22090.
Reviewed by Manuel Bouyer.
 1.185 29-Jun-2003  fvdl branches: 1.185.2;
Back out the lwp/ktrace changes. They contained a lot of colateral damage,
and need to be examined and discussed more.
 1.184 28-Jun-2003  darrenr Pass lwp pointers throughtout the kernel, as required, so that the lwpid can
be inserted into ktrace records. The general change has been to replace
"struct proc *" with "struct lwp *" in various function prototypes, pass
the lwp through and use l_proc to get the process pointer when needed.

Bump the kernel rev up to 1.6V
 1.183 10-May-2003  thorpej Change bounds_check_with_label() to take a pointer to the disk structure,
rather than the label itself. This paves the way for some future changes.
 1.182 02-May-2003  dsl Change return type of readdisklabel() to const char *
I hope I've found all the correct places!
 1.181 03-Apr-2003  fvdl Check RAW_PART against the media size instead of the disklabel.
Add the media size in 512-byte sectors to the softc, to avoid
some 64 bit computations. Bump the capacity stored in softcs
for disks to 64 bits.
 1.180 20-Mar-2003  dbj use PRId64 to printf bp->b_blkno, which is of type daddr_t
 1.179 25-Feb-2003  thorpej Add a new BUF_INIT() macro which initializes b_dep and b_interlock, and
use it. This fixes a few places where either b_dep or b_interlock were
not properly initialized.
 1.178 05-Feb-2003  pk Make the buffer cache code MP-safe.
 1.177 03-Feb-2003  thorpej Test callout_pending(), not callout_active(), and eliminate now-unnecessary
callout_deactivate() calls.
 1.176 23-Jan-2003  bad Being able to make image backups of your whole disk, not to mention not
causing certain ata disks to lock up by reading beyond the last block,
beats catering to broken devices.

bounds_check_with_label() RAW_PART too.
 1.175 20-Jan-2003  simonb The Double-Semi-Colon Police.
 1.174 13-Jan-2003  toshii Use the correct byte positions to read data returned by DVD_LU_SEND_RPC_STATE.
 1.173 15-Dec-2002  jmcneill Another sizeof -> 4 + 2048 fix for dvd_read_manufact after malloc changes
 1.172 15-Dec-2002  jmcneill In dvd_read_disckey, sizeof(buf) is no longer '4 + 2048' after the
malloc change. Replace 'sizeof(buf)' with '4 + 2048' -- this makes
dvd_read_disckey work properly again.
 1.171 15-Dec-2002  fvdl Don't waste too much kernel stack space on (infrequent) ioctl operations,
use malloc instead for temp space.
 1.170 09-Nov-2002  thorpej Fix signed/unsigned comparison warnings.
 1.169 01-Nov-2002  mrg implement separate read/write disk statistics:
- disk_unbusy() gets a new parameter to tell the IO direction.
- struct disk_sysctl gets 4 new members for read/write bytes/transfers.
when processing hw.diskstats, add the read&write bytes/transfers for
the old combined stats to attempt to keep backwards compatibility.

unfortunately, due to multiple bugs, this will cause new kernels and old
vmstat/iostat/systat programs to fail. however, the next time this is
change it will not fail again.

this is just the kernel portion.
 1.168 23-Oct-2002  jdolecek merge kqueue branch into -current

kqueue provides a stateful and efficient event notification framework
currently supported events include socket, file, directory, fifo,
pipe, tty and device changes, and monitoring of processes and signals

kqueue is supported by all writable filesystems in NetBSD tree
(with exception of Coda) and all device drivers supporting poll(2)

based on work done by Jonathan Lemon for FreeBSD
initial NetBSD port done by Luke Mewburn and Jason Thorpe
 1.167 18-Sep-2002  chs remove all vesitages of dk_establish().
 1.166 06-Sep-2002  gehenna Merge the gehenna-devsw branch into the trunk.

This merge changes the device switch tables from static array to
dynamically generated by config(8).

- All device switches is defined as a constant structure in device drivers.

- The new grammer ``device-major'' is introduced to ``files''.

device-major <prefix> char <num> [block <num>] [<rules>]

- All device major numbers must be listed up in port dependent majors.<arch>
by using this grammer.

- Added the new naming convention.
The name of the device switch must be <prefix>_[bc]devsw for auto-generation
of device switch tables.

- The backward compatibility of loading block/character device
switch by LKM framework is broken. This is necessary to convert
from block/character device major to device name in runtime and vice versa.

- The restriction to assign device major by LKM is completely removed.
We don't need to reserve LKM entries for dynamic loading of device switch.

- In compile time, device major numbers list is packed into the kernel and
the LKM framework will refer it to assign device major number dynamically.
 1.165 30-Aug-2002  hannken Remove the old device buffer queue interface.

Approved by: Jason R. Thorpe <thorpej@wasabisystems.com>
 1.164 22-Jul-2002  hannken Convert to new device buffer queue interface.
 1.163 27-May-2002  drochner put multisession offset code into a separate function and initialize
the p_cdsession field of partition 'a' in the default disklabel
 1.162 05-May-2002  bouyer branches: 1.162.2;
If periph->periph_callout is already active, don't freeze the periph again:
scispi_periph_timed_thaw() will be called only one time anyway.
 1.161 09-Dec-2001  veego Support for dvd region code (RPC).
 1.160 15-Nov-2001  lukem don't need <sys/types.h> when including <sys/param.h>
 1.159 13-Nov-2001  lukem add RCSIDs
 1.158 22-Oct-2001  bouyer We can't have XS_CTL_DATA_OUT and XS_CTL_DATA_IN at the same time.
Pointed out by Bernd Ernesti.
 1.157 02-Sep-2001  tsutsui branches: 1.157.2;
Use be16toh(), be32toh(), le16toh() and le32toh() rather than
ntohs(), ntohl() or homegrown bswap() functions.

XXX Does anyone use drives with PQUIRK_LITTLETOC on big endian machines?
 1.156 20-Aug-2001  ad Don't special case the retry for Media Changed... More later.
 1.155 20-Aug-2001  ad Two changes submitted by Sergey Svishchev <svs@ropnet.ru>:

- Don't send START UNIT upon open if the drive is playing audio (PR 11768).
- If we receive sense indicating that the media changed, retry (PR 8326).
 1.154 15-Aug-2001  eeh Add support for smaller sector sizes so we can mount ffs filesystems.
 1.153 18-Jul-2001  thorpej bcopy -> memcpy
 1.152 18-Jul-2001  thorpej bzero -> memset
 1.151 26-Jun-2001  bouyer branches: 1.151.2;
Add a XS_CTL_SILENT_NODEV flag: if the sense info is "not ready, medium
not present" don't print any message but still return ENODEV.
Use this in cd driver to allow open of character raw partition even if
the drive is empty (older drives fails at LOAD_UNLOAD time, newer
ones fail at TEST_UNIT_READY time).
 1.150 20-May-2001  christos PR/12991: Dave Huang: DVD ioctls don't work because we send the wrong size
CDB (16 instead of 12).
 1.149 17-May-2001  bouyer SDEV_DB -> SCSIPI_DB
 1.148 06-May-2001  drochner Since __HAVE_OLD_DISKLABEL, DISKMINOR(u, p) isn't continuous over "p"
anymore. So we can't pass a range to vdevgone() but have to loop over the
partitions explicitely.
(otherwise the next device unit will be killed)
 1.147 28-Apr-2001  tsutsui Don't forget to set XS_CTL_DATA_OUT (that was removed in previous).
 1.146 28-Apr-2001  thorpej Use simple tags for read and write, unless B_ORDERED is set, in
which case we use an ordered tag.
 1.145 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.144 19-Jan-2001  kenh branches: 1.144.2;
Retry requests if the CD-ROM drive returns the sense code "Device in
Process of Becoming Ready".
 1.143 08-Jan-2001  fvdl Return error in the case of using ODIOCGDINFO or ODIOCGDEFLABEL when
the number of partitions is > OLDMAXPARTITIONS. This is better
than silently truncating the label (don't want to silently throw
away partitions when using an old disklabel binary on a label with
> 8 partitions). From Enami Tsugutomo.
 1.142 07-Jan-2001  fvdl Adapt all disk devices in MI directories to handle ODIOC* calls
for ports that have bumped MAXPARTITIONS (and thus define
__HAVE_OLD_DISKLABEL).
 1.141 09-Jun-2000  enami branches: 1.141.2;
Prevent a process being swapped out during I/O if the data buffer is
allocated on stack. This potential problem is noticed by Noriyuki Soda
and the idea and sample code to fix is given by Jason R. Thorpe.
 1.140 30-May-2000  augustss Add a quirk, SDEV_ONLYBIG, which implies that the device cannot handle
the 6 byte versions of READ, WRITE, and MODE_SENSE.
This greatly simplifies the UFI (USB Floppy) handling.
 1.139 16-May-2000  thorpej branches: 1.139.2;
Nuke dk_establish() from orbit except from those ports which still use
it to determine the boot device: mvme68k, pc532, macppc, ofppc. Those
platforms should be changed to use device_register(). In the mean time,
those ports defined __BROKEN_DK_ESTABLISH.
 1.138 30-Mar-2000  augustss Get rid of register declarations.
 1.137 13-Mar-2000  soren Fix doubled 'the's in comments.
 1.136 07-Feb-2000  thorpej Fix a bug in disksort_*() which caused non-optimal ordering when multiple
active partitions were on a single spindle. Add a b_rawblkno member to
struct buf which contains the non-partition-relative block number to sort
by.
 1.135 21-Jan-2000  thorpej Update for sys/buf.h/disksort_*() changes.
 1.134 03-Nov-1999  matt use __vax__ and __i386__ instead of vax and i386
 1.133 31-Oct-1999  mycroft Fix the size of start_sector, end_sector and end_sector_l0. (I'm just going to
assume the Linux people will also fix this, since I reported it to them.)
 1.132 29-Oct-1999  mycroft Emulate the Linux DVD_* ioctls(2). This gets us 90% of the way to running the
LiViD DVD player. (See forthcoming mail to current-users.)
XXX NOTE: We should do something to probe capabilities, rather than allowing
these ioctls on any device.
 1.131 17-Oct-1999  ragge branches: 1.131.2; 1.131.4;
Don't call dk_establish() on vax either.
 1.130 30-Sep-1999  thorpej branches: 1.130.2;
Cleanup the scsipi_xfer flags:
- `flags' is now gone, replaced with `xs_control' and `xs_status'.
- Massive cleanup of the control flags. Now we explicitly say that
a job is to complete asynchronously, rather than relying on side-effects,
and use a new flag to now that device discovery is being performed.
- Do SCSI device discovery interrupt-driven.
 1.129 23-Sep-1999  enami Allow to detach wdc, atapibus, wd and cd.
 1.128 07-Aug-1999  mycroft Increase several timeouts to 30s.
 1.127 27-May-1999  bouyer in cdclose(), ignore "media change" when calling scsipi_prevent(), after
a 'eject -l' some ATAPI drives report this instead of illegal request.
 1.126 08-Apr-1999  bouyer Add SCSI_IGNORE_NOT_READY to the flags for CDIOCCLOSE: some devices will
attempt to spin up, and report an error if there's no CD. Fixes port-i386/7345.
 1.125 05-Apr-1999  mycroft If scsipi_command() fails, always print out the error code.
 1.124 28-Feb-1999  explorer branches: 1.124.2;
Update to slightly altered rnd_attach_source() api
 1.123 15-Feb-1999  bouyer For CDIOCCLOSE, ignore media changes, as some drive seems to return this
info here.
 1.122 10-Feb-1999  bouyer Abort transfer if b_blkno is negative. Closes PR kern/5553 by Johan Danielsson.
 1.121 08-Feb-1999  bouyer Change DIOCEJECT to do what's needed to eject a device before the eject
command (unlock for sd and cd) if no other partitions are open, return
EBUSY otherwise. DIOCEJECT will have the old semantic if its argument is not
0. The old ioctl has been renamed to ODIOCEJECT for binary compatibility.
 1.120 29-Jan-1999  bouyer Return ENODEV instead of EIO when we are trying to open a device without media
in the drive. restrict "opening of empty drive" to character devices only
(reading a block device returns a short read instead of ENODEV, which can lead
to confusion).
 1.119 26-Jan-1999  bouyer Allows the raw partition to be open()'ed, even when scsipi_start() fails
(no media or other ...) so that we can always send ioctl's to the device.
 1.118 04-Jan-1999  is Multisession-CD-Support, by Torsten Duwe <duwe@ns.lst.de>.
 1.117 08-Dec-1998  thorpej When closing, wait for pending xfers to drain before unlocking the door,
and wait again before deleting the reference to the adapter.
 1.116 20-Nov-1998  thorpej Add adapter reference counting for SCSI and ATAPI devices.
 1.115 17-Aug-1998  mycroft Assign my copyrights to TNF.
 1.114 05-Aug-1998  drochner Improve generation of default disklabels:
-store printable product ID in cd's and sd's softc, use it as "typename"
-for this, add a "destination buffer length" argument to scsipi_strvis()
-return ATAPI device type for ATAPI devices
 1.113 13-Jul-1998  hpeyerl Add support for ATA CD changer devices like the NEC CDR-251.
Evidence of managerial coding removed by Victor T. Cleaner (thorpej)
 1.112 16-Mar-1998  mycroft Implement CDIOCCLOSE.
 1.111 15-Jan-1998  cgd move the 'cd_cd' declaration back up to where it was before
the config changes were made. Though the nature of the declaration
had to change, there wasn't a reason to change its location.
 1.110 15-Jan-1998  cgd add support for an ATAPI attachment for 'sd'.
fix 'cd' driver's NCD_SCSI bogosity (was using testing wrong macro!)
clean up in various ways:
* make common atapi_mode_{sense,select}() functions.
* put ATAPI data structures in more sensible headers, split up by
device type.
* include headers a bit more carefully.
* pass flags to attachment-specific cd functions, and use them.
* get rid of SCSI bits in scsipi_base.h's scsipi_make_xs(), move
them into the correct place in scsi_base.c.
* fix minor typo in struct name in scsipiconf.h (which was apparently
never used except in a #define later in the same file).
* use __attribute__ to force 4-byte alignment for xs command store,
so that architectures trying to bus_space_write_multi_N() (where
N > 1) that data to a controller won't lose.
* clean up a few comments in typos, and make a few #defines easier to
understand/maintain.
* rename cd_link.h to cdvar.h (via repository copy). This is exactly
what a 'var' file is supposed to be.
 1.109 12-Jan-1998  thorpej Adjust for changes to config.
 1.108 02-Dec-1997  mikel fix typo; from Dave Sainty in PR kern/4602
 1.107 18-Oct-1997  thorpej branches: 1.107.2;
Implement two macros, scsipi_command() and scsipi_command_direct(), and
use them to hide the structure of the function pointers we jump through
to issue a command.
 1.106 13-Oct-1997  explorer o Make usage of /dev/random dependant on
pseudo-device rnd # /dev/random and in-kernel generator
in config files.

o Add declaration to all architectures.

o Clean up copyright message in rnd.c, rnd.h, and rndpool.c to include
that this code is derived in part from Ted Tyso's linux code.
 1.105 10-Oct-1997  explorer Add hooks to insert timing info into the random system
 1.104 08-Oct-1997  thorpej Implement DIOCGDEFLABEL.
 1.103 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.102 09-Sep-1997  bouyer Uses PLAY_MSF instead of PLAY for the CDIOCPLAYTRACKS ioctl. This avoid a int16
overflow in the PLAY cbd when the resquested track(s) are more than 65535
blocks long. Submitted by t-nkyma@tcp-ip.or.jp in PR kern/4092.
 1.101 27-Aug-1997  bouyer branches: 1.101.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.100 02-Apr-1997  mycroft Push the buffer cleanup code into scsi_done(), and split it so that biodone()
is called *after* the driver `done' routine. This fixes disk I/O statistics
on SCSI devices.

Also, calling the `done' routine with a `complete' argument of 0 and actually
having it do anything meaningful loses in at least 3 ways, so just nuke the
argument altogether and don't call it this way. If the driver needs to do
some error handling, that's what `err_handler' is for.
 1.99 29-Mar-1997  christos PR/3409: Koji Imada: cdsize() timeout too short for cd changers.
 1.98 21-Feb-1997  thorpej If posting an error condition because the media has been unloaded,
make sure to set the residual count to reflect that no data was
transfered.

From Naofumi HONDA / MINOURA Makoto, PR #3007.
 1.97 05-Dec-1996  cgd branches: 1.97.6;
update these so they compile whether or not __BROKEN_INDIRECT_CONFIG
is defined.
 1.96 12-Oct-1996  christos revert previous kprintf change
 1.95 10-Oct-1996  christos printf -> kprintf, sprintf -> ksprintf
 1.94 13-Aug-1996  explorer be quiet about read_subchannel. Some CD players use this to poll, and having
a screen filled with kernel messages isn't nice. Closes pr kern/817
 1.93 13-Aug-1996  explorer Fix condition where disk_unbusy may not get called
 1.92 05-May-1996  christos Cleanup the rest of the SCSIDEBUG printfs. From Bernd Ernesti.
 1.91 22-Apr-1996  christos remove include of <sys/cpu.h>
 1.90 30-Mar-1996  christos Eliminate scsi_conf.h.
 1.89 29-Mar-1996  mrg eliminate unused variables.
 1.88 27-Mar-1996  cgd fix pasto: sdminphys -> cdminphys in comment.
 1.87 27-Mar-1996  mycroft SDUNIT -> CDUNIT in last.
 1.86 26-Mar-1996  mycroft Put back dk_establish() for now.
 1.85 26-Mar-1996  mycroft Increase *OUTSTANDING.
Remove old dk_establish() garbage.
Support the `ancient' bit in both drivers.
 1.84 19-Mar-1996  mycroft Define a full set of [234][bl]tol() and lto[234][bl]() conversion functions,
inlined.
Use sized types in protocol structures.
Make the definition of scsi_sense_data less ugly.
 1.83 17-Mar-1996  thorpej New device attachment scheme:

- split softc size and match/attach out from cfdriver into
a new struct cfattach.

- new "attach" directive for files.*. May specify the name of
the cfattach structure, so that devices may be easily attached
to parents with different autoconfiguration semantics.
 1.82 14-Feb-1996  christos scsi prototypes
 1.81 30-Jan-1996  thorpej Accept DIOCEJECT as a synonym for CDIOCEJECT. Implement DIOCLOCK
separately from CDIOCALLOW and CDIOCPREVENT, even though they perform
basically the same function (with a different interface XXX).
 1.80 12-Jan-1996  thorpej Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.
 1.79 07-Jan-1996  thorpej New generic disk framework. Highlights:

- New metrics handling. Metrics are now kept in the new
`struct disk'. Busy time is now stored as a timeval, and
transfer count in bytes.

- Storage for disklabels is now dynamically allocated, so that
the size of the disk structure is not machine-dependent.

- Several new functions for attaching and detaching disks, and
handling metrics calculation.

Old-style instrumentation is still supported in drivers that did it before.
However, old-style instrumentation is being deprecated, and will go away
once the userland utilities are updated for the new framework.

For usage and architectural details, see the forthcoming disk(9) manual
page.
 1.78 07-Dec-1995  thorpej In both cd.c and sd.c:
If the read or write request can fit into a 6-byte cdb, then
use a 6-byte cdb, otherwise use the 10-byte as before.

In sd.c:
In sdattach(), make a note if the device is "ancient" (i.e.
inqbuf.version & SID_ANSII == 0).

Implement sdminphys(): if the device is "ancient", shorten the
transfer so it will fit into a 6-byte cdb.

These changes have eliminated the rejected read/write requests on my
Sun 3/60 with 2 ESDI disks behind an Emulex MD21.
 1.77 11-Nov-1995  mycroft Remove the gratuitous code to check for a disc at boot time.
 1.76 10-Oct-1995  mycroft branches: 1.76.2;
Return EINVAL if something other than a whole number of blocks is requested.
 1.75 26-Sep-1995  thorpej Don't declare Debugger(). It's handled in <sys/systm.h>.
#include <sys/systm.h> where necessary, as suggested by Jonathan Stone.
Fixes PR #1511.
 1.74 12-Aug-1995  mycroft Fix oversight in previous.
 1.73 12-Aug-1995  mycroft minphys() functions really should return void.
 1.72 05-Aug-1995  mycroft Use an intermediate variable to shorten label initialization code.
 1.71 24-Jul-1995  cgd update SCSI minphys routines' definitions to match standard minphys()
definition and usage.
 1.70 04-Jul-1995  mycroft Make each disk and tape driver define its own read and write functions.
Deprecate rawread() and rawwrite() completely. Remove d_strategy from cdevsw to
force the abstraction barrier.
 1.69 26-Jun-1995  cgd make dump stubs consistent
 1.68 03-May-1995  mycroft Make the byte-shifting code consistent.
 1.67 15-Apr-1995  mycroft Don't boundary check I/O to the `raw' partition.
 1.66 01-Apr-1995  mycroft Add a missing unlock.
 1.65 29-Mar-1995  mycroft Revamp the locking mechanism slightly.
 1.64 25-Mar-1995  mycroft Remove the write protect check altogether, and rely on the drive to do it.
 1.63 23-Mar-1995  mycroft Don't bother with DIOCWLABEL.
 1.62 23-Mar-1995  mycroft Check for read-only media in open(), not write().
 1.61 23-Mar-1995  mycroft Revert cdsize() to do nothing.
 1.60 23-Mar-1995  mycroft Rearrange DIOCWDINFO a little; closer to wd.c.
 1.59 23-Mar-1995  mycroft Fix typo.
 1.58 23-Mar-1995  mycroft Allow DIOCWDINFO even if label is not `writable'.
 1.57 07-Mar-1995  mycroft Correct spelling of `fictitious'. Add patterns for optical memory devices.
From Alistair Crooks.
 1.56 30-Jan-1995  mycroft Finish last change.
 1.55 30-Jan-1995  mycroft Some drives don't grok START with LoEj=1, either.
 1.54 30-Jan-1995  mycroft Set the LoadEject bit when issuing a START.
 1.53 30-Jan-1995  mycroft Some devices really do require the START before the PREVENT. This makes no
sense.
 1.52 26-Jan-1995  mycroft Update copyrights.
 1.51 26-Jan-1995  mycroft Redo the `wait for spin up' code. The Mach 3 method clearly doesn't work.
Also, rearrange the first open sequences a bit; SDEV_OPEN is no longer magic.
 1.50 23-Jan-1995  mycroft Do the PREVENT before the START.
 1.49 16-Jan-1995  mycroft Remove unused macros.
 1.48 13-Jan-1995  mycroft Always boundary check I/O.
 1.47 28-Dec-1994  mycroft Numerous changes. Many bugs fixed, better autoconfig, a few new features.
 1.46 16-Dec-1994  mycroft Remove DIOCSBAD handling. It's not actually handled, so why recognize it?
 1.45 14-Dec-1994  mycroft Remove dkbad.h.
 1.44 23-Nov-1994  mycroft There's no point in bothering to unlock what we didn't lock.
 1.43 22-Nov-1994  mycroft Add still more locking.
 1.42 21-Nov-1994  mycroft Replace dev_unit with device_softc in scsi_link. Change argument to foostart()
to void*.
 1.41 20-Nov-1994  mycroft Add some missing locking, and some general cleanup.
 1.40 30-Oct-1994  cgd be more careful with types, also pull in headers where necessary.
 1.39 20-Oct-1994  mycroft First cut at making user-level SCSI commands work. This is untested.
Partly from John Brezak.
 1.38 20-Oct-1994  mycroft Fix minor omission.
 1.37 20-Oct-1994  mycroft openpart -> openmask
 1.36 20-Oct-1994  mycroft Similar changes to sd.c.
 1.35 14-Oct-1994  cgd move arch dependence to archs. clean up a bit. deal with weird MAXPARTITONS.
 1.34 11-Aug-1994  mycroft On probe or open, retry the READ CAPACITY once if the drive reports NOT READY.
 1.33 05-Aug-1994  mycroft Fix spelling of `STEREO'.
 1.32 04-Aug-1994  mycroft Obey the command queue size for the controller.
 1.31 04-Jul-1994  chopps branches: 1.31.2;
change to support other MAXPARTITIONS vals aside from 8
 1.30 29-Jun-1994  cgd New RCS ID's, take two. they're more aesthecially pleasant, and use 'NetBSD'
 1.29 16-Jun-1994  chopps resolve confusion over who owns the buf after calling scsi_scsi_cmd()
 1.28 16-Jun-1994  mycroft b_un.b_addr -> b_data
 1.27 11-May-1994  mycroft Add dummy *dump() routines.
 1.26 09-May-1994  chopps remove union's from sense_data struct, conditionaly define RAW_PART
 1.25 11-Apr-1994  mycroft Fix various types. Remove some outdated flags.
 1.24 11-Apr-1994  mycroft Combine scsi_start_unit() and scsi_stop_unit(), and increase the start
timeout (again).
 1.23 29-Mar-1994  mycroft New SCSI system, based on Julian's more recent work.
 1.22 06-Feb-1994  mycroft Use b_actf, not av_forw.
 1.21 11-Jan-1994  mycroft *strategy functions return void.
 1.20 23-Dec-1993  cgd fix from Nick Cuccia (cuccia@remarque.berkeley.edu), apparently
originating from Gary Grebus <glg@k8lt.ampr.org>: partition size
must be in units of DEV_BSIZE.
 1.19 17-Dec-1993  mycroft Canonicalize all #includes.
 1.18 04-Aug-1993  brezak branches: 1.18.2;
Normalize play_msf changes with 386bsd patch.
 1.17 04-Aug-1993  brezak Add play_msf command and MSF variants. This is SCSI2 stuff and is needed for many flavors of player utilities.
 1.16 01-Aug-1993  mycroft Add RCS identifiers (this time on the correct side of the branch), and
incorporate recent changes in netbsd-0-9 branch.
 1.15 19-Jul-1993  cgd branches: 1.15.2;
patches from allen briggs to fix a minor bug in *attach()
 1.14 27-Jun-1993  andrew ANSIfications.
 1.13 17-Jun-1993  brezak Silence size complaint at probe.
 1.12 16-Jun-1993  deraadt whoops. typo.
 1.11 16-Jun-1993  deraadt fix to intuit the maximum number of scsi units available on a device
driver. this piece at least, should be safe from changing sizeof(dev_t)
 1.10 25-May-1993  deraadt patch00149 by Julian Elischer <julian@jules.dialix.oz.au> & Rodney Grimes.
When an error was encountered, the sd/cd drivers printed blockno&ff0000
rather that blockno.
 1.9 20-May-1993  deraadt First cut at cpu independent disklabels.
There will be niggly little details no doubt..
 1.8 20-May-1993  cgd add rcsids and clean up file headers
 1.7 11-May-1993  cgd fix stupid line transposition (from John Brezak <brezak@osf.org>)
 1.6 04-May-1993  deraadt support for making dev->id_alive be set, this is for iostat to
find disk devices. wee bit of a kludge. sub-device attach()
routines must now return 1 for successful attach(), 0 otherwise.
Other bsd's do this too..
 1.5 20-Apr-1993  mycroft Display more meaningful message on SCSI `unit attention'.
 1.4 12-Apr-1993  deraadt fixed various bugs like cdattach() returning garbage.
 1.3 12-Apr-1993  deraadt new scsi subsystem.
changes also in config/mkioconf.c
i386/isa/wd.c, fd.c, and all scsi drivers.
 1.2 10-Apr-1993  glass fixed to be compliant, subservient, and to take advantage of the newly
hacked config(8)
 1.1 21-Mar-1993  cgd after 0.2.2 "stable" patches applied
 1.15.2.1 31-Jul-1993  cgd give names, err, wmesg's, to my "pain" -- i.e. convert sleep() to tsleep()
 1.18.2.10 16-Feb-1994  mycroft More KNF-like.
 1.18.2.9 16-Feb-1994  mycroft Don't return the toc header in CDIOREADTOCENTRYS.
 1.18.2.8 06-Feb-1994  mycroft Use b_actf, not av_forw.
 1.18.2.7 01-Feb-1994  mycroft Another change from the Mac code...
 1.18.2.6 01-Feb-1994  mycroft Add scsi_stop_unit(), from Mac code. Other minor nits.
 1.18.2.5 29-Nov-1993  mycroft Use dk_establish().
 1.18.2.4 25-Nov-1993  mycroft Don't display extraneous errors if there is no CD present during probe.
 1.18.2.3 25-Nov-1993  mycroft (Hopefully) make cd work. Make cd and sd more similar. Implement variable
block sizes in sd.
 1.18.2.2 24-Nov-1993  mycroft Under construction...
 1.18.2.1 24-Sep-1993  mycroft cd.c, sd.c, st.c: strategy functions return void.
 1.31.2.2 05-Aug-1994  mycroft update from trunk
 1.31.2.1 04-Aug-1994  mycroft update from trunk
 1.76.2.1 04-Feb-1996  gwr Pull in rev 1.78 (Jason's fix to use 6-byte command blocks).
 1.97.6.1 12-Mar-1997  is Merge in changes from Trunk
 1.101.2.4 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.101.2.3 16-Sep-1997  thorpej Update marc-pcmcia branch from trunk.
 1.101.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.101.2.1 27-Aug-1997  thorpej file cd.c was added on branch marc-pcmcia on 1997-08-27 23:33:00 +0000
 1.107.2.1 18-Jan-1999  cgd pull up rev 1.118 from trunk (PR#6093). (is)
 1.124.2.2 24-Jun-1999  perry pullup 1.126->1.127 (bouyer)
 1.124.2.1 08-Apr-1999  bouyer branches: 1.124.2.1.2; 1.124.2.1.4;
Pull up 1.126 from trunk: add SCSI_IGNORE_NOT_READY to the flags for
CDIOCCLOSE, so if the drive tries to spin up after the clause and fails
because there's no media, eject(1) doesn't get ENODEV. Closes port-i386/7345.
 1.124.2.1.4.1 30-Nov-1999  itojun bring in latest KAME (as of 19991130, KAME/NetBSD141) into kame branch
just for reference purposes.
This commit includes 1.4 -> 1.4.1 sync for kame branch.

The branch does not compile at all (due to the lack of ALTQ and some other
source code). Please do not try to modify the branch, this is just for
referenre purposes.

synchronization to latest KAME will take place on HEAD branch soon.
 1.124.2.1.2.1 21-Jun-1999  thorpej Sync w/ -current.
 1.130.2.2 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.130.2.1 21-Dec-1999  wrstuden Initial commit of recent changes to make DEV_BSIZE go away.

Runs on i386, needs work on other arch's. Main kernel routines should be
fine, but a number of the stand programs need help.

cd, fd, ccd, wd, and sd have been updated. sd has been tested with non-512
byte block devices. vnd, raidframe, and lfs need work.

Non 2**n block support is automatic for LKM's and conditional for kernels
on "options NON_PO2_BLOCKS".
 1.131.4.1 15-Nov-1999  fvdl Sync with -current
 1.131.2.7 21-Apr-2001  bouyer Get rid of references to sc_link/scsipi_link in debug or comments.
 1.131.2.6 11-Feb-2001  bouyer Sync with HEAD.
 1.131.2.5 18-Jan-2001  bouyer Sync with head (for UBC+NFS fixes, mostly).
 1.131.2.4 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.131.2.3 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.131.2.2 20-Oct-1999  thorpej Remove an unnecessary comment regarding XS_CTL_NOSLEEP.
 1.131.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.139.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.141.2.3 26-Jun-2002  he Pull up revision 1.150 (via patch, requested by dillo):
Make DVD ioctls use the right size CDB (16 instead of 12). Fixes
PR#12991.
 1.141.2.2 06-May-2001  he Pull up revision 1.148 (requested by fvdl):
Compensate for the fact that after __HAVE_OLD_DISKLABEL,
DISKMINOR(u,p) is no longer contiguous over ``p'', so ranges
cannot be passed to vdevgone().
 1.141.2.1 01-May-2001  he Pull up revisions 1.142-1.143 (requested by fvdl):
Increase the number of BSD disklabel partitions on i386 to 16.
 1.144.2.13 15-Jan-2003  thorpej Sync with HEAD.
 1.144.2.12 19-Dec-2002  thorpej Sync with HEAD.
 1.144.2.11 11-Nov-2002  nathanw Catch up to -current
 1.144.2.10 18-Oct-2002  nathanw Catch up to -current.
 1.144.2.9 17-Sep-2002  nathanw Catch up to -current.
 1.144.2.8 01-Aug-2002  nathanw Catch up to -current.
 1.144.2.7 20-Jun-2002  nathanw Catch up to -current.
 1.144.2.6 08-Jan-2002  nathanw Catch up to -current.
 1.144.2.5 14-Nov-2001  nathanw Catch up to -current.
 1.144.2.4 22-Oct-2001  nathanw Catch up to -current.
 1.144.2.3 21-Sep-2001  nathanw Catch up to -current.
 1.144.2.2 24-Aug-2001  nathanw Catch up with -current.
 1.144.2.1 21-Jun-2001  nathanw Catch up to -current.
 1.151.2.7 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.151.2.6 06-Sep-2002  jdolecek sync kqueue branch with HEAD
 1.151.2.5 23-Jun-2002  jdolecek catch up with -current on kqueue branch
 1.151.2.4 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.151.2.3 13-Sep-2001  thorpej Update the kqueue branch to HEAD.
 1.151.2.2 25-Aug-2001  thorpej Merge Aug 24 -current into the kqueue branch.
 1.151.2.1 03-Aug-2001  lukem update to -current
 1.157.2.2 26-Sep-2001  fvdl * add a VCLONED vnode flag that indicates a vnode representing a cloned
device.
* rename REVOKEALL to REVOKEALIAS, and add a REVOKECLONE flag, to pass
to VOP_REVOKE
* the revoke system call will revoke all aliases, as before, but not the
clones
* vdevgone is called when detaching a device, so make it use REVOKECLONE
to get rid of all clones as well
* clean up all uses of VOP_OPEN wrt. locking.
* add a few VOPS to spec_vnops that need to do something when it's a
clone vnode (access and getattr)
* add a copy of the vnode vattr structure of the original 'master' vnode
to the specinfo of a cloned vnode. could possibly redirect getattr to
the 'master' vnode, but this has issues with revoke
* add a vdev_reassignvp function that disassociates a vnode from its
original device, and reassociates it with the specified dev_t. to be
used by cloning devices only, in case a new minor is allocated.
* change all direct references in drivers to v_devcookie and v_rdev
to vdev_privdata(vp) and vdev_rdev(vp). for diagnostic purposes
when debugging race conditions that still exist wrt. locking and
revoking vnodes.
* make the locking state of a vnode consistent when passed to
d_open and d_close (unlocked). locked would be better, but has
some deadlock issues
 1.157.2.1 07-Sep-2001  thorpej Commit my "devvp" changes to the thorpej-devvp branch. This
replaces the use of dev_t in most places with a struct vnode *.

This will form the basic infrastructure for real cloning device
support (besides being architecurally cleaner -- it'll be good
to get away from using numbers to represent objects).
 1.162.2.5 31-Aug-2002  gehenna catch up with -current.
 1.162.2.4 29-Aug-2002  gehenna catch up with -current.
 1.162.2.3 31-May-2002  gehenna fix conflicts.
 1.162.2.2 30-May-2002  gehenna Catch up with -current.
 1.162.2.1 16-May-2002  gehenna Add the block/character device switches.
Replace the direct-access to devsw table with calling devsw API.
 1.185.2.14 11-Dec-2005  christos Sync with head.
 1.185.2.13 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.185.2.12 01-Apr-2005  skrll Sync with HEAD.
 1.185.2.11 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.185.2.10 04-Feb-2005  skrll Sync with HEAD.
 1.185.2.9 18-Dec-2004  skrll Sync with HEAD.
 1.185.2.8 02-Nov-2004  skrll Sync with HEAD.
 1.185.2.7 19-Oct-2004  skrll Sync with HEAD
 1.185.2.6 21-Sep-2004  skrll Fix the sync with head I botched.
 1.185.2.5 18-Sep-2004  skrll Sync with HEAD.
 1.185.2.4 03-Sep-2004  skrll Sync with HEAD
 1.185.2.3 25-Aug-2004  skrll Sync with HEAD.
 1.185.2.2 03-Aug-2004  skrll Sync with HEAD
 1.185.2.1 02-Jul-2003  darrenr Apply the aborted ktrace-lwp changes to a specific branch. This is just for
others to review, I'm concerned that patch fuziness may have resulted in some
errant code being generated but I'll look at that later by comparing the diff
from the base to the branch with the file I attempt to apply to it. This will,
at the very least, put the changes in a better context for others to review
them and attempt to tinker with removing passing of 'struct lwp' through
the kernel.
 1.200.2.2 11-Sep-2004  he Pull up revisions 1.203-1.205 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.200.2.1 27-Apr-2004  jdc Pull up revision 1.201 (requested by pk in ticket #186)

Some older devices do not understand the `disable block descriptor' bit in
the mode sense request. So fall back on mode sense data including a block
descriptor section.

See also sd.c rev. 1.214. Again: should we bother trying DBD at all?
 1.212.4.2 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.212.4.1 12-Feb-2005  yamt sync with head.
 1.212.2.1 29-Apr-2005  kent sync with -current
 1.220.2.2 27-Apr-2006  tron Pull up following revision(s) (requested by pavel in ticket #1290):
sys/dev/scsipi/cd.c: revision 1.224
Always allow open() on the raw partition to succeed, even if any of the
SCSI command in cdopen() fails. Fix PR kern/30288 by Pavel Cahyna.
 1.220.2.1 06-Apr-2005  tron Pull up revision 1.221 (requested by yamt in ticket #112):
introduce a function to drain bufq and use it where appropriate.
 1.223.2.7 21-Jan-2008  yamt sync with head
 1.223.2.6 07-Dec-2007  yamt sync with head
 1.223.2.5 27-Oct-2007  yamt sync with head.
 1.223.2.4 03-Sep-2007  yamt sync with head.
 1.223.2.3 26-Feb-2007  yamt sync with head.
 1.223.2.2 30-Dec-2006  yamt sync with head.
 1.223.2.1 21-Jun-2006  yamt sync with head.
 1.234.2.2 18-Feb-2006  yamt sync with head.
 1.234.2.1 15-Jan-2006  yamt sync with head.
 1.237.2.1 09-Sep-2006  rpaulo sync with head
 1.238.2.1 22-Apr-2006  simonb Sync with head.
 1.240.6.1 31-Mar-2006  tron Merge 2006-03-31 NetBSD-current into the "peter-altq" branch.
 1.240.4.1 19-Apr-2006  elad sync with head.
 1.240.2.4 14-Sep-2006  yamt sync with head.
 1.240.2.3 03-Sep-2006  yamt sync with head.
 1.240.2.2 11-Aug-2006  yamt sync with head
 1.240.2.1 01-Apr-2006  yamt sync with head.
 1.242.6.4 11-Sep-2006  tron Pull up following revision(s) (requested by reinoud in ticket #147):
sys/dev/scsipi/cd.c: revision 1.250
Oeps! I overlooked the size argument of the SCSI call. It ought to be equal
to the number of bytes we are reading: READ_DISCINFO_BIGSIZE
sorry.
 1.242.6.3 11-Sep-2006  tron Pull up following revision(s) (requested by reinoud in ticket #147):
sys/dev/scsipi/cd.c: revision 1.249
The size of struct scsipi_read_discinfo_data is not even due to the data[1]
pseudo array in the structure. The scsi command issued used
sizeof(scsipi_read_discinfo_data) but included thus the last byte that
wasn't going to be inspected anyway. Using the constant
READ_DISCINFO_BIGSIZE fixes the bug.
Even though SCSI adapters *should* accept odd lengths, the Sun U10
(sparc64's) crashes in bus_space_read_multi_stream_2(). That explains the
crash.
NOTE: somewhere in NetBSD/sparc64's atapibus support there is the
assumption on the even size; this needs to be fixed! (Hi Sparc64 portmaster
-) )
 1.242.6.2 08-Sep-2006  rpaulo Pull up following revision(s) (requested by reinoud in ticket #124):
sys/dev/scsipi/cd.c: revision 1.245
sys/dev/scsipi/cd.c: revision 1.246
sys/dev/scsipi/cd.c: revision 1.247
Fix fencing bug on CD disk size; the comment was wrong, its the *length* of
the device and since we count from zero its equal to the number of sectors
and thus one higher than the last sector.
This fixes the read/write problems on the (raw) device where the last
sector was not readable/writable.
Remove two spurious empty lines in functions
Fix panic on reading/writing to a (raw) CD device and encountering an
error. It now correctly sets bp->b_resid to the full size of the buffer.
The failed SCSI read/write command allways contains the complete buffer.
When encountering a read (or write) error, the scsipi stack sets the
xs->resid to zero since the command has been accepted by the drive. The
scsi command set does not allow a partial read or write to be performed and
will signal a success or an error.
 1.242.6.1 12-Aug-2006  riz Pull up following revision(s) (requested by reinoud in ticket #12):
sys/fs/udf/udf_vfsops.c: revision 1.9
sys/dev/scsipi/cd.c: revision 1.243
Fix panic on badly or curruptly formatted discs due to an oddity in the
error handling chain.
If NetBSD 4.0 is allready branched at this time this ought to be pulled up.
Fix incorrect `last possible lba' reporting, remove some empty lines and
add mmc classification for some still rare HD DVD device types.
 1.248.2.3 01-Feb-2007  ad Sync with head.
 1.248.2.2 12-Jan-2007  ad Sync with head.
 1.248.2.1 18-Nov-2006  ad Sync with head.
 1.250.2.2 10-Dec-2006  yamt sync with head.
 1.250.2.1 22-Oct-2006  yamt sync with head
 1.260.2.2 01-Feb-2008  riz Pull up following revision(s) (requested by reinoud in ticket #1004):
sys/dev/scsipi/cd.c: revision 1.270
sys/sys/cdio.h: revision 1.29
sys/fs/udf/udf_subr.c: revision 1.42
Pullup fixes from UDF write development project. This patch fixes a
conceptional bug in mmc_discinfo, improves DVD+R feature detection and
prevents corruption of mmc_trackinfo readout on some devices that return
short trackinfo structures.
Pullup fixes from UDF RW support project. This commit fixes the
mmc_discinfo conceptional bug and also vastly improves VAT searching.
 1.260.2.1 15-Jan-2007  pavel branches: 1.260.2.1.2;
Pull up following revision(s) (requested by martin in ticket #371):
sys/dev/scsipi/cd.c: revision 1.261
sys/dev/ic/wdc.c: revision 1.244
If __NO_STRICT_ALIGNMENT is not defined, take special care to do PIO
transfers with proper alignement - this removes alignement restrictions
of the buffer pointer passed.
Thanks to bouyer, reinoud, jdc for helping with the code, and special
thanks to Dave Huang for testing.
The previous fix for PR 34202 was wrong (some sizeof used the union, not
the command block) - but instead of fixing this, we can now revert it
completley: the real fix has been applied to ../ic/wdc.c in rev. 1.244,
removing the alignement constraints.
 1.260.2.1.2.1 03-Jun-2008  skrll Sync with netbsd-4.
 1.261.2.1 12-Mar-2007  rmind Sync with HEAD.
 1.262.4.1 11-Jul-2007  mjf Sync with head.
 1.262.2.9 09-Oct-2007  ad Sync with head.
 1.262.2.8 24-Aug-2007  ad Sync with buffer cache locking changes. See buf.h/vfs_bio.c for details.
Some minor portions are incomplete and needs to be verified as a whole.
 1.262.2.7 20-Aug-2007  ad Sync with head.
 1.262.2.6 20-Aug-2007  ad Sync with HEAD.
 1.262.2.5 20-Aug-2007  ad - Alter disk attach/detach to fix a panic when closing a vnd device.
- Sync with HEAD.
 1.262.2.4 19-Aug-2007  ad - Back out the biodone() changes.
- Eliminate B_ERROR (from HEAD).
 1.262.2.3 15-Jul-2007  ad Sync with head.
 1.262.2.2 01-Jul-2007  ad Adapt to callout API change.
 1.262.2.1 13-May-2007  ad - Pass the error number and residual count to biodone(), and let it handle
setting error indicators. Prepare to eliminate B_ERROR.
- Add a flag argument to brelse() to be set into the buf's flags, instead
of doing it directly. Typically used to set B_INVAL.
- Add a "struct cpu_info *" argument to kthread_create(), to be used to
create bound threads. Change "bool mpsafe" to "int flags".
- Allow exit of LWPs in the IDL state when (l != curlwp).
- More locking fixes & conversion to the new API.
 1.265.2.1 15-Aug-2007  skrll Sync with HEAD.
 1.267.4.6 08-Dec-2007  jmcneill Rename pnp(9) -> pmf(9), as requested by many.
 1.267.4.5 03-Dec-2007  joerg Sync with HEAD.
 1.267.4.4 01-Dec-2007  jmcneill Since I'm not convinced that cd_suspend does anything useful other than
to complain that a disc is not present in my drive, nuke it.
 1.267.4.3 06-Nov-2007  joerg Flush caches on suspend as discussed with reinoud@
 1.267.4.2 26-Oct-2007  joerg Sync with HEAD.

Follow the merge of pmap.c on i386 and amd64 and move
pmap_init_tmp_pgtbl into arch/x86/x86/pmap.c. Modify the ACPI wakeup
code to restore CR4 before jumping back into kernel space as the large
page option might cover that.
 1.267.4.1 04-Aug-2007  jmcneill Sync with HEAD.
 1.268.6.2 04-Aug-2007  rumble Be sure not to make requests over MAXPHYS when employing bounce buffers.
It was previously possible to round up one sector length if the start block
were not properly aligned.

Resolves PR kern/36716. This may also fix the following reported issues
on port-sgimips:
http://mail-index.netbsd.org/port-sgimips/2005/09/12/0000.html
http://mail-index.netbsd.org/port-sgimips/2005/09/14/0000.html

While here, be sure to put back allocated bufs, which previously appear
to have been leaked. Further, remove the dead write bounce code which never
got executed anyway.

No complaints on tech-kern.
 1.268.6.1 04-Aug-2007  rumble file cd.c was added on branch matt-mips64 on 2007-08-04 02:59:00 +0000
 1.268.4.1 14-Oct-2007  yamt sync with head.
 1.268.2.2 09-Jan-2008  matt sync with HEAD
 1.268.2.1 06-Nov-2007  matt sync with HEAD
 1.269.4.3 18-Feb-2008  mjf Sync with HEAD.
 1.269.4.2 27-Dec-2007  mjf Sync with HEAD.
 1.269.4.1 08-Dec-2007  mjf Sync with HEAD.
 1.270.4.1 11-Dec-2007  yamt sync with head.
 1.270.2.2 26-Dec-2007  ad Sync with head.
 1.270.2.1 04-Dec-2007  ad Pull the vmlocking changes into a new branch.
 1.271.2.1 02-Jan-2008  bouyer Sync with HEAD
 1.272.6.7 17-Jan-2009  mjf Sync with HEAD.
 1.272.6.6 28-Sep-2008  mjf Sync with HEAD.
 1.272.6.5 29-Jun-2008  mjf Sync with HEAD.
 1.272.6.4 02-Jun-2008  mjf Sync with HEAD.
 1.272.6.3 06-Apr-2008  mjf - after some discussion with agc@ i agreed it would be a good idea to move
device_unregister_* to device_deregister_* to be more like the pmf(9)
functions, especially since a lot of the time the function calls are next
to each other.

- add device_register_name() support for dk(4).
 1.272.6.2 03-Apr-2008  mjf Sync with HEAD.
 1.272.6.1 29-Mar-2008  mjf - etc/devfsd.conf: Add some rules to give nodes like /dev/tty and
/dev/null better default modes, i.e. 0666.

- sbin/init: Run devfsd -s before going to multiuser.

- sys/arch: Provide arm32, i386, sparc with a mem_init() function to request
device nodes for /dev/null, /dev/zero, etc.

- sys/dev: Convert rnd, wd, agp, raid, cd, sd, wsdisplay, wskbd, wsmouse,
wsmux, tty, bpf, swap to devfs New World Order.

- sys/fs/devfs: Make the visibility attribute of device nodes configurable.
Also provide a function to mount a devfs on boot.

- sys/kern: Add a new boot flag, -n. This disables devfs support. Unless
the -n flag is specified the kernel will mount a devfs file
system on boot.
 1.273.4.6 11-Aug-2010  yamt sync with head.
 1.273.4.5 11-Mar-2010  yamt sync with head
 1.273.4.4 19-Aug-2009  yamt sync with head.
 1.273.4.3 20-Jun-2009  yamt sync with head
 1.273.4.2 04-May-2009  yamt sync with head.
 1.273.4.1 16-May-2008  yamt sync with head.
 1.273.2.2 17-Jun-2008  yamt sync with head.
 1.273.2.1 18-May-2008  yamt sync with head.
 1.278.2.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.278.2.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.281.2.1 18-Jun-2008  simonb Sync with head.
 1.282.2.1 19-Oct-2008  haad Sync with HEAD.
 1.283.4.3 20-Nov-2010  riz Pull up following revision(s) (requested by hubertf in ticket #1386):
sys/dev/raidframe/rf_netbsdkintf.c: revision 1.258
sys/dev/pad/pad.c: revision 1.11
sys/dev/scsipi/cd.c: revision 1.288
sys/dev/isa/pcppi.c: revision 1.33
sys/kern/subr_autoconf.c: revision 1.169
'boot -z' bogons
 1.283.4.2 08-Apr-2009  snj Pull up following revision(s) (requested by reinoud in ticket #633):
sys/dev/scsipi/cd.c: revision 1.291
sys/dev/scsipi/scsipi_cd.h: revision 1.21
Implement MMC_TRACKINFO_DATA and MMC_TRACKINFO_AUDIO detection for cdrom and
dvdrom devices. These two compatibility codes were forgotten when those were
introduced.
 1.283.4.1 24-Mar-2009  snj Pull up following revision(s) (requested by reinoud in ticket #586):
sys/dev/scsipi/cd.c: revision 1.289
Fix long standing problem with CD/DVD writing with `dd'. If done this way the
device will remain in a wait-for-more-stuff-to-be-written state delaying other
SCSI calls, esp. some read-size or read-layout calls, until its told there is
no more comming.
To fix this, on last close explicitly flush the caches releasing the device
from this mode and allowing other SCSI commands to reach it.
This fixes PR 40528
 1.283.2.3 28-Apr-2009  skrll Sync with HEAD.
 1.283.2.2 03-Mar-2009  skrll Sync with HEAD.
 1.283.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.287.2.2 23-Jul-2009  jym Sync with HEAD.
 1.287.2.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.297.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.298.2.2 31-May-2011  rmind sync with head
 1.298.2.1 30-May-2010  rmind sync with head
 1.302.2.1 06-Jun-2011  jruoho Sync with HEAD.
 1.303.4.3 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.303.4.2 23-May-2012  yamt sync with head.
 1.303.4.1 17-Apr-2012  yamt sync with head
 1.304.2.4 02-Jun-2012  mrg sync to latest -current.
 1.304.2.3 29-Apr-2012  mrg sync to latest -current.
 1.304.2.2 04-Mar-2012  mrg sync to latest -current.
 1.304.2.1 18-Feb-2012  mrg merge to -current.
 1.305.2.1 23-Apr-2012  riz Pull up following revision(s) (requested by bouyer in ticket #192):
sys/dev/scsipi/cd.c: revision 1.307
sys/dev/scsipi/scsiconf.c: revision 1.266
sys/dev/scsipi/sd.c: revision 1.298
sys/dev/scsipi/st_scsi.c: revision 1.35
sys/dev/scsipi/atapiconf.c: revision 1.85
sys/dev/scsipi/scsipiconf.h: revision 1.120
sys/dev/usb/umass_scsipi.c: revision 1.44
sys/dev/scsipi/scsiconf.h: revision 1.57
sys/dev/scsipi/st_atapi.c: revision 1.29
sys/dev/scsipi/scsipi_base.c: revision 1.158
sys/dev/scsipi/st.c: revision 1.221
sys/dev/scsipi/scsipi_ioctl.c: revision 1.67
Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.
This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.
In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.
Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.309.2.4 03-Dec-2017  jdolecek update from HEAD
 1.309.2.3 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.309.2.2 23-Jun-2013  tls resync from head
 1.309.2.1 12-Sep-2012  tls Initial snapshot of work to eliminate 64K MAXPHYS. Basically works for
physio (I/O to raw devices); needs more doing to get it going with the
filesystems, but it shouldn't damage data.

All work's been done on amd64 so far. Not hard to add support to other
ports. If others want to pitch in, one very helpful thing would be to
sort out when and how IDE disks can do 128K or larger transfers, and
adjust the various PCI IDE (or at least ahcisata) drivers and wd.c
accordingly -- it would make testing much easier. Another very helpful
thing would be to implement a smart minphys() for RAIDframe along the
lines detailed in the MAXPHYS-NOTES file.
 1.311.2.2 18-May-2014  rmind sync with head
 1.311.2.1 28-Aug-2013  rmind sync with head
 1.318.2.2 10-Aug-2014  tls Rebase.
 1.318.2.1 07-Apr-2014  tls Be a little more clear and consistent about harvesting entropy from devices:

1) deprecate RND_FLAG_NO_ESTIMATE

2) define RND_FLAG_COLLECT_TIME, RND_FLAG_COLLECT_VALUE

3) define RND_FLAG_ESTIMATE_TIME, RND_FLAG_ESTIMATE_VALUE

4) define RND_FLAG_DEFAULT: RND_FLAG_COLLECT_TIME|
RND_FLAG_COLLECT_VALUE|RND_FLAG_ESTIMATE_TIME

5) Make entropy harvesting from environmental sensors a little more generic
and remove it from individual sensor drivers.

6) Remove individual open-coded delta-estimators for values from a few
places in the tree (uvm, environmental drivers).

7) 0 -> RND_FLAG_DEFAULT, actually gather entropy from various drivers
that had stubbed out code, other minor cleanups.
 1.325.2.6 28-Aug-2017  skrll Sync with HEAD
 1.325.2.5 05-Feb-2017  skrll Sync with HEAD
 1.325.2.4 05-Dec-2016  skrll Sync with HEAD
 1.325.2.3 29-May-2016  skrll Sync with HEAD
 1.325.2.2 06-Jun-2015  skrll Sync with HEAD
 1.325.2.1 06-Apr-2015  skrll Sync with HEAD
 1.331.2.5 26-Apr-2017  pgoyette Sync with HEAD
 1.331.2.4 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.331.2.3 26-Jul-2016  pgoyette Rename LOCALCOUNT_INITIALIZER to DEVSW_MODULE_INIT. This better describes
what we're doing, and why.
 1.331.2.2 19-Jul-2016  pgoyette Instead of repeatedly typing the conditional initialization of the
.d_localcount members in the various {b,c}devsw, define an initializer
macro and use it. This also removes the need for defining new symbols
for each 'struct localcount'.

As suggested by riastradh@
 1.331.2.1 18-Jul-2016  pgoyette Rump drivers are always installed via devsw_attach() so we need to
always allocate a 'struct localcount' for these drivers whenever they
are built as modules.
 1.339.2.1 21-Apr-2017  bouyer Sync with HEAD
 1.340.6.3 11-Feb-2021  martin Pull up following revision(s) (requested by ryoon in ticket #1652):

sys/dev/scsipi/cd.c: revision 1.350
sys/dev/scsipi/sd.c: revision 1.331

PR 55986: Ryo Onodera: DK_BUSY must have mask as second argument. Make cd.c
consistent by also using __BIT()
 1.340.6.2 29-Mar-2020  martin Pull up following revision(s) (requested by mlelstv in ticket #1527):

sys/dev/scsipi/cd.c: revision 1.343
sys/kern/subr_disk.c: revision 1.130

Avoid division by zero if label isn't valid.
Allow open of RAWPART even when no medium is loaded.
Keep errors silent if no medium is loaded.
Fixes PR kern/55104
 1.340.6.1 21-Jun-2017  snj Pull up following revision(s) (requested by mlelstv in ticket #53):
sys/dev/scsipi/atapiconf.c: revision 1.91
sys/dev/scsipi/cd.c: revision 1.341
sys/dev/scsipi/scsi_base.c: revision 1.92
sys/dev/scsipi/scsiconf.c: revision 1.280
sys/dev/scsipi/scsipi_base.c: revisions 1.176, 1.177
sys/dev/scsipi/sd.c: revision 1.325
sys/dev/scsipi/ss.c: revision 1.89
sys/dev/scsipi/st.c: revision 1.231
The atapibus detach path did hold the channel mutex while calling into autoconf,
which would trigger a panic when unplugging a USB ATAPI CDROM.
Align detach code for scsibus and atapibus to fix this.
Also avoid races when detaching devices by replacing callout_stop with
callout_halt.
--
pass config_detach error to caller.
 1.340.4.2 29-Apr-2017  pgoyette Remove more unnecessary #include for sys/localcount.h
 1.340.4.1 27-Apr-2017  pgoyette Restore all work from the former pgoyette-localcount branch (which is
now abandoned doe to cvs merge botch).

The branch now builds, and installs via anita. There are still some
problems (cgd is non-functional and all atf tests time-out) but they
will get resolved soon.
 1.341.6.3 21-Apr-2020  martin Sync with HEAD
 1.341.6.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.341.6.1 10-Jun-2019  christos Sync with HEAD
 1.341.4.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.342.4.3 01-Feb-2022  martin Pull up following revision(s) (requested by reinoud in ticket #1421):

sys/dev/scsipi/cd.c: revision 1.351

Limit buffer size for device capabilities requests as a work-around for PR
kern/56109.
 1.342.4.2 11-Feb-2021  martin Pull up following revision(s) (requested by ryoon in ticket #1203):

sys/dev/scsipi/cd.c: revision 1.350
sys/dev/scsipi/sd.c: revision 1.331

PR 55986: Ryo Onodera: DK_BUSY must have mask as second argument. Make cd.c
consistent by also using __BIT()
 1.342.4.1 02-Apr-2020  martin Pull up following revision(s) (requested by mlelstv in ticket #814):

sys/dev/scsipi/cd.c: revision 1.343
sys/kern/subr_disk.c: revision 1.130

Avoid division by zero if label isn't valid.

Allow open of RAWPART even when no medium is loaded.
Keep errors silent if no medium is loaded.

Fixes PR kern/55104
 1.343.2.1 20-Apr-2020  bouyer Sync with HEAD
 1.349.2.1 03-Apr-2021  thorpej Sync with HEAD.
 1.350.2.1 17-Apr-2021  thorpej Sync with HEAD.
 1.355.2.1 02-Aug-2025  perseant Sync with HEAD
 1.28 08-Sep-2003  mycroft Eliminate the separate ATAPI and SCSI attachments for "cd".
 1.27 08-Sep-2003  mycroft Use the const shaker.
 1.26 07-Sep-2003  mycroft Set PQUIRK_ONLYBIG in the wdc-atapi frontend, obviating the need to ever test
the "bus type" for this.

Merge all the code in the SCSI and ATAPI backends for "cd" devices. All of
the mode page handling and whatnot is general to SCSI MMC devices, and should
never have been separated to begin with. This fixes a variety of problems,
and adds load/unload support for SCSI-attached devices.
 1.25 05-Sep-2003  mycroft Use SMS_DBD in a several places to avoid even thinking about block descriptors.
 1.24 10-Jul-2003  martin If a CD drive reports != 2048 byte block size, try to change it into
2048 byte mode. Fixes PR kern/22090.
Reviewed by Manuel Bouyer.
 1.23 02-Oct-2002  thorpej branches: 1.23.6;
Add trailing ; to CFATTACH_DECL.
 1.22 30-Sep-2002  thorpej Use CFATTACH_DECL().
 1.21 27-Sep-2002  thorpej Declare all cfattach structures const.
 1.20 23-Apr-2002  bouyer More copyright fixes, pointed out by Thomas. Thanks !
 1.19 09-Jan-2002  drochner -don't apply scsipi_strvis() twice, it tries to octal-print the string
terminating zero and uninitialized dada following
-use use the "vendor" field for the name which is used in the default
disklabel, the "product" field is meaningless or not existing for ATAPI
(serial number)
 1.18 15-Nov-2001  lukem don't need <sys/types.h> when including <sys/param.h>
 1.17 13-Nov-2001  lukem add RCSIDs
 1.16 18-Jul-2001  thorpej bzero -> memset
 1.15 14-May-2001  bouyer branches: 1.15.2;
Use SCSI/ATAPI common definition for MODE_{SELECT,SENSE}{,_BIG}. Define
functions to send theses commands in scsipi_base.c and use them instead
of ad-hoc commands setups.
 1.14 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.13 09-Jun-2000  enami branches: 1.13.4;
Prevent a process being swapped out during I/O if the data buffer is
allocated on stack. This potential problem is noticed by Noriyuki Soda
and the idea and sample code to fix is given by Jason R. Thorpe.
 1.12 29-Mar-2000  simonb branches: 1.12.2;
Don't need to include <sys/conf.h> here.
 1.11 23-Sep-1999  enami branches: 1.11.2;
Allow to detach wdc, atapibus, wd and cd.
 1.10 31-Aug-1998  cgd kill the last remnants of __BROKEN_INDIRECT_CONFIG. (only the pica port
used it, and it's non-working and apparently slated for replacement.)
 1.9 05-Aug-1998  drochner Improve generation of default disklabels:
-store printable product ID in cd's and sd's softc, use it as "typename"
-for this, add a "destination buffer length" argument to scsipi_strvis()
-return ATAPI device type for ATAPI devices
 1.8 13-Jul-1998  hpeyerl Add support for ATA CD changer devices like the NEC CDR-251.
Evidence of managerial coding removed by Victor T. Cleaner (thorpej)
 1.7 15-Jan-1998  cgd add support for an ATAPI attachment for 'sd'.
fix 'cd' driver's NCD_SCSI bogosity (was using testing wrong macro!)
clean up in various ways:
* make common atapi_mode_{sense,select}() functions.
* put ATAPI data structures in more sensible headers, split up by
device type.
* include headers a bit more carefully.
* pass flags to attachment-specific cd functions, and use them.
* get rid of SCSI bits in scsipi_base.h's scsipi_make_xs(), move
them into the correct place in scsi_base.c.
* fix minor typo in struct name in scsipiconf.h (which was apparently
never used except in a #define later in the same file).
* use __attribute__ to force 4-byte alignment for xs command store,
so that architectures trying to bus_space_write_multi_N() (where
N > 1) that data to a controller won't lose.
* clean up a few comments in typos, and make a few #defines easier to
understand/maintain.
* rename cd_link.h to cdvar.h (via repository copy). This is exactly
what a 'var' file is supposed to be.
 1.6 18-Oct-1997  thorpej Implement two macros, scsipi_command() and scsipi_command_direct(), and
use them to hide the structure of the function pointers we jump through
to issue a command.
 1.5 13-Oct-1997  explorer o Make usage of /dev/random dependant on
pseudo-device rnd # /dev/random and in-kernel generator
in config files.

o Add declaration to all architectures.

o Clean up copyright message in rnd.c, rnd.h, and rndpool.c to include
that this code is derived in part from Ted Tyso's linux code.
 1.4 10-Oct-1997  explorer Add hooks to insert timing info into the random system
 1.3 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.2 27-Aug-1997  bouyer branches: 1.2.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.1 01-Jul-1997  bouyer branches: 1.1.2;
file atapicd.c was initially added on branch bouyer-scsipi.
 1.1.2.7 04-Jul-1997  bouyer the "NEC CD-ROM DRIVE:260" seems definitively brocken, don't
keep the firmware revision number in the inquiry_match entry.
 1.1.2.6 01-Jul-1997  thorpej Remove extra newline printf.
 1.1.2.5 01-Jul-1997  thorpej "cd" instances attach to "scsibus" and "atapibus", so name the cfattach's
in a way what reflects that.
 1.1.2.4 01-Jul-1997  thorpej Use a naming convention consistent with all other drivers in the source
tree that use multiple attachments... i.e. cd_scsibus_* and cd_atapibus_*.
 1.1.2.3 01-Jul-1997  thorpej Use an operations vector set up at attach time rather than checking
for device type each time, for setvol/getvol/setchan operations.

Also, some KNF.
 1.1.2.2 01-Jul-1997  bouyer Get this compile on the i386.
 1.1.2.1 01-Jul-1997  bouyer New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
scsibus at aha
sd at scsibus
cd at scsibus
atapibus at wdc
cd at atapibus
cd has bus-specific config and function front-end in scsicd.c and atapicd.c
The call to theses functions from cd.c is conditionned to
NSCSICD and NATAPICD (all defined in cd.h by config).
 1.2.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.1 27-Aug-1997  thorpej file cd_atapi.c was added on branch marc-pcmcia on 1997-08-27 23:33:01 +0000
 1.11.2.3 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.11.2.2 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.11.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.12.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.13.4.8 18-Oct-2002  nathanw Catch up to -current.
 1.13.4.7 20-Jun-2002  nathanw Catch up to -current.
 1.13.4.6 28-Feb-2002  nathanw Catch up to -current.
 1.13.4.5 11-Jan-2002  nathanw More catchup.
 1.13.4.4 08-Jan-2002  nathanw Catch up to -current.
 1.13.4.3 14-Nov-2001  nathanw Catch up to -current.
 1.13.4.2 24-Aug-2001  nathanw Catch up with -current.
 1.13.4.1 21-Jun-2001  nathanw Catch up to -current.
 1.15.2.4 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.15.2.3 23-Jun-2002  jdolecek catch up with -current on kqueue branch
 1.15.2.2 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.15.2.1 03-Aug-2001  lukem update to -current
 1.23.6.1 03-Aug-2004  skrll Sync with HEAD
 1.5 15-Jan-1998  cgd add support for an ATAPI attachment for 'sd'.
fix 'cd' driver's NCD_SCSI bogosity (was using testing wrong macro!)
clean up in various ways:
* make common atapi_mode_{sense,select}() functions.
* put ATAPI data structures in more sensible headers, split up by
device type.
* include headers a bit more carefully.
* pass flags to attachment-specific cd functions, and use them.
* get rid of SCSI bits in scsipi_base.h's scsipi_make_xs(), move
them into the correct place in scsi_base.c.
* fix minor typo in struct name in scsipiconf.h (which was apparently
never used except in a #define later in the same file).
* use __attribute__ to force 4-byte alignment for xs command store,
so that architectures trying to bus_space_write_multi_N() (where
N > 1) that data to a controller won't lose.
* clean up a few comments in typos, and make a few #defines easier to
understand/maintain.
* rename cd_link.h to cdvar.h (via repository copy). This is exactly
what a 'var' file is supposed to be.
 1.4 13-Oct-1997  explorer o Make usage of /dev/random dependant on
pseudo-device rnd # /dev/random and in-kernel generator
in config files.

o Add declaration to all architectures.

o Clean up copyright message in rnd.c, rnd.h, and rndpool.c to include
that this code is derived in part from Ted Tyso's linux code.
 1.3 10-Oct-1997  explorer Add hooks to insert timing info into the random system
 1.2 27-Aug-1997  bouyer branches: 1.2.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.1 01-Jul-1997  bouyer branches: 1.1.2;
file cd_link.h was initially added on branch bouyer-scsipi.
 1.1.2.2 01-Jul-1997  thorpej Use an operations vector set up at attach time rather than checking
for device type each time, for setvol/getvol/setchan operations.

Also, some KNF.
 1.1.2.1 01-Jul-1997  bouyer New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
scsibus at aha
sd at scsibus
cd at scsibus
atapibus at wdc
cd at atapibus
cd has bus-specific config and function front-end in scsicd.c and atapicd.c
The call to theses functions from cd.c is conditionned to
NSCSICD and NATAPICD (all defined in cd.h by config).
 1.2.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.1 27-Aug-1997  thorpej file cd_link.h was added on branch marc-pcmcia on 1997-08-27 23:33:02 +0000
 1.32 08-Sep-2003  mycroft Eliminate the separate ATAPI and SCSI attachments for "cd".
 1.31 08-Sep-2003  mycroft Use the const shaker.
 1.30 07-Sep-2003  mycroft Set PQUIRK_ONLYBIG in the wdc-atapi frontend, obviating the need to ever test
the "bus type" for this.

Merge all the code in the SCSI and ATAPI backends for "cd" devices. All of
the mode page handling and whatnot is general to SCSI MMC devices, and should
never have been separated to begin with. This fixes a variety of problems,
and adds load/unload support for SCSI-attached devices.
 1.29 05-Sep-2003  mycroft Use SMS_DBD in a several places to avoid even thinking about block descriptors.
 1.28 10-Jul-2003  martin If a CD drive reports != 2048 byte block size, try to change it into
2048 byte mode. Fixes PR kern/22090.
Reviewed by Manuel Bouyer.
 1.27 03-Nov-2002  jdc branches: 1.27.6;
Apply patch from Takeshi Nakayama on current-users which fixes problems
when trying to play audio CD's on some drives. The original message was :

> In my analysis, rev 1.19 of cd_scsi.c used cd_scsibus_set_mode()
> instead of scsipi_mode_select(). cd_scsibus_set_mode() had the code
> which clear a data.header.data_length, but scsipi_mode_select()
> does not have. So we need `data.header.data_length = 0;' before
> calling scsipi_mode_select().

Fixes PR 7299 and probably PR 15155 and PR 16883 too.
 1.26 02-Oct-2002  thorpej Add trailing ; to CFATTACH_DECL.
 1.25 30-Sep-2002  thorpej Use CFATTACH_DECL().
 1.24 27-Sep-2002  thorpej Declare all cfattach structures const.
 1.23 15-Nov-2001  lukem branches: 1.23.10;
don't need <sys/types.h> when including <sys/param.h>
 1.22 13-Nov-2001  lukem add RCSIDs
 1.21 02-Sep-2001  thorpej Use the AUDIO_PAGE definition common to SCSI and ATAPI.
 1.20 14-May-2001  bouyer branches: 1.20.2;
Use SCSI/ATAPI common definition for MODE_{SELECT,SENSE}{,_BIG}. Define
functions to send theses commands in scsipi_base.c and use them instead
of ad-hoc commands setups.
 1.19 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.18 09-Jun-2000  enami branches: 1.18.4;
Prevent a process being swapped out during I/O if the data buffer is
allocated on stack. This potential problem is noticed by Noriyuki Soda
and the idea and sample code to fix is given by Jason R. Thorpe.
 1.17 29-Mar-2000  simonb branches: 1.17.2;
Don't need to include <sys/conf.h> here.
 1.16 30-Sep-1999  thorpej branches: 1.16.2;
Cleanup the scsipi_xfer flags:
- `flags' is now gone, replaced with `xs_control' and `xs_status'.
- Massive cleanup of the control flags. Now we explicitly say that
a job is to complete asynchronously, rather than relying on side-effects,
and use a new flag to now that device discovery is being performed.
- Do SCSI device discovery interrupt-driven.
 1.15 23-Sep-1999  enami Allow to detach wdc, atapibus, wd and cd.
 1.14 31-Aug-1998  cgd kill the last remnants of __BROKEN_INDIRECT_CONFIG. (only the pica port
used it, and it's non-working and apparently slated for replacement.)
 1.13 17-Aug-1998  mycroft Assign my copyrights to TNF.
 1.12 05-Aug-1998  drochner Improve generation of default disklabels:
-store printable product ID in cd's and sd's softc, use it as "typename"
-for this, add a "destination buffer length" argument to scsipi_strvis()
-return ATAPI device type for ATAPI devices
 1.11 13-Jul-1998  hpeyerl Add support for ATA CD changer devices like the NEC CDR-251.
Evidence of managerial coding removed by Victor T. Cleaner (thorpej)
 1.10 05-Feb-1998  mrg initial import of the new virtual memory system, UVM, into -current.

UVM was written by chuck cranor <chuck@maria.wustl.edu>, with some
minor portions derived from the old Mach code. i provided some help
getting swap and paging working, and other bug fixes/ideas. chuck
silvers <chuq@chuq.com> also provided some other fixes.

this is the rest of the MI portion changes.

this will be KNF'd shortly. :-)
 1.9 15-Jan-1998  cgd add support for an ATAPI attachment for 'sd'.
fix 'cd' driver's NCD_SCSI bogosity (was using testing wrong macro!)
clean up in various ways:
* make common atapi_mode_{sense,select}() functions.
* put ATAPI data structures in more sensible headers, split up by
device type.
* include headers a bit more carefully.
* pass flags to attachment-specific cd functions, and use them.
* get rid of SCSI bits in scsipi_base.h's scsipi_make_xs(), move
them into the correct place in scsi_base.c.
* fix minor typo in struct name in scsipiconf.h (which was apparently
never used except in a #define later in the same file).
* use __attribute__ to force 4-byte alignment for xs command store,
so that architectures trying to bus_space_write_multi_N() (where
N > 1) that data to a controller won't lose.
* clean up a few comments in typos, and make a few #defines easier to
understand/maintain.
* rename cd_link.h to cdvar.h (via repository copy). This is exactly
what a 'var' file is supposed to be.
 1.8 18-Oct-1997  thorpej Implement two macros, scsipi_command() and scsipi_command_direct(), and
use them to hide the structure of the function pointers we jump through
to issue a command.
 1.7 17-Oct-1997  bouyer kern/4267 (Takeshi Nakayama): Add a 'len' parameter to cd_scsibus_get_mode and
cd_scsibus_set_mode, to specifie the real len of "data" we need. These
functions were broken when scsi_cd_write_params_page was added to
scsi_cd_pages, thus changing the len of scsi_cd_mode_data.
 1.6 13-Oct-1997  explorer o Make usage of /dev/random dependant on
pseudo-device rnd # /dev/random and in-kernel generator
in config files.

o Add declaration to all architectures.

o Clean up copyright message in rnd.c, rnd.h, and rndpool.c to include
that this code is derived in part from Ted Tyso's linux code.
 1.5 10-Oct-1997  explorer Add hooks to insert timing info into the random system
 1.4 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.3 05-Sep-1997  bouyer Don't attach worm devices to the CD driver. This is a private hack, and should
never have been there !
 1.2 27-Aug-1997  bouyer branches: 1.2.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.1 01-Jul-1997  bouyer branches: 1.1.2;
file scsicd.c was initially added on branch bouyer-scsipi.
 1.1.2.6 01-Jul-1997  thorpej Remove extra newline printf.
 1.1.2.5 01-Jul-1997  thorpej "cd" instances attach to "scsibus" and "atapibus", so name the cfattach's
in a way what reflects that.
 1.1.2.4 01-Jul-1997  thorpej Use a naming convention consistent with all other drivers in the source
tree that use multiple attachments... i.e. cd_scsibus_* and cd_atapibus_*.
 1.1.2.3 01-Jul-1997  thorpej Use an operations vector set up at attach time rather than checking
for device type each time, for setvol/getvol/setchan operations.

Also, some KNF.
 1.1.2.2 01-Jul-1997  bouyer Get this compile on the i386.
 1.1.2.1 01-Jul-1997  bouyer New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
scsibus at aha
sd at scsibus
cd at scsibus
atapibus at wdc
cd at atapibus
cd has bus-specific config and function front-end in scsicd.c and atapicd.c
The call to theses functions from cd.c is conditionned to
NSCSICD and NATAPICD (all defined in cd.h by config).
 1.2.2.4 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.3 06-Sep-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.1 27-Aug-1997  thorpej file cd_scsi.c was added on branch marc-pcmcia on 1997-08-27 23:33:04 +0000
 1.16.2.3 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.16.2.2 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.16.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.17.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.18.4.6 11-Nov-2002  nathanw Catch up to -current
 1.18.4.5 18-Oct-2002  nathanw Catch up to -current.
 1.18.4.4 08-Jan-2002  nathanw Catch up to -current.
 1.18.4.3 14-Nov-2001  nathanw Catch up to -current.
 1.18.4.2 21-Sep-2001  nathanw Catch up to -current.
 1.18.4.1 21-Jun-2001  nathanw Catch up to -current.
 1.20.2.3 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.20.2.2 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.20.2.1 13-Sep-2001  thorpej Update the kqueue branch to HEAD.
 1.23.10.1 13-Dec-2002  jmc Pull up revisions 1.26-1.27 (requested by jdc in ticket #969)
Apply patch from Takeshi Nakayama on current-users which fixes problems
when trying to play audio CD's on some drives.
Fixes PR 7299 and probably PR 15155 and PR 16883 too.
 1.27.6.1 03-Aug-2004  skrll Sync with HEAD
 1.33 10-Dec-2016  mlelstv Refactored sd and cd to use common disk subroutines.
 1.32 14-Apr-2015  riastradh branches: 1.32.2;
Include <sys/rndsource.h> where it is actually used.

I had removed <sys/rnd.h> from files that didn't mention anything of
the rnd(9) API. But they included other files which assumed
<sys/rnd.h> had already been included.
 1.31 02-Feb-2012  tls branches: 1.31.6; 1.31.24;
Entropy-pool implementation move and cleanup.

1) Move core entropy-pool code and source/sink/sample management code
to sys/kern from sys/dev.

2) Remove use of NRND as test for presence of entropy-pool code throughout
source tree.

3) Remove use of RND_ENABLED in device drivers as microoptimization to
avoid expensive operations on disabled entropy sources; make the
rnd_add calls do this directly so all callers benefit.

4) Fix bug in recent rnd_add_data()/rnd_add_uint32() changes that might
have lead to slight entropy overestimation for some sources.

5) Add new source types for environmental sensors, power sensors, VM
system events, and skew between clocks, with a sample implementation
for each.

ok releng to go in before the branch due to the difficulty of later
pullup (widespread #ifdef removal and moved files). Tested with release
builds on amd64 and evbarm and live testing on amd64.
 1.30 19-Nov-2011  tls branches: 1.30.2;
First step of random number subsystem rework described in
<20111022023242.BA26F14A158@mail.netbsd.org>. This change includes
the following:

An initial cleanup and minor reorganization of the entropy pool
code in sys/dev/rnd.c and sys/dev/rndpool.c. Several bugs are
fixed. Some effort is made to accumulate entropy more quickly at
boot time.

A generic interface, "rndsink", is added, for stream generators to
request that they be re-keyed with good quality entropy from the pool
as soon as it is available.

The arc4random()/arc4randbytes() implementation in libkern is
adjusted to use the rndsink interface for rekeying, which helps
address the problem of low-quality keys at boot time.

An implementation of the FIPS 140-2 statistical tests for random
number generator quality is provided (libkern/rngtest.c). This
is based on Greg Rose's implementation from Qualcomm.

A new random stream generator, nist_ctr_drbg, is provided. It is
based on an implementation of the NIST SP800-90 CTR_DRBG by
Henric Jungheim. This generator users AES in a modified counter
mode to generate a backtracking-resistant random stream.

An abstraction layer, "cprng", is provided for in-kernel consumers
of randomness. The arc4random/arc4randbytes API is deprecated for
in-kernel use. It is replaced by "cprng_strong". The current
cprng_fast implementation wraps the existing arc4random
implementation. The current cprng_strong implementation wraps the
new CTR_DRBG implementation. Both interfaces are rekeyed from
the entropy pool automatically at intervals justifiable from best
current cryptographic practice.

In some quick tests, cprng_fast() is about the same speed as
the old arc4randbytes(), and cprng_strong() is about 20% faster
than rnd_extract_data(). Performance is expected to improve.

The AES code in src/crypto/rijndael is no longer an optional
kernel component, as it is required by cprng_strong, which is
not an optional kernel component.

The entropy pool output is subjected to the rngtest tests at
startup time; if it fails, the system will reboot. There is
approximately a 3/10000 chance of a false positive from these
tests. Entropy pool _input_ from hardware random numbers is
subjected to the rngtest tests at attach time, as well as the
FIPS continuous-output test, to detect bad or stuck hardware
RNGs; if any are detected, they are detached, but the system
continues to run.

A problem with rndctl(8) is fixed -- datastructures with
pointers in arrays are no longer passed to userspace (this
was not a security problem, but rather a major issue for
compat32). A new kernel will require a new rndctl.

The sysctl kern.arandom() and kern.urandom() nodes are hooked
up to the new generators, but the /dev/*random pseudodevices
are not, yet.

Manual pages for the new kernel interfaces are forthcoming.
 1.29 19-Oct-2009  bouyer branches: 1.29.12;
Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.28 24-Mar-2008  cube branches: 1.28.4;
Split device_t for cd(4).
 1.27 21-Jul-2007  ad branches: 1.27.26; 1.27.28;
Replace some uses of lockmgr().
 1.26 11-Dec-2005  christos branches: 1.26.30; 1.26.40;
merge ktrace-lwp.
 1.25 15-Oct-2005  yamt - change the way to specify a bufq strategy. (by string rather than by number)
- rather than embedding bufq_state in driver softc,
have a pointer to the former.
- move bufq related functions from kern/subr_disk.c to kern/subr_bufq.c.
- rename method to strategy for consistency.
- move some definitions which don't need to be exposed to the rest of kernel
from sys/bufq.h to sys/bufq_impl.h.
(is it better to move it to kern/ or somewhere?)
- fix some obvious breakage in dev/qbus/ts.c. (not tested)
 1.24 01-Feb-2005  reinoud branches: 1.24.6;
Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.23 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.22 17-Sep-2004  mycroft branches: 1.22.4;
Do not manipulate xs->bp in "generic" code -- do it only in the psw_done
routine. As part of this, pass down our pre-parsed error code -- though this
interface will probably change later to accomodate better error handling.
 1.21 27-Aug-2004  bouyer Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
Havard Eidnes's analysis of this problem is that the scsipi_xfer pool is
competing for resources with other pools, including the the inode and vnode
pools which can grow quite large.

*_scsipi_cmd(): don't biodone the buffer if scsipi_make_xs() fails, let the
caller deal with the problem
start function of block devices drivers: dequeue the buffer after the
scsipi_command() call. If scsipi_command() fails with ENOMEM don't dequeue
the buffer, and schedule a callout to call the start function after
some delay.
scsipi_init(): prime the scsipi_xfer_pool with one page. This ensure that
there is always some scsipi_xfer to play with. If scsipi_command() fails
because of pool_get(), we're sure there will be resources available later,
when the pending commands have completed.

Reviewed by Jason Thorpe and Havard Eidnes.
Todo: remove the "unable to allocate scsipi_xfer" and "not queued, error %d"
printfs, but I choose to keep them for now, to help make sure the code does
what it should.
 1.20 08-Sep-2003  mycroft branches: 1.20.2;
Eliminate the separate ATAPI and SCSI attachments for "cd".
 1.19 07-Sep-2003  mycroft Set PQUIRK_ONLYBIG in the wdc-atapi frontend, obviating the need to ever test
the "bus type" for this.

Merge all the code in the SCSI and ATAPI backends for "cd" devices. All of
the mode page handling and whatnot is general to SCSI MMC devices, and should
never have been separated to begin with. This fixes a variety of problems,
and adds load/unload support for SCSI-attached devices.
 1.18 10-Jul-2003  martin If a CD drive reports != 2048 byte block size, try to change it into
2048 byte mode. Fixes PR kern/22090.
Reviewed by Manuel Bouyer.
 1.17 10-May-2003  thorpej branches: 1.17.2;
Change bounds_check_with_label() to take a pointer to the disk structure,
rather than the label itself. This paves the way for some future changes.
 1.16 03-Apr-2003  fvdl Check RAW_PART against the media size instead of the disklabel.
Add the media size in 512-byte sectors to the softc, to avoid
some 64 bit computations. Bump the capacity stored in softcs
for disks to 64 bits.
 1.15 22-Jul-2002  hannken Convert to new device buffer queue interface.
 1.14 23-Apr-2002  bouyer branches: 1.14.2;
More copyright fixes, pointed out by Thomas. Thanks !
 1.13 25-Apr-2001  bouyer branches: 1.13.2;
Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.12 21-Jan-2000  thorpej branches: 1.12.6;
Update for sys/buf.h/disksort_*() changes.
 1.11 23-Sep-1999  enami branches: 1.11.2;
Allow to detach wdc, atapibus, wd and cd.
 1.10 02-Feb-1999  bouyer Bump CDRETRIES to 4. Now if a DMA error occurs, the driver should downgrade
to PIO mode and the command be retried properly.
 1.9 05-Aug-1998  drochner Improve generation of default disklabels:
-store printable product ID in cd's and sd's softc, use it as "typename"
-for this, add a "destination buffer length" argument to scsipi_strvis()
-return ATAPI device type for ATAPI devices
 1.8 13-Jul-1998  hpeyerl Add support for ATA CD changer devices like the NEC CDR-251.
Evidence of managerial coding removed by Victor T. Cleaner (thorpej)
 1.7 13-Feb-1998  enami Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.6 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.5 15-Jan-1998  cgd add support for an ATAPI attachment for 'sd'.
fix 'cd' driver's NCD_SCSI bogosity (was using testing wrong macro!)
clean up in various ways:
* make common atapi_mode_{sense,select}() functions.
* put ATAPI data structures in more sensible headers, split up by
device type.
* include headers a bit more carefully.
* pass flags to attachment-specific cd functions, and use them.
* get rid of SCSI bits in scsipi_base.h's scsipi_make_xs(), move
them into the correct place in scsi_base.c.
* fix minor typo in struct name in scsipiconf.h (which was apparently
never used except in a #define later in the same file).
* use __attribute__ to force 4-byte alignment for xs command store,
so that architectures trying to bus_space_write_multi_N() (where
N > 1) that data to a controller won't lose.
* clean up a few comments in typos, and make a few #defines easier to
understand/maintain.
* rename cd_link.h to cdvar.h (via repository copy). This is exactly
what a 'var' file is supposed to be.
 1.4 13-Oct-1997  explorer o Make usage of /dev/random dependant on
pseudo-device rnd # /dev/random and in-kernel generator
in config files.

o Add declaration to all architectures.

o Clean up copyright message in rnd.c, rnd.h, and rndpool.c to include
that this code is derived in part from Ted Tyso's linux code.
 1.3 10-Oct-1997  explorer Add hooks to insert timing info into the random system
 1.2 27-Aug-1997  bouyer branches: 1.2.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.1 01-Jul-1997  bouyer branches: 1.1.2;
file cd_link.h was initially added on branch bouyer-scsipi.
 1.1.2.2 01-Jul-1997  thorpej Use an operations vector set up at attach time rather than checking
for device type each time, for setvol/getvol/setchan operations.

Also, some KNF.
 1.1.2.1 01-Jul-1997  bouyer New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
scsibus at aha
sd at scsibus
cd at scsibus
atapibus at wdc
cd at atapibus
cd has bus-specific config and function front-end in scsicd.c and atapicd.c
The call to theses functions from cd.c is conditionned to
NSCSICD and NATAPICD (all defined in cd.h by config).
 1.2.2.2 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.1 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.11.2.2 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.11.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.12.6.3 01-Aug-2002  nathanw Catch up to -current.
 1.12.6.2 20-Jun-2002  nathanw Catch up to -current.
 1.12.6.1 21-Jun-2001  nathanw Catch up to -current.
 1.13.2.2 06-Sep-2002  jdolecek sync kqueue branch with HEAD
 1.13.2.1 23-Jun-2002  jdolecek catch up with -current on kqueue branch
 1.14.2.1 29-Aug-2002  gehenna catch up with -current.
 1.17.2.5 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.17.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.17.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.17.2.2 03-Sep-2004  skrll Sync with HEAD
 1.17.2.1 03-Aug-2004  skrll Sync with HEAD
 1.20.2.1 11-Sep-2004  he Pull up revision 1.21 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.22.4.1 29-Apr-2005  kent sync with -current
 1.24.6.2 03-Sep-2007  yamt sync with head.
 1.24.6.1 21-Jun-2006  yamt sync with head.
 1.26.40.1 15-Aug-2007  skrll Sync with HEAD.
 1.26.30.1 20-Aug-2007  ad Sync with HEAD.
 1.27.28.2 21-Jul-2007  ad Replace some uses of lockmgr().
 1.27.28.1 21-Jul-2007  ad file cdvar.h was added on branch matt-mips64 on 2007-07-21 19:51:49 +0000
 1.27.26.1 03-Apr-2008  mjf Sync with HEAD.
 1.28.4.1 11-Mar-2010  yamt sync with head
 1.29.12.1 17-Apr-2012  yamt sync with head
 1.30.2.1 18-Feb-2012  mrg merge to -current.
 1.31.24.2 05-Feb-2017  skrll Sync with HEAD
 1.31.24.1 06-Jun-2015  skrll Sync with HEAD
 1.31.6.1 03-Dec-2017  jdolecek update from HEAD
 1.32.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.96 18-Aug-2025  andvar Fix various typos, mainly in comments:
s/invaid/invalid/
s/instad/instead/
s/wich/with/
s/tranform/transform/
s/tranmist/transmit/
s/tranceiver/transceiver/
s/Tranparent/Transparent/
s/tranlated/translated/
s/tranfer/transfer/
s/tranmissions/transmissions/
s/condtions/conditions/
s/Recient/Recent/
 1.95 26-Sep-2021  thorpej Use seltrue_filtops rather than rolling our own with filt_seltrue.
 1.94 26-Sep-2021  thorpej Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89
 1.93 18-Dec-2020  thorpej Use sel{record,remove}_knote().
 1.92 25-Oct-2017  maya branches: 1.92.18;
Use C99 initializer for filterops

Mostly done with spatch with touchups for indentation

@@
expression a;
identifier b,c,d;
identifier p;
@@
const struct filterops p =
- { a, b, c, d
+ {
+ .f_isfd = a,
+ .f_attach = b,
+ .f_detach = c,
+ .f_event = d,
};
 1.91 20-Nov-2016  mlelstv Make scsipi framework MPSAFE.

Data structures are now protected by a per-adapter mutex at IPL_BIO
that is created by the scsibus or atapibus instance when the adapter
is configured.
The enable reference counter and the channel freeze counter which are
currently used by HBA code before the adapter is configured, are made
atomic.
The target drivers are now all tagged as D_MPSAFE.

Almost all HBA drivers still require the kernel lock to present,
so all callbacks into HBA code are still protected by kernel lock
unless the driver is tagged as SCSIPI_ADAPT_MPSAFE.

TODO: refactor sd and cd to use dksubr.
 1.90 25-Jul-2014  dholland branches: 1.90.4; 1.90.8;
Add d_discard to all struct cdevsw instances I could find.

All have been set to "nodiscard"; some should get a real implementation.
 1.89 16-Mar-2014  dholland branches: 1.89.2;
Change (mostly mechanically) every cdevsw/bdevsw I can find to use
designated initializers.

I have not built every extant kernel so I have probably broken at
least one build; however I've also found and fixed some wrong
cdevsw/bdevsw entries so even if so I think we come out ahead.
 1.88 09-Aug-2013  kardel bump command timeout to 5 minutes. several
types of changers (Overland PowerLoader, Dell
PowerVault) have been exceeding the 100 sec
limit aborting a perfectly (slowly) progressing
operation.
 1.87 27-Oct-2012  chs branches: 1.87.2;
split device_t/softc for all remaining drivers.
replace "struct device *" with "device_t".
use device_xname(), device_unit(), etc.
 1.86 23-Nov-2009  rmind branches: 1.86.12; 1.86.22;
Remove some unecessary includes sys/user.h header.
 1.85 21-Oct-2009  rmind Remove uarea swap-out functionality:

- Addresses the issue described in PR/38828.
- Some simplification in threading and sleepq subsystems.
- Eliminates pmap_collect() and, as a side note, allows pmap optimisations.
- Eliminates XS_CTL_DATA_ONSTACK in scsipi code.
- Avoids few scans on LWP list and thus potentially long holds of proc_lock.
- Cuts ~1.5k lines of code. Reduces amd64 kernel size by ~4k.
- Removes __SWAP_BROKEN cases.

Tested on x86, mips, acorn32 (thanks <mpumford>) and partly tested on
acorn26 (thanks to <bjh21>).

Discussed on <tech-kern>, reviewed by <ad>.
 1.84 12-May-2009  cegger struct device * -> device_t, no functional changes intended.
 1.83 12-May-2009  cegger struct cfdata * -> cfdata_t, no functional changes intended.
 1.82 08-Jun-2008  tsutsui branches: 1.82.12;
Use device_lookup_private() rather than using cd_devs[] directly to get softc.

XXX maybe we should change a type of cd_devs[] in struct cfdriver
from (void *) to device_t.
 1.81 28-Apr-2008  martin branches: 1.81.2;
Remove clause 3 and 4 from TNF licenses
 1.80 05-Apr-2008  cegger branches: 1.80.2; 1.80.4;
use aprint_*_dev and device_xname
 1.79 01-Mar-2008  rmind Welcome to 4.99.55:

- Add a lot of missing selinit() and seldestroy() calls.

- Merge selwakeup() and selnotify() calls into a single selnotify().

- Add an additional 'events' argument to selnotify() call. It will
indicate which event (POLL_IN, POLL_OUT, etc) happen. If unknown,
zero may be used.

Note: please pass appropriate value of 'events' where possible.
Proposed on: <tech-kern>
 1.78 05-Dec-2007  pooka branches: 1.78.8; 1.78.12;
Do not "return 1" from kqfilter for errors. That value is passed
directly to the userland caller and results in a mysterious EPERM.
Instead, return EINVAL or something else sensible depending on the
case.
 1.77 04-Mar-2007  christos branches: 1.77.14; 1.77.16; 1.77.22; 1.77.24;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.76 16-Nov-2006  christos branches: 1.76.4;
__unused removal on arguments; approved by core.
 1.75 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.74 28-Aug-2006  christos branches: 1.74.2; 1.74.4;
add missing initializer
 1.73 30-Mar-2006  thorpej Use device_private().
 1.72 11-Dec-2005  christos branches: 1.72.4; 1.72.6; 1.72.8; 1.72.10; 1.72.12;
merge ktrace-lwp.
 1.71 30-May-2005  christos branches: 1.71.2;
- remove bogus casts
- add more const
 1.70 25-Apr-2005  drochner fix more SSD_RCODE_VALID misuse introduced in cleanup
 1.69 27-Feb-2005  perry nuke trailing whitespace
 1.68 21-Feb-2005  thorpej Part 1 of a cleanup pass over the SCSI subsystem. The aim is to name
everything "scsi_*", since we really are talking about the SCSI command
set, ATAPI transport not withstanding. Improve the names of many structures,
and prepend "SCSI_" onto all SCSI command opcodes. Place items described
by the SCSI Primary Commands document into scsi_spc.h.
 1.67 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.66 31-Jan-2005  reinoud Fix LP64 problems introduced by my u_long->uint32_t conversion.
 1.65 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.64 18-Sep-2004  mycroft branches: 1.64.4; 1.64.6;
Standardize some variable names and the calling pattern for scsipi_command().
Use void pointer casts.
 1.63 17-Sep-2004  mycroft Remove the "xfer" argument to scsipi_command().
 1.62 09-Sep-2004  bouyer Make the xxstart() functions reentrant again, as some drivers HBA can call
scsipi_done() from their scsipi_request().
For this, add a struct scsipi_xfer * argument to scsipi_command().
If not NULL scsipi_command() will use this to enqueue this xfer, otherwise
it'll try to allocate a new one. This scsipi_xfer has to be allocated
and initialised by scsipi_make_xs() or equivalent.
In xxstart(), allocate a scsipi_xfer using scsipi_make_xs(), and if not NULL,
dequeue the buffer before calling scsipi_command(). This makes sure that
scsipi_command() will not fail, and also makes sure that xxstart() won't
be called again between the BUFQ_PEEK() and BUFQ_GET().

Fix "dequeued wrong buf" panics reported by Juergen Hannken-Illjes in
private mail and Andreas Wrede on current-users@.
Thanks to Jason Thorpe and Chuck Silver for review, and Andreas Wrede for
testing the patch.
 1.61 21-Aug-2004  thorpej Use ANSI function decls and make use of static.
 1.60 23-Jun-2004  seb Remove special handling of ASC/ASCQ 0x29/0x00 ("Power On, Reset, or Bus
Device Reset Occurred") when the device is not opened. So let the default
error handler deal with that.

This fix the retrieval of the sense page during autoconfig.

Provided by mycroft@.
Approved by bouyer@.
 1.59 23-Apr-2004  itojun use bounded string ops
 1.58 08-Sep-2003  mycroft branches: 1.58.2;
Use the const shaker.
 1.57 29-Jun-2003  fvdl branches: 1.57.2;
Back out the lwp/ktrace changes. They contained a lot of colateral damage,
and need to be examined and discussed more.
 1.56 28-Jun-2003  darrenr Pass lwp pointers throughtout the kernel, as required, so that the lwpid can
be inserted into ktrace records. The general change has been to replace
"struct proc *" with "struct lwp *" in various function prototypes, pass
the lwp through and use l_proc to get the process pointer when needed.

Bump the kernel rev up to 1.6V
 1.55 26-Nov-2002  christos si_ -> sel_
 1.54 23-Oct-2002  jdolecek merge kqueue branch into -current

kqueue provides a stateful and efficient event notification framework
currently supported events include socket, file, directory, fifo,
pipe, tty and device changes, and monitoring of processes and signals

kqueue is supported by all writable filesystems in NetBSD tree
(with exception of Coda) and all device drivers supporting poll(2)

based on work done by Jonathan Lemon for FreeBSD
initial NetBSD port done by Luke Mewburn and Jason Thorpe
 1.53 02-Oct-2002  thorpej Add trailing ; to CFATTACH_DECL.
 1.52 30-Sep-2002  thorpej Use CFATTACH_DECL().
 1.51 27-Sep-2002  thorpej Declare all cfattach structures const.
 1.50 06-Sep-2002  gehenna Merge the gehenna-devsw branch into the trunk.

This merge changes the device switch tables from static array to
dynamically generated by config(8).

- All device switches is defined as a constant structure in device drivers.

- The new grammer ``device-major'' is introduced to ``files''.

device-major <prefix> char <num> [block <num>] [<rules>]

- All device major numbers must be listed up in port dependent majors.<arch>
by using this grammer.

- Added the new naming convention.
The name of the device switch must be <prefix>_[bc]devsw for auto-generation
of device switch tables.

- The backward compatibility of loading block/character device
switch by LKM framework is broken. This is necessary to convert
from block/character device major to device name in runtime and vice versa.

- The restriction to assign device major by LKM is completely removed.
We don't need to reserve LKM entries for dynamic loading of device switch.

- In compile time, device major numbers list is packed into the kernel and
the LKM framework will refer it to assign device major number dynamically.
 1.49 06-May-2002  bouyer branches: 1.49.2;
Don't return an error for "Not Ready To Ready Transition (Medium May Have
Changed)" and "Power On, Reset, or Bus Device Reset Occurred" senses, if
we're trying to open the device.
 1.48 13-Nov-2001  lukem add RCSIDs
 1.47 18-Jul-2001  thorpej branches: 1.47.2;
bzero -> memset
 1.46 19-Jun-2001  lukem branches: 1.46.2;
delint
 1.45 14-May-2001  bouyer Use SCSI/ATAPI common definition for MODE_{SELECT,SENSE}{,_BIG}. Define
functions to send theses commands in scsipi_base.c and use them instead
of ad-hoc commands setups.
 1.44 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.43 09-Jun-2000  enami branches: 1.43.4;
Prevent a process being swapped out during I/O if the data buffer is
allocated on stack. This potential problem is noticed by Noriyuki Soda
and the idea and sample code to fix is given by Jason R. Thorpe.
 1.42 04-Jan-2000  mjacob branches: 1.42.2;
oops, bad patch
 1.41 04-Jan-2000  mjacob - If the user application issues an INITIALIZE ELEMENT STATUS which
succeeds, note that we now are valid.

- Don't attempt to try and run initialize element status from interrupt level-
we don't really support that yet. Also, key more correctly off of ASC/ASCQ
instead of just the sense key.

- Make the practice of doing an INITIALIZE ELEMENT STATUS automatically when
we get params (from chopen time even) a policy decision that is not the
default for now- this can be a dangerous practice as well as time consuming.
It's dangerous in that you can have a hung open when all you really want
to do is do a read of parameters- and parameters, including slot status,
are perfectly fine to read even before an INITIALIZE ELEMENT STATUS is
done- all the elements whos status your read are going to be marked with
an exception- so leave it up to the application to decide how important
this is.
 1.40 30-Sep-1999  thorpej branches: 1.40.2;
Cleanup the scsipi_xfer flags:
- `flags' is now gone, replaced with `xs_control' and `xs_status'.
- Massive cleanup of the control flags. Now we explicitly say that
a job is to complete asynchronously, rather than relying on side-effects,
and use a new flag to now that device discovery is being performed.
- Do SCSI device discovery interrupt-driven.
 1.39 09-Sep-1999  thorpej Rework the changer driver a bit:
- Take note of magazine changes, and enqueue "Element Status Changed" events
that user processes can read or select on.
- Normalize some structure names.
- Report back more status about changer elements:
* Volume tags (e.g. barcode labels on the backs of your tapes)
* External device names (for drive units in a changer)
* Last element a unit of media was moved from
* Sense information for SCSI changer elements in EXCEPT condition
* Vendor-specific data if the user requests it.
- Add support for setting volume tags.
 1.38 04-Apr-1999  bouyer Adjust timeout for 'ielem' to match the comment: 5 mn (not 5s) per element +
10mn initial.
 1.37 17-Dec-1998  gibbs branches: 1.37.2;
Return ENODEV when the device capability check fails for MOVE and EXCHANGE
operations.
 1.36 08-Dec-1998  thorpej When closing, wait for pending xfers to drain before deleting the reference
to the adapter.
 1.35 20-Nov-1998  thorpej Add adapter reference counting for SCSI and ATAPI devices.
 1.34 31-Aug-1998  cgd kill the last remnants of __BROKEN_INDIRECT_CONFIG. (only the pica port
used it, and it's non-working and apparently slated for replacement.)
 1.33 03-Jul-1998  mjacob kern/5514: take the submitter's advice, at least partially. The time it
takes to do IELEM can be proportional to the number of elements, but is
also affected by wierd things like how readable the barcodes on the
media are. There are worst case scenarios I've seen where there are
white labels on the back of tapes with pencilled in labels which is
*just* close enough to being a bar code that an Exabyte 120 would
peer at them myopically and long enough for a *really* long time to
pass in inventorying the jukebox.

I've upped the limit to be proportional to 5 minutes per element. That
is long enough that someone I'm sure will complain about "you wait
to long and should time out" for broken h/w.

As is also noted in the PR, there are a lot of other issues here. It's
really also a question as to whether to update this driver or go
with CAM's driver. This one doesn't have switching between block
descriptors and not, doesn't support volume tag setting, and so on.
Time is limited. This PR should have been closed and fixed right away,
tho.
 1.32 12-Jan-1998  thorpej Adjust for changes to config.
 1.31 18-Oct-1997  thorpej Implement two macros, scsipi_command() and scsipi_command_direct(), and
use them to hide the structure of the function pointers we jump through
to issue a command.
 1.30 01-Oct-1997  thorpej Add missing SCSI_DATA_IN when issuing a READ_ELEMENT_STATUS command.
Noticed by Dave Huang <khym@bga.com>.
 1.29 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.28 29-Sep-1997  mjacob Add 'initialize element status' command; note (for future fixing in ch.c)
that a number of jukeboxes can't cope with DBD (disable block descriptors)-
we'll have to fix that some day.
 1.27 27-Aug-1997  bouyer branches: 1.27.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.26 21-Feb-1997  thorpej - Tidy up some printf code in chattach().
- Implement a simple quirks framework for changers.
- Add a quirk for the Spectra 9000 8mm tape library; it requires a
slightly-longer-than-1-minute delay to take tape inventory. Many
thanks to David Webster at Cygnus Solutions for testing this for me.
 1.25 05-Dec-1996  cgd branches: 1.25.6;
update these so they compile whether or not __BROKEN_INDIRECT_CONFIG
is defined.
 1.24 12-Oct-1996  christos revert previous kprintf change
 1.23 10-Oct-1996  christos printf -> kprintf, sprintf -> ksprintf
 1.22 18-Sep-1996  thorpej For ioctl commands which may change the device's state, ensure that
the caller has the device open for writing.
 1.21 19-Apr-1996  christos branches: 1.21.4;
gcc -Wall delinting: unused variables, assignments inside if's and missing
arguments in printf statements.
 1.20 03-Apr-1996  thorpej New SCSI media changer driver, written from scratch by me. Some
influence from the old driver (written by Stefan Grefen) and from
an autochanger driver written by the Systems Programming Group at the
University of Utah Computer Science Department (currently residing
in src/sys/arch/hp300/dev/ac.c).
 1.19 30-Mar-1996  christos Eliminate scsi_conf.h.
 1.18 19-Mar-1996  mycroft Define a full set of [234][bl]tol() and lto[234][bl]() conversion functions,
inlined.
Use sized types in protocol structures.
Make the definition of scsi_sense_data less ugly.
 1.17 17-Mar-1996  thorpej New device attachment scheme:

- split softc size and match/attach out from cfdriver into
a new struct cfattach.

- new "attach" directive for files.*. May specify the name of
the cfattach structure, so that devices may be easily attached
to parents with different autoconfiguration semantics.
 1.16 05-Mar-1996  thorpej Normalize SCSI autoconfiguration output.
From Chris Demetriou <cgd@NetBSD.ORG>. Fixes PR #1958.
 1.15 14-Feb-1996  christos scsi prototypes
 1.14 16-Jan-1995  mycroft Remove unused macros.
 1.13 28-Dec-1994  mycroft Numerous changes. Many bugs fixed, better autoconfig, a few new features.
 1.12 21-Nov-1994  mycroft Clean up open and close routines somewhat.
 1.11 21-Nov-1994  mycroft Replace dev_unit with device_softc in scsi_link. Change argument to foostart()
to void*.
 1.10 30-Oct-1994  cgd be more careful with types, also pull in headers where necessary.
 1.9 20-Oct-1994  mycroft First cut at making user-level SCSI commands work. This is untested.
Partly from John Brezak.
 1.8 29-Jun-1994  cgd New RCS ID's, take two. they're more aesthecially pleasant, and use 'NetBSD'
 1.7 11-Apr-1994  mycroft Fix various types. Remove some outdated flags.
 1.6 29-Mar-1994  mycroft New SCSI system, based on Julian's more recent work.
 1.5 17-Dec-1993  mycroft Canonicalize all #includes.
 1.4 01-Aug-1993  mycroft branches: 1.4.2;
Add RCS identifiers (this time on the correct side of the branch), and
incorporate recent changes in netbsd-0-9 branch.
 1.3 20-May-1993  cgd branches: 1.3.2;
add rcsids and clean up file headers
 1.2 20-Apr-1993  mycroft Display more meaningful message on SCSI `unit attention'.
 1.1 21-Mar-1993  cgd after 0.2.2 "stable" patches applied
 1.3.2.1 31-Jul-1993  cgd give names, err, wmesg's, to my "pain" -- i.e. convert sleep() to tsleep()
 1.4.2.3 16-Feb-1994  mycroft More KNF-like.
 1.4.2.2 29-Nov-1993  mycroft Converted. Needs testing.
 1.4.2.1 24-Nov-1993  mycroft Under construction...
 1.21.4.1 14-Nov-1996  thorpej Pull up from trunk:

> For ioctl commands which may change the device's state, ensure that
> the caller has the device open for writing.
 1.25.6.1 12-Mar-1997  is Merge in changes from Trunk
 1.27.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.27.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.27.2.1 27-Aug-1997  thorpej file ch.c was added on branch marc-pcmcia on 1997-08-27 23:33:05 +0000
 1.37.2.1 05-Apr-1999  bouyer branches: 1.37.2.1.2;
Pull up rev 1.38 (correct 'ielem' timeout computation); approved by perry.
 1.37.2.1.2.1 21-Jun-1999  thorpej Sync w/ -current.
 1.40.2.2 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.40.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.42.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.43.4.8 11-Dec-2002  thorpej Sync with HEAD.
 1.43.4.7 11-Nov-2002  nathanw Catch up to -current
 1.43.4.6 18-Oct-2002  nathanw Catch up to -current.
 1.43.4.5 17-Sep-2002  nathanw Catch up to -current.
 1.43.4.4 20-Jun-2002  nathanw Catch up to -current.
 1.43.4.3 14-Nov-2001  nathanw Catch up to -current.
 1.43.4.2 24-Aug-2001  nathanw Catch up with -current.
 1.43.4.1 21-Jun-2001  nathanw Catch up to -current.
 1.46.2.7 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.46.2.6 02-Oct-2002  jdolecek do not need the (void *) cast for kn_hook anymore
 1.46.2.5 23-Jun-2002  jdolecek catch up with -current on kqueue branch
 1.46.2.4 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.46.2.3 08-Sep-2001  thorpej Use the seltrue filter as appropriate (or, rather, as the "poll"
entry points of these drivers indicate).
 1.46.2.2 08-Sep-2001  thorpej Add kqueue support.
 1.46.2.1 03-Aug-2001  lukem update to -current
 1.47.2.2 26-Sep-2001  fvdl * add a VCLONED vnode flag that indicates a vnode representing a cloned
device.
* rename REVOKEALL to REVOKEALIAS, and add a REVOKECLONE flag, to pass
to VOP_REVOKE
* the revoke system call will revoke all aliases, as before, but not the
clones
* vdevgone is called when detaching a device, so make it use REVOKECLONE
to get rid of all clones as well
* clean up all uses of VOP_OPEN wrt. locking.
* add a few VOPS to spec_vnops that need to do something when it's a
clone vnode (access and getattr)
* add a copy of the vnode vattr structure of the original 'master' vnode
to the specinfo of a cloned vnode. could possibly redirect getattr to
the 'master' vnode, but this has issues with revoke
* add a vdev_reassignvp function that disassociates a vnode from its
original device, and reassociates it with the specified dev_t. to be
used by cloning devices only, in case a new minor is allocated.
* change all direct references in drivers to v_devcookie and v_rdev
to vdev_privdata(vp) and vdev_rdev(vp). for diagnostic purposes
when debugging race conditions that still exist wrt. locking and
revoking vnodes.
* make the locking state of a vnode consistent when passed to
d_open and d_close (unlocked). locked would be better, but has
some deadlock issues
 1.47.2.1 07-Sep-2001  thorpej Commit my "devvp" changes to the thorpej-devvp branch. This
replaces the use of dev_t in most places with a struct vnode *.

This will form the basic infrastructure for real cloning device
support (besides being architecurally cleaner -- it'll be good
to get away from using numbers to represent objects).
 1.49.2.1 16-May-2002  gehenna Add the character device switch.
 1.57.2.7 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.57.2.6 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.57.2.5 21-Sep-2004  skrll Fix the sync with head I botched.
 1.57.2.4 18-Sep-2004  skrll Sync with HEAD.
 1.57.2.3 25-Aug-2004  skrll Sync with HEAD.
 1.57.2.2 03-Aug-2004  skrll Sync with HEAD
 1.57.2.1 02-Jul-2003  darrenr Apply the aborted ktrace-lwp changes to a specific branch. This is just for
others to review, I'm concerned that patch fuziness may have resulted in some
errant code being generated but I'll look at that later by comparing the diff
from the base to the branch with the file I attempt to apply to it. This will,
at the very least, put the changes in a better context for others to review
them and attempt to tinker with removing passing of 'struct lwp' through
the kernel.
 1.58.2.2 11-Sep-2004  he Pull up revision 1.62 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.58.2.1 24-Jun-2004  he Pull up revision 1.60 (requested by seb in ticket #545):
Remove special handling of ASC/ASCQ 0x29/0x00 ("Power On,
Reset, or Bus Device Reset Occurred") when the device is
not opened. Let the default error handler deal with that
instead. This fixes the retreival of the sense page during
autoconfig.
 1.64.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.64.4.1 29-Apr-2005  kent sync with -current
 1.71.2.5 17-Mar-2008  yamt sync with head.
 1.71.2.4 07-Dec-2007  yamt sync with head
 1.71.2.3 03-Sep-2007  yamt sync with head.
 1.71.2.2 30-Dec-2006  yamt sync with head.
 1.71.2.1 21-Jun-2006  yamt sync with head.
 1.72.12.1 31-Mar-2006  tron Merge 2006-03-31 NetBSD-current into the "peter-altq" branch.
 1.72.10.1 19-Apr-2006  elad sync with head.
 1.72.8.2 03-Sep-2006  yamt sync with head.
 1.72.8.1 01-Apr-2006  yamt sync with head.
 1.72.6.1 22-Apr-2006  simonb Sync with head.
 1.72.4.1 09-Sep-2006  rpaulo sync with head
 1.74.4.2 10-Dec-2006  yamt sync with head.
 1.74.4.1 22-Oct-2006  yamt sync with head
 1.74.2.1 18-Nov-2006  ad Sync with head.
 1.76.4.1 12-Mar-2007  rmind Sync with HEAD.
 1.77.24.1 08-Dec-2007  ad Sync with head.
 1.77.22.1 08-Dec-2007  mjf Sync with HEAD.
 1.77.16.2 23-Mar-2008  matt sync with HEAD
 1.77.16.1 09-Jan-2008  matt sync with HEAD
 1.77.14.1 09-Dec-2007  jmcneill Sync with HEAD.
 1.78.12.4 29-Jun-2008  mjf Sync with HEAD.
 1.78.12.3 02-Jun-2008  mjf Sync with HEAD.
 1.78.12.2 05-Apr-2008  mjf - add "file-system DEVFS" and "pseudo-device devfsctl" to conf/std seeing
as these are always needed.

- convert many, many drivers over to the New Devfs World Order. For a
list of device drivers yet to be converted see,
http://www.netbsd.org/~mjf/devfs-todo.html.

- add a new device_unregister_all(device_t) function to remove all device
names associated with a device_t, which saves us having to construct
device names when the driver is detached.

- add a DEV_AUDIO type for devices.
 1.78.12.1 03-Apr-2008  mjf Sync with HEAD.
 1.78.8.1 24-Mar-2008  keiichi sync with head.
 1.80.4.4 11-Mar-2010  yamt sync with head
 1.80.4.3 16-May-2009  yamt sync with head
 1.80.4.2 04-May-2009  yamt sync with head.
 1.80.4.1 16-May-2008  yamt sync with head.
 1.80.2.2 17-Jun-2008  yamt sync with head.
 1.80.2.1 18-May-2008  yamt sync with head.
 1.81.2.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.82.12.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.86.22.3 03-Dec-2017  jdolecek update from HEAD
 1.86.22.2 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.86.22.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.86.12.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.86.12.1 30-Oct-2012  yamt sync with head
 1.87.2.2 18-May-2014  rmind sync with head
 1.87.2.1 28-Aug-2013  rmind sync with head
 1.89.2.1 10-Aug-2014  tls Rebase.
 1.90.8.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.90.4.1 05-Dec-2016  skrll Sync with HEAD
 1.92.18.1 03-Jan-2021  thorpej Sync w/ HEAD.
 1.44 22-Dec-2022  nat Driver for DaynaPORT SCSI/Link (dse.4).

Written by Hiroshi Noguchi, of which an updated version was posted to
port-mac68k in 2001.

Attachments were added to kernel configs for platforms that already had
the Cabletron (se.4) driver added, although other platorms may benefit.

Reviewed on tech-net by Izumi Tsutsui.
 1.43 14-Apr-2022  pgoyette Split some common stuff into scsi_subr module. This enables loading
of the iscsi module whether or not there are any scsi things built
into the kernel.

Addresses the iscsi portion of kern/56772
 1.42 12-Feb-2019  kardel Fix, but disable the commented SUN compatibility in st.c to write
final file marks by opening and immediately closing the device
in O_WRONLY mode. That code has not been working since around 1998.
It can now be enabled with options ST_SUNCOMPAT.
 1.41 11-Dec-2005  christos branches: 1.41.138; 1.41.142; 1.41.150; 1.41.156; 1.41.164;
merge ktrace-lwp.
 1.40 15-Apr-2005  drochner make the scsibus target/lun and the atapibus drive locators optional
(they already had default values; we could kill the [...=N] syntax
completely or at least have config(8) assume that a locator with
a default is optional)
 1.39 27-Feb-2005  perry nuke trailing whitespace
 1.38 12-Aug-2004  thorpej branches: 1.38.4; 1.38.6;
Don't assume wdc-specific ATA / ATAPI code should be brought in if atabus
or atapibus are configured; use a separate wdc_common attribute to indicate
that the shared wdc code is also present.
 1.37 08-Oct-2003  bouyer Make the ATA mid-layer appears as atabus, as proposed in
http://mail-index.netbsd.org/tech-kern/2003/09/25/0006.html
This adds a device (atabus) between IDE controllers and wd or atapibus, to
have each ATA channel show up in the device tree. Later there will be atabus
devices in /dev, so that we can do IOCTL on them.
Each atabus has its own kernel thread, to handle operations that needs polling,
e.g. reset and others.

Device probing on each bus it defered to the atabus thread creation.
This allows to do the reset and basic device probes in parallel, which reduce
boot time on systems with several pciide controllers.
 1.36 18-Sep-2003  mycroft Merge the geometry and cache handling code for all direct access and optical
devices, as it's general to all SCSI MMC devices. In the process, remove
PQUIRK_NO_FLEX_PAGE.
 1.35 08-Sep-2003  mycroft Eliminate the separate ATAPI and SCSI attachments for "cd".
 1.34 08-Sep-2003  mycroft We only need a single attachment for uk at scsibus and atapibus.
 1.33 09-Oct-2002  thorpej branches: 1.33.6;
Define a "scsi_core" attribute. Make the "scsi" attribute depend on
"scsi_core". Make all the files previously selected by the "scsi"
attribute selected by the "scsi_core" attribute. Give the "scsibus"
device the "scsi_core" attribute.
 1.32 16-May-2002  thorpej Garbage-collect the empty-for-years "su" device.
 1.31 28-Nov-2001  lukem branches: 1.31.8;
- convert usage of "defopt" to "defflag" where the relevant option does
not support a value (e.g., it's to be used as "options FOO" instead of
"options FOO=xxx"). options that take a value were converted to
defparam recently.
- minor whitespace & formatting cleanups
 1.30 28-Nov-2001  fredette Added the new defopt SCSI_OLD_NOINQUIRY; this is used instead
of sun2 to wrap code that conjures up INQUIRY responses for
certain specific old devices.
 1.29 20-Nov-2001  lukem - replace "defopt" with "defparam" for options which must take a value,
as config(8) will warn for value-less defparam options
- minor whitespace/formatting cleanup
- consolidate opt_tcp_recvspace.h and opt_tcp_sendspace.h into opt_tcp_space.h
 1.28 22-May-2001  mjacob branches: 1.28.2;
Add SDRETRIES and SD_IO_TIMEOUT option for sd.
 1.27 04-May-2001  bouyer Add an ATAPI front-end to the st driver. Completely untested for now,
but st at scsi should still work :)
 1.26 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.25 24-Feb-2001  lukem branches: 1.25.2;
whitespace police
 1.24 22-Feb-2000  tls revert previous change
 1.23 22-Feb-2000  mjacob remove SES driver
 1.22 20-Jan-2000  mjacob add SES device
 1.21 06-Jan-2000  mjacob add ST_MOUNT_DELAY options for st
 1.20 07-Feb-1999  sommerfe branches: 1.20.8;
Don't bring scsi/atapi common files in under the "atapi" attribute.
Bring them in under atapibus since they're not needed unless something
attached to an atapibus is also attached.
Tested on i386 and arm32.
 1.19 17-Nov-1998  bouyer Rename scsi_interpret_sense() to scsipi_interpret_sense() and move it from
scsi_base.c to scsipi_base.c. Rename the functions from scsi_verbose.c
too, and rename the file itself. Cleaup includes too (scsi_*.h should not
be #included in scsipi_*.h files, which are supposed to be
common to atapi and scsi).
 1.18 19-Oct-1998  bouyer Allow uk to attach at atapibus.
 1.17 12-Oct-1998  bouyer Merge bouyer-ide
 1.16 30-Jul-1998  mjacob branches: 1.16.2;
opt_scsiverbose.h now is opt_scsi.h and contains the defopted
SCSIVERBOSE and the new ST_ENABLE_EARLYWARN.
 1.15 28-Mar-1998  christos branches: 1.15.2;
Split out the verbose printing of scsi messages, so that user programs
can use the printing routines.
 1.14 16-Feb-1998  thorpej Remove "class" declarations, and add "devclass" declarations where
appropriate. Fix several inconsistencies between device class and
attributes. Mostly from Chris Demetriou.
 1.13 15-Jan-1998  cgd add support for an ATAPI attachment for 'sd'.
fix 'cd' driver's NCD_SCSI bogosity (was using testing wrong macro!)
clean up in various ways:
* make common atapi_mode_{sense,select}() functions.
* put ATAPI data structures in more sensible headers, split up by
device type.
* include headers a bit more carefully.
* pass flags to attachment-specific cd functions, and use them.
* get rid of SCSI bits in scsipi_base.h's scsipi_make_xs(), move
them into the correct place in scsi_base.c.
* fix minor typo in struct name in scsipiconf.h (which was apparently
never used except in a #define later in the same file).
* use __attribute__ to force 4-byte alignment for xs command store,
so that architectures trying to bus_space_write_multi_N() (where
N > 1) that data to a controller won't lose.
* clean up a few comments in typos, and make a few #defines easier to
understand/maintain.
* rename cd_link.h to cdvar.h (via repository copy). This is exactly
what a 'var' file is supposed to be.
 1.12 12-Jan-1998  thorpej Adjust for changes to config.
 1.11 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.10 13-Sep-1997  enami Declare SCSIVERBOSE by defopt in files.scsipi. Include opt_scsiverbose.h
in scsi_base.c and st.c.
 1.9 27-Aug-1997  bouyer branches: 1.9.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.8 20-Jul-1997  pk config.new => config
 1.7 21-Mar-1997  matthias Add missing arp tag for se driver.
 1.6 18-Mar-1997  thorpej Driver for the Cabletron EA41x SCSI Ethernet Adaptor, written by
Ian Dall <ian.dall@dsto.defence.gov.au>. Converted to "new arp"
and some other (very) minor changes by me.
 1.5 21-Feb-1997  jeremy Fix typo in comment.
 1.4 16-May-1996  mycroft branches: 1.4.8;
Slight adjustments for new version of ahc driver.
 1.3 17-Mar-1996  thorpej New device attachment scheme:

- split softc size and match/attach out from cfdriver into
a new struct cfattach.

- new "attach" directive for files.*. May specify the name of
the cfattach structure, so that devices may be easily attached
to parents with different autoconfiguration semantics.
 1.2 18-Feb-1996  mycroft Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a bit. Needs more work.
 1.1 17-Apr-1995  cgd config.new description of SCSI mi files.
 1.4.8.1 12-Mar-1997  is Merge in changes from Trunk
 1.9.2.4 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.9.2.3 16-Sep-1997  thorpej Update marc-pcmcia branch from trunk.
 1.9.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.9.2.1 27-Aug-1997  thorpej file files.scsipi was added on branch marc-pcmcia on 1997-08-27 23:33:06 +0000
 1.15.2.2 13-Aug-1998  bouyer - sync with HEAD
- better error detection and report for wd drives
- better wddump() support
- wdcwait() now takes a timout argument, so that we can honnor the
timeout argument of scsipi cmds.
 1.15.2.1 04-Jun-1998  bouyer Commit changes to the IDE system in a branch. This allows a better separation
between higth-level and low-level (i.e. registers read/write) and generalize
the queue for all commands. This also add supports for IDE DMA.
 1.16.2.2 30-Jul-1998  mjacob opt_scsiverbose.h now is opt_scsi.h and contains the defopted
SCSIVERBOSE and the new ST_ENABLE_EARLYWARN.
 1.16.2.1 30-Jul-1998  mjacob file files.scsipi was added on branch eeh-paddr_t on 1998-07-30 00:49:20 +0000
 1.20.8.3 12-Mar-2001  bouyer Sync with HEAD.
 1.20.8.2 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.20.8.1 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.25.2.4 18-Oct-2002  nathanw Catch up to -current.
 1.25.2.3 20-Jun-2002  nathanw Catch up to -current.
 1.25.2.2 08-Jan-2002  nathanw Catch up to -current.
 1.25.2.1 21-Jun-2001  nathanw Catch up to -current.
 1.28.2.2 23-Jun-2002  jdolecek catch up with -current on kqueue branch
 1.28.2.1 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.31.8.1 30-May-2002  gehenna Catch up with -current.
 1.33.6.6 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.33.6.5 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.33.6.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.33.6.3 18-Sep-2004  skrll Sync with HEAD.
 1.33.6.2 12-Aug-2004  skrll Sync with HEAD.
 1.33.6.1 03-Aug-2004  skrll Sync with HEAD
 1.38.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.38.4.1 29-Apr-2005  kent sync with -current
 1.41.164.1 10-Jun-2019  christos Sync with HEAD
 1.41.156.1 01-Mar-2019  martin Pull up following revision(s) (requested by kardel in ticket #1197):

sys/dev/scsipi/st.c: revision 1.236
sys/dev/scsipi/st.c: revision 1.237
sys/dev/scsipi/files.scsipi: revision 1.42

Fix PR kern/53949:

Fix inconsistent/incomplete file mark handling to conform again
to mtio(4) at close(2) time. This was necessary as the PREVENT/ALLOW
bracket was reduced from a whole mount session to cover only the
open(2)/close(2) time on ~2002-03-22. The rationale was to allow
robots and humans to change the media during a mount session.

Unfortunately this lead to file marks being written to potentially other
media at the beginning on drives that used the two file marks as EOM
pattern. In order for that to happen the media had to be removed after
data and at most one file mark had been written before removal.

The mount error message has been clarified and a warning about
potential data/file mark lossage on UNIT ATTENTION
during an active mount session with unfinished file marks has been
added.

While there, fix, but disable the commented SUN compatibility to write
final file marks by opening and immediately closing the device
in O_WRONLY mode. That code has not been working since around 1998.
It can now be enabled with options ST_SUNCOMPAT.
Additionally debug output coverage has been extended.

-

Correct printing type of b_blkno (int64_t) in st.c

Fixes build with kUBSan on NetBSD/i386.
Fix, but disable the commented SUN compatibility in st.c to write
final file marks by opening and immediately closing the device
in O_WRONLY mode. That code has not been working since around 1998.
It can now be enabled with options ST_SUNCOMPAT.
 1.41.150.1 07-Mar-2019  martin Pull up following revision(s) (requested by kardel in ticket #1682):

sys/dev/scsipi/st.c: revision 1.236 (patch)
sys/dev/scsipi/st.c: revision 1.237
sys/dev/scsipi/files.scsipi: revision 1.42

Fix PR kern/53949:
Fix inconsistent/incomplete file mark handling to conform again
to mtio(4) at close(2) time. This was necessary as the PREVENT/ALLOW
bracket was reduced from a whole mount session to cover only the
open(2)/close(2) time on ~2002-03-22. The rationale was to allow
robots and humans to change the media during a mount session.

Unfortunately this lead to file marks being written to potentially other
media at the beginning on drives that used the two file marks as EOM
pattern. In order for that to happen the media had to be removed after
data and at most one file mark had been written before removal.

The mount error message has been clarified and a warning about
potential data/file mark lossage on UNIT ATTENTION
during an active mount session with unfinished file marks has been
added.

While there, fix, but disable the commented SUN compatibility to write
final file marks by opening and immediately closing the device
in O_WRONLY mode. That code has not been working since around 1998.

It can now be enabled with options ST_SUNCOMPAT.

Additionally debug output coverage has been extended.

Correct printing type of b_blkno (int64_t) in st.c

Fixes build with kUBSan on NetBSD/i386.

Fix, but disable the commented SUN compatibility in st.c to write
final file marks by opening and immediately closing the device
in O_WRONLY mode. That code has not been working since around 1998.
It can now be enabled with options ST_SUNCOMPAT.
 1.41.142.1 07-Mar-2019  martin Pull up following revision(s) (requested by kardel in ticket #1682):

sys/dev/scsipi/st.c: revision 1.236 (patch)
sys/dev/scsipi/st.c: revision 1.237
sys/dev/scsipi/files.scsipi: revision 1.42

Fix PR kern/53949:
Fix inconsistent/incomplete file mark handling to conform again
to mtio(4) at close(2) time. This was necessary as the PREVENT/ALLOW
bracket was reduced from a whole mount session to cover only the
open(2)/close(2) time on ~2002-03-22. The rationale was to allow
robots and humans to change the media during a mount session.

Unfortunately this lead to file marks being written to potentially other
media at the beginning on drives that used the two file marks as EOM
pattern. In order for that to happen the media had to be removed after
data and at most one file mark had been written before removal.

The mount error message has been clarified and a warning about
potential data/file mark lossage on UNIT ATTENTION
during an active mount session with unfinished file marks has been
added.

While there, fix, but disable the commented SUN compatibility to write
final file marks by opening and immediately closing the device
in O_WRONLY mode. That code has not been working since around 1998.

It can now be enabled with options ST_SUNCOMPAT.

Additionally debug output coverage has been extended.

Correct printing type of b_blkno (int64_t) in st.c

Fixes build with kUBSan on NetBSD/i386.

Fix, but disable the commented SUN compatibility in st.c to write
final file marks by opening and immediately closing the device
in O_WRONLY mode. That code has not been working since around 1998.
It can now be enabled with options ST_SUNCOMPAT.
 1.41.138.1 07-Mar-2019  martin Pull up following revision(s) (requested by kardel in ticket #1682):

sys/dev/scsipi/st.c: revision 1.236 (patch)
sys/dev/scsipi/st.c: revision 1.237
sys/dev/scsipi/files.scsipi: revision 1.42

Fix PR kern/53949:
Fix inconsistent/incomplete file mark handling to conform again
to mtio(4) at close(2) time. This was necessary as the PREVENT/ALLOW
bracket was reduced from a whole mount session to cover only the
open(2)/close(2) time on ~2002-03-22. The rationale was to allow
robots and humans to change the media during a mount session.

Unfortunately this lead to file marks being written to potentially other
media at the beginning on drives that used the two file marks as EOM
pattern. In order for that to happen the media had to be removed after
data and at most one file mark had been written before removal.

The mount error message has been clarified and a warning about
potential data/file mark lossage on UNIT ATTENTION
during an active mount session with unfinished file marks has been
added.

While there, fix, but disable the commented SUN compatibility to write
final file marks by opening and immediately closing the device
in O_WRONLY mode. That code has not been working since around 1998.

It can now be enabled with options ST_SUNCOMPAT.

Additionally debug output coverage has been extended.

Correct printing type of b_blkno (int64_t) in st.c

Fixes build with kUBSan on NetBSD/i386.

Fix, but disable the commented SUN compatibility in st.c to write
final file marks by opening and immediately closing the device
in O_WRONLY mode. That code has not been working since around 1998.
It can now be enabled with options ST_SUNCOMPAT.
 1.9 23-May-2025  nat Schedule callback immediately if there is a hint that there is more data.

This increased the data rate by 300%
 1.8 08-Sep-2024  nat Only input needs to be polled.

Tested with PDMA on mac68k (on emulated hw).
 1.7 08-Sep-2024  nat Use aprint_normal_dev for ethernet address.

NFC.
 1.6 06-Jul-2024  andvar s/occurence/occurrence/ in comment.
 1.5 01-Jan-2024  gutteridge branches: 1.5.2;
if_dse.c: s/addredses/addresses/ in comment
 1.4 20-Dec-2023  skrll Remove unnecssary #include
 1.3 22-Dec-2022  nat Fix condition for ending the pacet read loop.

len is unsigned 16 bit so testing for less than zero is not valid.
 1.2 22-Dec-2022  nat Remove unused commented out code.
Remove unintentional stray debug printfs.
Fix DSE_DEBUG build.
 1.1 22-Dec-2022  nat Driver for DaynaPORT SCSI/Link (dse.4).

Written by Hiroshi Noguchi, of which an updated version was posted to
port-mac68k in 2001.

Attachments were added to kernel configs for platforms that already had
the Cabletron (se.4) driver added, although other platorms may benefit.

Reviewed on tech-net by Izumi Tsutsui.
 1.5.2.1 02-Aug-2025  perseant Sync with HEAD
 1.119 20-Dec-2023  skrll Remove unnecssary #include
 1.118 29-Aug-2022  skrll Make this build again. Sorry about that.
 1.117 28-Aug-2022  skrll se(4): don't set if_watchdog as it's not used.

if_timer is never set in this driver and so if_watchdog will never be
called.
 1.116 07-Jul-2022  skrll Trailing whitespace
 1.115 01-Jan-2022  msaitoh s/sytem/system/
 1.114 19-Sep-2021  andvar fix various typos in comments, messages and documentation.
 1.113 16-Jun-2021  riastradh if_attach and if_initialize cannot fail, don't test return value

These were originally made failable back in 2017 when if_initialize
allocated a softint in every interface for link state changes, so
that it could fail gracefully instead of panicking:

https://mail-index.NetBSD.org/source-changes/2017/10/23/msg089053.html

However, this spawned many seldom- or never-tested error branches,
which are risky to have around. And that softint in every interface
has since been replaced by a single global workqueue, because link
state changes require thread context but not low latency or high
throughput:

https://mail-index.NetBSD.org/source-changes/2020/02/06/msg113759.html

So there is no longer any reason for if_initialize to fail. (The
subroutine if_stats_init can't fail because percpu_alloc can't fail
either.)

There is a snag: the softint_establish in if_percpuq_create could
fail, potentially leading to bad consequences later on trying to use
the softint. This change doesn't introduce any new bugs because of
the snag -- if_percpuq_attach was already broken. However, the snag
can be better addressed without spawning error branches, either by
using a single softint or making softints less scarce.

(Separate commit will change the signatures of if_attach and
if_initialize to return void, scheduled to ride whatever is the next
convenient kernel bump.)

Patch and testing on amd64 and evbmips64-eb by maya@; commit message
soliloquy, and compile-testing on evbppc/i386/earmv7hf, by me.
 1.112 29-Sep-2020  msaitoh branches: 1.112.6;
s/occurence/occurrence/
 1.111 27-Jul-2020  jdc Improve the workqueue and callout handling. Prompted by riastradh@.
 1.110 22-Jul-2020  riastradh Sort #includes. Nix trailing whitespace.

No functional change intended.
 1.109 22-Jul-2020  riastradh Omit needless <sys/pcq.h>.
 1.108 29-Jun-2020  riastradh Nix trailing whitespace.
 1.107 22-Jun-2020  jdc Add sedetach() and also use it to remove duplicate code from seattach().
Correct a comment about sedone().
 1.106 22-Jun-2020  jdc Use workqueues so that we don't call into the scsipi subsystem via
a softint from the network stack.
Don't recurse through scsipi_command() when we have multiple packets
in the send queue - use a loop instead. This means that we no longer
need sestart(), as we can now handle everything in sedone().
Fix a couple of XXX's.
Rework the locking logic slightly from the previous revision.
Now this works with DIAGNOSTIC+LOCKDEBUG.
 1.105 19-Jun-2020  jdc First pass at making this work again.
Remove spl and add some locking around network access (needs more work).
Make sure that we consistently use the channel lock for scsipi commands.
Remove the preference for send over receive, as this can lead to deadlocks
- we only advertise 1 opening, but we can try to send before the receive is
complete in this case.
Don't use XS_CTL_ASYNC because we don't provide a buffer.
Tested on UP sparc and compile-tested on atari.
Tested with LOCKDEBUG.
Still fails with DIAGNOSTIC because we can call into the scsipi routines
from a softint.
 1.104 29-Jan-2020  thorpej Adopt <net/if_stats.h>.
 1.103 10-Nov-2019  chs branches: 1.103.2;
in many device attach paths, allocate memory with M_WAITOK instead of M_NOWAIT
and remove code to handle failures that can no longer happen.
 1.102 28-May-2019  msaitoh Use ETHER_LOCK()/ETHER_UNLOCK() for all ethernet drivers to protect ec_multi*.
 1.101 23-May-2019  msaitoh Whitespace fix (mainly tabify).
 1.100 23-May-2019  msaitoh No functional change:
- Change ac(was arpcom) to ec(ethercom)
- Simplify MII structure initialization.
- u_int*_t -> uint*_t.
- KNF
 1.99 05-Feb-2019  msaitoh Remove very old IFF_NOTRAILERS flag.
 1.98 03-Sep-2018  riastradh Rename min/max -> uimin/uimax for better honesty.

These functions are defined on unsigned int. The generic name
min/max should not silently truncate to 32 bits on 64-bit systems.
This is purely a name change -- no functional change intended.

HOWEVER! Some subsystems have

#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))

even though our standard name for that is MIN/MAX. Although these
may invite multiple evaluation bugs, these do _not_ cause integer
truncation.

To avoid `fixing' these cases, I first changed the name in libkern,
and then compile-tested every file where min/max occurred in order to
confirm that it failed -- and thus confirm that nothing shadowed
min/max -- before changing it.

I have left a handful of bootloaders that are too annoying to
compile-test, and some dead code:

cobalt ews4800mips hp300 hppa ia64 luna68k vax
acorn32/if_ie.c (not included in any kernels)
macppc/if_gm.c (superseded by gem(4))

It should be easy to fix the fallout once identified -- this way of
doing things fails safe, and the goal here, after all, is to _avoid_
silent integer truncations, not introduce them.

Maybe one day we can reintroduce min/max as type-generic things that
never silently truncate. But we should avoid doing that for a while,
so that existing code has a chance to be detected by the compiler for
conversion to uimin/uimax without changing the semantics until we can
properly audit it all. (Who knows, maybe in some cases integer
truncation is actually intended!)
 1.97 26-Jun-2018  msaitoh branches: 1.97.2;
Implement the BPF direction filter (BIOC[GS]DIRECTION). It provides backward
compatibility with BIOC[GS]SEESENT ioctl. The userland interface is the same
as FreeBSD.

This change also fixes a bug that the direction is misunderstand on some
environment by passing the direction to bpf_mtap*() instead of checking
m->m_pkthdr.rcvif.
 1.96 22-Jun-2018  msaitoh It's not required to include net/bpfdesc.h. Remove it.
 1.95 23-Oct-2017  msaitoh branches: 1.95.2;
If if_initialize() failed in the attach function, free resources and return.
 1.94 15-Dec-2016  ozaki-r branches: 1.94.8;
Move bpf_mtap and if_ipackets++ on Rx of each driver to percpuq if_input

The benefits of the change are:
- We can reduce codes
- We can provide the same behavior between drivers
- Where/When if_ipackets is counted up
- Note that some drivers still update packet statistics in their own
way (periodical update)
- Moved bpf_mtap run in softint
- This makes it easy to MP-ify bpf

Proposed on tech-kern and tech-net
 1.93 20-Nov-2016  mlelstv Make scsipi framework MPSAFE.

Data structures are now protected by a per-adapter mutex at IPL_BIO
that is created by the scsibus or atapibus instance when the adapter
is configured.
The enable reference counter and the channel freeze counter which are
currently used by HBA code before the adapter is configured, are made
atomic.
The target drivers are now all tagged as D_MPSAFE.

Almost all HBA drivers still require the kernel lock to present,
so all callbacks into HBA code are still protected by kernel lock
unless the driver is tagged as SCSIPI_ADAPT_MPSAFE.

TODO: refactor sd and cd to use dksubr.
 1.92 02-Oct-2016  christos MFREE -> m_free
 1.91 07-Sep-2016  jakllsch '#if 0' a static inline function used only by an already #if 0 function.
 1.90 10-Jun-2016  ozaki-r branches: 1.90.2;
Introduce m_set_rcvif and m_reset_rcvif

The API is used to set (or reset) a received interface of a mbuf.
They are counterpart of m_get_rcvif, which will come in another
commit, hide internal of rcvif operation, and reduce the diff of
the upcoming change.

No functional change.
 1.89 09-Feb-2016  ozaki-r Introduce softint-based if_input

This change intends to run the whole network stack in softint context
(or normal LWP), not hardware interrupt context. Note that the work is
still incomplete by this change; to that end, we also have to softint-ify
if_link_state_change (and bpf) which can still run in hardware interrupt.

This change softint-ifies at ifp->if_input that is called from
each device driver (and ieee80211_input) to ensure Layer 2 runs
in softint (e.g., ether_input and bridge_input). To this end,
we provide a framework (called percpuq) that utlizes softint(9)
and percpu ifqueues. With this patch, rxintr of most drivers just
queues received packets and schedules a softint, and the softint
dequeues packets and does rest packet processing.

To minimize changes to each driver, percpuq is allocated in struct
ifnet for now and that is initialized by default (in if_attach).
We probably have to move percpuq to softc of each driver, but it's
future work. At this point, only wm(4) has percpuq in its softc
as a reference implementation.

Additional information including performance numbers can be found
in the thread at tech-kern@ and tech-net@:
http://mail-index.netbsd.org/tech-kern/2016/01/14/msg019997.html

Acknowledgment: riastradh@ greatly helped this work.
Thank you very much!
 1.88 24-Aug-2015  pooka would you like some freshly ground _KERNEL_OPT with that?
yes? excellent choice, sir/madam.
 1.87 25-Jul-2014  dholland branches: 1.87.4;
Add d_discard to all struct cdevsw instances I could find.

All have been set to "nodiscard"; some should get a real implementation.
 1.86 16-Mar-2014  dholland branches: 1.86.2;
Change (mostly mechanically) every cdevsw/bdevsw I can find to use
designated initializers.

I have not built every extant kernel so I have probably broken at
least one build; however I've also found and fixed some wrong
cdevsw/bdevsw entries so even if so I think we come out ahead.
 1.85 27-Oct-2012  chs branches: 1.85.2;
split device_t/softc for all remaining drivers.
replace "struct device *" with "device_t".
use device_xname(), device_unit(), etc.
 1.84 03-Feb-2012  christos branches: 1.84.6;
ansi prototypes
 1.83 02-Apr-2011  mbalmer branches: 1.83.4; 1.83.8;
Fix misplaced parenthesis. From henning.petersen@t-online.de, thanks.
 1.82 27-Jul-2010  jakllsch branches: 1.82.2;
Use %zu instead of %d for size_t.
Just in case anyone ever debugs se(4) on LP64.
 1.81 05-Apr-2010  joerg Push the bpf_ops usage back into bpf.h. Push the common ifp->if_bpf
check into the inline functions as well the fourth argument for
bpf_attach.
 1.80 19-Jan-2010  pooka branches: 1.80.2; 1.80.4;
Redefine bpf linkage through an always present op vector, i.e.
#if NBPFILTER is no longer required in the client. This change
doesn't yet add support for loading bpf as a module, since drivers
can register before bpf is attached. However, callers of bpf can
now be modularized.

Dynamically loadable bpf could probably be done fairly easily with
coordination from the stub driver and the real driver by registering
attachments in the stub before the real driver is loaded and doing
a handoff. ... and I'm not going to ponder the depths of unload
here.

Tested with i386/MONOLITHIC, modified MONOLITHIC without bpf and rump.
 1.79 21-Oct-2009  rmind Remove uarea swap-out functionality:

- Addresses the issue described in PR/38828.
- Some simplification in threading and sleepq subsystems.
- Eliminates pmap_collect() and, as a side note, allows pmap optimisations.
- Eliminates XS_CTL_DATA_ONSTACK in scsipi code.
- Avoids few scans on LWP list and thus potentially long holds of proc_lock.
- Cuts ~1.5k lines of code. Reduces amd64 kernel size by ~4k.
- Removes __SWAP_BROKEN cases.

Tested on x86, mips, acorn32 (thanks <mpumford>) and partly tested on
acorn26 (thanks to <bjh21>).

Discussed on <tech-kern>, reviewed by <ad>.
 1.78 12-May-2009  cegger struct device * -> device_t, no functional changes intended.
 1.77 12-May-2009  cegger struct cfdata * -> cfdata_t, no functional changes intended.
 1.76 14-Mar-2009  dsl ANSIfy another 1261 function definitions.
The only ones left in sys are beyond by sed script!
(or in sys/dist or sys/external)
Mostly they have function pointer parameters.
 1.75 14-Mar-2009  dsl Change about 4500 of the K&R function definitions to ANSI ones.
There are still about 1600 left, but they have ',' or /* ... */
in the actual variable definitions - which my awk script doesn't handle.
There are also many that need () -> (void).
(The script does handle misordered arguments.)
 1.74 11-Jan-2009  cegger branches: 1.74.2;
make this compile
 1.73 07-Nov-2008  dyoung *** Summary ***

When a link-layer address changes (e.g., ifconfig ex0 link
02:de:ad:be:ef:02 active), send a gratuitous ARP and/or a Neighbor
Advertisement to update the network-/link-layer address bindings
on our LAN peers.

Refuse a change of ethernet address to the address 00:00:00:00:00:00
or to any multicast/broadcast address. (Thanks matt@.)

Reorder ifnet ioctl operations so that driver ioctls may inherit
the functions of their "class"---ether_ioctl(), fddi_ioctl(), et
cetera---and the class ioctls may inherit from the generic ioctl,
ifioctl_common(), but both driver- and class-ioctls may override
the generic behavior. Make network drivers share more code.

Distinguish a "factory" link-layer address from others for the
purposes of both protecting that address from deletion and computing
EUI64.

Return consistent, appropriate error codes from network drivers.

Improve readability. KNF.

*** Details ***

In if_attach(), always initialize the interface ioctl routine,
ifnet->if_ioctl, if the driver has not already initialized it.
Delete if_ioctl == NULL tests everywhere else, because it cannot
happen.

In the ioctl routines of network interfaces, inherit common ioctl
behaviors by calling either ifioctl_common() or whichever ioctl
routine is appropriate for the class of interface---e.g., ether_ioctl()
for ethernets.

Stop (ab)using SIOCSIFADDR and start to use SIOCINITIFADDR. In
the user->kernel interface, SIOCSIFADDR's argument was an ifreq,
but on the protocol->ifnet interface, SIOCSIFADDR's argument was
an ifaddr. That was confusing, and it would work against me as I
make it possible for a network interface to overload most ioctls.
On the protocol->ifnet interface, replace SIOCSIFADDR with
SIOCINITIFADDR. In ifioctl(), return EPERM if userland tries to
invoke SIOCINITIFADDR.

In ifioctl(), give the interface the first shot at handling most
interface ioctls, and give the protocol the second shot, instead
of the other way around. Finally, let compatibility code (COMPAT_OSOCK)
take a shot.

Pull device initialization out of switch statements under
SIOCINITIFADDR. For example, pull ..._init() out of any switch
statement that looks like this:

switch (...->sa_family) {
case ...:
..._init();
...
break;
...
default:
..._init();
...
break;
}

Rewrite many if-else clauses that handle all permutations of IFF_UP
and IFF_RUNNING to use a switch statement,

switch (x & (IFF_UP|IFF_RUNNING)) {
case 0:
...
break;
case IFF_RUNNING:
...
break;
case IFF_UP:
...
break;
case IFF_UP|IFF_RUNNING:
...
break;
}

unifdef lots of code containing #ifdef FreeBSD, #ifdef NetBSD, and
#ifdef SIOCSIFMTU, especially in fwip(4) and in ndis(4).

In ipw(4), remove an if_set_sadl() call that is out of place.

In nfe(4), reuse the jumbo MTU logic in ether_ioctl().

Let ethernets register a callback for setting h/w state such as
promiscuous mode and the multicast filter in accord with a change
in the if_flags: ether_set_ifflags_cb() registers a callback that
returns ENETRESET if the caller should reset the ethernet by calling
if_init(), 0 on success, != 0 on failure. Pull common code from
ex(4), gem(4), nfe(4), sip(4), tlp(4), vge(4) into ether_ioctl(),
and register if_flags callbacks for those drivers.

Return ENOTTY instead of EINVAL for inappropriate ioctls. In
zyd(4), use ENXIO instead of ENOTTY to indicate that the device is
not any longer attached.

Add to if_set_sadl() a boolean 'factory' argument that indicates
whether a link-layer address was assigned by the factory or some
other source. In a comment, recommend using the factory address
for generating an EUI64, and update in6_get_hw_ifid() to prefer a
factory address to any other link-layer address.

Add a routing message, RTM_LLINFO_UPD, that tells protocols to
update the binding of network-layer addresses to link-layer addresses.
Implement this message in IPv4 and IPv6 by sending a gratuitous
ARP or a neighbor advertisement, respectively. Generate RTM_LLINFO_UPD
messages on a change of an interface's link-layer address.

In ether_ioctl(), do not let SIOCALIFADDR set a link-layer address
that is broadcast/multicast or equal to 00:00:00:00:00:00.

Make ether_ioctl() call ifioctl_common() to handle ioctls that it
does not understand.

In gif(4), initialize if_softc and use it, instead of assuming that
the gif_softc and ifp overlap.

Let ifioctl_common() handle SIOCGIFADDR.

Sprinkle rtcache_invariants(), which checks on DIAGNOSTIC kernels
that certain invariants on a struct route are satisfied.

In agr(4), rewrite agr_ioctl_filter() to be a bit more explicit
about the ioctls that we do not allow on an agr(4) member interface.

bzero -> memset. Delete unnecessary casts to void *. Use
sockaddr_in_init() and sockaddr_in6_init(). Compare pointers with
NULL instead of "testing truth". Replace some instances of (type
*)0 with NULL. Change some K&R prototypes to ANSI C, and join
lines.
 1.72 08-Jun-2008  tsutsui branches: 1.72.4; 1.72.6;
Use device_lookup_private() rather than using cd_devs[] directly to get softc.

XXX maybe we should change a type of cd_devs[] in struct cfdriver
from (void *) to device_t.
 1.71 05-Apr-2008  cegger branches: 1.71.2; 1.71.4; 1.71.6;
use aprint_*_dev and device_xname
 1.70 20-Dec-2007  dyoung branches: 1.70.6;
Constify struct ifnet->if_sadl and every use throughout the tree.
Add if_set_sadl() that both sets the link-layer address length and
replaces the current link-layer address with a new one, and use it
throughout the tree.
 1.69 01-Sep-2007  dyoung branches: 1.69.8; 1.69.12;
Remark that the data-length argument passed to se_scsipi_cmd() is
questionable.
 1.68 01-Sep-2007  dyoung Create a temporary, non-const copy of a sockaddr. Pass that to
se_set_multi() or se_remove_multi(), because neither is easily
constified. Thanks jmmv@ for reporting the issue.
 1.67 01-Sep-2007  dyoung Change a bazillion occurrences of code resembling this,

error = (cmd == SIOCADDMULTI) ?
ether_addmulti(ifr, &sc->sc_ec) :
ether_delmulti(ifr, &sc->sc_ec);

if (error == ENETRESET) {

to this,

if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {

which does the same thing.

(A bazillion is a very large number. This seems to make the i386
ALL kernel smaller by 3kB to 4kB.)

Use ifreq_getaddr() twice in es(4).

Whitespace nits.
 1.66 09-Jul-2007  ad branches: 1.66.2; 1.66.6; 1.66.8;
Merge some of the less invasive changes from the vmlocking branch:

- kthread, callout, devsw API changes
- select()/poll() improvements
- miscellaneous MT safety improvements
 1.65 04-Mar-2007  yamt branches: 1.65.2; 1.65.4;
fix fallout from caddr_t changes.
 1.64 04-Mar-2007  christos Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.63 13-Jan-2007  cube branches: 1.63.2;
Complete initializers so those files compile.
 1.62 07-Sep-2006  dogcow branches: 1.62.2;
remove more vestiges of CCITT, LLC, HDLC, NS, and NSIP.
 1.61 30-Mar-2006  thorpej Use device_private().
 1.60 24-Dec-2005  perry branches: 1.60.4; 1.60.6; 1.60.8; 1.60.10; 1.60.12;
Remove leading __ from __(const|inline|signed|volatile) -- it is obsolete.
 1.59 11-Dec-2005  christos merge ktrace-lwp.
 1.58 03-Jun-2005  jdc branches: 1.58.2;
Remove cast to non-const and make this compile again.
 1.57 27-Feb-2005  perry nuke trailing whitespace
 1.56 04-Feb-2005  perry de-__P
 1.55 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.54 31-Jan-2005  simonb Revert incorrect u_long->uint32_t conversion.
 1.53 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.52 31-Jan-2005  reinoud As part of cleaning up sys/scsipi, replace all u_char by uint8_t and
replace all `short' with int16_t.
 1.51 30-Oct-2004  thorpej branches: 1.51.4; 1.51.6;
When adding/deleting multicast addresses, only whack the address
filter if the interface is marked RUNNING.

Fixes kern/27678.
 1.50 18-Sep-2004  mycroft Standardize some variable names and the calling pattern for scsipi_command().
Use void pointer casts.
 1.49 17-Sep-2004  mycroft Remove the "xfer" argument to scsipi_command().
 1.48 17-Sep-2004  mycroft Do not manipulate xs->bp in "generic" code -- do it only in the psw_done
routine. As part of this, pass down our pre-parsed error code -- though this
interface will probably change later to accomodate better error handling.
 1.47 09-Sep-2004  bouyer Make the xxstart() functions reentrant again, as some drivers HBA can call
scsipi_done() from their scsipi_request().
For this, add a struct scsipi_xfer * argument to scsipi_command().
If not NULL scsipi_command() will use this to enqueue this xfer, otherwise
it'll try to allocate a new one. This scsipi_xfer has to be allocated
and initialised by scsipi_make_xs() or equivalent.
In xxstart(), allocate a scsipi_xfer using scsipi_make_xs(), and if not NULL,
dequeue the buffer before calling scsipi_command(). This makes sure that
scsipi_command() will not fail, and also makes sure that xxstart() won't
be called again between the BUFQ_PEEK() and BUFQ_GET().

Fix "dequeued wrong buf" panics reported by Juergen Hannken-Illjes in
private mail and Andreas Wrede on current-users@.
Thanks to Jason Thorpe and Chuck Silver for review, and Andreas Wrede for
testing the patch.
 1.46 23-Apr-2004  itojun use bounded string ops
 1.45 08-Sep-2003  mycroft branches: 1.45.2;
Use the const shaker.
 1.44 23-Oct-2002  jdolecek branches: 1.44.6;
merge kqueue branch into -current

kqueue provides a stateful and efficient event notification framework
currently supported events include socket, file, directory, fifo,
pipe, tty and device changes, and monitoring of processes and signals

kqueue is supported by all writable filesystems in NetBSD tree
(with exception of Coda) and all device drivers supporting poll(2)

based on work done by Jonathan Lemon for FreeBSD
initial NetBSD port done by Luke Mewburn and Jason Thorpe
 1.43 02-Oct-2002  thorpej Add trailing ; to CFATTACH_DECL.
 1.42 30-Sep-2002  thorpej Use CFATTACH_DECL().
 1.41 27-Sep-2002  thorpej Declare all cfattach structures const.
 1.40 06-Sep-2002  gehenna Merge the gehenna-devsw branch into the trunk.

This merge changes the device switch tables from static array to
dynamically generated by config(8).

- All device switches is defined as a constant structure in device drivers.

- The new grammer ``device-major'' is introduced to ``files''.

device-major <prefix> char <num> [block <num>] [<rules>]

- All device major numbers must be listed up in port dependent majors.<arch>
by using this grammer.

- Added the new naming convention.
The name of the device switch must be <prefix>_[bc]devsw for auto-generation
of device switch tables.

- The backward compatibility of loading block/character device
switch by LKM framework is broken. This is necessary to convert
from block/character device major to device name in runtime and vice versa.

- The restriction to assign device major by LKM is completely removed.
We don't need to reserve LKM entries for dynamic loading of device switch.

- In compile time, device major numbers list is packed into the kernel and
the LKM framework will refer it to assign device major number dynamically.
 1.39 15-Nov-2001  lukem branches: 1.39.8;
don't need <sys/types.h> when including <sys/param.h>
 1.38 13-Nov-2001  lukem add RCSIDs
 1.37 18-Jul-2001  thorpej branches: 1.37.2;
bcmp -> memcmp
 1.36 18-Jul-2001  thorpej bcopy -> memcpy
 1.35 18-Jul-2001  thorpej bzero -> memset
 1.34 25-Apr-2001  bouyer branches: 1.34.2;
Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.33 14-Dec-2000  thorpej branches: 1.33.2;
ALTQ'ify.
 1.32 15-Nov-2000  thorpej Move bpfattach()/bpfdetach() calls into ether_ifattach()/ether_ifdetach().
 1.31 01-Oct-2000  thorpej Move the check for "promisc + unicast + not for us" into ether_input(),
and change Ethernet drivers to always pass all received frames to
ether_input() (with a few exceptions, which are documented in the
code).
 1.30 09-Jun-2000  enami Prevent a process being swapped out during I/O if the data buffer is
allocated on stack. This potential problem is noticed by Noriyuki Soda
and the idea and sample code to fix is given by Jason R. Thorpe.
 1.29 30-Mar-2000  augustss branches: 1.29.2;
Get rid of register declarations.
 1.28 23-Mar-2000  thorpej New callout mechanism with two major improvements over the old
timeout()/untimeout() API:
- Clients supply callout handle storage, thus eliminating problems of
resource allocation.
- Insertion and removal of callouts is constant time, important as
this facility is used quite a lot in the kernel.

The old timeout()/untimeout() API has been removed from the kernel.
 1.27 13-Mar-2000  soren Fix doubled 'the's in comments.
 1.26 13-Nov-1999  matthias Make this work again by adding XS_CTL_ASYNC to the SCSI requests in
se_ifstart and se_recv.
 1.25 30-Sep-1999  thorpej branches: 1.25.2; 1.25.4; 1.25.8;
Cleanup the scsipi_xfer flags:
- `flags' is now gone, replaced with `xs_control' and `xs_status'.
- Massive cleanup of the control flags. Now we explicitly say that
a job is to complete asynchronously, rather than relying on side-effects,
and use a new flag to now that device discovery is being performed.
- Do SCSI device discovery interrupt-driven.
 1.24 18-May-1999  thorpej Rework layer 2 protocol input routines. Instead of calling e.g. ether_input()
directly, call the function pointer (*if_input)(ifp, m). The input routine
expects the packet header to be at the head of the packet, and will adjust
as necessary. Privatize the layer 2 input and output routines, allowing
*_ifattach() to set them up as appropriate.
 1.23 12-Dec-1998  mycroft branches: 1.23.4;
Simplify the copy loops a bit.
 1.22 08-Dec-1998  thorpej When closing, wait for pending xfers to drain before deleting the reference
to the adapter.
 1.21 20-Nov-1998  thorpej Add adapter reference counting for SCSI and ATAPI devices.
 1.20 13-Oct-1998  kim Use ETHERTYPE_ATALK instead of ETHERTYPE_AT. The former seems more common.
Our other constants also use "ATALK".

Added many new ETHERTYPE constants to sys/net/ethertypes.h, including the
ones from libpcap and tcpdump "ethertype.h" files.
 1.19 25-Sep-1998  is correct obvious typo
 1.18 31-Aug-1998  cgd kill the last remnants of __BROKEN_INDIRECT_CONFIG. (only the pica port
used it, and it's non-working and apparently slated for replacement.)
 1.17 05-Jul-1998  jonathan defopt NS, NSIP.
 1.16 05-Jul-1998  jonathan defopt LLC
 1.15 05-Jul-1998  jonathan defopt CCITT.
 1.14 05-Jul-1998  jonathan defopt INET, NETATALK.
 1.13 12-Jan-1998  thorpej Adjust for changes to config.
 1.12 18-Oct-1997  thorpej Implement two macros, scsipi_command() and scsipi_command_direct(), and
use them to hide the structure of the function pointers we jump through
to issue a command.
 1.11 14-Oct-1997  matthias Apply Ian Dall's patch from kern/4004. Ian's comment:
The following patch employs a smarter adaptive polling
scheme. It also improves the comments, in particular giving
due credit to Phil Budne for his efforts in nunderstanding
the device. It also relaxes the pattern for auto config so
more devices match.
 1.10 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.9 27-Aug-1997  bouyer branches: 1.9.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.8 28-Apr-1997  mycroft Oops; forgot to GC the last mbuf allocated when out of clusters.
 1.7 24-Apr-1997  mycroft Fix typo in previous.
 1.6 24-Apr-1997  mycroft If we fail to allocate a cluster to hold a large packet, simply
drop it rather than using a chain of tiny mbufs.
Do not force the initial part of a packet into a separate mbuf.
 1.5 04-Apr-1997  matthias * Do some KNFing.
* Make all local functions static.
* Add some untested netatalk support.
 1.4 02-Apr-1997  mycroft Push the buffer cleanup code into scsi_done(), and split it so that biodone()
is called *after* the driver `done' routine. This fixes disk I/O statistics
on SCSI devices.

Also, calling the `done' routine with a `complete' argument of 0 and actually
having it do anything meaningful loses in at least 3 ways, so just nuke the
argument altogether and don't call it this way. If the driver needs to do
some error handling, that's what `err_handler' is for.
 1.3 24-Mar-1997  thorpej Fix a screwup (my fault, oops) that caused the stack to get corrupted.
From Ian Dall <Ian.Dall@dsto.defence.gov.au> on port-pc532.
 1.2 18-Mar-1997  cgd add appropriate includes so that this deals properly with NS and CCITT+LLC.
(Includes stolen from dev/ic/am7990.c, because it's a (the?) canonical
networking hardware driver.)
 1.1 18-Mar-1997  thorpej Driver for the Cabletron EA41x SCSI Ethernet Adaptor, written by
Ian Dall <ian.dall@dsto.defence.gov.au>. Converted to "new arp"
and some other (very) minor changes by me.
 1.9.2.4 15-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.9.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.9.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.9.2.1 27-Aug-1997  thorpej file if_se.c was added on branch marc-pcmcia on 1997-08-27 23:33:07 +0000
 1.23.4.1 21-Jun-1999  thorpej Sync w/ -current.
 1.25.8.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.25.4.1 15-Nov-1999  fvdl Sync with -current
 1.25.2.5 05-Jan-2001  bouyer Sync with HEAD
 1.25.2.4 22-Nov-2000  bouyer Sync with HEAD.
 1.25.2.3 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.25.2.2 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.25.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.29.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.33.2.7 11-Nov-2002  nathanw Catch up to -current
 1.33.2.6 18-Oct-2002  nathanw Catch up to -current.
 1.33.2.5 17-Sep-2002  nathanw Catch up to -current.
 1.33.2.4 08-Jan-2002  nathanw Catch up to -current.
 1.33.2.3 14-Nov-2001  nathanw Catch up to -current.
 1.33.2.2 24-Aug-2001  nathanw Catch up with -current.
 1.33.2.1 21-Jun-2001  nathanw Catch up to -current.
 1.34.2.3 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.34.2.2 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.34.2.1 03-Aug-2001  lukem update to -current
 1.37.2.2 26-Sep-2001  fvdl * add a VCLONED vnode flag that indicates a vnode representing a cloned
device.
* rename REVOKEALL to REVOKEALIAS, and add a REVOKECLONE flag, to pass
to VOP_REVOKE
* the revoke system call will revoke all aliases, as before, but not the
clones
* vdevgone is called when detaching a device, so make it use REVOKECLONE
to get rid of all clones as well
* clean up all uses of VOP_OPEN wrt. locking.
* add a few VOPS to spec_vnops that need to do something when it's a
clone vnode (access and getattr)
* add a copy of the vnode vattr structure of the original 'master' vnode
to the specinfo of a cloned vnode. could possibly redirect getattr to
the 'master' vnode, but this has issues with revoke
* add a vdev_reassignvp function that disassociates a vnode from its
original device, and reassociates it with the specified dev_t. to be
used by cloning devices only, in case a new minor is allocated.
* change all direct references in drivers to v_devcookie and v_rdev
to vdev_privdata(vp) and vdev_rdev(vp). for diagnostic purposes
when debugging race conditions that still exist wrt. locking and
revoking vnodes.
* make the locking state of a vnode consistent when passed to
d_open and d_close (unlocked). locked would be better, but has
some deadlock issues
 1.37.2.1 07-Sep-2001  thorpej Commit my "devvp" changes to the thorpej-devvp branch. This
replaces the use of dev_t in most places with a struct vnode *.

This will form the basic infrastructure for real cloning device
support (besides being architecurally cleaner -- it'll be good
to get away from using numbers to represent objects).
 1.39.8.1 16-May-2002  gehenna Add the character device switch.
 1.44.6.8 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.44.6.7 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.44.6.6 04-Feb-2005  skrll Sync with HEAD.
 1.44.6.5 21-Nov-2004  skrll Adapt to branch. Alpha kernels now compile.
 1.44.6.4 02-Nov-2004  skrll Sync with HEAD.
 1.44.6.3 21-Sep-2004  skrll Fix the sync with head I botched.
 1.44.6.2 18-Sep-2004  skrll Sync with HEAD.
 1.44.6.1 03-Aug-2004  skrll Sync with HEAD
 1.45.2.1 11-Sep-2004  he branches: 1.45.2.1.2;
Pull up revision 1.47 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.45.2.1.2.1 24-Jan-2005  he Pull up revision 1.51 (requested by thorpej in ticket #939):
When adding or deleting multicast addresses, only change
the address filter if the interface is marked RUNNING.
Fixes PR#27678.
 1.51.6.2 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.51.6.1 12-Feb-2005  yamt sync with head.
 1.51.4.1 29-Apr-2005  kent sync with -current
 1.58.2.5 21-Jan-2008  yamt sync with head
 1.58.2.4 03-Sep-2007  yamt sync with head.
 1.58.2.3 26-Feb-2007  yamt sync with head.
 1.58.2.2 30-Dec-2006  yamt sync with head.
 1.58.2.1 21-Jun-2006  yamt sync with head.
 1.60.12.1 31-Mar-2006  tron Merge 2006-03-31 NetBSD-current into the "peter-altq" branch.
 1.60.10.1 19-Apr-2006  elad sync with head.
 1.60.8.2 14-Sep-2006  yamt sync with head.
 1.60.8.1 01-Apr-2006  yamt sync with head.
 1.60.6.1 22-Apr-2006  simonb Sync with head.
 1.60.4.1 09-Sep-2006  rpaulo sync with head
 1.62.2.1 01-Feb-2007  ad Sync with head.
 1.63.2.1 12-Mar-2007  rmind Sync with HEAD.
 1.65.4.1 11-Jul-2007  mjf Sync with head.
 1.65.2.2 09-Oct-2007  ad Sync with head.
 1.65.2.1 01-Jul-2007  ad Adapt to callout API change.
 1.66.8.2 09-Jan-2008  matt sync with HEAD
 1.66.8.1 06-Nov-2007  matt sync with HEAD
 1.66.6.1 03-Sep-2007  jmcneill Sync with HEAD.
 1.66.2.1 03-Sep-2007  skrll Sync with HEAD.
 1.69.12.1 02-Jan-2008  bouyer Sync with HEAD
 1.69.8.1 26-Dec-2007  ad Sync with head.
 1.70.6.3 17-Jan-2009  mjf Sync with HEAD.
 1.70.6.2 29-Jun-2008  mjf Sync with HEAD.
 1.70.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.71.6.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.71.4.4 11-Aug-2010  yamt sync with head.
 1.71.4.3 11-Mar-2010  yamt sync with head
 1.71.4.2 16-May-2009  yamt sync with head
 1.71.4.1 04-May-2009  yamt sync with head.
 1.71.2.1 17-Jun-2008  yamt sync with head.
 1.72.6.2 28-Apr-2009  skrll Sync with HEAD.
 1.72.6.1 19-Jan-2009  skrll Sync with HEAD.
 1.72.4.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.74.2.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.80.4.3 21-Apr-2011  rmind sync with head
 1.80.4.2 05-Mar-2011  rmind sync with head
 1.80.4.1 30-May-2010  rmind sync with head
 1.80.2.2 17-Aug-2010  uebayasi Sync with HEAD.
 1.80.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.82.2.1 06-Jun-2011  jruoho Sync with HEAD.
 1.83.8.1 18-Feb-2012  mrg merge to -current.
 1.83.4.3 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.83.4.2 30-Oct-2012  yamt sync with head
 1.83.4.1 17-Apr-2012  yamt sync with head
 1.84.6.3 03-Dec-2017  jdolecek update from HEAD
 1.84.6.2 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.84.6.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.85.2.1 18-May-2014  rmind sync with head
 1.86.2.1 10-Aug-2014  tls Rebase.
 1.87.4.6 05-Feb-2017  skrll Sync with HEAD
 1.87.4.5 05-Dec-2016  skrll Sync with HEAD
 1.87.4.4 05-Oct-2016  skrll Sync with HEAD
 1.87.4.3 09-Jul-2016  skrll Sync with HEAD
 1.87.4.2 19-Mar-2016  skrll Sync with HEAD
 1.87.4.1 22-Sep-2015  skrll Sync with HEAD
 1.90.2.2 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.90.2.1 04-Nov-2016  pgoyette Sync with HEAD
 1.94.8.2 26-Jul-2018  snj Pull up following revision(s) (requested by msaitoh in ticket #938):
sys/arch/acorn32/podulebus/if_ie.c: revision 1.41
sys/arch/amiga/dev/if_es.c: revision 1.58
sys/arch/amiga/dev/if_qn.c: revision 1.45
sys/arch/arm/at91/at91emac.c: revision 1.20
sys/arch/arm/ep93xx/epe.c: revision 1.37
sys/arch/emips/ebus/if_le_ebus.c: revision 1.14
sys/arch/emips/ebus/if_le_ebus.c: revision 1.15
sys/arch/mac68k/dev/if_mc.c: revision 1.46
sys/arch/macppc/dev/am79c950.c: revision 1.39
sys/arch/newsmips/apbus/if_sn.c: revision 1.40
sys/arch/next68k/dev/mb8795.c: revision 1.59
sys/arch/playstation2/dev/if_smap.c: revision 1.25
sys/arch/playstation2/dev/if_smap.c: revision 1.26
sys/arch/sun2/dev/if_ec.c: revision 1.28
sys/arch/sun3/dev/if_ie.c: revision 1.63
sys/arch/x68k/dev/if_ne_intio.c: revision 1.19
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.75
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.63
sys/dev/bi/if_ni.c: revision 1.45
sys/dev/cadence/if_cemac.c: revision 1.12
sys/dev/ic/am7990.c: revision 1.78
sys/dev/ic/am79900.c: revision 1.27
sys/dev/ic/an.c: revision 1.67
sys/dev/ic/cs89x0.c: revision 1.40
sys/dev/ic/dm9000.c: revision 1.13
sys/dev/ic/dm9000.c: revision 1.14
sys/dev/ic/dp8390.c: revision 1.88
sys/dev/ic/elink3.c: revision 1.141
sys/dev/ic/elinkxl.c: revision 1.122
sys/dev/ic/hme.c: revision 1.98
sys/dev/ic/i82586.c: revision 1.77
sys/dev/ic/lance.c: revision 1.53
sys/dev/ic/mb86950.c: revision 1.27
sys/dev/ic/mb86960.c: revision 1.86
sys/dev/ic/mtd803.c: revision 1.34
sys/dev/ic/pdq_ifsubr.c: revision 1.59
sys/dev/ic/rrunner.c: revision 1.86
sys/dev/ic/seeq8005.c: revision 1.58
sys/dev/ic/sgec.c: revision 1.47
sys/dev/ic/smc90cx6.c: revision 1.72
sys/dev/ic/smc91cxx.c: revision 1.96
sys/dev/ic/tropic.c: revision 1.49
sys/dev/ic/wi.c: revision 1.245
sys/dev/isa/if_eg.c: revision 1.93
sys/dev/isa/if_el.c: revision 1.95
sys/dev/isa/if_iy.c: revision 1.101
sys/dev/ofw/ofnet.c: revision 1.58
sys/dev/pci/if_alc.c: revision 1.27
sys/dev/pci/if_de.c: revision 1.152
sys/dev/pci/if_fpa.c: revision 1.61
sys/dev/pci/if_jme.c: revision 1.34
sys/dev/pci/if_tl.c: revision 1.108
sys/dev/pci/if_vte.c: revision 1.19
sys/dev/pci/ixgbe/ixgbe.h: revision 1.50
sys/dev/pcmcia/if_cnw.c: revision 1.62
sys/dev/pcmcia/if_malo_pcmcia.c: revision 1.17
sys/dev/pcmcia/if_ray.c: revision 1.89
sys/dev/pcmcia/if_xi.c: revision 1.81
sys/dev/pcmcia/mhzc.c: revision 1.51
sys/dev/pcmcia/xirc.c: revision 1.34
sys/dev/qbus/if_de.c: revision 1.33
sys/dev/qbus/if_qe.c: revision 1.78
sys/dev/qbus/if_qt.c: revision 1.22
sys/dev/sbus/be.c: revision 1.87
sys/dev/sbus/qe.c: revision 1.68
sys/dev/scsipi/if_se.c: revision 1.96
sys/dev/usb/if_atu.c: revision 1.59
sys/net/if_l2tp.c: revision 1.28 via patch
sys/net/if_ppp.c: revision 1.160
It's not required to include net/bpfdesc.h. Remove it.
--
Simplify like other drivers. NULL check of ifp->if_bpf is done in
bpf_mtap(), so it's not required to do it here.
--
Remove duplicated inclusion of net/bpf.h.
--
Remove duplicated inclusion of net/bpf.h.
--
Simplify bpf_mtap() call. No functional change.
 1.94.8.1 10-Dec-2017  snj Pull up following revision(s) (requested by msaitoh in ticket #427):
sys/arch/amiga/dev/if_bah_zbus.c: 1.17
sys/arch/arm/broadcom/bcm53xx_eth.c: 1.30
sys/arch/powerpc/booke/dev/pq3etsec.c: 1.32
sys/arch/usermode/dev/if_veth.c: 1.9
sys/dev/ic/an.c: 1.66
sys/dev/ic/athn.c: 1.17
sys/dev/ic/atw.c: 1.162
sys/dev/ic/bwi.c: 1.33
sys/dev/ic/dwc_gmac.c: 1.41-1.42
sys/dev/ic/malo.c: 1.10
sys/dev/ic/rt2560.c: 1.31
sys/dev/ic/rt2661.c: 1.36
sys/dev/ic/rt2860.c: 1.29
sys/dev/ic/rtw.c: 1.127
sys/dev/ic/rtwvar.h: 1.46
sys/dev/ic/smc90cx6.c: 1.71
sys/dev/ic/smc90cx6var.h: 1.12
sys/dev/ic/wi.c: 1.244
sys/dev/pci/if_ipw.c: 1.66
sys/dev/pci/if_iwi.c: 1.104
sys/dev/pci/if_iwm.c: 1.76
sys/dev/pci/if_iwn.c: 1.86
sys/dev/pci/if_rtwn.c: 1.13
sys/dev/pci/if_wm.c: 1.541
sys/dev/pci/if_wpi.c: 1.79
sys/dev/pci/ixgbe/ixgbe.c: 1.106
sys/dev/pci/ixgbe/ixv.c: 1.73 via patch
sys/dev/pcmcia/if_malo_pcmcia.c: 1.15
sys/dev/scsipi/if_se.c: 1.95
sys/dev/usb/if_upl.c: 1.60
sys/net/if.c: 1.396
sys/net/if.h: 1.241
sys/net/if_arc.h: 1.23
sys/net/if_arcsubr.c: 1.78
sys/net/if_bridge.c: 1.136-1.137
sys/net/if_etherip.c: 1.39
sys/net/if_faith.c: 1.56
sys/net/if_gif.c: 1.131
sys/net/if_loop.c: 1.96
sys/net/if_mpls.c: 1.30
sys/net/if_pppoe.c: 1.129
sys/net/if_srt.c: 1.27
sys/net/if_stf.c: 1.102
sys/net/if_tap.c: 1.100
sys/net/if_vlan.c: 1.105
sys/netinet/ip_carp.c: 1.91
sys/rump/net/lib/libshmif/if_shmem.c: 1.73-1.74
sys/rump/net/lib/libvirtif/if_virt.c: 1.55-1.56
if_initalize() and if_attach() failed when resource allocation failed
(e.g. allocating softint). Without this change, it panics. It's bad because
resource shortage really occured when a lot of pseudo interface is created.
To avoid this problem, don't panic and change return value of if_initialize()
and if_attach() to int. Caller fanction will be recover from error cleanly by
checking the return value.
Return if bah_attach_subr() failed.
If if_attach() failed in the attach function, return.
- If if_initialize() failed in the attach function, free resources and return.
- Add some missing frees in bridge_clone_destroy().
- KNF
If error occured in bcmeth_ccb_attach(), free resources and return.
If error occured in pq3etsec_attach(), free resources and return.
If error occured in the attach function, free resources and return.
- If if_initialize() failed in athn_attach(), free resources and return.
- Add missing pmf_event_deregister() in athn_detach().
- Free resources correctly on some errors in atw_attach().
- Use apint*() insread of printf() in the attach function.
If if_initialize() failed in the attach function, return.
- If if_initialize() failed in the attach function, free resources and return.
- Add missing dwc_gmac_free_dma_rings() and mutex_destroy() when attach
failed.
- If if_initialize() failed in the attach function, free resources and return.
- ifp is always not NULL in iwi_detach(). Check correctly with ifp->if_softc.
- If if_initialize() failed in the attach function, free resources and return.
- Fix error path in the attach function correctly.
If if_initialize() failed in the attach function, free resources and return.
If if_attach() failed in the attach function, free resources and return.
- If if_initialize() failed in the attach function, free resources and return.
- KNF
- If if_attach() failed in the attach function, free resources and return.
- KNF
Fix compile error.
Fix compile error.
We don't need '&mii', but just 'mii' for mii_detach().
Don't free sc_rthash twice
 1.95.2.3 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.95.2.2 28-Jul-2018  pgoyette Sync with HEAD
 1.95.2.1 25-Jun-2018  pgoyette Sync with HEAD
 1.97.2.3 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.97.2.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.97.2.1 10-Jun-2019  christos Sync with HEAD
 1.103.2.1 29-Feb-2020  ad Sync with head.
 1.112.6.1 17-Jun-2021  thorpej Sync w/ HEAD.
 1.25 25-Feb-2012  shattered Fix typos in comments.

OK by wiz@
 1.24 11-Dec-2005  christos branches: 1.24.110; 1.24.114;
merge ktrace-lwp.
 1.23 21-Feb-2005  thorpej Part 1 of a cleanup pass over the SCSI subsystem. The aim is to name
everything "scsi_*", since we really are talking about the SCSI command
set, ATAPI transport not withstanding. Improve the names of many structures,
and prepend "SCSI_" onto all SCSI command opcodes. Place items described
by the SCSI Primary Commands document into scsi_spc.h.
 1.22 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.21 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.20 07-Dec-2004  thorpej branches: 1.20.2; 1.20.4;
Nothing references "struct scsi_generic" anymore; delete it.
 1.19 06-Jan-2003  matt branches: 1.19.2;
Add multiple inclusion protection.
 1.18 14-May-2001  bouyer Use SCSI/ATAPI common definition for MODE_{SELECT,SENSE}{,_BIG}. Define
functions to send theses commands in scsipi_base.c and use them instead
of ad-hoc commands setups.
 1.17 19-Apr-2001  augustss Correct the opcode for SCSI_MODE_SENSE_BIG.
 1.16 29-Nov-2000  wiz branches: 1.16.2;
Fix typo in comment reported by Tetsuya Isaki in kern/11587.
 1.15 05-Oct-2000  ad Add some more completion status codes.
 1.14 13-Feb-1998  enami branches: 1.14.14;
Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.13 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.12 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.11 27-Aug-1997  bouyer branches: 1.11.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.10 12-Sep-1996  thorpej Remove a couple of opcodes that are already (and more appropriately)
defined in scsi_changer.h
 1.9 03-Sep-1996  thorpej Store the SCSI version in the scsi_link, and set the LUN in the CDB
if the version is <= SCSI-2. This should help some older SCSI
devices that previously needed the "NOLUNS" quirk. While this is
not strictly necessary on SCSI-2 devices, the spec allows it,
so we set it for SCSI-2 devices "just in case". See section 7.2.2 of
Draft X3T9.2 Rev 10L for details.
 1.8 10-Jul-1996  explorer Add SCSI_QUEUE_FULL ; part of pr port-i386/2600
 1.7 19-Mar-1996  mycroft branches: 1.7.4;
Define a full set of [234][bl]tol() and lto[234][bl]() conversion functions,
inlined.
Use sized types in protocol structures.
Make the definition of scsi_sense_data less ugly.
 1.6 28-Dec-1994  mycroft Numerous changes. Many bugs fixed, better autoconfig, a few new features.
 1.5 29-Jun-1994  cgd New RCS ID's, take two. they're more aesthecially pleasant, and use 'NetBSD'
 1.4 09-May-1994  chopps remove union's from sense_data struct, conditionaly define RAW_PART
 1.3 29-Mar-1994  mycroft New SCSI system, based on Julian's more recent work.
 1.2 20-May-1993  cgd branches: 1.2.3;
add rcsids and clean up file headers
 1.1 21-Mar-1993  cgd after 0.2.2 "stable" patches applied
 1.2.3.1 24-Nov-1993  mycroft Under construction...
 1.7.4.1 18-Jul-1996  jtc Pulled up from rev 1.8, ahc fixes
 1.11.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.11.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.11.2.1 27-Aug-1997  thorpej file scsi_all.h was added on branch marc-pcmcia on 1997-08-27 23:33:09 +0000
 1.14.14.3 21-Apr-2001  bouyer Sync with HEAD
 1.14.14.2 08-Dec-2000  bouyer Sync with HEAD.
 1.14.14.1 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.16.2.2 07-Jan-2003  thorpej Sync with HEAD.
 1.16.2.1 21-Jun-2001  nathanw Catch up to -current.
 1.19.2.2 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.19.2.1 18-Dec-2004  skrll Sync with HEAD.
 1.20.4.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.20.2.1 29-Apr-2005  kent sync with -current
 1.24.114.1 04-Mar-2012  mrg sync to latest -current.
 1.24.110.1 17-Apr-2012  yamt sync with head
 1.93 03-May-2019  mlelstv Avoid null pointer deref in printing xfer mode when no target driver
is attached. Fixes kern/54157.
 1.92 17-Jun-2017  mlelstv branches: 1.92.6;
The atapibus detach path did hold the channel mutex while calling into autoconf,
which would trigger a panic when unplugging a USB ATAPI CDROM.

Align detach code for scsibus and atapibus to fix this.

Also avoid races when detaching devices by replacing callout_stop with
callout_halt.
 1.91 20-Nov-2016  mlelstv branches: 1.91.8;
Make scsipi framework MPSAFE.

Data structures are now protected by a per-adapter mutex at IPL_BIO
that is created by the scsibus or atapibus instance when the adapter
is configured.
The enable reference counter and the channel freeze counter which are
currently used by HBA code before the adapter is configured, are made
atomic.
The target drivers are now all tagged as D_MPSAFE.

Almost all HBA drivers still require the kernel lock to present,
so all callbacks into HBA code are still protected by kernel lock
unless the driver is tagged as SCSIPI_ADAPT_MPSAFE.

TODO: refactor sd and cd to use dksubr.
 1.90 20-Apr-2012  bouyer branches: 1.90.2; 1.90.16; 1.90.20;
Add a bustype_async_event_xfer_mode() callback to scsipi_bustype (which can
be NULL), so that transport-specific details of transfer mode setting/printing
can be handled more easily.
Move scsipi_async_event_xfer_mode() and scsipi_print_xfer_mode() to
scsi_base.c and split in parallel scsi and FC/SAS parts.
size of struct scsipi_bustype has changed, welcome to 6.99.5
 1.89 28-Apr-2008  martin branches: 1.89.34; 1.89.38;
Remove clause 3 and 4 from TNF licenses
 1.88 05-Apr-2008  cegger branches: 1.88.2; 1.88.4;
use aprint_*_dev and device_xname
 1.87 16-Nov-2006  christos branches: 1.87.48;
__unused removal on arguments; approved by core.
 1.86 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.85 11-Dec-2005  christos branches: 1.85.20; 1.85.22;
merge ktrace-lwp.
 1.84 18-Sep-2004  mycroft Standardize some variable names and the calling pattern for scsipi_command().
Use void pointer casts.
 1.83 17-Sep-2004  mycroft Remove the "xfer" argument to scsipi_command().
 1.82 17-Sep-2004  mycroft Change the way bustype_cmd is used. Rather than having it be responsible for
calling scsipi_make_xs() and scsipi_execute_xs(), instead push these into
scsipi_command. Make bustype_cmd and PHOLD/PRELE be called from
scsipi_execute_xs(). This allows us to create a xfer structure -- possibly on
the stack -- and call scsipi_execute_xs() directly.
 1.81 09-Sep-2004  bouyer Make the xxstart() functions reentrant again, as some drivers HBA can call
scsipi_done() from their scsipi_request().
For this, add a struct scsipi_xfer * argument to scsipi_command().
If not NULL scsipi_command() will use this to enqueue this xfer, otherwise
it'll try to allocate a new one. This scsipi_xfer has to be allocated
and initialised by scsipi_make_xs() or equivalent.
In xxstart(), allocate a scsipi_xfer using scsipi_make_xs(), and if not NULL,
dequeue the buffer before calling scsipi_command(). This makes sure that
scsipi_command() will not fail, and also makes sure that xxstart() won't
be called again between the BUFQ_PEEK() and BUFQ_GET().

Fix "dequeued wrong buf" panics reported by Juergen Hannken-Illjes in
private mail and Andreas Wrede on current-users@.
Thanks to Jason Thorpe and Chuck Silver for review, and Andreas Wrede for
testing the patch.
 1.80 27-Aug-2004  bouyer Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
Havard Eidnes's analysis of this problem is that the scsipi_xfer pool is
competing for resources with other pools, including the the inode and vnode
pools which can grow quite large.

*_scsipi_cmd(): don't biodone the buffer if scsipi_make_xs() fails, let the
caller deal with the problem
start function of block devices drivers: dequeue the buffer after the
scsipi_command() call. If scsipi_command() fails with ENOMEM don't dequeue
the buffer, and schedule a callout to call the start function after
some delay.
scsipi_init(): prime the scsipi_xfer_pool with one page. This ensure that
there is always some scsipi_xfer to play with. If scsipi_command() fails
because of pool_get(), we're sure there will be resources available later,
when the pending commands have completed.

Reviewed by Jason Thorpe and Havard Eidnes.
Todo: remove the "unable to allocate scsipi_xfer" and "not queued, error %d"
printfs, but I choose to keep them for now, to help make sure the code does
what it should.
 1.79 21-Aug-2004  thorpej Use ANSI function decls and make use of static.
 1.78 05-Aug-2004  bouyer scsi_kill_pending(): don't kill the commands in periph_xferq here.
The controller is handling them, calling scsipi_done() here will end up in the
xfer being scsipi_done()'ed a second time when it completes in the controller
code. In addition, the way the loop was done here would end up in an infinite
loop, because the channel kernel thread needs to run to remove a command from
this queue.

scsibusdetach(): scsipi_done() all commands from periph_xferq. The controller
is already gone, and these commands will never complete.
Shut down the channel (which will cause the kenrel thread to exit) after
detaching the childs, as they will need the kernel thread for
scsipi_wait_drain().

Fix kernel hang or deadlock when detaching devices (either by scsictl detach
or unplug) with active commands.
 1.77 15-Nov-2001  lukem branches: 1.77.16; 1.77.18;
don't need <sys/types.h> when including <sys/param.h>
 1.76 13-Nov-2001  lukem add RCSIDs
 1.75 18-Jul-2001  thorpej bzero -> memset
 1.74 25-Apr-2001  bouyer branches: 1.74.2;
Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.73 03-Apr-2000  enami branches: 1.73.6;
When killing pending xfers on device detach, we can't expect scsipi_done
to remove all xfers from the pending queue. It removes only xfers for
asynchronous transactions. So, simply loop over all pending xfers
with calling scsipi_done and wait xfers to drain. Addresses PR#9703.
 1.72 17-Mar-2000  soren scsiprint() is needed even without scsibus'es, so move it to scsi_base.c .
 1.71 17-Jan-2000  bouyer Use SCSIPIRETRIES instead of hard-coded value '2' for number of retries in
common routines. Define SCSIPIRETRIES as 4, so that the command will finally
succeed after several consecutive downgrades from UDMA2 to PIO4.
 1.70 20-Oct-1999  enami Cancel active transfers on aic/wdc detach.
Also makes LS-120 drive works for me again.
 1.69 30-Sep-1999  thorpej branches: 1.69.2; 1.69.4; 1.69.6;
Cleanup the scsipi_xfer flags:
- `flags' is now gone, replaced with `xs_control' and `xs_status'.
- Massive cleanup of the control flags. Now we explicitly say that
a job is to complete asynchronously, rather than relying on side-effects,
and use a new flag to now that device discovery is being performed.
- Do SCSI device discovery interrupt-driven.
 1.68 28-Aug-1999  thorpej If scsipi_make_xs() fails, make sure to flag the buffer as error'd,
if provided, and biodone() it. Callers of scsipi_command() expect
this to be done.
 1.67 12-Jun-1999  pk scsipi_free_xs() wants to be called at splbio; do it.
 1.66 17-Nov-1998  bouyer branches: 1.66.4; 1.66.6; 1.66.8;
Rename scsi_interpret_sense() to scsipi_interpret_sense() and move it from
scsi_base.c to scsipi_base.c. Rename the functions from scsi_verbose.c
too, and rename the file itself. Cleaup includes too (scsi_*.h should not
be #included in scsipi_*.h files, which are supposed to be
common to atapi and scsi).
 1.65 15-Aug-1998  mycroft Assign my copyrights to TNF.
 1.64 30-Jul-1998  mjacob branches: 1.64.2;
opt_scsiverbose.h now is opt_scsi.h and contains the defopted
SCSIVERBOSE and the new ST_ENABLE_EARLYWARN.
 1.63 15-Jul-1998  mjacob part of fix for kern/3835: use of enumerated returns from target sense handlers
 1.62 11-Jul-1998  mjacob Clarify that Sense Key 1 is a "RECOVERED ERROR"- not just an error.
 1.61 01-Jul-1998  mjacob Use new Sense Key defines (for clarity)
 1.60 28-Mar-1998  christos Split out the verbose printing of scsi messages, so that user programs
can use the printing routines.
 1.59 05-Feb-1998  mrg initial import of the new virtual memory system, UVM, into -current.

UVM was written by chuck cranor <chuck@maria.wustl.edu>, with some
minor portions derived from the old Mach code. i provided some help
getting swap and paging working, and other bug fixes/ideas. chuck
silvers <chuq@chuq.com> also provided some other fixes.

this is the rest of the MI portion changes.

this will be KNF'd shortly. :-)
 1.58 15-Jan-1998  cgd add support for an ATAPI attachment for 'sd'.
fix 'cd' driver's NCD_SCSI bogosity (was using testing wrong macro!)
clean up in various ways:
* make common atapi_mode_{sense,select}() functions.
* put ATAPI data structures in more sensible headers, split up by
device type.
* include headers a bit more carefully.
* pass flags to attachment-specific cd functions, and use them.
* get rid of SCSI bits in scsipi_base.h's scsipi_make_xs(), move
them into the correct place in scsi_base.c.
* fix minor typo in struct name in scsipiconf.h (which was apparently
never used except in a #define later in the same file).
* use __attribute__ to force 4-byte alignment for xs command store,
so that architectures trying to bus_space_write_multi_N() (where
N > 1) that data to a controller won't lose.
* clean up a few comments in typos, and make a few #defines easier to
understand/maintain.
* rename cd_link.h to cdvar.h (via repository copy). This is exactly
what a 'var' file is supposed to be.
 1.57 30-Dec-1997  is EACCESS->EROFS, for the write on readonly medium error.
 1.56 23-Nov-1997  enami Remove newline after printing FRU CODE instead of removing
newline before printing SKSV information.
 1.55 18-Oct-1997  thorpej Implement two macros, scsipi_command() and scsipi_command_direct(), and
use them to hide the structure of the function pointers we jump through
to issue a command.
 1.54 08-Oct-1997  mjacob Slight formatting change.
 1.53 08-Oct-1997  mjacob The bit pointer field is a 3 bit value- not 4.
 1.52 02-Oct-1997  mjacob kern/3022: make sure that printing sense info in SCSIDEBUG case starts at
the right place and contains the right number of bytes.
 1.51 01-Oct-1997  mjacob Silly boy.... don't print out sense info if the command was marked SCSI_SILENT,
and print the opcode with a leading 0x if you're printing in hex.
 1.50 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.49 13-Sep-1997  enami Declare SCSIVERBOSE by defopt in files.scsipi. Include opt_scsiverbose.h
in scsi_base.c and st.c.
 1.48 05-Sep-1997  bouyer Restore indentation of SCSIVERBOSE output.
 1.47 27-Aug-1997  bouyer branches: 1.47.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.46 20-Aug-1997  mjacob Add in (enabled by SCSIVERBOSE option since it adds a fair amount of string
gup to the kernel) more detailed SCSI error printing, based off of final
SCSI-2 specification. This will also print out things like progress indicator
errors, and will also dump out additional sense bytes that haven't been decoded.

Also, turned the error message 'DELAYED' to 'DEFERRED' since that's more
accurate.
 1.45 04-Aug-1997  fair %x -> 0x%x
 1.44 09-Jun-1997  matthias branches: 1.44.4;
In case of a SCSI-error print the extended sense information and not
some sort of random garbage.
 1.43 02-Apr-1997  mycroft Push the buffer cleanup code into scsi_done(), and split it so that biodone()
is called *after* the driver `done' routine. This fixes disk I/O statistics
on SCSI devices.

Also, calling the `done' routine with a `complete' argument of 0 and actually
having it do anything meaningful loses in at least 3 ways, so just nuke the
argument altogether and don't call it this way. If the driver needs to do
some error handling, that's what `err_handler' is for.
 1.42 20-Mar-1997  thorpej Back out part of the previous patch - not only does stuffing a sizeof()
into 8 bits generate (bogus) warnings on some architectures, but the
change to pad on "scsi inquiry" is no longer needed, apparently.
Thanks to Matthias Pfaller for pointing the latter out to me.
 1.41 18-Mar-1997  thorpej Two changes from Ian Dall <ian.dall@dsto.defence.gov.au>:
- Deal with devices that ignore the length specified in the
inquiry command.
- Allow asynchronous requests without using a buf (key off NOSLEEP instead).
 1.40 12-Oct-1996  christos revert previous kprintf change
 1.39 10-Oct-1996  christos printf -> kprintf, sprintf -> ksprintf
 1.38 03-Sep-1996  thorpej Store the SCSI version in the scsi_link, and set the LUN in the CDB
if the version is <= SCSI-2. This should help some older SCSI
devices that previously needed the "NOLUNS" quirk. While this is
not strictly necessary on SCSI-2 devices, the spec allows it,
so we set it for SCSI-2 devices "just in case". See section 7.2.2 of
Draft X3T9.2 Rev 10L for details.
 1.37 05-Jul-1996  christos - Apply PR/2535: Add support for flex scsi disks.
- Add a quirk called SCSI_NOMODESENSE for drives like the iomega jaz,
that don't support mode sense.
 1.36 03-May-1996  christos - SCSIDEBUG printf() fixes.
 1.35 22-Apr-1996  christos remove include of <sys/cpu.h>
 1.34 19-Mar-1996  mycroft Define a full set of [234][bl]tol() and lto[234][bl]() conversion functions,
inlined.
Use sized types in protocol structures.
Make the definition of scsi_sense_data less ugly.
 1.33 14-Feb-1996  christos scsi prototypes
 1.32 31-Jan-1996  briggs Handle COMMAND ABORTED by forcing a retry. PR#2003, but with some more
diagnostic info.
 1.31 12-Jan-1996  thorpej Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.
 1.30 26-Sep-1995  thorpej Don't declare Debugger(). It's handled in <sys/systm.h>.
#include <sys/systm.h> where necessary, as suggested by Jonathan Stone.
Fixes PR #1511.
 1.29 03-May-1995  mycroft Make the byte-shifting code consistent.
 1.28 26-Jan-1995  mycroft Update copyrights.
 1.27 26-Jan-1995  mycroft Redo the `wait for spin up' code. The Mach 3 method clearly doesn't work.
Also, rearrange the first open sequences a bit; SDEV_OPEN is no longer magic.
 1.26 16-Jan-1995  mycroft Trivial simplification.
 1.25 13-Jan-1995  mycroft Add a DIAGNOSTIC check.
 1.24 30-Dec-1994  mycroft Fix some oversights in the debugging code.
 1.23 29-Dec-1994  mycroft Fix a couple of things in the sense printing code.
 1.22 28-Dec-1994  mycroft Numerous changes. Many bugs fixed, better autoconfig, a few new features.
 1.21 01-Dec-1994  mycroft Remove some redundant code.
 1.20 01-Dec-1994  mycroft Remove unneeded override of SCSI_NOSLEEP.
 1.19 01-Dec-1994  mycroft Uncomment call to scsi_user_done(), and remove a duplicate biodone() (and
another si_free()).
 1.18 22-Nov-1994  deraadt C botch repair from bostic
 1.17 21-Nov-1994  mycroft Replace dev_unit with device_softc in scsi_link. Change argument to foostart()
to void*.
 1.16 30-Aug-1994  deraadt remove an un-aligned access
 1.15 29-Jun-1994  deraadt branches: 1.15.2;
knf
 1.14 29-Jun-1994  cgd New RCS ID's, take two. they're more aesthecially pleasant, and use 'NetBSD'
 1.13 09-May-1994  chopps remove union's from sense_data struct, conditionaly define RAW_PART
 1.12 20-Apr-1994  mycroft Maintain the xs free list with LIST_*().
 1.11 11-Apr-1994  mycroft Fix various types. Remove some outdated flags. Actually sleep a bit if
there are too many commands pending on the controller.
 1.10 11-Apr-1994  mycroft Combine scsi_start_unit() and scsi_stop_unit(), and increase the start
timeout (again).
 1.9 29-Mar-1994  mycroft New SCSI system, based on Julian's more recent work.
 1.8 16-Feb-1994  mycroft Increase the timeout for start_unit.
 1.7 14-Feb-1994  mycroft PARANOID --> DIAGNOSTIC for inexpensive tests.
 1.6 01-Feb-1994  mycroft Add scsi_stop_unit(), from Mac code. Other minor nits.
 1.5 13-Dec-1993  mycroft 0xfe000000 --> KERNBASE
 1.4 27-Nov-1993  mycroft Remove remaining sleep()s.
 1.3 25-Nov-1993  mycroft Patch from Julian to fix hangage when opening a device multiple times.
 1.2 24-Nov-1993  mycroft Still under construction...
 1.1 24-Nov-1993  mycroft Under construction...
 1.15.2.1 06-Oct-1994  mycroft Update from trunk.
 1.44.4.1 23-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.47.2.5 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.47.2.4 16-Sep-1997  thorpej Update marc-pcmcia branch from trunk.
 1.47.2.3 06-Sep-1997  thorpej Update marc-pcmcia branch from trunk.
 1.47.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.47.2.1 27-Aug-1997  thorpej file scsi_base.c was added on branch marc-pcmcia on 1997-08-27 23:33:10 +0000
 1.64.2.2 30-Jul-1998  mjacob opt_scsiverbose.h now is opt_scsi.h and contains the defopted
SCSIVERBOSE and the new ST_ENABLE_EARLYWARN.
 1.64.2.1 30-Jul-1998  mjacob file scsi_base.c was added on branch eeh-paddr_t on 1998-07-30 00:49:21 +0000
 1.66.8.1 30-Nov-1999  itojun bring in latest KAME (as of 19991130, KAME/NetBSD141) into kame branch
just for reference purposes.
This commit includes 1.4 -> 1.4.1 sync for kame branch.

The branch does not compile at all (due to the lack of ALTQ and some other
source code). Please do not try to modify the branch, this is just for
referenre purposes.

synchronization to latest KAME will take place on HEAD branch soon.
 1.66.6.1 21-Jun-1999  thorpej Sync w/ -current.
 1.66.4.2 23-Jan-2000  he Pull up revision 1.71 (requested by bouyer):
Use SCSIPIRETRIES instead of hard-coded value '2' for number of
retries in common routines, and define it as 4 so that ATAPI
command will succeed after several downgrade.
 1.66.4.1 24-Jun-1999  perry pullup 1.66->1.67 (pk): deal with missing "raise interrupt level" code
 1.69.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.69.4.1 15-Nov-1999  fvdl Sync with -current
 1.69.2.4 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.69.2.3 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.69.2.2 20-Oct-1999  thorpej Sync w/ trunk.
 1.69.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.73.6.4 08-Jan-2002  nathanw Catch up to -current.
 1.73.6.3 14-Nov-2001  nathanw Catch up to -current.
 1.73.6.2 24-Aug-2001  nathanw Catch up with -current.
 1.73.6.1 21-Jun-2001  nathanw Catch up to -current.
 1.74.2.2 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.74.2.1 03-Aug-2001  lukem update to -current
 1.77.18.1 11-Sep-2004  he Pull up revisions 1.80-1.81 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.77.16.5 21-Sep-2004  skrll Fix the sync with head I botched.
 1.77.16.4 18-Sep-2004  skrll Sync with HEAD.
 1.77.16.3 03-Sep-2004  skrll Sync with HEAD
 1.77.16.2 25-Aug-2004  skrll Sync with HEAD.
 1.77.16.1 12-Aug-2004  skrll Sync with HEAD.
 1.85.22.2 10-Dec-2006  yamt sync with head.
 1.85.22.1 22-Oct-2006  yamt sync with head
 1.85.20.1 18-Nov-2006  ad Sync with head.
 1.87.48.1 02-Jun-2008  mjf Sync with HEAD.
 1.88.4.1 16-May-2008  yamt sync with head.
 1.88.2.1 18-May-2008  yamt sync with head.
 1.89.38.1 29-Apr-2012  mrg sync to latest -current.
 1.89.34.1 23-May-2012  yamt sync with head.
 1.90.20.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.90.16.2 28-Aug-2017  skrll Sync with HEAD
 1.90.16.1 05-Dec-2016  skrll Sync with HEAD
 1.90.2.1 03-Dec-2017  jdolecek update from HEAD
 1.91.8.1 21-Jun-2017  snj Pull up following revision(s) (requested by mlelstv in ticket #53):
sys/dev/scsipi/atapiconf.c: revision 1.91
sys/dev/scsipi/cd.c: revision 1.341
sys/dev/scsipi/scsi_base.c: revision 1.92
sys/dev/scsipi/scsiconf.c: revision 1.280
sys/dev/scsipi/scsipi_base.c: revisions 1.176, 1.177
sys/dev/scsipi/sd.c: revision 1.325
sys/dev/scsipi/ss.c: revision 1.89
sys/dev/scsipi/st.c: revision 1.231
The atapibus detach path did hold the channel mutex while calling into autoconf,
which would trigger a panic when unplugging a USB ATAPI CDROM.
Align detach code for scsibus and atapibus to fix this.
Also avoid races when detaching devices by replacing callout_stop with
callout_halt.
--
pass config_detach error to caller.
 1.92.6.1 10-Jun-2019  christos Sync with HEAD
 1.17 07-Sep-2003  mycroft Set PQUIRK_ONLYBIG in the wdc-atapi frontend, obviating the need to ever test
the "bus type" for this.

Merge all the code in the SCSI and ATAPI backends for "cd" devices. All of
the mode page handling and whatnot is general to SCSI MMC devices, and should
never have been separated to begin with. This fixes a variety of problems,
and adds load/unload support for SCSI-attached devices.
 1.16 05-Sep-2003  mycroft Use SMS_DBD in a several places to avoid even thinking about block descriptors.
 1.15 02-Sep-2001  thorpej branches: 1.15.20;
Use the AUDIO_PAGE definition common to SCSI and ATAPI.
 1.14 02-Sep-2001  thorpej Centralize the definition of the page code mask and page-savable bit
in the pages returned by MODE SENSE.
 1.13 14-May-2001  bouyer branches: 1.13.2;
Use SCSI/ATAPI common definition for MODE_{SELECT,SENSE}{,_BIG}. Define
functions to send theses commands in scsipi_base.c and use them instead
of ad-hoc commands setups.
 1.12 13-Feb-1998  enami branches: 1.12.26;
Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.11 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.10 17-Oct-1997  bouyer kern/4267 (Takeshi Nakayama): Add a 'len' parameter to cd_scsibus_get_mode and
cd_scsibus_set_mode, to specifie the real len of "data" we need. These
functions were broken when scsi_cd_write_params_page was added to
scsi_cd_pages, thus changing the len of scsi_cd_mode_data.
 1.9 03-Oct-1997  thorpej Define the "write parameters" mode page.
 1.8 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.7 27-Aug-1997  bouyer branches: 1.7.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.6 19-Mar-1996  mycroft Define a full set of [234][bl]tol() and lto[234][bl]() conversion functions,
inlined.
Use sized types in protocol structures.
Make the definition of scsi_sense_data less ugly.
 1.5 28-Dec-1994  mycroft Numerous changes. Many bugs fixed, better autoconfig, a few new features.
 1.4 29-Jun-1994  cgd New RCS ID's, take two. they're more aesthecially pleasant, and use 'NetBSD'
 1.3 29-Mar-1994  mycroft New SCSI system, based on Julian's more recent work.
 1.2 20-May-1993  cgd branches: 1.2.3;
add rcsids and clean up file headers
 1.1 21-Mar-1993  cgd after 0.2.2 "stable" patches applied
 1.2.3.1 24-Nov-1993  mycroft Under construction...
 1.7.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.7.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.7.2.1 27-Aug-1997  thorpej file scsi_cd.h was added on branch marc-pcmcia on 1997-08-27 23:33:12 +0000
 1.12.26.2 21-Sep-2001  nathanw Catch up to -current.
 1.12.26.1 21-Jun-2001  nathanw Catch up to -current.
 1.13.2.1 13-Sep-2001  thorpej Update the kqueue branch to HEAD.
 1.15.20.1 03-Aug-2004  skrll Sync with HEAD
 1.22 31-Dec-2021  andvar fix few typos in comments, mainly in word "parameter".
 1.21 08-Mar-2019  msaitoh s/ are are / are /
s/ a a / a /
 1.20 11-Dec-2005  christos branches: 1.20.164;
merge ktrace-lwp.
 1.19 27-Feb-2005  perry nuke trailing whitespace
 1.18 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.17 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.16 02-Sep-2001  thorpej branches: 1.16.20; 1.16.28; 1.16.30;
Centralize the definition of the page code mask and page-savable bit
in the pages returned by MODE SENSE.
 1.15 24-Jun-2001  wiz branches: 1.15.2;
Fix some typos in comments.
 1.14 09-Sep-1999  thorpej branches: 1.14.14;
Rework the changer driver a bit:
- Take note of magazine changes, and enqueue "Element Status Changed" events
that user processes can read or select on.
- Normalize some structure names.
- Report back more status about changer elements:
* Volume tags (e.g. barcode labels on the backs of your tapes)
* External device names (for drive units in a changer)
* Last element a unit of media was moved from
* Sense information for SCSI changer elements in EXCEPT condition
* Vendor-specific data if the user requests it.
- Add support for setting volume tags.
 1.13 22-Jul-1999  thorpej Define the Volume Tag format.
 1.12 17-Dec-1998  gibbs branches: 1.12.4;
Correct the definition of the changer device capabilities page. The
previous definition confused some reserved bytes for exchange capabilities.
 1.11 13-Feb-1998  enami Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.10 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.9 29-Sep-1997  mjacob Fix a misspelling.
 1.8 27-Aug-1997  bouyer branches: 1.8.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.7 03-Apr-1996  thorpej New SCSI media changer driver, written from scratch by me. Some
influence from the old driver (written by Stefan Grefen) and from
an autochanger driver written by the Systems Programming Group at the
University of Utah Computer Science Department (currently residing
in src/sys/arch/hp300/dev/ac.c).
 1.6 19-Mar-1996  mycroft Define a full set of [234][bl]tol() and lto[234][bl]() conversion functions,
inlined.
Use sized types in protocol structures.
Make the definition of scsi_sense_data less ugly.
 1.5 28-Dec-1994  mycroft Numerous changes. Many bugs fixed, better autoconfig, a few new features.
 1.4 29-Jun-1994  cgd New RCS ID's, take two. they're more aesthecially pleasant, and use 'NetBSD'
 1.3 29-Mar-1994  mycroft New SCSI system, based on Julian's more recent work.
 1.2 20-May-1993  cgd branches: 1.2.3;
add rcsids and clean up file headers
 1.1 21-Mar-1993  cgd after 0.2.2 "stable" patches applied
 1.2.3.1 24-Nov-1993  mycroft Under construction...
 1.8.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.8.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.8.2.1 27-Aug-1997  thorpej file scsi_changer.h was added on branch marc-pcmcia on 1997-08-27 23:33:13 +0000
 1.12.4.1 02-Aug-1999  thorpej Update from trunk.
 1.14.14.2 21-Sep-2001  nathanw Catch up to -current.
 1.14.14.1 24-Aug-2001  nathanw Catch up with -current.
 1.15.2.1 13-Sep-2001  thorpej Update the kqueue branch to HEAD.
 1.16.30.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.16.28.1 29-Apr-2005  kent sync with -current
 1.16.20.2 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.16.20.1 04-Feb-2005  skrll Sync with HEAD.
 1.20.164.1 10-Jun-2019  christos Sync with HEAD
 1.9 11-Dec-2005  christos merge ktrace-lwp.
 1.8 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.7 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.6 13-Feb-1998  enami branches: 1.6.48; 1.6.56;
Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.5 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.4 27-Aug-1997  bouyer branches: 1.4.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.3 04-Apr-1997  matthias * Do some KNFing.
* Make all local functions static.
* Add some untested netatalk support.
 1.2 24-Mar-1997  thorpej Fix a screwup (my fault, oops) that caused the stack to get corrupted.
From Ian Dall <Ian.Dall@dsto.defence.gov.au> on port-pc532.
 1.1 18-Mar-1997  thorpej Driver for the Cabletron EA41x SCSI Ethernet Adaptor, written by
Ian Dall <ian.dall@dsto.defence.gov.au>. Converted to "new arp"
and some other (very) minor changes by me.
 1.4.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.4.2.1 27-Aug-1997  thorpej file scsi_ctron_ether.h was added on branch marc-pcmcia on 1997-08-27 23:33:14 +0000
 1.6.56.1 29-Apr-2005  kent sync with -current
 1.6.48.1 04-Feb-2005  skrll Sync with HEAD.
 1.38 27-Feb-2025  jakllsch sd(4): Translate discard to UNMAP (10) command
 1.37 27-Feb-2025  jakllsch more whitespace fixes
 1.36 27-Feb-2025  jakllsch fix comment grammar and whitespace
 1.35 07-Dec-2023  andvar branches: 1.35.2;
s/multiplcation/multiplication/ in comment.
 1.34 10-Nov-2021  msaitoh s/desciptor/descriptor/ in comment.
 1.33 27-Dec-2019  msaitoh s/transfered/transferred/
 1.32 19-Nov-2016  flxd branches: 1.32.16;
Add "getrealloc" and "setrealloc" commands to get/set automatic reallocation
parameters/enables for error recovery, similar to {get,set}cache.
Many old SCSI disks shipped with reallocation disabled, albeit supporting it.
Minor (cosmetic) fixup of scsi_disk_pages while there.
Based upon code in PR bin/29165 by Greg A. Woods.
OK christos@
 1.31 11-Dec-2005  christos branches: 1.31.116; 1.31.120; 1.31.138; 1.31.140; 1.31.144; 1.31.146;
merge ktrace-lwp.
 1.30 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.29 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.28 07-Jan-2005  ginsbach branches: 1.28.2;
Add command to read the defect data from a direct access device.
 1.27 07-Dec-2004  thorpej Define SYNCHRONIZE CACHE (16).
 1.26 07-Dec-2004  thorpej SYNCHRONIZE_CACHE -> SYNCHRONIZE_CACHE_10
 1.25 07-Dec-2004  thorpej USe more appropriate macro/struct names for READ/WRITE (6) and
READ/WRITE (10).
 1.24 05-Sep-2003  mycroft Forgot to commit this. Add the "rpm" field to the flex geometry page.
 1.23 04-Feb-2003  wrstuden branches: 1.23.2;
Add defines and structure for control mode page.
 1.22 06-Jan-2003  matt Add multiple inclusion protection.
 1.21 24-Jul-2002  mjacob Fixes kern/17573: correctly describe the bfi && physical sector defect list
formats.

Obtained from PR submitter.
 1.20 03-Sep-2001  thorpej branches: 1.20.12;
Define more caching parameters from SCSI-3.
 1.19 02-Sep-2001  thorpej Oops, name the caching parameters page structure member.
 1.18 01-Sep-2001  thorpej Add the caching parameters mode page.
 1.17 30-Oct-1998  thorpej branches: 1.17.24; 1.17.26;
Define the FORMAT UNIT command, and several related structures.
 1.16 09-Jun-1998  thorpej Define the SYNCHRONIZE CACHE command.
 1.15 13-Feb-1998  enami Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.14 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.13 03-Oct-1997  thorpej Define the REZERO UNIT command.
 1.12 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.11 27-Aug-1997  bouyer branches: 1.11.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.10 05-Jul-1996  christos - Apply PR/2535: Add support for flex scsi disks.
- Add a quirk called SCSI_NOMODESENSE for drives like the iomega jaz,
that don't support mode sense.
 1.9 19-Mar-1996  mycroft branches: 1.9.4;
Define a full set of [234][bl]tol() and lto[234][bl]() conversion functions,
inlined.
Use sized types in protocol structures.
Make the definition of scsi_sense_data less ugly.
 1.8 15-Oct-1995  thorpej Add missing spindle synch control, rotational offset, and RPM members
to the rigid geometry mode page. These missing members are at the
end of the page, in space wich was previously marked "reserved".
 1.7 28-Dec-1994  mycroft Numerous changes. Many bugs fixed, better autoconfig, a few new features.
 1.6 22-Nov-1994  deraadt C botch repair from bostic
 1.5 29-Jun-1994  cgd New RCS ID's, take two. they're more aesthecially pleasant, and use 'NetBSD'
 1.4 29-Mar-1994  mycroft New SCSI system, based on Julian's more recent work.
 1.3 20-May-1993  cgd branches: 1.3.3;
add rcsids and clean up file headers
 1.2 12-Apr-1993  deraadt new scsi subsystem.
changes also in config/mkioconf.c
i386/isa/wd.c, fd.c, and all scsi drivers.
 1.1 21-Mar-1993  cgd after 0.2.2 "stable" patches applied
 1.3.3.2 01-Feb-1994  mycroft Add scsi_stop_unit(), from Mac code. Other minor nits.
 1.3.3.1 24-Nov-1993  mycroft Under construction...
 1.9.4.1 04-Mar-1997  mycroft Pull up support for fetching geometry info on optical disks.
 1.11.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.11.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.11.2.1 27-Aug-1997  thorpej file scsi_disk.h was added on branch marc-pcmcia on 1997-08-27 23:33:15 +0000
 1.17.26.2 06-Sep-2002  jdolecek sync kqueue branch with HEAD
 1.17.26.1 13-Sep-2001  thorpej Update the kqueue branch to HEAD.
 1.17.24.3 07-Jan-2003  thorpej Sync with HEAD.
 1.17.24.2 01-Aug-2002  nathanw Catch up to -current.
 1.17.24.1 21-Sep-2001  nathanw Catch up to -current.
 1.20.12.1 29-Aug-2002  gehenna catch up with -current.
 1.23.2.5 17-Jan-2005  skrll Sync with HEAD.
 1.23.2.4 18-Dec-2004  skrll Sync with HEAD.
 1.23.2.3 21-Sep-2004  skrll Fix the sync with head I botched.
 1.23.2.2 18-Sep-2004  skrll Sync with HEAD.
 1.23.2.1 03-Aug-2004  skrll Sync with HEAD
 1.28.2.1 29-Apr-2005  kent sync with -current
 1.31.146.1 18-Jan-2017  skrll Sync with netbsd-5
 1.31.144.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.31.140.1 05-Dec-2016  skrll Sync with HEAD
 1.31.138.1 03-Dec-2016  martin Pull up following revision(s) (requested by flxd in ticket #1276):
sys/dev/scsipi/scsi_disk.h: revision 1.32
sbin/scsictl/scsictl.8: revision 1.27-1.30
sbin/scsictl/scsictl.c: revision 1.39
Add "getrealloc" and "setrealloc" commands to get/set automatic reallocation
parameters/enables for error recovery, similar to {get,set}cache.
Many old SCSI disks shipped with reallocation disabled, albeit
supporting it.
Minor (cosmetic) fixup of scsi_disk_pages while there.
Based upon code in PR bin/29165 by Greg A. Woods.
OK christos@
Bump date for previous.
Use more and more appropriate markup while here.
Add crossreference to scsi(4) per note in PR 9627.
Whitespace, sort.
 1.31.120.1 03-Dec-2017  jdolecek update from HEAD
 1.31.116.1 15-Jan-2017  snj Pull up following revision(s) (requested by flxd in ticket #1418):
sbin/scsictl/scsictl.8: 1.27, 1.29, 1.30 via patch
sbin/scsictl/scsictl.c: revision 1.39 via patch
sys/dev/scsipi/scsi_disk.h: revision 1.32
Add "getrealloc" and "setrealloc" commands to get/set automatic reallocation
parameters/enables for error recovery, similar to {get,set}cache.
Many old SCSI disks shipped with reallocation disabled, albeit
supporting it.
Minor (cosmetic) fixup of scsi_disk_pages while there.
Based upon code in PR bin/29165 by Greg A. Woods.
OK christos@
--
Bump date for previous.
--
Add crossreference to scsi(4) per note in PR 9627.
Whitespace, sort.
 1.32.16.1 08-Apr-2020  martin Merge changes from current as of 20200406
 1.35.2.1 02-Aug-2025  perseant Sync with HEAD
 1.5 11-Dec-2005  christos merge ktrace-lwp.
 1.4 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.3 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.2 19-Apr-2003  fvdl branches: 1.2.2; 1.2.10;
Add NetBSD RCS id.
 1.1 19-Apr-2003  fvdl IU pkt/task definitions include file. From FreeBSD.
 1.2.10.1 29-Apr-2005  kent sync with -current
 1.2.2.1 04-Feb-2005  skrll Sync with HEAD.
 1.17 26-Aug-2006  bjh21 QAS REQUEST is a one-byte message. Make MSG_IS1BYTE recognise this.
 1.16 11-Dec-2005  christos branches: 1.16.4; 1.16.8;
merge ktrace-lwp.
 1.15 27-Feb-2005  perry branches: 1.15.4;
nuke trailing whitespace
 1.14 20-Apr-2003  fvdl branches: 1.14.2; 1.14.10; 1.14.12;
One more FreeBSD compat define.
 1.13 20-Apr-2003  fvdl Correct typo in previous.
 1.12 20-Apr-2003  fvdl Add FreeBSD compat define for PPR_PCOM
 1.11 19-Apr-2003  fvdl Add MSG_QAS_REQUEST definition.
 1.10 06-Dec-2002  fvdl Remove duplicate define that crept in.
 1.9 06-Dec-2002  fvdl Sync this with the FreeBSD version, adding a few aliases and new definitions.
 1.8 23-Apr-2002  bouyer Add the Parallel Protocol Request message.
 1.7 04-Nov-2001  tsutsui Add common macro to check message length.
 1.6 03-Nov-2001  tsutsui Replace a magic number with macro.
 1.5 25-Apr-2001  bouyer branches: 1.5.2; 1.5.6;
Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.4 15-Mar-2000  fvdl branches: 1.4.6;
Add a couple of defines/aliases for messages.
 1.3 05-Jan-1998  perry branches: 1.3.14;
RCSID Police.
 1.2 08-Oct-1996  gibbs branches: 1.2.2;
dev/microcode/aic7xxx/aic7xxx.seq,
dev/microcode/aic7xxx_seq.h,
dev/ic/aic7xxxreg.h:
Remove intrinsic knowledge about SDTR and WDTR messages and replace it
with a generic message system that allows the kernel driver to handle
SDTR, WDTR and any other type of extended message it chooses too. This
makes the sequencer code much simpler, makes extended message handling
debuggable since the bulk of the work is in the kernel driver, and saves
lots of instruction space.

Regen microcode header file.

dev/ic/aic7xxx.c, dev/ic/aic7xxxvar.h:
Add code to handle WDTR and SDTR negotiation in light of the changes in
the message interface to the sequencer. Don't reject targets that
negotiate async by sending an SDTR with a 0 offset. Use an sdtr message
with 0,0 to negotiate async when a target suggests a period that is too
long for us to handle. Some tape and cdrom drives don't like us doing
the message reject that we did in the past.

Fix a problem with handing the QUEUE FULL condition.

Fix a race condition (most likely the cause of the SCB paging problems) that
might allow the sequencer to get unpaused before the condition that caused
it to be paused (a SEQINT) was handled.

Race condition pointed out by Doug Ledford <dledford@dialnet.net> and
by "Dan Willis" <dan@plutotech.com>.

dev/pci/ahc_pci.c:
Add support for the 2940AU, an aic7860 based controller.

dev/pci/pcidevs.h, dev/pci/pcidevs_data.h:
Add product IDs for the 2940AU, aic7860 and aic7855.

Regen data file.

scsi/scsi_message.h:
Add MSG_EXT_SDTR_LEN and MSG_EXT_WDTR_LEN - the length of bytes in these
extended messages.

Thanks to Chuck Cranor <chuck@maria.wustl.edu> for testing these changes
out for me.
 1.1 28-Dec-1994  mycroft branches: 1.1.6;
Numerous changes. Many bugs fixed, better autoconfig, a few new features.
 1.1.6.1 04-Mar-1997  mycroft Add a few constants for the ahc driver.
 1.2.2.1 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.3.14.1 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.4.6.4 11-Dec-2002  thorpej Sync with HEAD.
 1.4.6.3 20-Jun-2002  nathanw Catch up to -current.
 1.4.6.2 14-Nov-2001  nathanw Catch up to -current.
 1.4.6.1 21-Jun-2001  nathanw Catch up to -current.
 1.5.6.1 12-Nov-2001  thorpej Sync the thorpej-mips-cache branch with -current.
 1.5.2.2 23-Jun-2002  jdolecek catch up with -current on kqueue branch
 1.5.2.1 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.14.12.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.14.10.1 29-Apr-2005  kent sync with -current
 1.14.2.1 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.15.4.1 30-Dec-2006  yamt sync with head.
 1.16.8.1 03-Sep-2006  yamt sync with head.
 1.16.4.1 09-Sep-2006  rpaulo sync with head
 1.11 11-Dec-2005  christos merge ktrace-lwp.
 1.10 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.9 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.8 07-Dec-2004  thorpej branches: 1.8.2;
USe more appropriate macro/struct names for READ/WRITE (6) and
READ/WRITE (10).
 1.7 19-Nov-2001  soren branches: 1.7.16;
Remove duplicate struct scsipi_start_stop.
 1.6 13-Feb-1998  enami branches: 1.6.26; 1.6.28;
Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.5 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.4 05-Jan-1998  perry RCSID Police.
 1.3 27-Aug-1997  bouyer branches: 1.3.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.2 19-Mar-1996  mycroft Define a full set of [234][bl]tol() and lto[234][bl]() conversion functions,
inlined.
Use sized types in protocol structures.
Make the definition of scsi_sense_data less ugly.
 1.1 18-Feb-1996  mycroft Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a bit. Needs more work.
 1.3.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.3.2.1 27-Aug-1997  thorpej file scsi_scanner.h was added on branch marc-pcmcia on 1997-08-27 23:33:17 +0000
 1.6.28.1 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.6.26.1 08-Jan-2002  nathanw Catch up to -current.
 1.7.16.1 18-Dec-2004  skrll Sync with HEAD.
 1.8.2.1 29-Apr-2005  kent sync with -current
 1.1 19-Feb-2020  riastradh branches: 1.1.2; 1.1.6;
Sprinkle some dtrace probes into scsi(4).
 1.1.6.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.1.6.1 19-Feb-2020  martin file scsi_sdt.h was added on branch phil-wifi on 2020-04-08 14:08:12 +0000
 1.1.2.2 29-Feb-2020  ad Sync with head.
 1.1.2.1 19-Feb-2020  ad file scsi_sdt.h was added on branch ad-namecache on 2020-02-29 20:19:16 +0000
 1.8 09-Feb-2024  andvar fix spelling mistakes, mainly in comments and log messages.
 1.7 27-Jan-2022  jakllsch Try REPORT LUNS command to enumerate logical units.
 1.6 28-Mar-2019  kardel Add reading of supported opcodes and their timeouts
at attachment time. Though this information is optional,
it allows to override our fixed timeouts with device
provided timeouts. These timeouts will override the
hardcoded values if the device provided timeouts
exceed the hardcoded values and are less than a day.

Using the device provided timeouts avoids premature
device resets and unreliable operation due to
inadequate timeouts.

Due to the limited implementations of USB
umass devices this feature is disabled for all
umass attached devices.
 1.5 06-Feb-2010  cegger branches: 1.5.64;
scsi_sense_data: add comments taken from src/sys/dev/ieee1394/sbp.h
 1.4 28-Apr-2008  martin Remove clause 3 and 4 from TNF licenses
 1.3 11-Dec-2005  christos branches: 1.3.70; 1.3.72; 1.3.74;
merge ktrace-lwp.
 1.2 27-Feb-2005  perry branches: 1.2.2; 1.2.6; 1.2.8;
nuke trailing whitespace
 1.1 21-Feb-2005  thorpej Part 1 of a cleanup pass over the SCSI subsystem. The aim is to name
everything "scsi_*", since we really are talking about the SCSI command
set, ATAPI transport not withstanding. Improve the names of many structures,
and prepend "SCSI_" onto all SCSI command opcodes. Place items described
by the SCSI Primary Commands document into scsi_spc.h.
 1.2.8.2 29-Apr-2005  kent sync with -current
 1.2.8.1 27-Feb-2005  kent file scsi_spc.h was added on branch kent-audio2 on 2005-04-29 11:29:16 +0000
 1.2.6.2 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.2.6.1 27-Feb-2005  yamt file scsi_spc.h was added on branch yamt-km on 2005-03-19 08:35:47 +0000
 1.2.2.2 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.2.2.1 27-Feb-2005  skrll file scsi_spc.h was added on branch ktrace-lwp on 2005-03-04 16:50:33 +0000
 1.3.74.2 11-Mar-2010  yamt sync with head
 1.3.74.1 16-May-2008  yamt sync with head.
 1.3.72.1 18-May-2008  yamt sync with head.
 1.3.70.1 02-Jun-2008  mjf Sync with HEAD.
 1.5.64.1 10-Jun-2019  christos Sync with HEAD
 1.1 14-Apr-2022  pgoyette Split some common stuff into scsi_subr module. This enables loading
of the iscsi module whether or not there are any scsi things built
into the kernel.

Addresses the iscsi portion of kern/56772
 1.25 28-Apr-2008  martin Remove clause 3 and 4 from TNF licenses
 1.24 11-Dec-2005  christos branches: 1.24.70; 1.24.72; 1.24.74;
merge ktrace-lwp.
 1.23 28-Feb-2005  soren Remove a bit of old cruft.
 1.22 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.21 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.20 25-Apr-2001  bouyer branches: 1.20.22; 1.20.30; 1.20.32;
Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.19 21-Feb-2000  mjacob branches: 1.19.6;
Patches from msouth@scruz.net to handle QIC 3220 (HP T20).
 1.18 30-Sep-1999  soren branches: 1.18.2;
Remove outdated list of density codes. mt(1) has helpful
hints and the SCSI specification has the real, current list.
 1.17 17-Aug-1998  mycroft Assign my copyrights to TNF.
 1.16 15-Aug-1998  mycroft Make copyright notices with my name consistent.
 1.15 06-Aug-1998  drochner fix incorrect length of LOCATE CDB (reported in PR kern/5306 by
Dave Huang <khym@bga.com>)
 1.14 13-Feb-1998  enami Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.13 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.12 01-Oct-1997  enami branches: 1.12.2;
Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.11 29-Sep-1997  mjacob Add SCSI-3 DATA COMPRESSION page structure.
Add READ POSITION command structure. Add READ_POSITION and LOCATE opcodes.
 1.10 27-Aug-1997  bouyer branches: 1.10.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.9 24-May-1996  thorpej Implement ST_Q_UNIMODAL quirk, which indicates that a drive will reject
all attempts to mode select, even if the attempted mode is supported.
Add the ST_Q_UNIMODAL quirk for the HP Colorado T4000s which exhibits
this behavior. (Someone please lob a tactical nuke in that direction.)

From David Rosenthal <dshr@vitria.com> on netbsd-bugs.
 1.8 19-Mar-1996  mycroft Define a full set of [234][bl]tol() and lto[234][bl]() conversion functions,
inlined.
Use sized types in protocol structures.
Make the definition of scsi_sense_data less ugly.
 1.7 11-Jan-1996  thorpej Honor cache request and implement erase command, and add the SCSI tape
device configuration page. Fixes PRs 807, 1201, and 1705.
From John Kohl <jtk@kolvir.blrc.ma.us>.
 1.6 28-Dec-1994  mycroft Numerous changes. Many bugs fixed, better autoconfig, a few new features.
 1.5 29-Jun-1994  cgd New RCS ID's, take two. they're more aesthecially pleasant, and use 'NetBSD'
 1.4 13-Apr-1994  mycroft Various cleanup, but no functional differences.
 1.3 29-Mar-1994  mycroft New SCSI system, based on Julian's more recent work.
 1.2 20-May-1993  cgd branches: 1.2.3;
add rcsids and clean up file headers
 1.1 21-Mar-1993  cgd after 0.2.2 "stable" patches applied
 1.2.3.1 24-Nov-1993  mycroft Under construction...
 1.10.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.10.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.10.2.1 27-Aug-1997  thorpej file scsi_tape.h was added on branch marc-pcmcia on 1997-08-27 23:33:18 +0000
 1.12.2.1 30-Sep-1998  cgd pull up rev 1.15 from trunk (drochner)
 1.18.2.1 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.19.6.1 21-Jun-2001  nathanw Catch up to -current.
 1.20.32.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.20.30.1 29-Apr-2005  kent sync with -current
 1.20.22.2 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.20.22.1 04-Feb-2005  skrll Sync with HEAD.
 1.24.74.1 16-May-2008  yamt sync with head.
 1.24.72.1 18-May-2008  yamt sync with head.
 1.24.70.1 02-Jun-2008  mjf Sync with HEAD.
 1.9 17-Nov-1998  bouyer Rename scsi_interpret_sense() to scsipi_interpret_sense() and move it from
scsi_base.c to scsipi_base.c. Rename the functions from scsi_verbose.c
too, and rename the file itself. Cleaup includes too (scsi_*.h should not
be #included in scsipi_*.h files, which are supposed to be
common to atapi and scsi).
 1.8 15-Aug-1998  mycroft Assign my copyrights to TNF.
 1.7 30-Jul-1998  mjacob branches: 1.7.2;
opt_scsiverbose.h now is opt_scsi.h and contains the defopted
SCSIVERBOSE and the new ST_ENABLE_EARLYWARN.
 1.6 11-Jul-1998  mjacob Clarify that Sense Key 1 is a "RECOVERED ERROR"- not just an error.
 1.5 10-Jul-1998  mjacob hmm, char sign extension bit me
 1.4 01-Jul-1998  mjacob Align output for Tape specific bits and use new Sense Key defines (for clarity)
 1.3 24-Jun-1998  mjacob When printing an error, print more than just the opcode. Print the
entire CDB, whose length is actually deterministic. This makes a *big*
difference when actually trying to bind errors to commands.
 1.2 15-Apr-1998  mjacob a tab crept in
 1.1 28-Mar-1998  christos Split out the verbose printing of scsi messages, so that user programs
can use the printing routines.
 1.7.2.2 30-Jul-1998  mjacob opt_scsiverbose.h now is opt_scsi.h and contains the defopted
SCSIVERBOSE and the new ST_ENABLE_EARLYWARN.
 1.7.2.1 30-Jul-1998  mjacob file scsi_verbose.c was added on branch eeh-paddr_t on 1998-07-30 00:49:22 +0000
 1.1 01-Jul-1997  bouyer branches: 1.1.2;
file scsicd.c was initially added on branch bouyer-scsipi.
 1.1.2.2 01-Jul-1997  bouyer Moved atapicd and scsicd to cd_scsi and cd_atapi, to be concistent with
other bus front-end naming conventions (Suggested by Mike Long)
 1.1.2.1 01-Jul-1997  bouyer New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
scsibus at aha
sd at scsibus
cd at scsibus
atapibus at wdc
cd at atapibus
cd has bus-specific config and function front-end in scsicd.c and atapicd.c
The call to theses functions from cd.c is conditionned to
NSCSICD and NATAPICD (all defined in cd.h by config).
 1.306 22-Nov-2024  mlelstv The code tried to limit number of LUNs per target to 3, but would
only default to a single LUN when that limit is exceeded.

With the limit removed, more LUNs will be attached (up to the limit
imposed by the host adapter driver).
 1.305 29-Sep-2024  nat Add quirk for the standard PiSCSI cdrom emulation.
 1.304 22-Jun-2024  palle branches: 1.304.2;
Add quirk for sparc64/sun4v ldom virtual cd devices
 1.303 15-Oct-2022  jmcneill Add PQUIRK_ONLYBIG for Oracle OCI BlockVolumes.

Oracle cloud BlockVolumes do not appear to support SCSI READ6 or WRITE6
commands, so set PQUIRK_ONLYBIG to avoid it here.
 1.302 14-Apr-2022  pgoyette Split some common stuff into scsi_subr module. This enables loading
of the iscsi module whether or not there are any scsi things built
into the kernel.

Addresses the iscsi portion of kern/56772
 1.301 09-Apr-2022  riastradh sys: Use membar_release/acquire around reference drop.

This just goes through my recent reference count membar audit and
changes membar_exit to membar_release and membar_enter to
membar_acquire -- this should make everything cheaper on most CPUs
without hurting correctness, because membar_acquire is generally
cheaper than membar_enter.
 1.300 12-Mar-2022  riastradh scsi(9): Handle bogus number of LUNs in SCSI_REPORT_LUNS.

Reported-by: syzbot+76ef9084533d4bccec66@syzkaller.appspotmail.com
 1.299 12-Mar-2022  riastradh sys: Membar audit around reference count releases.

If two threads are using an object that is freed when the reference
count goes to zero, we need to ensure that all memory operations
related to the object happen before freeing the object.

Using an atomic_dec_uint_nv(&refcnt) == 0 ensures that only one
thread takes responsibility for freeing, but it's not enough to
ensure that the other thread's memory operations happen before the
freeing.

Consider:

Thread A Thread B
obj->foo = 42; obj->baz = 73;
mumble(&obj->bar); grumble(&obj->quux);
/* membar_exit(); */ /* membar_exit(); */
atomic_dec -- not last atomic_dec -- last
/* membar_enter(); */
KASSERT(invariant(obj->foo,
obj->bar));
free_stuff(obj);

The memory barriers ensure that

obj->foo = 42;
mumble(&obj->bar);

in thread A happens before

KASSERT(invariant(obj->foo, obj->bar));
free_stuff(obj);

in thread B. Without them, this ordering is not guaranteed.

So in general it is necessary to do

membar_exit();
if (atomic_dec_uint_nv(&obj->refcnt) != 0)
return;
membar_enter();

to release a reference, for the `last one out hit the lights' style
of reference counting. (This is in contrast to the style where one
thread blocks new references and then waits under a lock for existing
ones to drain with a condvar -- no membar needed thanks to mutex(9).)

I searched for atomic_dec to find all these. Obviously we ought to
have a better abstraction for this because there's so much copypasta.
This is a stop-gap measure to fix actual bugs until we have that. It
would be nice if an abstraction could gracefully handle the different
styles of reference counting in use -- some years ago I drafted an
API for this, but making it cover everything got a little out of hand
(particularly with struct vnode::v_usecount) and I ended up setting
it aside to work on psref/localcount instead for better scalability.

I got bored of adding #ifdef __HAVE_ATOMIC_AS_MEMBAR everywhere, so I
only put it on things that look performance-critical on 5sec review.
We should really adopt membar_enter_preatomic/membar_exit_postatomic
or something (except they are applicable only to atomic r/m/w, not to
atomic_load/store_*, making the naming annoying) and get rid of all
the ifdefs.
 1.298 05-Feb-2022  hannken Initialize "replun" -- found with KMSAN.
 1.297 29-Jan-2022  martin In some cases the gcc optimizer is not smart enough to figure out why
the luns and nluns variables are never actually used when they are not
initialized - so initialize them always.
 1.296 28-Jan-2022  christos Factor out the lun detection code to simplify control flow.
 1.295 28-Jan-2022  jakllsch shut up GCC about possibly-uninit; some KNF
 1.294 27-Jan-2022  jakllsch Try REPORT LUNS command to enumerate logical units.
 1.293 21-Dec-2021  riastradh scsi(4): Take kernel lock around entry into autoconf.

This code paths is entered by kthreads marked MP-safe, not just from
autoconf.

I'm not sure this is sufficient -- it's not clear to me whether
anything prevents concurrently scanning the same target. Someone with
a better understanding of scsi(4) locking will have to audit this.

(For example, maybe it is guaranteed only to happen only either (a)
in autoconf, or (b) in a thread that doesn't start until autoconf is
done. But I don't know -- and if it is this, it should be asserted
so we can verify it.)
 1.292 07-Aug-2021  thorpej Merge thorpej-cfargs2.
 1.291 24-Apr-2021  thorpej branches: 1.291.8;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).
 1.290 18-Sep-2020  jakllsch branches: 1.290.4;
Revert scsiconf.c 1.288, it only worked for LUN 1.

vioscsi(4) now sets PQUIRK_FORCELUNS, which fixes the original issue for
all LUNs.

To-do: should issue REPORT LUNS and use the information it returns to
probe LUNs in an optimized way.
 1.289 17-Sep-2020  jakllsch Some misspelling-in-comments fixes for scsipi
 1.288 11-Jul-2020  kim Continue scanning a SCSI bus when a LUN is reported not present

This fixes disk attachment under Qemu when there is no disk on LUN 0 on
a SCSI bus but there is a disk on LUN 1. The inquiry for LUN 0 returns
SID_QUAL_LU_NOTPRESENT & T_NODEVICE. Quirks are only checked if neither
one of those are set, so cannot use a quirk entry.

Use case 1: Proxmox 6 configures each disk on its own bus when using
the "Virtio SCSI single" SCSI controller. However, while the "scsi0"
disk is on LUN 0, the "scsi1" disk is on LUN 1.

Use case 2: A Linode boot profile with multiple disks results in
the first disk ("sda") on LUN 1, while the second disk ("sdb") is
on LUN 0, each on their own bus.
 1.287 02-May-2020  jdc Don't attempt to read opcodes and their timeouts at attach time for
old devices. The MAINTENANCE IN command was introduced with SCSI-3
and sending it to older peripherals can cause timeouts or them not
to respond to further requests.
 1.286 19-Feb-2020  riastradh C99 initializers for scsipi_bustype. No functional change intended.
 1.285 10-Nov-2019  chs branches: 1.285.2;
in many device attach paths, allocate memory with M_WAITOK instead of M_NOWAIT
and remove code to handle failures that can no longer happen.
 1.284 28-Mar-2019  kardel branches: 1.284.4;
Add reading of supported opcodes and their timeouts
at attachment time. Though this information is optional,
it allows to override our fixed timeouts with device
provided timeouts. These timeouts will override the
hardcoded values if the device provided timeouts
exceed the hardcoded values and are less than a day.

Using the device provided timeouts avoids premature
device resets and unreliable operation due to
inadequate timeouts.

Due to the limited implementations of USB
umass devices this feature is disabled for all
umass attached devices.
 1.283 12-Jan-2019  tsutsui Add NOLUNS quirk for more SEAGATE SCA/WIDE drives.

Tested on NetBSD/luna68k and LUNA with SCA 80pin -> NARROW 50pin and
WIDE 68pin -> NARROW 50pin connectors.
 1.282 07-Oct-2018  christos Handle the SATA to USB external enclosure sold by "Sabrent" and
made by JMicro (vendor=0x152d product=0x0578). This bridge does
not understand FUA, so add a quirk for it.
 1.281 01-Sep-2018  mlelstv Wait in detach if the discovery thread is still running. Avoids crashes
when a device is attached/detached rapidly.
 1.280 17-Jun-2017  mlelstv branches: 1.280.4; 1.280.6;
The atapibus detach path did hold the channel mutex while calling into autoconf,
which would trigger a panic when unplugging a USB ATAPI CDROM.

Align detach code for scsibus and atapibus to fix this.

Also avoid races when detaching devices by replacing callout_stop with
callout_halt.
 1.279 18-Mar-2017  tsutsui branches: 1.279.6;
Add NOLUNS quirk for SEAGATE ST39236LC disk drives.

Found on "SCSI drive conservation activity" for poor Tier-II machines.
 1.278 01-Dec-2016  mlelstv branches: 1.278.2;
CID 1396620: Null pointer dereferences
 1.277 29-Nov-2016  mlelstv reference count adapter mutex possibly shared by multiple channels.

fix error in atapibusdetach, when a child device cannot be detached,
keep atapibus instance alive.
 1.276 20-Nov-2016  mlelstv Make scsipi framework MPSAFE.

Data structures are now protected by a per-adapter mutex at IPL_BIO
that is created by the scsibus or atapibus instance when the adapter
is configured.
The enable reference counter and the channel freeze counter which are
currently used by HBA code before the adapter is configured, are made
atomic.
The target drivers are now all tagged as D_MPSAFE.

Almost all HBA drivers still require the kernel lock to present,
so all callbacks into HBA code are still protected by kernel lock
unless the driver is tagged as SCSIPI_ADAPT_MPSAFE.

TODO: refactor sd and cd to use dksubr.
 1.275 26-Jun-2016  mlelstv branches: 1.275.2;
Create a dedicated thread for the initial scsibus discovery instead
of using the completion thread. This prevents a deadlock when a
command fails during discovery which needs to be handled by the
completion thread.
 1.274 02-May-2016  christos move scsipi_strvis -> libkern:strnvisx()
change the prototype to match userland
fix sizes of strings passed to it
 1.273 25-Jul-2014  dholland branches: 1.273.4;
Add d_discard to all struct cdevsw instances I could find.

All have been set to "nodiscard"; some should get a real implementation.
 1.272 16-Mar-2014  dholland branches: 1.272.2;
Change (mostly mechanically) every cdevsw/bdevsw I can find to use
designated initializers.

I have not built every extant kernel so I have probably broken at
least one build; however I've also found and fixed some wrong
cdevsw/bdevsw entries so even if so I think we come out ahead.
 1.271 12-Oct-2013  christos Pass the device name in, so we can debug what deferred drivers did not work.
 1.270 15-Sep-2013  martin Remove unused variable
 1.269 21-Aug-2012  bouyer branches: 1.269.2; 1.269.4;
If the controller supports more than 256 commands per target,
clamp it to 256 (maximum number of tags in SCSI). Newer controllers
(such as mpii(4), and mfi(4) when fixed to announce tagged queuing support)
support more than 256 outstanding commands and don't use the scsi tag,
but at this time scsipi will always allocate a tag, and panic if a periph
tries to send more than 256 commands.
 1.268 13-May-2012  jakllsch Fix locking issue triggered by drvctr -r of a scsibus(4).
Pullup to netbsd-6 should be requested.
 1.267 20-Apr-2012  bouyer Add a bustype_async_event_xfer_mode() callback to scsipi_bustype (which can
be NULL), so that transport-specific details of transfer mode setting/printing
can be handled more easily.
Move scsipi_async_event_xfer_mode() and scsipi_print_xfer_mode() to
scsi_base.c and split in parallel scsi and FC/SAS parts.
size of struct scsipi_bustype has changed, welcome to 6.99.5
 1.266 19-Apr-2012  bouyer Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.

This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.

In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.

Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.265 06-Apr-2012  christos Add a quirk for the Apple iPod whose mode sense commands fails with not ready.
Seems to work just fine if we send a start command first...
 1.264 12-Mar-2012  mrg take the kernel lock a few more places when doing detach, to avoid
triggering KERNEL_LOCK_P() asserts in both scsi and usb code.

with this and other recent fixes i can now "drvctl -d ehci0".
 1.263 11-Mar-2012  mrg scsidevdetached ioctl path enters scsipi code without kernel lock
and this upsets the newer kasserts. take kernel lock here.
 1.262 26-Apr-2011  hannken branches: 1.262.4; 1.262.8; 1.262.10;
Fixup previous.

The bug was in scsibusdetach(), which is not doing things in the proper
order: it has to detach its children and check for error. If no error,
then it can release the resources that the children were using.

From David Young via source-changes-d.
 1.261 25-Apr-2011  hannken Don't kill outstanding requests when detaching a scsibus on shutdown.
Both the controller and tyhe targets are still running.
 1.260 18-Apr-2011  rmind Replace few simple_lock and ltsleep/wakeup uses with mutex(9) and condvar(9).

Note to all: please replace old primitives in your code! Thanks.
 1.259 02-Apr-2011  macallan Add a quirks entry for Seagate SX173404LC drives, now they will work at higher
speeds than 8bit/async
While there, also disable sync for ZIP drives - at least some of them will
pretend to support sync and then act up.
 1.258 07-Jun-2010  pgoyette branches: 1.258.2;
Update scsiverbose module to use module_autoload() rather than module_load().
Load the module right before each attempt to use its features, and let the
module subsystem handle unloading.
 1.257 30-May-2010  pgoyette Extract SCSIVERBOSE into a kernel module. The module can be builtin
by defining 'options SCSIVERBOSE' in the kernel config file (no change
from current behavior), or it can be loaded at boot time on those
architectures that support the boot loader's "load" command.

The module is built for all architectures, whether or not SCSI or
atapi support exists.
 1.256 27-Apr-2010  dyoung For clarity of scsidevdetached(), rename some variables: sc ->
self, dev -> child, ssc -> sc.
 1.255 12-Nov-2009  dyoung branches: 1.255.2; 1.255.4;
Remove superfluous activation hooks.
 1.254 21-Oct-2009  rmind Remove uarea swap-out functionality:

- Addresses the issue described in PR/38828.
- Some simplification in threading and sleepq subsystems.
- Eliminates pmap_collect() and, as a side note, allows pmap optimisations.
- Eliminates XS_CTL_DATA_ONSTACK in scsipi code.
- Avoids few scans on LWP list and thus potentially long holds of proc_lock.
- Cuts ~1.5k lines of code. Reduces amd64 kernel size by ~4k.
- Removes __SWAP_BROKEN cases.

Tested on x86, mips, acorn32 (thanks <mpumford>) and partly tested on
acorn26 (thanks to <bjh21>).

Discussed on <tech-kern>, reviewed by <ad>.
 1.253 12-May-2009  cegger struct device * -> device_t, no functional changes intended.
 1.252 12-May-2009  cegger struct cfdata * -> cfdata_t, no functional changes intended.
 1.251 07-Apr-2009  dyoung Detach atapibus(4), scsibus(4), cd(4), and sd(4) during shutdown.
Destroy sd->sc_callout in sddetach(). Delete some dead code in
cddetach().
 1.250 16-Jul-2008  drochner branches: 1.250.2; 1.250.8;
split device/softc for scsibus
 1.249 03-Jul-2008  hannken branches: 1.249.2;
The Intel SCA backplane "ESG-SHV" does not support logical units.
 1.248 08-Jun-2008  tsutsui branches: 1.248.2;
Use device_lookup_private() rather than using cd_devs[] directly to get softc.

XXX maybe we should change a type of cd_devs[] in struct cfdriver
from (void *) to device_t.
 1.247 28-Apr-2008  martin branches: 1.247.2;
Remove clause 3 and 4 from TNF licenses
 1.246 05-Apr-2008  cegger branches: 1.246.2; 1.246.4;
use aprint_*_dev and device_xname
 1.245 04-Jan-2008  ad branches: 1.245.6;
Start detangling lock.h from intr.h. This is likely to cause short term
breakage, but the mess of dependencies has been regularly breaking the
build recently anyhow.
 1.244 09-Dec-2007  jmcneill branches: 1.244.2;
Merge jmcneill-pm branch.
 1.243 04-Mar-2007  christos branches: 1.243.14; 1.243.16; 1.243.22; 1.243.24; 1.243.26;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.242 17-Feb-2007  bouyer The SEAGATE ST318203LSUN18G announces DT only capabilitie although it also
supports ST. Because of this the HBA doesn't initiate sync/wide negotiation,
so add PQUIRK_CAP_SYNC | PQUIRK_CAP_WIDE16 for this drive; fix
problem reported by Havard Eidnes.
 1.241 30-Nov-2006  christos branches: 1.241.2; 1.241.4;
Add a quirk for Initio drives (from Rhialto)
 1.240 16-Nov-2006  christos __unused removal on arguments; approved by core.
 1.239 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.238 03-Sep-2006  christos branches: 1.238.2; 1.238.4;
add missing initializer
 1.237 29-Aug-2006  bjh21 The Fujitsu drive in my A540 supports sync transfers but apparently
doesn't advertise this in INQUIRY. It makes it nice and obvious with
SDTR, though, so add it to the list.
 1.236 30-Mar-2006  thorpej Use device_private().
 1.235 29-Mar-2006  thorpej Replace device_locators() with device_locator(), and use it.
 1.234 11-Dec-2005  christos branches: 1.234.4; 1.234.6; 1.234.8; 1.234.10; 1.234.12;
merge ktrace-lwp.
 1.233 26-Nov-2005  tsutsui Fix typo (FALLTHOUGH -> FALLTHROUGH). From Jeff Ito.
 1.232 25-Aug-2005  drochner branches: 1.232.6;
kill a number of autoconf submatch functions which follow the
standard scheme:
if (<configured> != <wildcard> && <configured> != <real>)
then fail
else
ask device match function

This is handled by config_stdsubmatch() now.
 1.231 25-Aug-2005  drochner replace the "locdesc_t" structure carrying the number of locators
explicitely by a plain integer array
the length in now known to all relevant parties, so this avoids
duplication of information, and we can allocate that thing in
drivers without hacks
 1.230 30-May-2005  christos branches: 1.230.2;
- remove bogus casts
- add more const
 1.229 27-Feb-2005  perry branches: 1.229.2;
nuke trailing whitespace
 1.228 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.227 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.226 21-Aug-2004  thorpej branches: 1.226.4; 1.226.6;
Use ANSI function decls and make use of static.
 1.225 18-Aug-2004  drochner Use the new autoconf functions to rescan busses and detach devices
on user request.
This duplicates the functionality provided by a private ioctl
interface (accessible through scsictl(8)), but in a more generic way.
 1.224 12-Aug-2004  mycroft Whoops. Swap the order of the delref() and the config_pending_decr().
 1.223 10-Aug-2004  mycroft Hold a reference to the adapter until scsibus_config() has been called. This
avoids an extra enable/disable cycle on removable controllers (i.e. PCMCIA).
 1.222 06-Aug-2004  bouyer Ops, don't return before the end of the function.
Catched by enami tsugutomo.
 1.221 05-Aug-2004  bouyer scsi_kill_pending(): don't kill the commands in periph_xferq here.
The controller is handling them, calling scsipi_done() here will end up in the
xfer being scsipi_done()'ed a second time when it completes in the controller
code. In addition, the way the loop was done here would end up in an infinite
loop, because the channel kernel thread needs to run to remove a command from
this queue.

scsibusdetach(): scsipi_done() all commands from periph_xferq. The controller
is already gone, and these commands will never complete.
Shut down the channel (which will cause the kenrel thread to exit) after
detaching the childs, as they will need the kernel thread for
scsipi_wait_drain().

Fix kernel hang or deadlock when detaching devices (either by scsictl detach
or unplug) with active commands.
 1.220 12-Mar-2004  bouyer branches: 1.220.4;
Add a drive with broken tagged queuing support. From Jim Faulkner.
Fix PR kern/23815.
 1.219 22-Feb-2004  mycroft Remove PQUIRK_CDROM. It is definitely not correct -- witness that no other
OS needs such a hack, and the same drive works fine on Suns -- and is much
more likely to be a bug in the host adapter driver (which is corroborated by
the PQUIRK_NOLUNS).
 1.218 10-Oct-2003  matt Remove a quirk I added for a Hitachi drive.
 1.217 18-Sep-2003  mycroft Don't print junk if an INQUIRY fails (usually with an ILLEGAL REQUEST due to
an unused LUN). Also, if the qualifier says the LUN is non-existant, don't
try to attach a device here.
 1.216 12-Sep-2003  mycroft Some devices really want INQUIRY to be the first command they receive. Also,
the result of the extra TEST UNIT READY was being ignored anyway. So, I wrote
it, I nuke it.
 1.215 10-Sep-2003  mycroft Set up the blank fields in the INQUIRY buffer *before* executing the command --
because we can't really rely on the "additional length" being correct (any
more?). Fixes some problems with devices showing up as "<, , >".
 1.214 09-Sep-2003  mycroft Remove NOMODESENSE quirks for Iomega drives -- they're totally unneeded.
 1.213 09-Sep-2003  mycroft Delete the NOMODESENSE quirks for Maxtor USB hard drives, as these are no
doubt resolved (and were never actually needed in the first place -- someone
was let out without supervision).
 1.212 09-Sep-2003  mycroft Exorcise PQUIRK_NODOORLOCK.
 1.211 08-Sep-2003  mycroft There is absolutely no sense in having a PQUIRK_NODOORLOCK entry for a
non-removable device. Nuke it.
 1.210 08-Sep-2003  mycroft If maxlun>0, automatically set PQUIRK_FORCELUNS, rather than using quirk
table entries.
 1.209 08-Sep-2003  mycroft Do a START UNIT only if the TEST UNIT READY reports that the device is not
ready. This avoids gratuitously starting the motor on floppy and CD-ROM
drives, and eliminates the need for the audio playing test in cdopen().

Therefore, also remove PQUIRK_NOSTARTUNIT.
 1.208 07-Aug-2003  jrf Added three scsi IDs which were tested by Dave Barnes who sent in
three PRs regarding them: 17836, 17837, 17838. Did a few kernel
compiles with them just to make sure they are okay. Approved by
christos@, thanks to Dave for sending the PRs and verifying to me
that they work.
 1.207 29-Jun-2003  fvdl branches: 1.207.2;
Back out the lwp/ktrace changes. They contained a lot of colateral damage,
and need to be examined and discussed more.
 1.206 28-Jun-2003  darrenr Pass lwp pointers throughtout the kernel, as required, so that the lwpid can
be inserted into ktrace records. The general change has been to replace
"struct proc *" with "struct lwp *" in various function prototypes, pass
the lwp through and use l_proc to get the process pointer when needed.

Bump the kernel rev up to 1.6V
 1.205 13-May-2003  thorpej Use aprint*().
 1.204 02-May-2003  fvdl Local change I had lying around: add NOLUNS quirk for an older seagate
drive.
 1.203 21-Apr-2003  fvdl Add another IBM drive that incorrectly reports DT only.
 1.202 19-Apr-2003  fvdl Add a PCAP_NODT quirk. I have two older Quantum Atlas drives that work
fine at U2W, but barf at U160, with different controllers. Unfortunately
sometimes just being initialized at U160 does the trick, so setting them
to lower speed in the BIOS may also be needed.
 1.201 14-Mar-2003  bouyer Move the SCSI_DELAY before the serialization for probe. multiple SCSI busses
can sleep, only probes have to be keept ordered.
 1.200 10-Feb-2003  pk Remove NOTAG quirk for the Seagate ST11200N; the cause of the failure that
prompted this entry was actually a driver bug.
 1.199 01-Feb-2003  bouyer Add PQUIRK_NOLUNS entry for "NEC ", "CD-ROM DRIVE:502".
Fixes PR kern/18479
 1.198 27-Jan-2003  bouyer Remove revision in quirk entry for TOSHIBA XM-4101TASUNSLCD. The same drive with a newer revision also fails, so assume all revisions are bad until proven otherwise. Should fix PR kern/19912.
 1.197 09-Jan-2003  pk Add two disks capable of doing synchronous transfer mode, but don't
report SCSI2: "MICROP 1924" and "FUJITSU M2266"
 1.196 01-Jan-2003  thorpej Use aprint_normal() in cfprint routines.
 1.195 05-Dec-2002  jdolecek Add NOLUNS|NOSYNC quirk for NEC CD-ROM DRIVE:222.
Change sent in PR kern/19286 by Dave Barnes.
 1.194 23-Oct-2002  jdolecek merge kqueue branch into -current

kqueue provides a stateful and efficient event notification framework
currently supported events include socket, file, directory, fifo,
pipe, tty and device changes, and monitoring of processes and signals

kqueue is supported by all writable filesystems in NetBSD tree
(with exception of Coda) and all device drivers supporting poll(2)

based on work done by Jonathan Lemon for FreeBSD
initial NetBSD port done by Luke Mewburn and Jason Thorpe
 1.193 04-Oct-2002  soren Slightly simplicy the SCSI device attachment message:
- Use the plain words 'disk' and 'tape' instead of 'direct' and 'sequential'.
- Media status will be printed in the frontend, so don't bother with it.
- Don't bother printing the SCSI version, which is fixed these days, or the
numberic device type.
 1.192 02-Oct-2002  thorpej Add trailing ; to CFATTACH_DECL.
 1.191 30-Sep-2002  thorpej Use CFATTACH_DECL().
 1.190 27-Sep-2002  thorpej Declare all cfattach structures const.
 1.189 27-Sep-2002  thorpej Introduce a new routine, config_match(), which invokes the
cfattach->ca_match function in behalf of the caller. Use it
rather than invoking cfattach->ca_match directly.
 1.188 19-Sep-2002  jmc Force the initial probes to happen within the newly forked off kthread.
This eliminates problems where the underlying interrupt handler isn't the
specific layer calling scsipi_complete() for a given scsi transaction.
This avoids deadlocks where the kthread that called the autoconf routines
to configure a scsibus shouldn't be the one put to sleep waiting on a
scsipi_complete (only the scsibus's kthread should be doing that).

To avoid jitter this will force the scsibus's to probe in the order they
run through autoconf (so machines with multiple bus's don't move sd* devices
around on every reboot).
 1.187 06-Sep-2002  gehenna Merge the gehenna-devsw branch into the trunk.

This merge changes the device switch tables from static array to
dynamically generated by config(8).

- All device switches is defined as a constant structure in device drivers.

- The new grammer ``device-major'' is introduced to ``files''.

device-major <prefix> char <num> [block <num>] [<rules>]

- All device major numbers must be listed up in port dependent majors.<arch>
by using this grammer.

- Added the new naming convention.
The name of the device switch must be <prefix>_[bc]devsw for auto-generation
of device switch tables.

- The backward compatibility of loading block/character device
switch by LKM framework is broken. This is necessary to convert
from block/character device major to device name in runtime and vice versa.

- The restriction to assign device major by LKM is completely removed.
We don't need to reserve LKM entries for dynamic loading of device switch.

- In compile time, device major numbers list is packed into the kernel and
the LKM framework will refer it to assign device major number dynamically.
 1.186 02-Aug-2002  bouyer Add yet another broken sony cdrom. This one, in addition to broken LUN
handling, has broken sync negotiation. Also add PQUIRK_NOWIDE to be safe.
Fix PR kern/17773.
 1.185 23-Jul-2002  matt Add a quirk for a HITACHI driver (CAP_SYNC|CAP_WIDE16).
 1.184 15-May-2002  bouyer branches: 1.184.2; 1.184.4;
Change PQUIRK_CAP_SYNC to set PERIPH_CAP_SYNC aslo for devices that
improperly claim to only support DT clocking.
Add a PQUIRK_CAP_WIDE16 quirk.
Add a quirk for IBM DXHS36D SCSI disk (claims to only support DT).
 1.183 05-May-2002  thorpej A statement must follow a label.
 1.182 23-Apr-2002  bouyer The IBM drive I have here supports DT, but claim version 3 only.
So check for version 3, not 4 when looking for DT support.
This should be safe as these bits are reserved for older devices, they
should be set to 0 when not supported.
 1.181 23-Apr-2002  bouyer Report SPC-2 features (DT clocking, quick arbitration and selection,
information unit transfers) though periph_cap.
 1.180 15-Apr-2002  joda Maxtor D040H2 also can't modesense
 1.179 01-Apr-2002  bouyer Add a chan_name to struct scsipi_channel, holding the channel's name.
Set this to dv_xname for scsibus and atapibus.
Set the name of the kernel thread to chan_name instead of controller's
name:channel number (so that we can use this name for controller-specific
threads).
 1.178 16-Mar-2002  chs add a quirk for some disks I have that don't like tagged queuing.
 1.177 23-Jan-2002  uwe Add notag quirk for Seagate "ST11200N SUN1.05".
 1.176 29-Dec-2001  augustss Move some USB device quirks to the umass_quirk table.
 1.175 29-Dec-2001  augustss Remove some quirk entries that are handled in umass_quirk now.
 1.174 27-Dec-2001  nathanw Use PQUIRK_ONLYBIG for all Olympus cameras, not just the C-1.
Use PQUIRK_ONLYBIG for the PQI TravelFlash.
 1.173 14-Dec-2001  nathanw The Olympus C-1 engine cameras seem to need the PQIRK_ONLYBIG quirk.
 1.172 02-Dec-2001  bouyer Clean up attach of wd/atapibus:
kill ata_atapi_attach. Change atapibus to use a struct scsipi_channel instead
of ata_atapi_attach as attach arch. Create a ata_device, compatible with
scsipi_channel, to attach wd.
 1.171 26-Nov-2001  fredette Added quirk entries for Adaptec and Emulex SCSI interposer boards.
Decode a limited set of SASI/SCSI-1 sense codes, and under sun2
only, conjure up INQUIRY responses for these boards.
 1.170 19-Nov-2001  tsutsui Add a new quirk value PQUIRK_CAP_SYNC for SCSI1 devices which support
sync transfers, and set PERIPH_CAP_SYNC for them in scsi_probe_device().
As per discussion on tech-kern.
 1.169 19-Nov-2001  soren Floptical drive needs PQUIRK_NODOORLOCK as well.
 1.168 19-Nov-2001  soren Insite Floptical drive needs PQUIRK_NOLUNS.
 1.167 18-Nov-2001  tsutsui Fix formatting of scsi_quirk_patterns[] and wrap long lines.
 1.166 15-Nov-2001  lukem don't need <sys/types.h> when including <sys/param.h>
 1.165 13-Nov-2001  lukem add RCSIDs
 1.164 06-Nov-2001  augustss Add a quirk for a USB CD/RW.
 1.163 21-Oct-2001  mjl branches: 1.163.2;
Add ST15150N to quirk table, as per PR/13817.
 1.162 16-Oct-2001  christos PR/14256: Kevin P. Neal: Add quirk for micropolis drive.
 1.161 31-Aug-2001  augustss branches: 1.161.2;
Add a new quirk, PQUIRK_NOBIGMODESENSE, for devices that need big
SCSI ops, but still fails on big mode sense.
Makes M-Sys DiskOnKey work.
 1.160 31-Aug-2001  augustss Some quirks for USB M-Sys DiskOnKey. Still not enough to make it work.
 1.159 18-Jul-2001  bouyer Adn scsipi_target_detach() and scsipi_thread_call_callback() as discussed
on tech-kern. scsipi_target_detach() accept wildcard target/lun as requested.
 1.158 11-Jun-2001  pk branches: 1.158.2;
Add nolun/nosync quirk for "IBM CDRM00201 !F"
See PR#13031.
 1.157 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.156 26-Feb-2001  fvdl branches: 1.156.2;
Add quirks for VMware emulated disks, for NetBSD as a guest OS.
 1.155 16-Feb-2001  pk Turn off command tagging by default for pre-SCSI2 devices.
 1.154 18-Jan-2001  jdolecek constify
 1.153 03-Dec-2000  ad I2O HBAs provide an abstracted view of the bus; use SCBUSIOLLSCAN to give
them an oppertunity to re-scan it before we do.
 1.152 14-Nov-2000  pk Mark the "TOSHIBA, XM-4101TASUNSLCD" CD-R for a-sync operation only.
 1.151 22-Sep-2000  ad Add a new, optional method to scsipi_adapter (scsipi_accesschk()), and use
it when considering whether to attach devices. This is to facilitate
`non-SCSI' RAID controller drivers that want to provide SCSI pass-through
services to the kernel.
 1.150 13-Aug-2000  mjacob Add "SYMBIOS" processor type to the SDEV_NOLUN category. These are usually
the GEM chips on a Sun D1000- and they cause Qlogic SBus firmware to blow
chunks if you access past lun 0.
 1.149 08-Aug-2000  mjacob During probe, use SCBUSACCEL ioctl to adapters that support it to
enable fast/wide/tagged.
 1.148 03-Aug-2000  bouyer Add quirk entry for NEDICOM CRD-BP2, from kern/10738.
 1.147 09-Jun-2000  enami branches: 1.147.2;
Prevent a process being swapped out during I/O if the data buffer is
allocated on stack. This potential problem is noticed by Noriyuki Soda
and the idea and sample code to fix is given by Jason R. Thorpe.
 1.146 30-May-2000  augustss Add NOMODESENSE quirk for TEAC USB floppy.
 1.145 30-May-2000  augustss Add NOMODESENSE quirk for Y-E data floppy (thanks Jason for reminding me).
 1.144 29-May-2000  bouyer ADEV_CDROM -> SDEV_CDROM, for consistency.
 1.143 28-May-2000  gmcgarry Handle ADEV_CDROM in quirks table.
Quirk entry for another Toshiba cdrom.
 1.142 15-May-2000  dante branches: 1.142.2;
Add few peripheral device type
Remove "???" from T_IT8_1/2. They actually are pre-press devices for graphic arts as described by ASC IT8
Zeros and blanks scsipi_inquiry_data from byte 58 to byte 74 if additional_length is less than 58
 1.141 14-May-2000  dante Change scsipi_inquiry_data strucure to be ANSI SPC-2 rev16 compliant
 1.140 19-Apr-2000  enami Allocate the variable `inqbuf' in scsi_probedev on stack rather than
statically. Since this function may called for another luns immediately,
allocating it statically doesn't make sense and may cause race condition
as pointed out by PR#9749.
 1.139 02-Apr-2000  augustss Change the initial field in struct ata_atapi_attach and struct scsipi_link
slightly to allow scsibus and atapibus to attach to the same device.
Furthermore, only attach a scsibus when the bus type is BUS_SCSI.
 1.138 27-Mar-2000  augustss In attach message, spell it "target" not "targ", as in the locator.
 1.137 19-Mar-2000  sjg Added NOLUNS quirk for YAMAHA CRW8424S
 1.136 17-Mar-2000  soren scsiprint() is needed even without scsibus'es, so move it to scsi_base.c .
 1.135 13-Mar-2000  martin Added Artec/Ultima A6000C scanner to the quirks table (no LUNs).
 1.134 20-Jan-2000  mjacob Nobody said no to adding a pointer to original scsi inquiry data
to the scsibus attach args. Make sure it's nulled for ATAPI. Also,
for scsiconf.c, modify SENA's quirk entry.
 1.133 13-Jan-2000  nisimura Add tweaks for TEAC compact cassette tape drive.
 1.132 14-Nov-1999  soren Allow SCSI_DELAY to be shorter than 2 seconds.
 1.131 20-Oct-1999  enami Cancel active transfers on aic/wdc detach.
Also makes LS-120 drive works for me again.
 1.130 11-Oct-1999  hwr branches: 1.130.2; 1.130.4;
Add ad quirk for CDU-561 CD-ROM. From PR kern/8608
by SUNAGAWA Keiki <kei_sun@ba2.so-net.ne.jp>.
 1.129 10-Oct-1999  hwr Add a quirk to recognize a Toshiba XM-3401TA SCSI CD-ROM as
removable SCSI device. From Matthew Fredette <fredette@mit.edu> in
kern/7438.
 1.128 30-Sep-1999  thorpej branches: 1.128.2;
Cleanup the scsipi_xfer flags:
- `flags' is now gone, replaced with `xs_control' and `xs_status'.
- Massive cleanup of the control flags. Now we explicitly say that
a job is to complete asynchronously, rather than relying on side-effects,
and use a new flag to now that device discovery is being performed.
- Do SCSI device discovery interrupt-driven.
 1.127 19-Sep-1999  nathanw Add NOLUNS quirk for another Texel CD-ROM revision.
 1.126 11-Sep-1999  thorpej Implement detaching of SCSI busses.
 1.125 09-Sep-1999  hwr Add a quirk for Wangtec SCSI-36 (QIC-120) tape drive.
From Izumi Tsutsiu in PR 8357.
 1.124 26-Jul-1999  explorer make the JVC 2626 match more than one version, since all seem to be
returning errors on the LUN probe.
 1.123 14-Jul-1999  tron Skip LUN check for all versions of the UMAX Astra 1220S as suggested
by Dan McMahill in PR kern/7991.
 1.122 10-Dec-1998  mjacob branches: 1.122.2; 1.122.4;
add Adaptec RAID units as devices that do not return geometry pages
 1.121 08-Dec-1998  thorpej When allocating a device's scsipi_link, initialize the pending_xfers
queue.
 1.120 05-Dec-1998  mjacob NOMODESENSE for all Seagate ST19171- not just FC
 1.119 05-Dec-1998  mjacob Eliminate the moreluns entry as it makes no sense for fat SCSI busses (e.g,
FC loops). Change the semantics of scsi_probedev so that it returns 1 if
you should continue probing at this target, else 0 for not.

Replace the blanket use of '7' with the use of the new sc_maxlun property
that is now gathered from HBAs. Allocate scsipi_link arrays based upon this.
Fix a really nasty and silly bug that has been there for a while where the
number of first level scsipi_link structures was one less than it needed
to be.
 1.118 26-Nov-1998  leo The IOMEGA ZIP 100, J.03 does not grok LUN's.
 1.117 19-Nov-1998  thorpej Add a reference to the adapter when the scsibus is opened, and delete it
when it is closed.
 1.116 19-Nov-1998  thorpej Add a reference to the adapter before probing the bus, and delete it
once we are done probing.
 1.115 17-Nov-1998  bouyer Rename scsi_interpret_sense() to scsipi_interpret_sense() and move it from
scsi_base.c to scsipi_base.c. Rename the functions from scsi_verbose.c
too, and rename the file itself. Cleaup includes too (scsi_*.h should not
be #included in scsipi_*.h files, which are supposed to be
common to atapi and scsi).
 1.114 20-Oct-1998  thorpej Fix a sight open flags buglet pointed out by Matthias Scheler.
 1.113 10-Oct-1998  thorpej Enforce open-for-writing on ioctls that change the bus's state.

Implement ioctl pass-through to the host bus adapter, allowing both
SCBUS* ioctls handled at that level and host adapter-specific ioctls
to be implemented. Implement SCBUSIORESET as a pass-through.

Inspired by PR #6090, from Matt Jacob.
 1.112 10-Oct-1998  thorpej Implement the SCBUSIOSCAN ioctl. Rescans the bus for new devices.
 1.111 10-Oct-1998  thorpej Add the open/close/ioctl entry points for the SCSI bus, i.e. /dev/scsibusN.
 1.110 08-Oct-1998  thorpej Add a NOSYNCCACHE quirk for the Micropolis 2217-15MQ1091501, as reported
by Matt Jacob, PR #6027.
 1.109 08-Oct-1998  thorpej Add a NOLUNS quirk for the Sony CDL1100 changer, from Chris Jones, PR #6238.
 1.108 08-Sep-1998  mjacob Reflect changes in quirk flags, and also now add case post retrieval
of Inquiry data during probe where SDEV_NOSYNC, SDEV_NOTAG and SDEV_NOWIDE
can be set (with quirk data overriding) per device probed.
 1.107 31-Aug-1998  cgd kill the last remnants of __BROKEN_INDIRECT_CONFIG. (only the pica port
used it, and it's non-working and apparently slated for replacement.)
 1.106 17-Aug-1998  mycroft Assign my copyrights to TNF.
 1.105 15-Aug-1998  mycroft Make copyright notices with my name consistent.
 1.104 05-Aug-1998  drochner Improve generation of default disklabels:
-store printable product ID in cd's and sd's softc, use it as "typename"
-for this, add a "destination buffer length" argument to scsipi_strvis()
-return ATAPI device type for ATAPI devices
 1.103 31-Jul-1998  thorpej Use the pool allocator for scsipi_xfer structures.
 1.102 30-Jul-1998  fvdl branches: 1.102.2;
Add NOLUNS quirk for the UMAX Astra 1220 scanner.
 1.101 22-Jun-1998  bouyer Ricoh IS60 to the quirk table (SDEV_NOLUN). Fixes PR 5473 by
Feico Dillema < dillema@acm.org>
 1.100 04-May-1998  thorpej The DEC RRD42 doesn't deal with LUNs very well either.
kern/5376, Simon Burge <simonb@telstra.com.au>
 1.99 04-May-1998  thorpej Add the UMAX Astra 1200S scanner to the list of the LUN-clueless.
kern/5390, Rene Hexel <rh@vip.at>
 1.98 23-Apr-1998  explorer Ignore LUN on yet another cdrom
 1.97 10-Apr-1998  mjacob trim ID of ST19171FC so that SUN drives are caught
 1.96 07-Mar-1998  scottr Quirk table entries for IBM H3171 and Quantum ELS85S disks.
 1.95 21-Jan-1998  mikel add NOLUNS quirk for UMAX S-12; from Andreas Wrede in PR kern/4849
 1.94 12-Jan-1998  thorpej Adjust for changes to config.
 1.93 03-Oct-1997  thorpej branches: 1.93.2;
Clean up and comment the device types, and add the "enclosure services",
"storage array", and "IT8" types.
 1.92 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.91 29-Sep-1997  bouyer Add quirks entries for Hitachi DK515C disk drives and Cipher ST150S tape drive.
Closes PR kern/4171 from jbernard@tater.mines.edu.
 1.90 19-Sep-1997  mjacob Another couple NOMODESENSE drives.
 1.89 09-Sep-1997  pk Add SDEV_NOLUNS to another CD-ROM player.
 1.88 27-Aug-1997  bouyer branches: 1.88.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.87 17-Aug-1997  mjacob A few more SDEV_NOLUNS entries- one tape drive (SONY Beta/VHS) and a couple
of PCMCIA card readers (one as T_DIRECT, the other as T_PROCESSOR).
 1.86 16-Aug-1997  mjacob This fibre channel disk doesn't support the geometry page.
 1.85 14-Aug-1997  explorer add SDEV_NOLUNS for my scanner (UMAX S-6E)
 1.84 17-Jul-1997  perry branches: 1.84.2;
More CD quirks, from Dave Huang (pr-3843)
 1.83 24-Apr-1997  mycroft Another silly CD-ROM drive...
 1.82 20-Apr-1997  thorpej And Yet Another Broken IBM disk (the 664 this time), once again
reported by Hubert Feyrer <feyrer@grizu.fh-regensburg.de>.
 1.81 20-Apr-1997  thorpej Add Yet Another Broken IBM disk to the SDEV_AUTOSAVE list. This one
doesn't report a vendor string (yuck)!. Problem reported by
Hubert Feyrer <feyrer@grizu.fh-regensburg.de>.
 1.80 19-Apr-1997  pk Two more quirks:
Python 28454-XXX tape drive: NOLUNS
1588-15MBSUN0669 disk: AUTOSAVE
 1.79 08-Apr-1997  scottr Add another IBM disk that needs SDEV_AUTOSAVE.
 1.78 02-Apr-1997  mycroft Add a SDEV_AUTOSAVE quirk for the Emulex SCSI<->ESDI bridge.
 1.77 01-Apr-1997  mikel loosen the version number check in the quirk list for the CDR-H98MV;
from Koji Imada in PR kern/3419.
 1.76 27-Mar-1997  scottr Add SDEV_AUTOSAVE quirks for IBM 0663H{08,12} disks
 1.75 25-Mar-1997  scottr Add SDEV_NOLUNS quirk for Seagate ST125N.
 1.74 11-Mar-1997  mikel SDEV_NOMODESENSE quirks are no longer needed for optical devices; from
Enami Tsugutomo in PR kern/3308.
 1.73 14-Dec-1996  mycroft branches: 1.73.6;
Make sure to initialize the memory we just allocated.
 1.72 10-Dec-1996  thorpej Add a "max_target" member to struct scsi_link, which is filled in by
host adapter drivers, indicating the highest SCSI target they can
address. Use this value to dynamically allocate data structures, rather
than hard-coding 8 targets.

These changes allow targets > 7 to be addressed on wide SCSI busses.

Fixes PRs #1674 and #2892.
 1.71 05-Dec-1996  cgd update these so they compile whether or not __BROKEN_INDIRECT_CONFIG
is defined.
 1.70 29-Nov-1996  thorpej Add Yet Another Broken CD-ROM Drive to The List.
From Lennart Augustsson <augustss@cs.chalmers.se>, PR #2971.
 1.69 08-Nov-1996  explorer Another quirky cdrom drive ; pr kern/2917
 1.68 06-Nov-1996  mikel Add another broken CDROM. Fixes PR kern/2913.
Some misc. cleanup.
 1.67 23-Oct-1996  matthias * At least the Teac FC-1 Shugart-SCSI bridgeboard does motor on/off
management by itself. But when it gets a start unit request, it keeps
the floppy motor running all the time. This adds code for dealing with
yet another quirk (SDEV_NOSTARTUNIT) that prevents sd.c from sending
start unit requests. A entry for the Teac FC-1 is added to the quirk
table.
 1.66 18-Oct-1996  explorer Add quirk entries for two more optical drives; closes pr kern/2861
 1.65 12-Oct-1996  christos revert previous kprintf change
 1.64 10-Oct-1996  christos printf -> kprintf, sprintf -> ksprintf
 1.63 03-Sep-1996  thorpej Store the SCSI version in the scsi_link, and set the LUN in the CDB
if the version is <= SCSI-2. This should help some older SCSI
devices that previously needed the "NOLUNS" quirk. While this is
not strictly necessary on SCSI-2 devices, the spec allows it,
so we set it for SCSI-2 devices "just in case". See section 7.2.2 of
Draft X3T9.2 Rev 10L for details.
 1.62 28-Aug-1996  cgd (1) add a 'channel' field to scsi_link.
(2) in scsibusmatch, match channel as appropriate.
(3) add a scsiprint() function, to do the "scsibus at..."
and channel (if not SCSI_CHANNEL_ONLY_ONE) printing,
i.e. the common functionality that all SCSI drivers currently
should be doing.
 1.61 27-Aug-1996  cgd change cfprint_t type definition to take a const char *, rather than
a char *, because that's what was really intended, and because
if the print function modifies the string, various things could become
unhappy (so the string should _not_ be modified).
 1.60 24-Jul-1996  thorpej Match any Maxtor XT-8760S for the SDEV_NOLUNS quirk. Some of these
drives carry Sun OEM identifiers, as well.

Noticed by Taras Ivanenko <ivanenko@ctpa03.mit.edu>.
 1.59 05-Jul-1996  explorer add ZIP quirk for SDEV_NOMODESENSE
 1.58 05-Jul-1996  christos - Apply PR/2535: Add support for flex scsi disks.
- Add a quirk called SCSI_NOMODESENSE for drives like the iomega jaz,
that don't support mode sense.
 1.57 02-May-1996  neil branches: 1.57.4;
Added another quirked SCSI device, re PR 2320 from Manuel Bouyer
 1.56 22-Apr-1996  christos remove include of <sys/cpu.h>
 1.55 21-Mar-1996  scottr Add a SDEV_NOLUNS quirk for the Epson OMD-5010 removable MO cartridge drive.
 1.54 18-Mar-1996  hpeyerl Fix for NEC D3847 1.6gB drive which reported itself at lun[1-7].
 1.53 17-Mar-1996  thorpej New device attachment scheme:

- split softc size and match/attach out from cfdriver into
a new struct cfattach.

- new "attach" directive for files.*. May specify the name of
the cfattach structure, so that devices may be easily attached
to parents with different autoconfiguration semantics.
 1.52 05-Mar-1996  thorpej Add another broken drive that doesn't deal with LUNs properly to The List.
Fixes PR 1961, from Kevin P. Neal.
 1.51 05-Mar-1996  thorpej Normalize SCSI autoconfiguration output.
From Chris Demetriou <cgd@NetBSD.ORG>. Fixes PR #1958.
 1.50 22-Feb-1996  mycroft Add a SDEV_AUTOSAVE quirk for the DEC RZ55.
 1.49 18-Feb-1996  mycroft Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a bit. Needs more work.
 1.48 14-Feb-1996  christos scsi prototypes
 1.47 12-Jan-1996  pk LUN deficient device: Tandberg 3600 (Ken Raeburn; PR#1933).
 1.46 31-Dec-1995  thorpej Add another broken CD-ROM drive to The List (Chinon CDS-525), per PR #1686.
From Kortelainen Mika <k125374@cs.tut.fi>.
 1.45 11-Dec-1995  mycroft Trim NULs, in addition to spaces, in scsi_strvis().
 1.44 01-Nov-1995  pk More rogues.
 1.43 13-Oct-1995  gwr Add quirk entries for Wangtek SCSI tapes. Also, add entry for
Tandberg 3800 (fixes PR#1592 - from Jochen Pohl).
 1.42 26-Sep-1995  thorpej Add another broken NEC CD-ROM drive to The List. Fixes PR #1451.
 1.41 21-Aug-1995  pk Set the scsi link before calling config_attach(), so target/lun info is
available to, say, dk_establish().
 1.40 14-Aug-1995  briggs CHINON CDS-535 version Q117 also needs NOLUNS. Assume all 535s do.
 1.39 13-Aug-1995  briggs Another SDEV_NOLUNS device from paul@pgoyette.bdt.com (Paul Goyette).
 1.38 06-Aug-1995  mycroft Add another Seagate disk to the list, and make all the disks match any
firmware revision.
 1.37 12-Jul-1995  cgd add/change a few quirks:
(1) all Chinon CDS-431 CD-ROMs (regardless of revision)
are forced to only having LUN 0, at the suggestion
of Michael Hitch.
(2) _force_ searching of extra LUNs for the Emulex MD21/S2
ESDI bridge. It's pre-SCSI 1, but knows about LUNs.
"amazing." From Jason Thorpe.
(3) recognize an Emulex tape adapter in front of a QIC-36
tape, and have it forced to only LUN 0. This is
an odd one; vendor, name, and rev strings are all
spaces. Anything that mathes this is very likely
broken, anyway, so might as well give it a shot.
Again from Jason Thorpe.
 1.36 12-Jul-1995  cgd implement SDEV_FORCELUNS flag, as suggested by Jason Thorpe.
 1.35 09-Jul-1995  cgd Morningstar SnapLink -> SDEV_NOLUNS, per Peter Galbavy in pr 1192.
 1.34 09-Jul-1995  cgd play the alphabetization game
 1.33 09-Jul-1995  cgd QUANTUM P105S 910-10-94x A.3 -> SDEV_NOLUNS
 1.32 27-Jun-1995  cgd another CD-ROM quirk, a la PR 1166
 1.31 24-Jun-1995  cgd add entry for another tweaked Texel CD-ROM. from pr 1151
 1.30 18-Jun-1995  mycroft Add another CD-ROM drive to The List.
 1.29 02-Jun-1995  pk Add `Maxtor LXT-213S', this time without a Sun OEM addition.
 1.28 04-Apr-1995  mycroft Add another broken CD-ROM drive to The List.
 1.27 22-Feb-1995  mycroft Be less picky about revision numbers for some broken devices.
 1.26 09-Feb-1995  pk Another Maxtor botch.
 1.25 01-Feb-1995  mycroft Add Exabyte 8200 to the list.
 1.24 30-Jan-1995  mycroft All revisions of the TDC 3600 are rogues.
 1.23 16-Jan-1995  mycroft Don't clear the debug flags when copying the prototype scsi_link.
 1.22 12-Jan-1995  mycroft Add two more broken devices.
 1.21 01-Jan-1995  mycroft Put Dan's broken tape drive in The List.
 1.20 30-Dec-1994  mycroft And a little better...
 1.19 30-Dec-1994  mycroft Deal with short inquiry response a little better.
 1.18 28-Dec-1994  mycroft Numerous changes. Many bugs fixed, better autoconfig, a few new features.
 1.17 03-Nov-1994  mycroft Oops; update scsibusmatch() arg list.
 1.16 03-Nov-1994  mycroft Always use direct configuration for SCSI devices.
 1.15 03-Nov-1994  mycroft Remove a couple of i386-specific hacks, and make a couple of others #ifdef
CONFIG_INDIRECT instead.
 1.14 29-Jun-1994  cgd New RCS ID's, take two. they're more aesthecially pleasant, and use 'NetBSD'
 1.13 11-Apr-1994  mycroft Fix various types. Remove some outdated flags.
 1.12 29-Mar-1994  mycroft New SCSI system, based on Julian's more recent work.
 1.11 25-Mar-1994  mycroft Put controller target in scsi_switch.
 1.10 17-Dec-1993  mycroft Canonicalize all #includes.
 1.9 27-May-1993  deraadt branches: 1.9.3;
scsi probing spent too much time on the bus -- sped it up now,
and simplified the message printing code (works the same)
 1.8 25-May-1993  deraadt patch00149 by Julian Elischer <julian@jules.dialix.oz.au> & Rodney Grimes.
When an error was encountered, the sd/cd drivers printed blockno&ff0000
rather that blockno.
 1.7 20-May-1993  cgd add rcsids and clean up file headers
 1.6 05-May-1993  deraadt fixed bug from last commit
 1.5 04-May-1993  deraadt support for making dev->id_alive be set, this is for iostat to
find disk devices. wee bit of a kludge. sub-device attach()
routines must now return 1 for successful attach(), 0 otherwise.
Other bsd's do this too..
 1.4 20-Apr-1993  deraadt a test for mycroft..
 1.3 19-Apr-1993  mycroft Don't overwrite global kernel version string.
 1.2 12-Apr-1993  deraadt new scsi subsystem.
changes also in config/mkioconf.c
i386/isa/wd.c, fd.c, and all scsi drivers.
 1.1 21-Mar-1993  cgd after 0.2.2 "stable" patches applied
 1.9.3.7 22-Jan-1994  briggs lu -> lun in SCSI_DEBUG section...
 1.9.3.6 25-Nov-1993  mycroft Compare the device name rather than the attach function.
 1.9.3.5 24-Nov-1993  mycroft Final tweaks to get it going.
 1.9.3.4 24-Nov-1993  mycroft Some cleanup, and fix a bug I just introduced.
 1.9.3.3 24-Nov-1993  mycroft More construction...
 1.9.3.2 24-Nov-1993  mycroft Still under construction...
 1.9.3.1 24-Nov-1993  mycroft Under construction...
 1.57.4.1 31-Jul-1996  jtc Pulled up diff between revs 1.59 and 1.60 by request from Jason Thorpe
 1.73.6.1 12-Mar-1997  is Merge in changes from Trunk
 1.84.2.1 23-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.88.2.5 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.88.2.4 22-Sep-1997  thorpej Update marc-pcmcia branch from trunk.
 1.88.2.3 16-Sep-1997  thorpej Update marc-pcmcia branch from trunk.
 1.88.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.88.2.1 27-Aug-1997  thorpej file scsiconf.c was added on branch marc-pcmcia on 1997-08-27 23:33:19 +0000
 1.93.2.1 05-May-1998  mycroft Pull up 1.96, per request of scottr. Also do 1.95 and 1.97-1.100.
 1.102.2.2 08-Aug-1998  eeh Revert cdevsw mmap routines to return int.
 1.102.2.1 30-Jul-1998  eeh file scsiconf.c was added on branch eeh-paddr_t on 1998-08-08 03:06:51 +0000
 1.122.4.1 02-Aug-1999  thorpej Update from trunk.
 1.122.2.3 16-Jan-2000  he Apply patch (requested by ad):
When probing a SCSI target, do not report an error if the target
indicates that LUNs are not supported.
 1.122.2.2 27-Nov-1999  he Pull up revision 1.123 (requested by dmcmahill):
Add SCSI quirk for UMAX Astra 1220S scanner: skip LUN check,
fixes PR#7991.
 1.122.2.1 10-Nov-1999  he Pull up revisions 1.129-1.130 (requested by hwr):
Add quirks for Toshiba XM-3401TA SCSI CD-ROM and CDU-561 CD-ROM,
fixing PR#7438 and PR#8608.
 1.128.2.2 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.128.2.1 21-Dec-1999  wrstuden Initial commit of recent changes to make DEV_BSIZE go away.

Runs on i386, needs work on other arch's. Main kernel routines should be
fine, but a number of the stand programs need help.

cd, fd, ccd, wd, and sd have been updated. sd has been tested with non-512
byte block devices. vnd, raidframe, and lfs need work.

Non 2**n block support is automatic for LKM's and conditional for kernels
on "options NON_PO2_BLOCKS".
 1.130.4.1 15-Nov-1999  fvdl Sync with -current
 1.130.2.13 23-Apr-2001  ad If SCSIPI_CHAN_NOSETTLE is set in the channel's flags, then don't bother
with SCSI_DELAY.
 1.130.2.12 11-Apr-2001  mjacob oops: reversed conditional
 1.130.2.11 11-Apr-2001  mjacob Make scsipi_channel_init a function returning an int- non-zero means
it failed to initialize the channel (this should be acceptable)- in
which case we complain and don't schedule bus probing for later.

We make the internal memory allocations for the periph and the chan_periphs
array M_NOWAIT- this way we have a hope of booting instead of silently hanging
during boot if we've run out of memory.
 1.130.2.10 12-Mar-2001  bouyer Sync with HEAD.
 1.130.2.9 11-Feb-2001  bouyer Sync with HEAD.
 1.130.2.8 08-Dec-2000  bouyer Sync with HEAD.
 1.130.2.7 22-Nov-2000  bouyer Sync with HEAD.
 1.130.2.6 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.130.2.5 04-Feb-2000  thorpej Make sure the channel's completion thread exits when the channel
is detached, and completely encapsulate the way periph's are stored
by the channel.
 1.130.2.4 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.130.2.3 26-Oct-1999  thorpej Deal a little more gracefully with the fact that xfer mode parameters
are for the I_T Nexus, and make all xfer mode updates `async events'.
 1.130.2.2 20-Oct-1999  thorpej Sync w/ trunk.
 1.130.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.142.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.147.2.6 11-Feb-2003  msaitoh Pull up revision 1.199 (requested by bouyer):
Add SDEV_NOLUNS quirk entry for "NEC ", "CD-ROM DRIVE:502".
Fixes kern/18479 .
 1.147.2.5 04-Sep-2002  itojun sys/dev/scsipi/scsipiconf.c 1.186 via patch (bouyer)
add quirk entry for yet another broken sony cdrom. Fix kern/17773
 1.147.2.4 13-Nov-2001  he Pull up revision 1.162 (via patch, requested by christos):
Add a ``no tagged queueing'' for an old Micropolis drive. Fixes
PR#14256.
 1.147.2.3 25-Oct-2001  he Pull up revision 1.153 (requested by ad):
Add Mylex DACC960, CAC-EISA, and I2O block/SCSI drivers.
 1.147.2.2 28-Aug-2000  mjacob Add support for accelleration SCSI ioctls (that allow for the
midlayer to enable sync/wide/tagged).

Approved for pullup to netbsd-1-5 by thorpej@netbsd.org.
 1.147.2.1 03-Aug-2000  bouyer pullup 1.147 -> 1.148 (approved by thorpej):
Add quirk entry for NEDICOM CRD-BP2, from kern/10738.
 1.156.2.21 15-Jan-2003  thorpej Sync with HEAD.
 1.156.2.20 03-Jan-2003  thorpej Sync with HEAD.
 1.156.2.19 11-Dec-2002  thorpej Sync with HEAD.
 1.156.2.18 11-Nov-2002  nathanw Catch up to -current
 1.156.2.17 18-Oct-2002  nathanw Catch up to -current.
 1.156.2.16 17-Sep-2002  nathanw Catch up to -current.
 1.156.2.15 13-Aug-2002  nathanw Catch up to -current.
 1.156.2.14 01-Aug-2002  nathanw Catch up to -current.
 1.156.2.13 12-Jul-2002  nathanw No longer need to pull in lwp.h; proc.h pulls it in for us.
 1.156.2.12 24-Jun-2002  nathanw Curproc->curlwp renaming.

Change uses of "curproc->l_proc" back to "curproc", which is more like the
original use. Bare uses of "curproc" are now "curlwp".

"curproc" is now #defined in proc.h as ((curlwp) ? (curlwp)->l_proc) : NULL)
so that it is always safe to reference curproc (*de*referencing curproc
is another story, but that's always been true).
 1.156.2.11 20-Jun-2002  nathanw Catch up to -current.
 1.156.2.10 17-Apr-2002  nathanw Catch up to -current.
 1.156.2.9 01-Apr-2002  nathanw Catch up to -current.
(CVS: It's not just a program. It's an adventure!)
 1.156.2.8 28-Feb-2002  nathanw Catch up to -current.
 1.156.2.7 08-Jan-2002  nathanw Catch up to -current.
 1.156.2.6 14-Nov-2001  nathanw Catch up to -current.
 1.156.2.5 22-Oct-2001  nathanw Catch up to -current.
 1.156.2.4 21-Sep-2001  nathanw Catch up to -current.
 1.156.2.3 24-Aug-2001  nathanw Catch up with -current.
 1.156.2.2 21-Jun-2001  nathanw Catch up to -current.
 1.156.2.1 05-Mar-2001  nathanw Initial commit of scheduler activations and lightweight process support.
 1.158.2.7 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.158.2.6 06-Sep-2002  jdolecek sync kqueue branch with HEAD
 1.158.2.5 23-Jun-2002  jdolecek catch up with -current on kqueue branch
 1.158.2.4 11-Feb-2002  jdolecek Sync w/ -current.
 1.158.2.3 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.158.2.2 13-Sep-2001  thorpej Update the kqueue branch to HEAD.
 1.158.2.1 03-Aug-2001  lukem update to -current
 1.161.2.2 26-Sep-2001  fvdl * add a VCLONED vnode flag that indicates a vnode representing a cloned
device.
* rename REVOKEALL to REVOKEALIAS, and add a REVOKECLONE flag, to pass
to VOP_REVOKE
* the revoke system call will revoke all aliases, as before, but not the
clones
* vdevgone is called when detaching a device, so make it use REVOKECLONE
to get rid of all clones as well
* clean up all uses of VOP_OPEN wrt. locking.
* add a few VOPS to spec_vnops that need to do something when it's a
clone vnode (access and getattr)
* add a copy of the vnode vattr structure of the original 'master' vnode
to the specinfo of a cloned vnode. could possibly redirect getattr to
the 'master' vnode, but this has issues with revoke
* add a vdev_reassignvp function that disassociates a vnode from its
original device, and reassociates it with the specified dev_t. to be
used by cloning devices only, in case a new minor is allocated.
* change all direct references in drivers to v_devcookie and v_rdev
to vdev_privdata(vp) and vdev_rdev(vp). for diagnostic purposes
when debugging race conditions that still exist wrt. locking and
revoking vnodes.
* make the locking state of a vnode consistent when passed to
d_open and d_close (unlocked). locked would be better, but has
some deadlock issues
 1.161.2.1 07-Sep-2001  thorpej Commit my "devvp" changes to the thorpej-devvp branch. This
replaces the use of dev_t in most places with a struct vnode *.

This will form the basic infrastructure for real cloning device
support (besides being architecurally cleaner -- it'll be good
to get away from using numbers to represent objects).
 1.163.2.1 12-Nov-2001  thorpej Sync the thorpej-mips-cache branch with -current.
 1.184.4.4 15-Mar-2004  jmc Pullup rev 1.220 (requested by bouyer in ticket #1625)

Add a drive with broken tagged queuing support. PR#23815
 1.184.4.3 02-Jun-2003  tron Pull up revision 1.198 (requested by bouyer in ticket #1155):
Remove revision in quirk entry for TOSHIBA XM-4101TASUNSLCD.
The same drive with a newer revision also fails, so assume all revisions
are bad until proven otherwise.
 1.184.4.2 02-Jun-2003  tron Pull up revision 1.199 (requested by bouyer):
Add PQUIRK_NOLUNS entry for "NEC ", "CD-ROM DRIVE:502".
Fixes PR kern/18479
 1.184.4.1 02-Aug-2002  lukem Pull up revision 1.186 (requested by bouyer in ticket #608):
Add yet another broken sony cdrom. This one, in addition to broken LUN
handling, has broken sync negotiation. Also add PQUIRK_NOWIDE to be safe.
Fix PR kern/17773.
 1.184.2.2 29-Aug-2002  gehenna catch up with -current.
 1.184.2.1 16-May-2002  gehenna Add the character device switch.
 1.207.2.9 11-Dec-2005  christos Sync with head.
 1.207.2.8 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.207.2.7 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.207.2.6 21-Sep-2004  skrll Fix the sync with head I botched.
 1.207.2.5 18-Sep-2004  skrll Sync with HEAD.
 1.207.2.4 25-Aug-2004  skrll Sync with HEAD.
 1.207.2.3 12-Aug-2004  skrll Sync with HEAD.
 1.207.2.2 03-Aug-2004  skrll Sync with HEAD
 1.207.2.1 02-Jul-2003  darrenr Apply the aborted ktrace-lwp changes to a specific branch. This is just for
others to review, I'm concerned that patch fuziness may have resulted in some
errant code being generated but I'll look at that later by comparing the diff
from the base to the branch with the file I attempt to apply to it. This will,
at the very least, put the changes in a better context for others to review
them and attempt to tinker with removing passing of 'struct lwp' through
the kernel.
 1.220.4.1 20-Feb-2007  tron Pull up following revision(s) (requested by bouyer in ticket #11051):
sys/dev/scsipi/scsiconf.c: revision 1.242 via patch
The SEAGATE ST318203LSUN18G announces DT only capabilitie although it also
supports ST. Because of this the HBA doesn't initiate sync/wide negotiation,
so add PQUIRK_CAP_SYNC | PQUIRK_CAP_WIDE16 for this drive; fix
problem reported by Havard Eidnes.
 1.226.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.226.4.1 29-Apr-2005  kent sync with -current
 1.229.2.1 19-Feb-2007  tron Pull up following revision(s) (requested by bouyer in ticket #1668):
sys/dev/scsipi/scsiconf.c: revision 1.242 via patch
The SEAGATE ST318203LSUN18G announces DT only capabilitie although it also
supports ST. Because of this the HBA doesn't initiate sync/wide negotiation,
so add PQUIRK_CAP_SYNC | PQUIRK_CAP_WIDE16 for this drive; fix
problem reported by Havard Eidnes.
 1.230.2.5 21-Jan-2008  yamt sync with head
 1.230.2.4 03-Sep-2007  yamt sync with head.
 1.230.2.3 26-Feb-2007  yamt sync with head.
 1.230.2.2 30-Dec-2006  yamt sync with head.
 1.230.2.1 21-Jun-2006  yamt sync with head.
 1.232.6.1 29-Nov-2005  yamt sync with head.
 1.234.12.1 31-Mar-2006  tron Merge 2006-03-31 NetBSD-current into the "peter-altq" branch.
 1.234.10.1 19-Apr-2006  elad sync with head.
 1.234.8.2 03-Sep-2006  yamt sync with head.
 1.234.8.1 01-Apr-2006  yamt sync with head.
 1.234.6.1 22-Apr-2006  simonb Sync with head.
 1.234.4.1 09-Sep-2006  rpaulo sync with head
 1.238.4.2 10-Dec-2006  yamt sync with head.
 1.238.4.1 22-Oct-2006  yamt sync with head
 1.238.2.2 12-Jan-2007  ad Sync with head.
 1.238.2.1 18-Nov-2006  ad Sync with head.
 1.241.4.2 12-Mar-2007  rmind Sync with HEAD.
 1.241.4.1 27-Feb-2007  yamt - sync with head.
- move sched_changepri back to kern_synch.c as it doesn't know PPQ anymore.
 1.241.2.1 19-Feb-2007  riz Pull up following revision(s) (requested by bouyer in ticket #452):
sys/dev/scsipi/scsiconf.c: revision 1.242
The SEAGATE ST318203LSUN18G announces DT only capabilitie although it also
supports ST. Because of this the HBA doesn't initiate sync/wide negotiation,
so add PQUIRK_CAP_SYNC | PQUIRK_CAP_WIDE16 for this drive; fix
problem reported by Havard Eidnes.
 1.243.26.1 11-Dec-2007  yamt sync with head.
 1.243.24.1 26-Dec-2007  ad Sync with head.
 1.243.22.1 18-Feb-2008  mjf Sync with HEAD.
 1.243.16.1 09-Jan-2008  matt sync with HEAD
 1.243.14.2 08-Dec-2007  jmcneill Rename pnp(9) -> pmf(9), as requested by many.
 1.243.14.1 06-Nov-2007  joerg Refactor PNP API:
- Make suspend/resume directly a device functionality. It consists of
three layers (class logic, device logic, bus logic), all of them being
optional. This replaces D0/D3 transitions.
- device_is_active returns true if the device was not disabled and was
not suspended (even partially), device_is_enabled returns true if the
device was enabled.
- Change pnp_global_transition into pnp_system_suspend and
pnp_system_resume. Before running any suspend/resume handlers, check
that all currently attached devices support power management and bail
out otherwise. The latter is not done for the shutdown/panic case.
- Make the former bus-specific generic network handlers a class handler.
- Make PNP message like volume up/down/toogle PNP events. Each device
can register what events they are interested in and whether the handler
should be global or not.
- Introduce device_active API for devices to mark themselve in use from
either the system or the device. Use this to implement the idle handling
for audio and input devices. This is intended to replace most ad-hoc
watchdogs as well.
- Fix somes situations in which audio resume would lose mixer settings.
- Make USB host controllers better deal with suspend in the light of
shared interrupts.
- Flush filesystem cache on suspend.
- Flush disk caches on suspend. Put ATA disks into standby on suspend as
well.
- Adopt drivers to use the new PNP API.
- Fix a critical bug in the generic cardbus layer that made D0->D3
break.
- Fix ral(4) to set if_stop.
- Convert cbb(4) to the new PNP API.
- Apply the PCI Express SCI fix on resume again.
 1.244.2.1 08-Jan-2008  bouyer Sync with HEAD
 1.245.6.5 28-Sep-2008  mjf Sync with HEAD.
 1.245.6.4 29-Jun-2008  mjf Sync with HEAD.
 1.245.6.3 02-Jun-2008  mjf Sync with HEAD.
 1.245.6.2 06-Apr-2008  mjf - after some discussion with agc@ i agreed it would be a good idea to move
device_unregister_* to device_deregister_* to be more like the pmf(9)
functions, especially since a lot of the time the function calls are next
to each other.

- add device_register_name() support for dk(4).
 1.245.6.1 05-Apr-2008  mjf - add "file-system DEVFS" and "pseudo-device devfsctl" to conf/std seeing
as these are always needed.

- convert many, many drivers over to the New Devfs World Order. For a
list of device drivers yet to be converted see,
http://www.netbsd.org/~mjf/devfs-todo.html.

- add a new device_unregister_all(device_t) function to remove all device
names associated with a device_t, which saves us having to construct
device names when the driver is detached.

- add a DEV_AUDIO type for devices.
 1.246.4.5 11-Aug-2010  yamt sync with head.
 1.246.4.4 11-Mar-2010  yamt sync with head
 1.246.4.3 16-May-2009  yamt sync with head
 1.246.4.2 04-May-2009  yamt sync with head.
 1.246.4.1 16-May-2008  yamt sync with head.
 1.246.2.2 17-Jun-2008  yamt sync with head.
 1.246.2.1 18-May-2008  yamt sync with head.
 1.247.2.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.247.2.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.248.2.2 18-Jul-2008  simonb Sync with head.
 1.248.2.1 03-Jul-2008  simonb Sync with head.
 1.249.2.1 19-Oct-2008  haad Sync with HEAD.
 1.250.8.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.250.2.1 28-Apr-2009  skrll Sync with HEAD.
 1.255.4.3 31-May-2011  rmind sync with head
 1.255.4.2 21-Apr-2011  rmind sync with head
 1.255.4.1 30-May-2010  rmind sync with head
 1.255.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.258.2.1 06-Jun-2011  jruoho Sync with HEAD.
 1.262.10.4 03-Sep-2012  riz Pull up following revision(s) (requested by bouyer in ticket #523):
sys/dev/scsipi/scsiconf.c: revision 1.269
If the controller supports more than 256 commands per target,
clamp it to 256 (maximum number of tags in SCSI). Newer controllers
(such as mpii(4), and mfi(4) when fixed to announce tagged queuing support)
support more than 256 outstanding commands and don't use the scsi tag,
but at this time scsipi will always allocate a tag, and panic if a periph
tries to send more than 256 commands.
 1.262.10.3 22-May-2012  riz branches: 1.262.10.3.2;
Pull up following revision(s) (requested by jakllsch in ticket #277):
sys/dev/scsipi/scsiconf.c: revision 1.268
Fix locking issue triggered by drvctr -r of a scsibus(4).
Pullup to netbsd-6 should be requested.
 1.262.10.2 23-Apr-2012  riz Pull up following revision(s) (requested by bouyer in ticket #192):
sys/dev/scsipi/cd.c: revision 1.307
sys/dev/scsipi/scsiconf.c: revision 1.266
sys/dev/scsipi/sd.c: revision 1.298
sys/dev/scsipi/st_scsi.c: revision 1.35
sys/dev/scsipi/atapiconf.c: revision 1.85
sys/dev/scsipi/scsipiconf.h: revision 1.120
sys/dev/usb/umass_scsipi.c: revision 1.44
sys/dev/scsipi/scsiconf.h: revision 1.57
sys/dev/scsipi/st_atapi.c: revision 1.29
sys/dev/scsipi/scsipi_base.c: revision 1.158
sys/dev/scsipi/st.c: revision 1.221
sys/dev/scsipi/scsipi_ioctl.c: revision 1.67
Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.
This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.
In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.
Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.262.10.1 19-Mar-2012  riz Pull up following revision(s) (requested by mrg in ticket #125):
sys/dev/scsipi/scsiconf.c: revision 1.263
sys/dev/scsipi/scsiconf.c: revision 1.264
sys/dev/ic/ncr53c9x.c: revision 1.144
sys/dev/ic/ninjascsi32.c: revision 1.22
sys/dev/usb/uhub.c: revision 1.117
take the kernel lock in functions called from attach*().
scsidevdetached ioctl path enters scsipi code without kernel lock
and this upsets the newer kasserts. take kernel lock here.
take the kernel lock a few more places when doing detach, to avoid
triggering KERNEL_LOCK_P() asserts in both scsi and usb code.
with this and other recent fixes i can now "drvctl -d ehci0".
 1.262.10.3.2.1 01-Nov-2012  matt sync with netbsd-6-0-RELEASE.
 1.262.8.3 02-Jun-2012  mrg sync to latest -current.
 1.262.8.2 29-Apr-2012  mrg sync to latest -current.
 1.262.8.1 12-Mar-2012  mrg merge scsiconf.c 1.264.
 1.262.4.4 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.262.4.3 30-Oct-2012  yamt sync with head
 1.262.4.2 23-May-2012  yamt sync with head.
 1.262.4.1 17-Apr-2012  yamt sync with head
 1.269.4.1 18-May-2014  rmind sync with head
 1.269.2.2 03-Dec-2017  jdolecek update from HEAD
 1.269.2.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.272.2.1 10-Aug-2014  tls Rebase.
 1.273.4.4 28-Aug-2017  skrll Sync with HEAD
 1.273.4.3 05-Dec-2016  skrll Sync with HEAD
 1.273.4.2 09-Jul-2016  skrll Sync with HEAD
 1.273.4.1 29-May-2016  skrll Sync with HEAD
 1.275.2.2 20-Mar-2017  pgoyette Sync with HEAD
 1.275.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.278.2.1 21-Apr-2017  bouyer Sync with HEAD
 1.279.6.3 19-Dec-2020  martin Pull up following revision(s) (requested by tsutsui in ticket #1640):

sys/dev/scsipi/scsiconf.c: revision 1.283

Add NOLUNS quirk for more SEAGATE SCA/WIDE drives.

Tested on NetBSD/luna68k and LUNA with SCA 80pin -> NARROW 50pin and
WIDE 68pin -> NARROW 50pin connectors.
 1.279.6.2 13-Jul-2020  martin Pull up following revision(s) (requested by kim in ticket #1571):

sys/dev/scsipi/scsiconf.c: revision 1.288

Continue scanning a SCSI bus when a LUN is reported not present

This fixes disk attachment under Qemu when there is no disk on LUN 0 on
a SCSI bus but there is a disk on LUN 1. The inquiry for LUN 0 returns
SID_QUAL_LU_NOTPRESENT & T_NODEVICE. Quirks are only checked if neither
one of those are set, so cannot use a quirk entry.

Use case 1: Proxmox 6 configures each disk on its own bus when using
the "Virtio SCSI single" SCSI controller. However, while the "scsi0"
disk is on LUN 0, the "scsi1" disk is on LUN 1.

Use case 2: A Linode boot profile with multiple disks results in
the first disk ("sda") on LUN 1, while the second disk ("sdb") is
on LUN 0, each on their own bus.
 1.279.6.1 21-Jun-2017  snj Pull up following revision(s) (requested by mlelstv in ticket #53):
sys/dev/scsipi/atapiconf.c: revision 1.91
sys/dev/scsipi/cd.c: revision 1.341
sys/dev/scsipi/scsi_base.c: revision 1.92
sys/dev/scsipi/scsiconf.c: revision 1.280
sys/dev/scsipi/scsipi_base.c: revisions 1.176, 1.177
sys/dev/scsipi/sd.c: revision 1.325
sys/dev/scsipi/ss.c: revision 1.89
sys/dev/scsipi/st.c: revision 1.231
The atapibus detach path did hold the channel mutex while calling into autoconf,
which would trigger a panic when unplugging a USB ATAPI CDROM.
Align detach code for scsibus and atapibus to fix this.
Also avoid races when detaching devices by replacing callout_stop with
callout_halt.
--
pass config_detach error to caller.
 1.280.6.3 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.280.6.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.280.6.1 10-Jun-2019  christos Sync with HEAD
 1.280.4.3 18-Jan-2019  pgoyette Synch with HEAD
 1.280.4.2 20-Oct-2018  pgoyette Sync with head
 1.280.4.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.284.4.2 13-Jul-2020  martin Pull up following revision(s) (requested by kim in ticket #1000):

sys/dev/scsipi/scsiconf.c: revision 1.288

Continue scanning a SCSI bus when a LUN is reported not present

This fixes disk attachment under Qemu when there is no disk on LUN 0 on
a SCSI bus but there is a disk on LUN 1. The inquiry for LUN 0 returns
SID_QUAL_LU_NOTPRESENT & T_NODEVICE. Quirks are only checked if neither
one of those are set, so cannot use a quirk entry.

Use case 1: Proxmox 6 configures each disk on its own bus when using
the "Virtio SCSI single" SCSI controller. However, while the "scsi0"
disk is on LUN 0, the "scsi1" disk is on LUN 1.

Use case 2: A Linode boot profile with multiple disks results in
the first disk ("sda") on LUN 1, while the second disk ("sdb") is
on LUN 0, each on their own bus.
 1.284.4.1 04-May-2020  martin Pull up following revision(s) (requested by jdc in ticket #885):

sys/dev/scsipi/scsiconf.c: revision 1.287

Don't attempt to read opcodes and their timeouts at attach time for
old devices. The MAINTENANCE IN command was introduced with SCSI-3
and sending it to older peripherals can cause timeouts or them not
to respond to further requests.
 1.285.2.1 29-Feb-2020  ad Sync with head.
 1.290.4.4 03-Apr-2021  thorpej config_attach_loc() -> config_attach() with CFARG_LOCATORS argument.
 1.290.4.3 28-Mar-2021  thorpej Unwrap a line.
 1.290.4.2 21-Mar-2021  thorpej CFARG_IATTR usage audit:

If a device carries only one interface attribute, there is no need
to specify it when calling config_search(); that specification is
meant only to disambiguate which interface attribute (which is a
proxy for "what kind of attach args are being used") is having
children attached. cfparent_match() will take care of ensuring that
any potential children can attach to one of the parent's iterface
attributes, and if the parent only carries one, no disambiguation is
necessary.
 1.290.4.1 20-Mar-2021  thorpej The proliferation if config_search_*() and config_found_*() combinations
is a little absurd, so begin to tidy this up:

- Introduce a new cfarg_t enumerated type, that defines the types of
tag-value variadic arguments that can be passed to the various
config_*() functions (CFARG_SUBMATCH, CFARG_IATTR, and CFARG_LOCATORS,
for now, plus a CFARG_EOL sentinel).
- Collapse config_search_*() into config_search() that takes these
variadic arguments.
- Convert all call sites of config_search_*() to the new signature.
Noticed several incorrect usages along the way, which will be
audited in a future commit.
 1.291.8.1 04-Aug-2021  thorpej Adapt to CFARGS().
 1.304.2.1 02-Aug-2025  perseant Sync with HEAD
 1.58 20-Apr-2012  bouyer Add a bustype_async_event_xfer_mode() callback to scsipi_bustype (which can
be NULL), so that transport-specific details of transfer mode setting/printing
can be handled more easily.
Move scsipi_async_event_xfer_mode() and scsipi_print_xfer_mode() to
scsi_base.c and split in parallel scsi and FC/SAS parts.
size of struct scsipi_bustype has changed, welcome to 6.99.5
 1.57 19-Apr-2012  bouyer Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.

This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.

In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.

Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.56 16-Jul-2008  drochner branches: 1.56.28; 1.56.32; 1.56.34;
split device/softc for scsibus
 1.55 28-Apr-2008  martin branches: 1.55.2; 1.55.4; 1.55.6;
Remove clause 3 and 4 from TNF licenses
 1.54 11-Dec-2005  christos branches: 1.54.70; 1.54.72; 1.54.74;
merge ktrace-lwp.
 1.53 17-Sep-2004  mycroft Change the way bustype_cmd is used. Rather than having it be responsible for
calling scsipi_make_xs() and scsipi_execute_xs(), instead push these into
scsipi_command. Make bustype_cmd and PHOLD/PRELE be called from
scsipi_execute_xs(). This allows us to create a xfer structure -- possibly on
the stack -- and call scsipi_execute_xs() directly.
 1.52 09-Sep-2004  bouyer Make the xxstart() functions reentrant again, as some drivers HBA can call
scsipi_done() from their scsipi_request().
For this, add a struct scsipi_xfer * argument to scsipi_command().
If not NULL scsipi_command() will use this to enqueue this xfer, otherwise
it'll try to allocate a new one. This scsipi_xfer has to be allocated
and initialised by scsipi_make_xs() or equivalent.
In xxstart(), allocate a scsipi_xfer using scsipi_make_xs(), and if not NULL,
dequeue the buffer before calling scsipi_command(). This makes sure that
scsipi_command() will not fail, and also makes sure that xxstart() won't
be called again between the BUFQ_PEEK() and BUFQ_GET().

Fix "dequeued wrong buf" panics reported by Juergen Hannken-Illjes in
private mail and Andreas Wrede on current-users@.
Thanks to Jason Thorpe and Chuck Silver for review, and Andreas Wrede for
testing the patch.
 1.51 21-Aug-2004  thorpej - De-__P.
- Use ANSI function decls.
 1.50 06-Jan-2003  matt branches: 1.50.2; 1.50.4;
Add multiple inclusion protection.
 1.49 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.48 20-Oct-1999  enami branches: 1.48.8;
Cancel active transfers on aic/wdc detach.
Also makes LS-120 drive works for me again.
 1.47 05-Dec-1998  mjacob branches: 1.47.10; 1.47.12; 1.47.14;
Eliminate the moreluns entry as it makes no sense for fat SCSI busses (e.g,
FC loops). Change the semantics of scsi_probedev so that it returns 1 if
you should continue probing at this target, else 0 for not.

Replace the blanket use of '7' with the use of the new sc_maxlun property
that is now gathered from HBAs. Allocate scsipi_link arrays based upon this.
Fix a really nasty and silly bug that has been there for a while where the
number of first level scsipi_link structures was one less than it needed
to be.
 1.46 17-Nov-1998  bouyer Rename scsi_interpret_sense() to scsipi_interpret_sense() and move it from
scsi_base.c to scsipi_base.c. Rename the functions from scsi_verbose.c
too, and rename the file itself. Cleaup includes too (scsi_*.h should not
be #included in scsipi_*.h files, which are supposed to be
common to atapi and scsi).
 1.45 20-Oct-1998  thorpej Fix a sight open flags buglet pointed out by Matthias Scheler.
 1.44 17-Aug-1998  mycroft Assign my copyrights to TNF.
 1.43 15-Aug-1998  mycroft Make copyright notices with my name consistent.
 1.42 28-Mar-1998  christos Split out the verbose printing of scsi messages, so that user programs
can use the printing routines.
 1.41 13-Feb-1998  enami Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.40 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.39 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.38 27-Aug-1997  bouyer branches: 1.38.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.37 20-Aug-1997  mjacob Add in prototype for routine to print SCSI errors in detail.
 1.36 07-Jul-1997  cgd branches: 1.36.2;
mark prototypes for static inline functions as possibly unused (with
__attribute__ ((unused))), to avoid generating warnings when compiling
without optimization but with most ports' default warning flags.
 1.35 02-Apr-1997  mycroft Push the buffer cleanup code into scsi_done(), and split it so that biodone()
is called *after* the driver `done' routine. This fixes disk I/O statistics
on SCSI devices.

Also, calling the `done' routine with a `complete' argument of 0 and actually
having it do anything meaningful loses in at least 3 ways, so just nuke the
argument altogether and don't call it this way. If the driver needs to do
some error handling, that's what `err_handler' is for.
 1.34 10-Dec-1996  thorpej Add a "max_target" member to struct scsi_link, which is filled in by
host adapter drivers, indicating the highest SCSI target they can
address. Use this value to dynamically allocate data structures, rather
than hard-coding 8 targets.

These changes allow targets > 7 to be addressed on wide SCSI busses.

Fixes PRs #1674 and #2892.
 1.33 23-Oct-1996  matthias * At least the Teac FC-1 Shugart-SCSI bridgeboard does motor on/off
management by itself. But when it gets a start unit request, it keeps
the floppy motor running all the time. This adds code for dealing with
yet another quirk (SDEV_NOSTARTUNIT) that prevents sd.c from sending
start unit requests. A entry for the Teac FC-1 is added to the quirk
table.
 1.32 03-Sep-1996  thorpej Store the SCSI version in the scsi_link, and set the LUN in the CDB
if the version is <= SCSI-2. This should help some older SCSI
devices that previously needed the "NOLUNS" quirk. While this is
not strictly necessary on SCSI-2 devices, the spec allows it,
so we set it for SCSI-2 devices "just in case". See section 7.2.2 of
Draft X3T9.2 Rev 10L for details.
 1.31 28-Aug-1996  cgd (1) add a 'channel' field to scsi_link.
(2) in scsibusmatch, match channel as appropriate.
(3) add a scsiprint() function, to do the "scsibus at..."
and channel (if not SCSI_CHANNEL_ONLY_ONE) printing,
i.e. the common functionality that all SCSI drivers currently
should be doing.
 1.30 05-Jul-1996  christos - Apply PR/2535: Add support for flex scsi disks.
- Add a quirk called SCSI_NOMODESENSE for drives like the iomega jaz,
that don't support mode sense.
 1.29 19-Mar-1996  mycroft branches: 1.29.4;
Define a full set of [234][bl]tol() and lto[234][bl]() conversion functions,
inlined.
Use sized types in protocol structures.
Make the definition of scsi_sense_data less ugly.
 1.28 18-Feb-1996  mycroft Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a bit. Needs more work.
 1.27 14-Feb-1996  christos scsi prototypes
 1.26 12-Jan-1996  thorpej Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.
 1.25 12-Aug-1995  mycroft minphys() functions really should return void.
 1.24 24-Jul-1995  cgd restore a deleted semicolon
 1.23 24-Jul-1995  cgd kill structure offest comments in non-fixed-size structures.
In some of our worlds, pointers just Aren't 4 bytes.
 1.22 24-Jul-1995  cgd update SCSI minphys routines' definitions to match standard minphys()
definition and usage.
 1.21 12-Jul-1995  cgd add a SDEV_FORCELUNS flag, as suggested by Jason Thorpe, to force
the SCSI code to look for LUNs on prehistoric (pre-SCSI 1) drives &
controllers, where normally it would not.
 1.20 26-Jan-1995  mycroft Update copyrights.
 1.19 26-Jan-1995  mycroft Redo the `wait for spin up' code. The Mach 3 method clearly doesn't work.
Also, rearrange the first open sequences a bit; SDEV_OPEN is no longer magic.
 1.18 30-Dec-1994  mycroft Fix some oversights in the debugging code.
 1.17 28-Dec-1994  mycroft Numerous changes. Many bugs fixed, better autoconfig, a few new features.
 1.16 21-Nov-1994  mycroft Replace dev_unit with device_softc in scsi_link. Change argument to foostart()
to void*.
 1.15 03-Nov-1994  mycroft Always use direct configuration for SCSI devices.
 1.14 03-Nov-1994  mycroft Remove a couple of i386-specific hacks, and make a couple of others #ifdef
CONFIG_INDIRECT instead.
 1.13 30-Oct-1994  cgd be more careful with types, also pull in headers where necessary.
 1.12 20-Oct-1994  mycroft First cut at making user-level SCSI commands work. This is untested.
Partly from John Brezak.
 1.11 29-Jun-1994  cgd New RCS ID's, take two. they're more aesthecially pleasant, and use 'NetBSD'
 1.10 12-May-1994  mycroft Nuke some junk in scsi_link, as pointed out by Gordon.
 1.9 20-Apr-1994  mycroft Maintain the xs free list with LIST_*().
 1.8 11-Apr-1994  mycroft Fix various types. Remove some outdated flags.
 1.7 11-Apr-1994  mycroft Combine scsi_start_unit() and scsi_stop_unit(), and increase the start
timeout (again).
 1.6 29-Mar-1994  mycroft New SCSI system, based on Julian's more recent work.
 1.5 25-Mar-1994  mycroft Put controller target in scsi_switch.
 1.4 20-May-1993  cgd branches: 1.4.3;
add rcsids and clean up file headers
 1.3 04-May-1993  deraadt support for making dev->id_alive be set, this is for iostat to
find disk devices. wee bit of a kludge. sub-device attach()
routines must now return 1 for successful attach(), 0 otherwise.
Other bsd's do this too..
 1.2 12-Apr-1993  deraadt new scsi subsystem.
changes also in config/mkioconf.c
i386/isa/wd.c, fd.c, and all scsi drivers.
 1.1 21-Mar-1993  cgd after 0.2.2 "stable" patches applied
 1.4.3.5 01-Feb-1994  mycroft Add scsi_stop_unit(), from Mac code. Other minor nits.
 1.4.3.4 28-Nov-1993  mycroft Correct some function return types.
 1.4.3.3 24-Nov-1993  mycroft More construction...
 1.4.3.2 24-Nov-1993  mycroft Still under construction...
 1.4.3.1 24-Nov-1993  mycroft Under construction...
 1.29.4.1 04-Mar-1997  mycroft Pull up support for fetching geometry info on optical disks.
 1.36.2.1 23-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.38.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.38.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.38.2.1 27-Aug-1997  thorpej file scsiconf.h was added on branch marc-pcmcia on 1997-08-27 23:33:21 +0000
 1.47.14.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.47.12.1 15-Nov-1999  fvdl Sync with -current
 1.47.10.2 20-Oct-1999  thorpej Sync w/ trunk.
 1.47.10.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.48.8.2 07-Jan-2003  thorpej Sync with HEAD.
 1.48.8.1 21-Jun-2001  nathanw Catch up to -current.
 1.50.4.1 11-Sep-2004  he Pull up revision 1.52 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.50.2.3 21-Sep-2004  skrll Fix the sync with head I botched.
 1.50.2.2 18-Sep-2004  skrll Sync with HEAD.
 1.50.2.1 25-Aug-2004  skrll Sync with HEAD.
 1.54.74.2 04-May-2009  yamt sync with head.
 1.54.74.1 16-May-2008  yamt sync with head.
 1.54.72.1 18-May-2008  yamt sync with head.
 1.54.70.2 28-Sep-2008  mjf Sync with HEAD.
 1.54.70.1 02-Jun-2008  mjf Sync with HEAD.
 1.55.6.1 19-Oct-2008  haad Sync with HEAD.
 1.55.4.1 18-Jul-2008  simonb Sync with head.
 1.55.2.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.56.34.1 23-Apr-2012  riz Pull up following revision(s) (requested by bouyer in ticket #192):
sys/dev/scsipi/cd.c: revision 1.307
sys/dev/scsipi/scsiconf.c: revision 1.266
sys/dev/scsipi/sd.c: revision 1.298
sys/dev/scsipi/st_scsi.c: revision 1.35
sys/dev/scsipi/atapiconf.c: revision 1.85
sys/dev/scsipi/scsipiconf.h: revision 1.120
sys/dev/usb/umass_scsipi.c: revision 1.44
sys/dev/scsipi/scsiconf.h: revision 1.57
sys/dev/scsipi/st_atapi.c: revision 1.29
sys/dev/scsipi/scsipi_base.c: revision 1.158
sys/dev/scsipi/st.c: revision 1.221
sys/dev/scsipi/scsipi_ioctl.c: revision 1.67
Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.
This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.
In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.
Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.56.32.1 29-Apr-2012  mrg sync to latest -current.
 1.56.28.1 23-May-2012  yamt sync with head.
 1.35 10-Feb-2025  jakllsch Fix misspelling of unused #define
 1.34 09-Nov-2024  mlelstv Add VPD descriptor definitions.
 1.33 25-Dec-2007  perry branches: 1.33.136;
Convert many of the uses of __attribute__ to equivalent
__packed, __unused and __dead macros from cdefs.h
 1.32 01-Dec-2006  martin branches: 1.32.24; 1.32.30; 1.32.34; 1.32.38;
On Christos' request, reinstantiate the packed->__packed__ part of the
backed out change.
 1.31 01-Dec-2006  martin Back out align(4) for all struct decls - it breaks various things all
over the tree (for example esiop on sparc64 won't boot any more).
 1.30 30-Nov-2006  christos PR/34202: Dave Huang: Accessing cd device on alpha causes kernel trap

From the PR:
Since struct scsipi_read_discinfo_data is an odd number of bytes long
(35 bytes), ti ends up being unaligned. wdc_datain_pio() uses
bus_space_read_multi_4() to read the response from the drive, and
since ti is unaligned, it causes an unaligned access fault.

Add __aligned__(4) to all the structures.
 1.29 11-Dec-2005  christos branches: 1.29.20; 1.29.22;
merge ktrace-lwp.
 1.28 21-Feb-2005  thorpej branches: 1.28.4;
Part 1 of a cleanup pass over the SCSI subsystem. The aim is to name
everything "scsi_*", since we really are talking about the SCSI command
set, ATAPI transport not withstanding. Improve the names of many structures,
and prepend "SCSI_" onto all SCSI command opcodes. Place items described
by the SCSI Primary Commands document into scsi_spc.h.
 1.27 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.26 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.25 16-Oct-2003  mycroft branches: 1.25.8; 1.25.10;
Do a 36-byte SCSI 2 inquiry first, and iff that returns an additional length
>32 do a 74-byte inquiry. Fixes problems with devices that barf on longer
inquiries. (Linux uses 36 bytes almost everywhere, as a data point.)
 1.24 08-Sep-2003  mycroft Perform the exorcism on scsipi_mode_select() too.
 1.23 08-Sep-2003  mycroft Exorcise something evil and wrong in scsipi_mode_sense().
 1.22 19-Nov-2001  soren branches: 1.22.16;
Remove duplicate struct scsipi_start_stop.
 1.21 02-Sep-2001  thorpej Comment what the PF bit means in the MODE SELECT command, and
define a scsipi_mode_page_header, which has the common fields
(page code and page length) of all mode pages.
 1.20 02-Sep-2001  thorpej Centralize the definition of the page code mask and page-savable bit
in the pages returned by MODE SENSE.
 1.19 22-May-2001  bouyer branches: 1.19.2;
Remove one extra byte from scsipi_mode_select; pointed out by Juergen
Hannken-Illjes.
 1.18 18-May-2001  is When abusing structs to describe on-wire (or in-hardware) data structures,
they need to be packed. (Without this, breaks on at least m68k.)
 1.17 14-May-2001  bouyer Use SCSI/ATAPI common definition for MODE_{SELECT,SENSE}{,_BIG}. Define
functions to send theses commands in scsipi_base.c and use them instead
of ad-hoc commands setups.
 1.16 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.15 15-May-2000  dante branches: 1.15.6;
Add few peripheral device type
Remove "???" from T_IT8_1/2. They actually are pre-press devices for graphic arts as described by ASC IT8
Zeros and blanks scsipi_inquiry_data from byte 58 to byte 74 if additional_length is less than 58
 1.14 14-May-2000  dante Fix a typo
 1.13 14-May-2000  dante Change scsipi_inquiry_data strucure to be ANSI SPC-2 rev16 compliant
 1.12 30-Dec-1998  dbj branches: 1.12.8;
Added parens around ADD_BYTES_LIM() macro definition to protects its
use of the low precedence ?: conditional operator. This fixes a bug
in scsipi_interpret_sense when printing out debugging info.
 1.11 19-Nov-1998  thorpej Protect against multiple inclusion.
 1.10 11-Jul-1998  mjacob Clarify that Sense Key 1 is a "RECOVERED ERROR"- not just an error.
 1.9 01-Jul-1998  mjacob oh, for goodness sake, define the bloody Sense Keys...
 1.8 24-Jun-1998  mjacob add the SCSI-2/SCSI-3 Group ID definitions
 1.7 13-Feb-1998  enami Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.6 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.5 03-Oct-1997  thorpej Clean up and comment the device types, and add the "enclosure services",
"storage array", and "IT8" types.
 1.4 02-Oct-1997  mjacob kern/3022: make sure that printing sense info in SCSIDEBUG case starts at
the right place and contains the right number of bytes.
 1.3 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.2 27-Aug-1997  bouyer branches: 1.2.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.1 01-Jul-1997  bouyer branches: 1.1.2;
file scsipi_all.h was initially added on branch bouyer-scsipi.
 1.1.2.2 01-Jul-1997  thorpej Purely cosmetic formatting changes: some spaces -> tabs, indenting
consistency in prototypes.
 1.1.2.1 01-Jul-1997  bouyer New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
scsibus at aha
sd at scsibus
cd at scsibus
atapibus at wdc
cd at atapibus
cd has bus-specific config and function front-end in scsicd.c and atapicd.c
The call to theses functions from cd.c is conditionned to
NSCSICD and NATAPICD (all defined in cd.h by config).
 1.2.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.1 27-Aug-1997  thorpej file scsipi_all.h was added on branch marc-pcmcia on 1997-08-27 23:33:22 +0000
 1.12.8.2 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.12.8.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.15.6.3 08-Jan-2002  nathanw Catch up to -current.
 1.15.6.2 21-Sep-2001  nathanw Catch up to -current.
 1.15.6.1 21-Jun-2001  nathanw Catch up to -current.
 1.19.2.2 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.19.2.1 13-Sep-2001  thorpej Update the kqueue branch to HEAD.
 1.22.16.4 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.22.16.3 21-Sep-2004  skrll Fix the sync with head I botched.
 1.22.16.2 18-Sep-2004  skrll Sync with HEAD.
 1.22.16.1 03-Aug-2004  skrll Sync with HEAD
 1.25.10.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.25.8.1 29-Apr-2005  kent sync with -current
 1.28.4.2 21-Jan-2008  yamt sync with head
 1.28.4.1 30-Dec-2006  yamt sync with head.
 1.29.22.1 10-Dec-2006  yamt sync with head.
 1.29.20.1 12-Jan-2007  ad Sync with head.
 1.32.38.1 02-Jan-2008  bouyer Sync with HEAD
 1.32.34.1 26-Dec-2007  ad Sync with head.
 1.32.30.1 18-Feb-2008  mjf Sync with HEAD.
 1.32.24.1 09-Jan-2008  matt sync with HEAD
 1.33.136.1 02-Aug-2025  perseant Sync with HEAD
 1.193 29-Oct-2024  nat Revert to previous - scsipi_done_once is not necessary.

A follow up commit will contain a simpler change (from riastradh@) to
ncr5380sbc.
 1.192 28-Oct-2024  nat Allow medium errors to be retried.

This was necessary for some variants of the BlueSCSI-v2. For these devices
retrying upon receiving a medium error they would work.

Addreses kern/58452.

As posted to tech-kern:
https://mail-index.netbsd.org/tech-kern/2024/08/02/msg029652.html

Ok thorpej@.
 1.191 28-Oct-2024  nat Introduce scsipi_done_once.

This allows for transfers to be sucessfully aborted on the ncr5380sbc(4).

This may be usefull in future for other scsi controllers.

Callers of scsipi_done are not affected by this change.

Part of kern/58452.

As posted to tech-kern:
https://mail-index.netbsd.org/tech-kern/2024/08/02/msg029652.html

Ok thorpej@.
 1.190 14-Jun-2024  kardel branches: 1.190.2;
Ignore unit attention caused EIO errors when attempting to fetch
supported op-codes and their timeout values during device attachment.
 1.189 09-Apr-2022  riastradh sys: Use membar_release/acquire around reference drop.

This just goes through my recent reference count membar audit and
changes membar_exit to membar_release and membar_enter to
membar_acquire -- this should make everything cheaper on most CPUs
without hurting correctness, because membar_acquire is generally
cheaper than membar_enter.
 1.188 12-Mar-2022  riastradh sys: Membar audit around reference count releases.

If two threads are using an object that is freed when the reference
count goes to zero, we need to ensure that all memory operations
related to the object happen before freeing the object.

Using an atomic_dec_uint_nv(&refcnt) == 0 ensures that only one
thread takes responsibility for freeing, but it's not enough to
ensure that the other thread's memory operations happen before the
freeing.

Consider:

Thread A Thread B
obj->foo = 42; obj->baz = 73;
mumble(&obj->bar); grumble(&obj->quux);
/* membar_exit(); */ /* membar_exit(); */
atomic_dec -- not last atomic_dec -- last
/* membar_enter(); */
KASSERT(invariant(obj->foo,
obj->bar));
free_stuff(obj);

The memory barriers ensure that

obj->foo = 42;
mumble(&obj->bar);

in thread A happens before

KASSERT(invariant(obj->foo, obj->bar));
free_stuff(obj);

in thread B. Without them, this ordering is not guaranteed.

So in general it is necessary to do

membar_exit();
if (atomic_dec_uint_nv(&obj->refcnt) != 0)
return;
membar_enter();

to release a reference, for the `last one out hit the lights' style
of reference counting. (This is in contrast to the style where one
thread blocks new references and then waits under a lock for existing
ones to drain with a condvar -- no membar needed thanks to mutex(9).)

I searched for atomic_dec to find all these. Obviously we ought to
have a better abstraction for this because there's so much copypasta.
This is a stop-gap measure to fix actual bugs until we have that. It
would be nice if an abstraction could gracefully handle the different
styles of reference counting in use -- some years ago I drafted an
API for this, but making it cover everything got a little out of hand
(particularly with struct vnode::v_usecount) and I ended up setting
it aside to work on psref/localcount instead for better scalability.

I got bored of adding #ifdef __HAVE_ATOMIC_AS_MEMBAR everywhere, so I
only put it on things that look performance-critical on 5sec review.
We should really adopt membar_enter_preatomic/membar_exit_postatomic
or something (except they are applicable only to atomic r/m/w, not to
atomic_load/store_*, making the naming annoying) and get rid of all
the ifdefs.
 1.187 17-Sep-2020  jakllsch Some misspelling-in-comments fixes for scsipi
 1.186 13-Apr-2020  chs slightly change and fix the semantics of pool_set*wat(), pool_sethardlimit()
and pool_prime() (and their pool_cache_* counterparts):

- the pool_set*wat() APIs are supposed to specify thresholds for the count of
free items in the pool before pool pages are automatically allocated or freed
during pool_get() / pool_put(), whereas pool_sethardlimit() and pool_prime()
are supposed to specify minimum and maximum numbers of total items
in the pool (both free and allocated). these were somewhat conflated
in the existing code, so separate them as they were intended.

- change pool_prime() to take an absolute number of items to preallocate
rather than an increment over whatever was done before, and wait for
any memory allocations to succeed. since pool_prime() can no longer fail
after this, change its return value to void and adjust all callers.

- pool_setlowat() is documented as not immediately attempting to allocate
any memory, but it was changed some time ago to immediately try to allocate
up to the lowat level, so just fix the manpage to describe the current
behaviour.

- add a pool_cache_prime() to complete the API set.
 1.185 19-Feb-2020  riastradh branches: 1.185.4;
Sprinkle some dtrace probes into scsi(4).
 1.184 10-Nov-2019  chs branches: 1.184.2;
in many device attach paths, allocate memory with M_WAITOK instead of M_NOWAIT
and remove code to handle failures that can no longer happen.
 1.183 19-Sep-2019  msaitoh Use unsigned to avoid undefined behavior in scsipi_{get,put}_tag().
Found by kUBSan.
 1.182 28-Mar-2019  kardel branches: 1.182.4;
Add reading of supported opcodes and their timeouts
at attachment time. Though this information is optional,
it allows to override our fixed timeouts with device
provided timeouts. These timeouts will override the
hardcoded values if the device provided timeouts
exceed the hardcoded values and are less than a day.

Using the device provided timeouts avoids premature
device resets and unreliable operation due to
inadequate timeouts.

Due to the limited implementations of USB
umass devices this feature is disabled for all
umass attached devices.
 1.181 05-Feb-2019  mrg avoid a fallthru checker complaint and make one case like the rest. NFCI.
 1.180 24-Nov-2018  bouyer Add more KASSERTS about locking.
 1.179 03-Sep-2018  riastradh Rename min/max -> uimin/uimax for better honesty.

These functions are defined on unsigned int. The generic name
min/max should not silently truncate to 32 bits on 64-bit systems.
This is purely a name change -- no functional change intended.

HOWEVER! Some subsystems have

#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))

even though our standard name for that is MIN/MAX. Although these
may invite multiple evaluation bugs, these do _not_ cause integer
truncation.

To avoid `fixing' these cases, I first changed the name in libkern,
and then compile-tested every file where min/max occurred in order to
confirm that it failed -- and thus confirm that nothing shadowed
min/max -- before changing it.

I have left a handful of bootloaders that are too annoying to
compile-test, and some dead code:

cobalt ews4800mips hp300 hppa ia64 luna68k vax
acorn32/if_ie.c (not included in any kernels)
macppc/if_gm.c (superseded by gem(4))

It should be easy to fix the fallout once identified -- this way of
doing things fails safe, and the goal here, after all, is to _avoid_
silent integer truncations, not introduce them.

Maybe one day we can reintroduce min/max as type-generic things that
never silently truncate. But we should avoid doing that for a while,
so that existing code has a chance to be detected by the compiler for
conversion to uimin/uimax without changing the semantics until we can
properly audit it all. (Who knows, maybe in some cases integer
truncation is actually intended!)
 1.178 14-Jul-2017  christos branches: 1.178.2; 1.178.4; 1.178.6;
Async event can be called before the adapter is running (pmax tc asc)
 1.177 19-Jun-2017  mlelstv pass config_detach error to caller.
 1.176 17-Jun-2017  mlelstv The atapibus detach path did hold the channel mutex while calling into autoconf,
which would trigger a panic when unplugging a USB ATAPI CDROM.

Align detach code for scsibus and atapibus to fix this.

Also avoid races when detaching devices by replacing callout_stop with
callout_halt.
 1.175 22-Dec-2016  mlelstv branches: 1.175.8;
fix comment. request_sense is called unlocked.
 1.174 18-Dec-2016  skrll KNF

No functional change.
 1.173 18-Dec-2016  skrll Whitespace
 1.172 18-Dec-2016  skrll mlelstv accidentaly dropped a mutex_enter
 1.171 18-Dec-2016  mlelstv The mutex passed to cv_wait must also be held when calling cv_broadcast.
Also optimizing mutex handling in completion thread.

From nick@.
 1.170 16-Dec-2016  mlelstv Add locking for periph_active and flags. The operations aren't atomic.
 1.169 29-Nov-2016  mlelstv reference count adapter mutex possibly shared by multiple channels.

fix error in atapibusdetach, when a child device cannot be detached,
keep atapibus instance alive.
 1.168 21-Nov-2016  mlelstv Lock is already taken when handling async events, don't lock again
in scsipi_lookup_periph.

Fixes PR kern/51641.
 1.167 20-Nov-2016  mlelstv Make scsipi framework MPSAFE.

Data structures are now protected by a per-adapter mutex at IPL_BIO
that is created by the scsibus or atapibus instance when the adapter
is configured.
The enable reference counter and the channel freeze counter which are
currently used by HBA code before the adapter is configured, are made
atomic.
The target drivers are now all tagged as D_MPSAFE.

Almost all HBA drivers still require the kernel lock to present,
so all callbacks into HBA code are still protected by kernel lock
unless the driver is tagged as SCSIPI_ADAPT_MPSAFE.

TODO: refactor sd and cd to use dksubr.
 1.166 02-Oct-2016  jdolecek change scsipi_execute_xs() to default to simple tags for !XS_CTL_URGENT
if not specified by caller; this is mostly for documentation purposes
only, as sd(4) and cd(4) explicitly use simple tags already
 1.165 24-Aug-2015  pooka branches: 1.165.2;
would you like some freshly ground _KERNEL_OPT with that?
yes? excellent choice, sir/madam.
 1.164 18-Nov-2014  joerg branches: 1.164.2;
Use size for the size argument of memcmp, not the result of a compare.
 1.163 17-Nov-2014  christos PR/49054: Add a quirk for the ES-6600 RAID controller which does not do
INQUIRY3 properly. Unfortunately looking at the length does not solve
the problem since other devices send greater lengths too.
 1.162 18-Oct-2014  snj src is too big these days to tolerate superfluous apostrophes. It's
"its", people!
 1.161 06-Oct-2014  christos PR/49054: Uwe Toenjes: Some RAID controllers return more bytes in the
scsi 3 inquiry command than expected by the size of the scsi 3 inquiry
packet. This can be detected by looking at the additional_length field
returned by the scsi 2 inquiry. If that's the case, avoid doing the
scsi 3 inquiry because we can't handle the extra bytes later.
XXX: Pullup -7
 1.160 13-Jul-2014  dholland branches: 1.160.2;
"peripheral", not "peripherial".
 1.159 20-Apr-2012  bouyer branches: 1.159.2; 1.159.12;
Add a bustype_async_event_xfer_mode() callback to scsipi_bustype (which can
be NULL), so that transport-specific details of transfer mode setting/printing
can be handled more easily.
Move scsipi_async_event_xfer_mode() and scsipi_print_xfer_mode() to
scsi_base.c and split in parallel scsi and FC/SAS parts.
size of struct scsipi_bustype has changed, welcome to 6.99.5
 1.158 19-Apr-2012  bouyer Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.

This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.

In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.

Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.157 18-Apr-2012  bouyer Fix KASSERT(): autoconf doesn't run under the KERNEL_LOCK
 1.156 20-Feb-2012  mrg assert kernel lock is held in a few places in inside scsipi.
lock the kernel when calling into scsipi from umass and usscanner.

with these two in place on usbmp branch, umass appears stable.
 1.155 13-Nov-2010  uebayasi branches: 1.155.8; 1.155.12; 1.155.14;
Don't pull in the whole uvm(9) API to access only PAGE_SIZE and
some other constants. These are provided by sys/param.h now.
 1.154 23-Aug-2010  pooka Convert one second(!!) delay to kpause. It was originally done
from a callout, so delay was the only option (in those days). Then
the caller was converted to a thread, but left as a delay. It
still may block the scsipi completion thread processing (so I
seriously doubt this code path is executed very often on a live
system).
 1.153 07-Jun-2010  pgoyette Update scsiverbose module to use module_autoload() rather than module_load().
Load the module right before each attempt to use its features, and let the
module subsystem handle unloading.
 1.152 30-May-2010  pgoyette Extract SCSIVERBOSE into a kernel module. The module can be builtin
by defining 'options SCSIVERBOSE' in the kernel config file (no change
from current behavior), or it can be loaded at boot time on those
architectures that support the boot loader's "load" command.

The module is built for all architectures, whether or not SCSI or
atapi support exists.
 1.151 12-Feb-2010  pooka branches: 1.151.2;
delay() is used here, so need to include machine/param.h.
 1.150 21-Oct-2009  rmind branches: 1.150.2;
Remove uarea swap-out functionality:

- Addresses the issue described in PR/38828.
- Some simplification in threading and sleepq subsystems.
- Eliminates pmap_collect() and, as a side note, allows pmap optimisations.
- Eliminates XS_CTL_DATA_ONSTACK in scsipi code.
- Avoids few scans on LWP list and thus potentially long holds of proc_lock.
- Cuts ~1.5k lines of code. Reduces amd64 kernel size by ~4k.
- Removes __SWAP_BROKEN cases.

Tested on x86, mips, acorn32 (thanks <mpumford>) and partly tested on
acorn26 (thanks to <bjh21>).

Discussed on <tech-kern>, reviewed by <ad>.
 1.149 07-Apr-2009  dyoung Destroy a scsipi_xfer's callout before putting it back into the pool.
 1.148 11-May-2008  mlelstv branches: 1.148.6; 1.148.12;
Reintroduce the NODOORLOCK quirk. Helps with PR kern/23875.
 1.147 28-Apr-2008  martin branches: 1.147.2;
Remove clause 3 and 4 from TNF licenses
 1.146 05-Apr-2008  cegger branches: 1.146.2; 1.146.4;
use aprint_*_dev and device_xname
 1.145 09-Jul-2007  ad branches: 1.145.28;
Merge some of the less invasive changes from the vmlocking branch:

- kthread, callout, devsw API changes
- select()/poll() improvements
- miscellaneous MT safety improvements
 1.144 12-Mar-2007  ad branches: 1.144.2;
Pass an ipl argument to pool_init/POOL_INIT to be used when initializing
the pool's lock.
 1.143 09-Feb-2007  ad branches: 1.143.2; 1.143.6;
Merge newlock2 to head.
 1.142 26-Nov-2006  itohy If the block size reported by Read Capacity looks valid, just use it.
Use Request Sense only if Read Capacity succeeded and did not return
valid block size.
Discussed on tech-kern.
Fix the easier part of NetBSD PR kern/26537.
(The harder part is the device hangs on large (>= 8KB) transfer.
Possibly umass BBB problem?)

Remove scsipi_size() and scsipi_validate_secsize() from scsipi_base.c
and add their functions to sd.c since they are used only by sd.c.

Use SCSI term `block' instead of `sector' where applicable.
 1.141 16-Nov-2006  christos __unused removal on arguments; approved by core.
 1.140 20-Oct-2006  scw Validate the sector size returned by READ CAPACITY. If it looks bogus
print a warning and fallback to a suitable default.

Fixes a problem on hp700 reported by skrll@
 1.139 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.138 09-Oct-2006  scw Some removable umass(4) devices don't respond to mode sense page 6, or
simply return zero for logical block size. In either case, use the sector
length reported by READ_CAPACITY instead of defaulting to 512 bytes.

This partially addresses the problems reported in PR port-i386/34707 and
PR kern/34737. Namely the incorrectly reported drive geometry and the
'hanging' issue.

However, since the device in question reports 2048-byte physical sectors
it will remain unusable until DEV_BSIZE is banished.
 1.137 11-Sep-2006  reinoud branches: 1.137.2;
Aparently i've misread, backing out change.
 1.136 11-Sep-2006  reinoud Don't add one to the capacity returned of READ CAPACITY! it results in
reporting a sd* disc that is one sector too big (!) Normally not much a
problem in FFS since its clustered but other filingsystems *do* care.
 1.135 17-Apr-2006  nathanw branches: 1.135.8;
In scsipi_done(), bail out early if the scsipi_xfer has already been
marked as done. Works around one problem with detaching in-use sd
devices, as described on tech-kern:

http://mail-index.netbsd.org/tech-kern/2005/09/22/0002.html
 1.134 20-Feb-2006  thorpej branches: 1.134.2; 1.134.4; 1.134.6;
Use device_is_active() rather than testing dv_flags for DVF_ACTIVE
directly.
 1.133 24-Dec-2005  perry branches: 1.133.2; 1.133.4; 1.133.6;
Remove leading __ from __(const|inline|signed|volatile) -- it is obsolete.
 1.132 11-Dec-2005  christos merge ktrace-lwp.
 1.131 31-May-2005  xtraeme branches: 1.131.2;
Add missing const.
 1.130 29-May-2005  christos - Sprinkle const
- Avoid variable shadowing.
- Eliminate some caddr_t abuse.
 1.129 04-Apr-2005  yamt scsipi_completion_thread:
if we seemt to need more resources after doing ADAPTER_REQ_GROW_RESOURCES,
yield cpu rather than busy-looping. PR/25164.
 1.128 27-Feb-2005  perry nuke trailing whitespace
 1.127 21-Feb-2005  thorpej Part 1 of a cleanup pass over the SCSI subsystem. The aim is to name
everything "scsi_*", since we really are talking about the SCSI command
set, ATAPI transport not withstanding. Improve the names of many structures,
and prepend "SCSI_" onto all SCSI command opcodes. Place items described
by the SCSI Primary Commands document into scsi_spc.h.
 1.126 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.125 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.124 31-Jan-2005  reinoud As part of cleaning up sys/scsipi, replace all u_char by uint8_t and
replace all `short' with int16_t.
 1.123 07-Dec-2004  thorpej branches: 1.123.2; 1.123.4;
READ_CAPACITY -> READ_CAPACITY_10
 1.122 03-Dec-2004  thorpej And thus spake SBC-3:

If the number of logical blocks exceeds the maximum value that is able to be
specified in the RETURNED LOGICAL BLOCK ADDRESS field, the device server
shall set the RETURNED LOGICAL BLOCK ADDRESS field to FFFFFFFFh. The
application client should then issue a READ CAPACITY (16) command (see 5.11)
to retrieve the READ CAPACITY (16) parameter data.

Implement this in scsipi_size().

First issue in kern/28514.
 1.121 01-Oct-2004  enami Don't touch free'ed object. Fixes l_holdcnt leak reported by Andreas Wrede
on current-users.
 1.120 18-Sep-2004  mycroft If our enqueue failed -- because we're polling and there is already something
in the queue -- do not attempt to requeue it. We only poll in two cases:

1) We have a non-interrupting controller. In this case, execution of the
previous command should have left the queue empty. (Perhaps there should be
a KASSERT() to this effect.)

2) We're in the shutdown path, either doing a cache sync or a dump. In these
cases, the retry behavior is useless, because we will no longer get
interrupts to notify us that the earlier commands completed. Instead we
just spin for a few seconds and fail anyway. (XXX We should probably clear
the queue explicitly so that the shutdown/dump will succeed.)
 1.119 18-Sep-2004  mycroft Since we always defer probing SCSI and ATAPI devices now, we can never be
"cold" -- so change the check for this to a KASSERT().
 1.118 18-Sep-2004  mycroft Oops; eliminate the EJUSTRETURN return value from scsipi_execute_xs(). Don't
know how this worked when I tested it.
 1.117 18-Sep-2004  mycroft Minor rearrangement. Whitespace and #include cleanup.
 1.116 18-Sep-2004  mycroft Standardize some variable names and the calling pattern for scsipi_command().
Use void pointer casts.
 1.115 17-Sep-2004  mycroft Remove the "xfer" argument to scsipi_command().
 1.114 17-Sep-2004  mycroft Change the way bustype_cmd is used. Rather than having it be responsible for
calling scsipi_make_xs() and scsipi_execute_xs(), instead push these into
scsipi_command. Make bustype_cmd and PHOLD/PRELE be called from
scsipi_execute_xs(). This allows us to create a xfer structure -- possibly on
the stack -- and call scsipi_execute_xs() directly.
 1.113 17-Sep-2004  mycroft Do not manipulate xs->bp in "generic" code -- do it only in the psw_done
routine. As part of this, pass down our pre-parsed error code -- though this
interface will probably change later to accomodate better error handling.
 1.112 09-Sep-2004  bouyer Make the xxstart() functions reentrant again, as some drivers HBA can call
scsipi_done() from their scsipi_request().
For this, add a struct scsipi_xfer * argument to scsipi_command().
If not NULL scsipi_command() will use this to enqueue this xfer, otherwise
it'll try to allocate a new one. This scsipi_xfer has to be allocated
and initialised by scsipi_make_xs() or equivalent.
In xxstart(), allocate a scsipi_xfer using scsipi_make_xs(), and if not NULL,
dequeue the buffer before calling scsipi_command(). This makes sure that
scsipi_command() will not fail, and also makes sure that xxstart() won't
be called again between the BUFQ_PEEK() and BUFQ_GET().

Fix "dequeued wrong buf" panics reported by Juergen Hannken-Illjes in
private mail and Andreas Wrede on current-users@.
Thanks to Jason Thorpe and Chuck Silver for review, and Andreas Wrede for
testing the patch.
 1.111 02-Sep-2004  chs make this compile on platforms where PAGE_SIZE is not a compile-time constant.
 1.110 27-Aug-2004  bouyer Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
Havard Eidnes's analysis of this problem is that the scsipi_xfer pool is
competing for resources with other pools, including the the inode and vnode
pools which can grow quite large.

*_scsipi_cmd(): don't biodone the buffer if scsipi_make_xs() fails, let the
caller deal with the problem
start function of block devices drivers: dequeue the buffer after the
scsipi_command() call. If scsipi_command() fails with ENOMEM don't dequeue
the buffer, and schedule a callout to call the start function after
some delay.
scsipi_init(): prime the scsipi_xfer_pool with one page. This ensure that
there is always some scsipi_xfer to play with. If scsipi_command() fails
because of pool_get(), we're sure there will be resources available later,
when the pending commands have completed.

Reviewed by Jason Thorpe and Havard Eidnes.
Todo: remove the "unable to allocate scsipi_xfer" and "not queued, error %d"
printfs, but I choose to keep them for now, to help make sure the code does
what it should.
 1.109 23-Aug-2004  bouyer Fix typo pointed out by Patrick Welche on current-users@
 1.108 21-Aug-2004  thorpej Use ANSI function decls and make use of static.
 1.107 18-Aug-2004  drochner Use the new autoconf functions to rescan busses and detach devices
on user request.
This duplicates the functionality provided by a private ioctl
interface (accessible through scsictl(8)), but in a more generic way.
 1.106 04-Aug-2004  bouyer Fix some problems in the scsipi detach code:
- only call periph_switch->psw_start() if the device is active; if it is not
psw_start() may try to access invalid data.
- remove the TAILQ_FIRST(&periph->periph_xferq) != NULL diagnostic in
scsipi_kill_pending(). This can't be true at this point (unless the device
was idle at the time of the detach), because the scsipi channel kernel
thread has to run for the queue to be flushed.
There are still other problems to be fixed here ...
 1.105 27-Apr-2004  bouyer Revert part of 1.102:
Don't decrease/check xs_retries when the device report "Power On, Reset, or
Bus Device Reset" sense condition, just retry the command. The initial bus
reset would cause the first TEST_UNIT_READY to report this condition,
and as xs_retries is set to 0 when XS_CTL_DISCOVERY is set, it would report an
error instead of being retried, causing the disk probe to report "drive
offline" instead of the geometry and capacity. Checking/decreasing
xs_retries on the bus reset reported by the adapter is enouth to avoid the
problem reported by rev 1.102.

Problem analysed by Paul Kranenburg, fix confirmed by Anders Hjalmars,
and explaination as to why the INQUIRY wasn't affected by this provided by
Bill Studenmund.
 1.104 16-Mar-2004  bouyer branches: 1.104.2;
cbd -> cdb
Command Block Descriptor -> Command Descriptor Block
Pointed out by Allen Briggs.
 1.103 15-Mar-2004  bouyer Extract the code printing the CBD from scsipi_print_sense(), so that it's
usable in other context.
Use the new scsipi_print_cbd() to dump the command in case of timeout
in siop/esiop.
 1.102 10-Mar-2004  bouyer Decrease xs_retries before retrying aborted commands, and report EIO if it
reaches 0. Avoids looping on aborded command in some special cases.
 1.101 03-Jan-2004  thorpej callout_init() after memset().
 1.100 18-Nov-2003  briggs Use aprint_normal instead of printf in scsipi_print_xfer_mode().
 1.99 28-Oct-2003  simonb Fix bogus uninitialised warning.
 1.98 17-Oct-2003  mycroft Add a comment explaining the INQUIRE behavior.
 1.97 16-Oct-2003  mycroft Whoops, set cmd_length correctly for the 36-byte INQUIRE.
 1.96 16-Oct-2003  mycroft Do a 36-byte SCSI 2 inquiry first, and iff that returns an additional length
>32 do a 74-byte inquiry. Fixes problems with devices that barf on longer
inquiries. (Linux uses 36 bytes almost everywhere, as a data point.)
 1.95 12-Oct-2003  thorpej Revert previous change.
 1.94 10-Oct-2003  thorpej Warn if a sync period requiring DT was negotiated on a peripheral not
capable of DT.
 1.93 09-Sep-2003  mycroft Exorcise PQUIRK_NODOORLOCK.
 1.92 08-Sep-2003  mycroft Do a START UNIT only if the TEST UNIT READY reports that the device is not
ready. This avoids gratuitously starting the motor on floppy and CD-ROM
drives, and eliminates the need for the audio playing test in cdopen().

Therefore, also remove PQUIRK_NOSTARTUNIT.
 1.91 08-Sep-2003  mycroft On further investigation... don't panic if we try to do a 6-byte mode
sense/select on ATAPI. Some tape drives require it. And who knows? Some
fool might wire a RBC device to ATAPI.
 1.90 08-Sep-2003  mycroft Perform the exorcism on scsipi_mode_select() too.
 1.89 08-Sep-2003  mycroft Exorcise something evil and wrong in scsipi_mode_sense().
 1.88 19-Apr-2003  fvdl branches: 1.88.2;
A device should always respond to inquiry or test unit ready; disable
retries for these during discovery. From Pascal Renauld at Network
Storage Solutions, Inc
 1.87 16-Apr-2003  nathanw Remove extra right paren.
 1.86 16-Apr-2003  thorpej * Change the APIs that handle the sync period to work with 100ths of ns,
rather than 10ths of ns. This is necessary in order to represent
Ultra320 SCSI.
* Add Ultra320 SCSI to the scsipi_syncparams[] table.

We're not going to bother bumping any version numbers with this change; only
the "hba" driver uses scsipi_sync_period_to_factor(), and the uses of
scsipi_sync_factor_to_period() are all internal to the scsipi code. Most
things just pass the factor around, which is unchanged by this.

Reviewed by Frank van der Linden.
 1.85 03-Apr-2003  fvdl Check RAW_PART against the media size instead of the disklabel.
Add the media size in 512-byte sectors to the softc, to avoid
some 64 bit computations. Bump the capacity stored in softcs
for disks to 64 bits.
 1.84 03-Feb-2003  thorpej Test callout_pending(), not callout_active(), and eliminate now-unnecessary
callout_deactivate() calls.
 1.83 20-Jan-2003  simonb Only declare and set the "info" variable #ifndef SCSIVERBOSE.
 1.82 24-Nov-2002  scw Quell an uninitialised variable warning.
 1.81 09-Nov-2002  thorpej Fix signed/unsigned comparison warnings.
 1.80 04-Oct-2002  soren Leave error printing to the callers of scsipi_size().
 1.79 19-Sep-2002  jmc Force the initial probes to happen within the newly forked off kthread.
This eliminates problems where the underlying interrupt handler isn't the
specific layer calling scsipi_complete() for a given scsi transaction.
This avoids deadlocks where the kthread that called the autoconf routines
to configure a scsibus shouldn't be the one put to sleep waiting on a
scsipi_complete (only the scsibus's kthread should be doing that).

To avoid jitter this will force the scsibus's to probe in the order they
run through autoconf (so machines with multiple bus's don't move sd* devices
around on every reboot).
 1.78 26-Jul-2002  wiz Spell '[Rr]ight' correctly. From Jim Bernard.
 1.77 05-Jun-2002  mjacob As per a discussion on tech-kern a while back- honor retries for
XS_SELTIMEOUT and XS_TIMEOUT errors- but only do so if the device exists
already.

Devices that are being probed won't be found via periph_lookup, so we can
use that to find if a device exists for the purposes of honoring retries.
 1.76 03-Jun-2002  bouyer Doh, the return of scsipi_lookup_periph() was meant to be assigned to
periph.
periph was used uninitialised, and caused a panic when the scsibus is reset
with siop or esiop, and possibly others HBA drivers.
 1.75 17-May-2002  mjacob branches: 1.75.2;
Give XS_DRIVER_STUFFUP a case all by itself.
 1.74 16-May-2002  thorpej Don't use a 2-dimensional array for the channel's periph table. Instead,
hash the target and lun together and use a hash table. This will be
necessary in order to support very large (64-bit) LUN number spaces.
 1.73 15-May-2002  bouyer branches: 1.73.2;
scsipi_print_xfer_mode(): PERIPH_CAP_DT is an equivalent of
(PERIPH_CAP_SYNC | PERIPH_CAP_WIDE16) here.
 1.72 05-May-2002  bouyer If periph->periph_callout is already active, don't freeze the periph again:
scispi_periph_timed_thaw() will be called only one time anyway.
 1.71 01-Apr-2002  bouyer Add a chan_name to struct scsipi_channel, holding the channel's name.
Set this to dv_xname for scsibus and atapibus.
Set the name of the kernel thread to chan_name instead of controller's
name:channel number (so that we can use this name for controller-specific
threads).
 1.70 28-Mar-2002  christos PR/16110: Chris Jepeway: scsipi_complete() calls (*psw_done)() b/4
setting buffer fields
 1.69 16-Mar-2002  bouyer Present the same interface to periph driver for ASYNC scsipi_command() in the
normal case, and in the polling or "no thread yet" cases:
don't return an error from scsipi_complete(), as the error should already have
been handled in scsipi_complete() and eventually periph driver callbacks.
Should fix kern/15190.
 1.68 08-Mar-2002  thorpej Pool deals fairly well with physical memory shortage, but it doesn't
deal with shortages of the VM maps where the backing pages are mapped
(usually kmem_map). Try to deal with this:

* Group all information about the backend allocator for a pool in a
separate structure. The pool references this structure, rather than
the individual fields.
* Change the pool_init() API accordingly, and adjust all callers.
* Link all pools using the same backend allocator on a list.
* The backend allocator is responsible for waiting for physical memory
to become available, but will still fail if it cannot callocate KVA
space for the pages. If this happens, carefully drain all pools using
the same backend allocator, so that some KVA space can be freed.
* Change pool_reclaim() to indicate if it actually succeeded in freeing
some pages, and use that information to make draining easier and more
efficient.
* Get rid of PR_URGENT. There was only one use of it, and it could be
dealt with by the caller.

From art@openbsd.org.
 1.67 21-Feb-2002  enami Fix broken indentation.
 1.66 12-Jan-2002  tsutsui Call malloc(9) with M_ZERO flag instead of memset() after malloc().
 1.65 28-Nov-2001  fredette Added the new defopt SCSI_OLD_NOINQUIRY; this is used instead
of sun2 to wrap code that conjures up INQUIRY responses for
certain specific old devices.
 1.64 26-Nov-2001  fredette Added quirk entries for Adaptec and Emulex SCSI interposer boards.
Decode a limited set of SASI/SCSI-1 sense codes, and under sun2
only, conjure up INQUIRY responses for these boards.
 1.63 15-Nov-2001  lukem don't need <sys/types.h> when including <sys/param.h>
 1.62 13-Nov-2001  lukem add RCSIDs
 1.61 14-Oct-2001  bouyer Refuse to register a callback if the completion thread isn't started yet.
 1.60 14-Oct-2001  bouyer Call ADAPTER_REQ_GROW_RESOURCES from the completion thread, if possible.
This allows HBA drivers to call bus_dmamem_map() safely.
 1.59 14-Oct-2001  bouyer Split channel flags in chan_flags used for communications between
scsipi and HBA, and chan_tflags used for communications between scsipi
and its kernel thread. No functionnal change.
 1.58 27-Sep-2001  mjacob Restore previous functionality- in scsipi_periph_timed_thaw check to
make sure the completion thread is running before you try to schedule
it.

Fixes port-i386/14013
 1.57 21-Sep-2001  fvdl Unfortunately, the previous change seems to make most (all?) configurations
using the ahc driver hang when probing scsi devices. The problem may be in
the ahc driver and not in this change, but back it out until this has been
fixed anyway.
 1.56 18-Sep-2001  mjacob Rather than run a periph's queue from scsipi_periph_timed_thaw which is
called via a callout, kick the completion thread to run it for us (uses
a new flag, SCSIPI_CHAN_KICK).

If we've received BUSY status and we haven't started the completion
thread yet, don't freeze do a callout to scsipi_periph_timed_thaw which
then will try and kick the completion thread- instead treat the command
as if it were a polled command and just call delay for 1 second.

If DIAGNOSTIC is defined, and the periph qfreeze count is less than
zero, panic because some HBA has corrupted the periph structure's
accounting.
 1.55 01-Sep-2001  mjacob branches: 1.55.2;
Add table value for Ultra3, so configuring an Ultra3 disk shows:

sd1: sync (12.5ns offset 14), 16-bit (160.000MB/s) transfers, tagged queueing enabled

instead of:

sd1: sync (36.0ns offset 14), 16-bit (55.554MB/s) transfers, tagged queueing enabled
 1.54 20-Aug-2001  ad Cosmetic change.
 1.53 20-Aug-2001  ad Medium Not Present has number of defined ASCQs. Pointed out by Sergey
Svishchev <svs@ropnet.ru> in PR 8326.
 1.52 19-Aug-2001  yamt fix scsipi_target_detach with wildcard target.
 1.51 18-Jul-2001  bouyer Adn scsipi_target_detach() and scsipi_thread_call_callback() as discussed
on tech-kern. scsipi_target_detach() accept wildcard target/lun as requested.
 1.50 18-Jul-2001  thorpej bzero -> memset
 1.49 13-Jul-2001  bouyer scsipi_set_xfer_mode(): issue a ADAPTER_REQ_SET_XFER_MODE request to adapter
only if we succesfully attached at last one device for this I_T.
 1.48 27-Jun-2001  ross branches: 1.48.2;
compile tweak for non-SCSIVERBOSE
 1.47 27-Jun-2001  bouyer Better diagnostic when a REQUEST SENSE is terminated with CHECK CONDITION.
 1.46 26-Jun-2001  bouyer Add a XS_CTL_SILENT_NODEV flag: if the sense info is "not ready, medium
not present" don't print any message but still return ENODEV.
Use this in cd driver to allow open of character raw partition even if
the drive is empty (older drives fails at LOAD_UNLOAD time, newer
ones fail at TEST_UNIT_READY time).
 1.45 13-Jun-2001  bjh21 Add explicit support for IDE and SCSI adaptors which don't support interrupts.
On such adaptors, all transfers are done in polling mode.

OK'ed by Manuel on tech-kern.
 1.44 23-May-2001  bouyer In scsipi_channel_thaw(), if the count drops to 0, call scsipi_run_queue().
 1.43 18-May-2001  enami Don't capitalize the word sync or async. It's inconsistient with other
messages.
 1.42 18-May-2001  bouyer Workaround for broken drives (explained to me by Chris G. Demetriou):
some devices can't handle tag number larger than some values and always
reject commands with QUEUE FULL if the tag number is larger than this.
Under some circonstances the scsipi system may not decrease periph_openings
(as a workaround of other odd behavior) and we may end up requeuing the command
with a hight tag value again, and the driver could loop on this.
Workaround: in case of queue full, decrease periph_openings to
min(periph_active - 1, periph_openings - 1) so that, after some iteration,
periph_openings is less than the max tag value acceptable by the device.

Solves the problem with tagged queuing on ncr53c9x for me.
 1.41 14-May-2001  bouyer Use SCSI/ATAPI common definition for MODE_{SELECT,SENSE}{,_BIG}. Define
functions to send theses commands in scsipi_base.c and use them instead
of ad-hoc commands setups.
 1.40 27-Apr-2001  bouyer Don't forget to call psw_done() !
 1.39 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.38 09-Jun-2000  enami branches: 1.38.4;
Prevent a process being swapped out during I/O if the data buffer is
allocated on stack. This potential problem is noticed by Noriyuki Soda
and the idea and sample code to fix is given by Jason R. Thorpe.
 1.37 31-May-2000  fvdl Initialize xs_status to 0 after allocating a scsipi_xfer struct. Makes life
easier for driver debugging.
 1.36 27-May-2000  fvdl At least try to do something useful in the XS_BUSY case; don't cause
a panic by sleeping in an interrupt context.
 1.35 23-May-2000  bouyer branches: 1.35.2;
scsipi_get_xs(): if we have XS_CTL_URGENT, return an xfer even if
active >= openings. An XS_CTL_URGENT command could otherwise fail, especially
if openings == 1.
 1.34 03-Apr-2000  enami When killing pending xfers on device detach, we can't expect scsipi_done
to remove all xfers from the pending queue. It removes only xfers for
asynchronous transactions. So, simply loop over all pending xfers
with calling scsipi_done and wait xfers to drain. Addresses PR#9703.
 1.33 03-Apr-2000  enami Fold long line.
 1.32 02-Apr-2000  augustss With SCSIVERBOSE, only print sense data if there actually is a problem.
 1.31 02-Apr-2000  enami Add missing protect from disk interrupt while calling scsipi_free_xs.
 1.30 23-Mar-2000  thorpej New callout mechanism with two major improvements over the old
timeout()/untimeout() API:
- Clients supply callout handle storage, thus eliminating problems of
resource allocation.
- Insertion and removal of callouts is constant time, important as
this facility is used quite a lot in the kernel.

The old timeout()/untimeout() API has been removed from the kernel.
 1.29 17-Jan-2000  bouyer Use SCSIPIRETRIES instead of hard-coded value '2' for number of retries in
common routines. Define SCSIPIRETRIES as 4, so that the command will finally
succeed after several consecutive downgrades from UDMA2 to PIO4.
 1.28 14-Jan-2000  mjacob Print out the contents of an otherwise unreported undecodable
sense data buffer. This helps catch adapter breakage mostly.
 1.27 20-Oct-1999  enami Cancel active transfers on aic/wdc detach.
Also makes LS-120 drive works for me again.
 1.26 17-Oct-1999  enami branches: 1.26.2; 1.26.4;
ENODEV is not a value supposed to be assigned to xs->error. Use
XS_DRIVER_STUFFUP instead.
 1.25 06-Oct-1999  mjacob If we're discovering, don't tsleep on lbolt if we had a busy status.
 1.24 30-Sep-1999  thorpej branches: 1.24.2;
Cleanup the scsipi_xfer flags:
- `flags' is now gone, replaced with `xs_control' and `xs_status'.
- Massive cleanup of the control flags. Now we explicitly say that
a job is to complete asynchronously, rather than relying on side-effects,
and use a new flag to now that device discovery is being performed.
- Do SCSI device discovery interrupt-driven.
 1.23 11-Sep-1999  thorpej Implement a function to kill off all commands pending for a given
scsipi_link.
 1.22 16-Jun-1999  pk In scsipi_done(), call scsipi_free_xs() at splbio().
 1.21 07-Apr-1999  bouyer Now that we do real use of CDIOCCLOSE, we can have SSS_START|SSS_LOEJ
in scsipi_start(). Adjust test so that timeout will let enouth time to
the drive to spin up.
 1.20 02-Feb-1999  bouyer branches: 1.20.2;
If sense_key == UNIT_ATTENTION and ASC/ASQ == "Power On, Reset, or Bus Device
Reset", retry the command instead of returning an error. XS_RESET is useless
without this, as the retryed command will die with this unit attention.
 1.19 29-Jan-1999  bouyer Return ENODEV instead of EIO when we are trying to open a device without media
in the drive. restrict "opening of empty drive" to character devices only
(reading a block device returns a short read instead of ENODEV, which can lead
to confusion).
 1.18 19-Jan-1999  bouyer Move test for SDEV_NOSTARTUNIT quirk from sd.c to scsipi_start().
Add a SDEV_NOSTARTUNIT quirk entry for BCD-16X 1997-04-25", "", "VER 2.2"
CD-rom (from Michael Santos).
 1.17 10-Jan-1999  mjacob Make the error condition of neither 0x70 or 0x71 error codes a tad
more informative than the lower case 'error code %d'.
 1.16 08-Dec-1998  thorpej Actually, restructure scsipi_wait_drain() slightly.
 1.15 08-Dec-1998  thorpej - When allocating a scsipi_xfer, add it to the specified device's
pending_xfers queue.
- When freeing a scsipi_xfer, remove it from the device's pending_xfers
queue. If the queue is empty, and SDEV_WAITDRAIN is set, wakeup
those waiting for the queue to drain.
- Implement scsipi_wait_drain(), which waits for a device's pending_xfers
queue to drain.
 1.14 19-Nov-1998  thorpej Add support for reference counting and enabling/disabling SCSI and
ATAPI controllers. This will eventually be used for power management
of e.g. PCMCIA SCSI and IDE controller cards.
 1.13 17-Nov-1998  bouyer Rename scsi_interpret_sense() to scsipi_interpret_sense() and move it from
scsi_base.c to scsipi_base.c. Rename the functions from scsi_verbose.c
too, and rename the file itself. Cleaup includes too (scsi_*.h should not
be #included in scsipi_*.h files, which are supposed to be
common to atapi and scsi).
 1.12 10-Oct-1998  thorpej If the adapter returned XS_RESET and the xfer specifies a retry count,
attempt to reissue the command (which was destroyed by the bus reset).

Slightly modified from PR #6090, from Matt Jacob.
 1.11 18-Sep-1998  scottr SCSI_ASYNCREQ turns out to be redundant; we can simply free the scsipi_xfer
in scsipi_done() if the transfer is asynchronous. This reduces the size
of the critical section in scsipi_execute_xs() somewhat (in fact,
back to its original size).
 1.10 16-Sep-1998  scottr Found a race in scsipi_execute_xs(): if an asynchronous transfer completes
(probably due to an interrupt) between the time it is scheduled and the
time we get around to setting the SCSI_ASYNCREQ flag, we can lose the xs.

Fix this by checking to see if the transfer has already completed after
the scsi_cmd function returns SUCCESSFULLY_QUEUED, and just return to the
caller if so.
 1.9 14-Sep-1998  scottr Fix a problem uncovered when we started to use the pool allocator to manage
scsipi_xfer structures.

When scsipi_execute_xs() calls the driver's scsi_cmd function, it assumes
that it can still dereference a pointer to the scsipi_xfer struct. Since
scsipi_done() has already been called, which in turn has called
scsipi_free_xs(), the struct has already been returned to the pool! In
other words, xs->flags has been compromised, but we are still testing it.

These changes resolve the problem by doing the following:

- In scsipi_execute_xs(), if the lower-level driver's scsi_cmd function
returns SUCCESSFULLY_QUEUED and SCSI_NOSLEEP is set in xs->flags, set a
new flag (SCSI_ASYNCREQ). This indicates that scsipi_done() should free
the scsipi_xfer struct.

If the lower-level driver's scsi_cmd function returns SUCCESSFULLY_QUEUED
but SCSI_NOSLEEP is not set, we wait (via tsleep()) for the request to
complete, then fall through to the COMPLETE case.

If the lower-level driver's scsi_cmd function returns COMPLETE, we now
simply return any actual errors, or 0 if none occurred. (Previously,
we may have returned EJUSTRETURN, of which the sole effect was to
avoid freeing the scsipi_xfer struct in our caller. No code seems
to depend on this behavior, however.)

- In scsipi_done(), only free the scsipi_xfer struct for async requests.
The contents of the struct will otherwise remain valid until the
function that initiated the transfer frees it.

With this change, responsibility for freeing the struct now lies in two
places, depending on the type of the request:

- For synchronous requests, the routine calling scsipi_execute_xs()
must clean up.

- For asynchronous requests, scsipi_done() cleans up (as it always has).

[Note: this change also corrects a problem with sddump(): scsipi_done()
was attempting to return a static scsipi_xfer struct to the pool! Since
dumps are performed synchronously, we now handle this correctly.]

This solution was provided by Jason Thorpe, after I got him to look at
some related (but insufficient) attempts of my own.
 1.8 15-Aug-1998  mycroft Assign my copyrights to TNF.
 1.7 11-Aug-1998  scottr Eliminate a potential (but not common) NULL dereference.
 1.6 31-Jul-1998  thorpej Use the pool allocator for scsipi_xfer structures.
 1.5 10-Feb-1998  thorpej branches: 1.5.2;
Clear the scsipi_xfer's status byte before executing the command. This
fixes a condition where stale data can be left in the status byte, causing
user programs that interpret it to fail.

Fixes kern/4964 from Chris Jones <cjones@honors.montana.edu>.
 1.4 18-Oct-1997  thorpej Implement two macros, scsipi_command() and scsipi_command_direct(), and
use them to hide the structure of the function pointers we jump through
to issue a command.
 1.3 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.2 27-Aug-1997  bouyer branches: 1.2.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.1 01-Jul-1997  bouyer branches: 1.1.2;
file scsipi_base.c was initially added on branch bouyer-scsipi.
 1.1.2.2 14-Aug-1997  bouyer Sync with trunk.
 1.1.2.1 01-Jul-1997  bouyer New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
scsibus at aha
sd at scsibus
cd at scsibus
atapibus at wdc
cd at atapibus
cd has bus-specific config and function front-end in scsicd.c and atapicd.c
The call to theses functions from cd.c is conditionned to
NSCSICD and NATAPICD (all defined in cd.h by config).
 1.2.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.1 27-Aug-1997  thorpej file scsipi_base.c was added on branch marc-pcmcia on 1997-08-27 23:33:23 +0000
 1.5.2.1 08-Aug-1998  eeh Revert cdevsw mmap routines to return int.
 1.20.2.4 23-Jan-2000  he Pull up revision 1.29 (via patch, requested by bouyer):
Use SCSIPIRETRIES instead of hard-coded value '2' for number of
retries in common routines, and define it as 4 so that ATAPI
command will succeed after several downgrade.
 1.20.2.3 16-Jan-2000  he Apply patch (requested by ad):
When probing a SCSI target, do not report an error if the target
indicates that LUNs are not supported.
 1.20.2.2 24-Jun-1999  perry pullup 1.21->1.22 (pk): deal with missing "raise interrupt level" code
 1.20.2.1 07-Apr-1999  bouyer branches: 1.20.2.1.2; 1.20.2.1.4;
Pull up rev 1.21: fix timeout for SSS_START|SSS_LOEJ in scsipi_start().
 1.20.2.1.4.1 30-Nov-1999  itojun bring in latest KAME (as of 19991130, KAME/NetBSD141) into kame branch
just for reference purposes.
This commit includes 1.4 -> 1.4.1 sync for kame branch.

The branch does not compile at all (due to the lack of ALTQ and some other
source code). Please do not try to modify the branch, this is just for
referenre purposes.

synchronization to latest KAME will take place on HEAD branch soon.
 1.20.2.1.2.1 21-Jun-1999  thorpej Sync w/ -current.
 1.24.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.26.4.1 15-Nov-1999  fvdl Sync with -current
 1.26.2.17 23-Apr-2001  bouyer In channel completion thread, tsleep() at splbio, to avoid race condition
where scsipi_done would put something in the queue and wakeup between
splx(s) and tsleep().
 1.26.2.16 23-Apr-2001  mjacob Don't let the chan_qfreeze count go negative.

It's possible that the adapters shouldn't do this. But if you think about
it, this is a function that could be called from anywhere. In any case,
the specific reason here was that there is *sometimes* one more Loop UP
message from QLogic FC cards than then Loop Down, so making the chan_qfreeze
go negative blocked the queue needlessly.

Add a splbio/splx around the test against XS_STS_DONE in scsipi_execute_xs
for the non-async case so we don't do the classic sleep after test crock.
 1.26.2.15 22-Apr-2001  bouyer Keep track of the number of commands per periph really sent to the adapter.
In scsipi_run_queue(), compare this to periph_openings instead of periph_active
When a drive reject all queued commands with QUEUE FULL (my IBM DDRS34560D
does this on large writes) we end up with all commands in the queue, and
periph_active > periph_openings.
While doing this I found a bug in scsipi_async_event_channel_reset(): calling
scsipi_done() would corrupt accounting.
 1.26.2.14 11-Apr-2001  mjacob Make scsipi_channel_init a function returning an int- non-zero means
it failed to initialize the channel (this should be acceptable)- in
which case we complain and don't schedule bus probing for later.

We make the internal memory allocations for the periph and the chan_periphs
array M_NOWAIT- this way we have a hope of booting instead of silently hanging
during boot if we've run out of memory.
 1.26.2.13 03-Apr-2001  bouyer When we send a untagged command, block the queue until this command is
complete.
 1.26.2.12 28-Mar-2001  bouyer Better handling of errors for request sense commands.
 1.26.2.11 27-Mar-2001  bouyer Kill scsipi_link from comments.
 1.26.2.10 22-Jan-2001  bouyer Don't forget to remove xfer from the channel's queue in
scsipi_async_event_channel_reset()
thaw the periph when request_sense failed(), and when calling user done,
as we're not going though the usual path.
Implement XS_REQUEUE, for drivers needing to requeue a xfer.
 1.26.2.9 15-Jan-2001  bouyer Handle CHECK CONDITION status in mid-layer:
the REQUEST_SENSE command is generated from scsipi_complete() so it can
tsleep() (we use a regular scsipi_command() call for this). Add a new
periph_flag, PERIPH_SENSE, and a new xs_control, XS_CTL_REQSENSE.
When PERIPH_SENSE is set only xfer with XS_CTL_REQSENSE may be sent
(same logic as PERIPH_RECOVERING but with higther priority). xfer with
XS_CTL_REQSENSE need to have XS_CTL_URGENT urgent too.
XS_CTL_USERCMD xfers are now handled in scsipi_complete().

We need to pay special attention to SCSI resets, as we may have:
- an aborted REQUEST_SENSE. In this case we need to requeue the original
command, not the request sense.
- sense pending for a command no longer in the queue but for which a
request sense has not yet been queued. In this case we should not issue
the request sense but requeue the original command instead.

For this:
- the xfer with the CHECK CONDITION status is stored in the scsipi_periph
(periph_xscheck); the CHECK CONDITION is tested in scsipi_done, PERIPH_SENSE
and periph_xscheck is set here.
- XS_CTL_REQSENSE xfers are not allowed to be requeued, and are terminated
with EINTR in case of reset.
- we have a new async event, "ASYNC_EVENT_RESET", which cleanup the
xfer in periph_xscheck.
- appropriate splbio/splx to avoid race condition (especially,
scsipi_request_sense() runs at splbio. Should not be a real problem as it
doesn't happen often.

While I'm there kill req_sense_length from struct scsipi_xfer, it's always set
to 0 and only checked by a few drivers.
 1.26.2.8 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.26.2.7 04-Feb-2000  thorpej Make sure the channel's completion thread exits when the channel
is detached, and completely encapsulate the way periph's are stored
by the channel.
 1.26.2.6 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.26.2.5 26-Oct-1999  thorpej Deal a little more gracefully with the fact that xfer mode parameters
are for the I_T Nexus, and make all xfer mode updates `async events'.
 1.26.2.4 20-Oct-1999  thorpej Sync w/ trunk.
 1.26.2.3 20-Oct-1999  thorpej Add channel freeze/thaw, and when a timed thaw occurs on either a
channel or periph, make sure to kick the channel queue.
 1.26.2.2 19-Oct-1999  thorpej Add an asynchronous event for the adapter to notify the midlayer that
xfer mode parameters have changed for an I_T Nexus.
 1.26.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.35.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.38.4.15 11-Dec-2002  thorpej Sync with HEAD.
 1.38.4.14 11-Nov-2002  nathanw Catch up to -current
 1.38.4.13 18-Oct-2002  nathanw Catch up to -current.
 1.38.4.12 01-Aug-2002  nathanw Catch up to -current.
 1.38.4.11 20-Jun-2002  nathanw Catch up to -current.
 1.38.4.10 17-Apr-2002  nathanw Catch up to -current.
 1.38.4.9 01-Apr-2002  nathanw Catch up to -current.
(CVS: It's not just a program. It's an adventure!)
 1.38.4.8 28-Feb-2002  nathanw Catch up to -current.
 1.38.4.7 08-Jan-2002  nathanw Catch up to -current.
 1.38.4.6 14-Nov-2001  nathanw Catch up to -current.
 1.38.4.5 22-Oct-2001  nathanw Catch up to -current.
 1.38.4.4 08-Oct-2001  nathanw Catch up to -current.
 1.38.4.3 21-Sep-2001  nathanw Catch up to -current.
 1.38.4.2 24-Aug-2001  nathanw Catch up with -current.
 1.38.4.1 21-Jun-2001  nathanw Catch up to -current.
 1.48.2.9 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.48.2.8 06-Sep-2002  jdolecek sync kqueue branch with HEAD
 1.48.2.7 23-Jun-2002  jdolecek catch up with -current on kqueue branch
 1.48.2.6 16-Mar-2002  jdolecek Catch up with -current.
 1.48.2.5 11-Feb-2002  jdolecek Sync w/ -current.
 1.48.2.4 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.48.2.3 13-Sep-2001  thorpej Update the kqueue branch to HEAD.
 1.48.2.2 25-Aug-2001  thorpej Merge Aug 24 -current into the kqueue branch.
 1.48.2.1 03-Aug-2001  lukem update to -current
 1.55.2.1 01-Oct-2001  fvdl Catch up with -current.
 1.73.2.3 29-Aug-2002  gehenna catch up with -current.
 1.73.2.2 20-Jun-2002  gehenna catch up with -current.
 1.73.2.1 30-May-2002  gehenna Catch up with -current.
 1.75.2.1 04-Jun-2002  lukem Pull up revision 1.76 (requested by bouyer in ticket #149):
Doh, the return of scsipi_lookup_periph() was meant to be assigned to
periph.
periph was used uninitialised, and caused a panic when the scsibus is reset
with siop or esiop, and possibly others HBA drivers.
 1.88.2.11 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.88.2.10 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.88.2.9 18-Dec-2004  skrll Sync with HEAD.
 1.88.2.8 19-Oct-2004  skrll Sync with HEAD
 1.88.2.7 24-Sep-2004  skrll Sync with HEAD.
 1.88.2.6 21-Sep-2004  skrll Fix the sync with head I botched.
 1.88.2.5 18-Sep-2004  skrll Sync with HEAD.
 1.88.2.4 03-Sep-2004  skrll Sync with HEAD
 1.88.2.3 25-Aug-2004  skrll Sync with HEAD.
 1.88.2.2 12-Aug-2004  skrll Sync with HEAD.
 1.88.2.1 03-Aug-2004  skrll Sync with HEAD
 1.104.2.2 11-Sep-2004  he Pull up revisions 1.110-1.112 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.104.2.1 29-Apr-2004  jmc Pullup rev 1.105 (requested by bouyer in ticket #214)

Revert part of 1.102:
Don't decrease/check xs_retries when the device report "Power On, Reset, or
Bus Device Reset" sense condition, just retry the command. The initial bus
reset would cause the first TEST_UNIT_READY to report this condition,
and as xs_retries is set to 0 when XS_CTL_DISCOVERY is set, it would report an
error instead of being retried, causing the disk probe to report "drive
offline" instead of the geometry and capacity. Checking/decreasing
xs_retries on the bus reset reported by the adapter is enouth to avoid the
problem reported by rev 1.102.
 1.123.4.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.123.2.1 29-Apr-2005  kent sync with -current
 1.131.2.4 03-Sep-2007  yamt sync with head.
 1.131.2.3 26-Feb-2007  yamt sync with head.
 1.131.2.2 30-Dec-2006  yamt sync with head.
 1.131.2.1 21-Jun-2006  yamt sync with head.
 1.133.6.1 22-Apr-2006  simonb Sync with head.
 1.133.4.1 09-Sep-2006  rpaulo sync with head
 1.133.2.1 01-Mar-2006  yamt sync with head.
 1.134.6.1 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.134.4.1 19-Apr-2006  elad sync with head.
 1.134.2.1 24-May-2006  yamt sync with head.
 1.135.8.6 06-Feb-2007  ad Now that kthreads always run with kernel priority, don't rely on preempt()
to actually yield the CPU.
 1.135.8.5 30-Jan-2007  ad Remove support for SA. Ok core@.
 1.135.8.4 19-Jan-2007  ad Put back arg to preempt() for nwo.
 1.135.8.3 19-Jan-2007  ad Acquire proclist_mutex before sending signals.
 1.135.8.2 12-Jan-2007  ad Sync with head.
 1.135.8.1 18-Nov-2006  ad Sync with head.
 1.137.2.2 10-Dec-2006  yamt sync with head.
 1.137.2.1 22-Oct-2006  yamt sync with head
 1.143.6.6 01-Jul-2007  ad Adapt to callout API change.
 1.143.6.5 13-May-2007  ad - Pass the error number and residual count to biodone(), and let it handle
setting error indicators. Prepare to eliminate B_ERROR.
- Add a flag argument to brelse() to be set into the buf's flags, instead
of doing it directly. Typically used to set B_INVAL.
- Add a "struct cpu_info *" argument to kthread_create(), to be used to
create bound threads. Change "bool mpsafe" to "int flags".
- Allow exit of LWPs in the IDL state when (l != curlwp).
- More locking fixes & conversion to the new API.
 1.143.6.4 10-Apr-2007  ad Nuke the deferred kthread creation stuff, as it's no longer needed.
Pointed out by thorpej@.
 1.143.6.3 09-Apr-2007  ad - Add two new arguments to kthread_create1: pri_t pri, bool mpsafe.
- Fork kthreads off proc0 as new LWPs, not new processes.
 1.143.6.2 05-Apr-2007  ad Compile fixes.
 1.143.6.1 13-Mar-2007  ad Sync with head.
 1.143.2.1 24-Mar-2007  yamt sync with head.
 1.144.2.1 11-Jul-2007  mjf Sync with head.
 1.145.28.1 02-Jun-2008  mjf Sync with HEAD.
 1.146.4.5 09-Oct-2010  yamt sync with head
 1.146.4.4 11-Aug-2010  yamt sync with head.
 1.146.4.3 11-Mar-2010  yamt sync with head
 1.146.4.2 04-May-2009  yamt sync with head.
 1.146.4.1 16-May-2008  yamt sync with head.
 1.146.2.1 18-May-2008  yamt sync with head.
 1.147.2.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.148.12.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.148.6.1 28-Apr-2009  skrll Sync with HEAD.
 1.150.2.3 22-Oct-2010  uebayasi Sync with HEAD (-D20101022).
 1.150.2.2 17-Aug-2010  uebayasi Sync with HEAD.
 1.150.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.151.2.2 05-Mar-2011  rmind sync with head
 1.151.2.1 03-Jul-2010  rmind sync with head
 1.155.14.1 23-Apr-2012  riz Pull up following revision(s) (requested by bouyer in ticket #192):
sys/dev/scsipi/cd.c: revision 1.307
sys/dev/scsipi/scsiconf.c: revision 1.266
sys/dev/scsipi/sd.c: revision 1.298
sys/dev/scsipi/st_scsi.c: revision 1.35
sys/dev/scsipi/atapiconf.c: revision 1.85
sys/dev/scsipi/scsipiconf.h: revision 1.120
sys/dev/usb/umass_scsipi.c: revision 1.44
sys/dev/scsipi/scsiconf.h: revision 1.57
sys/dev/scsipi/st_atapi.c: revision 1.29
sys/dev/scsipi/scsipi_base.c: revision 1.158
sys/dev/scsipi/st.c: revision 1.221
sys/dev/scsipi/scsipi_ioctl.c: revision 1.67
Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.
This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.
In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.
Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.155.12.2 29-Apr-2012  mrg sync to latest -current.
 1.155.12.1 20-Feb-2012  mrg pullup from -current:
>assert kernel lock is held in a few places in inside scsipi.
>lock the kernel when calling into scsipi from umass and usscanner.
>
>with these two in place on usbmp branch, umass appears stable.
 1.155.8.2 23-May-2012  yamt sync with head.
 1.155.8.1 17-Apr-2012  yamt sync with head
 1.159.12.1 10-Aug-2014  tls Rebase.
 1.159.2.2 03-Dec-2017  jdolecek update from HEAD
 1.159.2.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.160.2.1 26-Mar-2015  martin Pull up the following revisions, requested by christos in #644:

sys/dev/scsipi/scsipi_base.c 1.161 - 1.164

Use size for the size argument of memcmp, not the result of a compare.

PR/49054: Add a quirk for the ES-6600 RAID controller which does not do
INQUIRY3 properly. Unfortunately looking at the length does not solve
the problem since other devices send greater lengths too.

src is too big these days to tolerate superfluous apostrophes. It's
"its", people!

PR/49054: Uwe Toenjes: Some RAID controllers return more bytes in the
scsi 3 inquiry command than expected by the size of the scsi 3 inquiry
packet. This can be detected by looking at the additional_length field
returned by the scsi 2 inquiry. If that's the case, avoid doing the
scsi 3 inquiry because we can't handle the extra bytes later.
 1.164.2.5 28-Aug-2017  skrll Sync with HEAD
 1.164.2.4 05-Feb-2017  skrll Sync with HEAD
 1.164.2.3 05-Dec-2016  skrll Sync with HEAD
 1.164.2.2 05-Oct-2016  skrll Sync with HEAD
 1.164.2.1 22-Sep-2015  skrll Sync with HEAD
 1.165.2.2 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.165.2.1 04-Nov-2016  pgoyette Sync with HEAD
 1.175.8.2 07-Sep-2018  martin Pull up following revision(s) (requested by mrg in ticket #1010):

sys/dev/scsipi/scsipi_base.c: revision 1.178

Async event can be called before the adapter is running (pmax tc asc)
 1.175.8.1 21-Jun-2017  snj Pull up following revision(s) (requested by mlelstv in ticket #53):
sys/dev/scsipi/atapiconf.c: revision 1.91
sys/dev/scsipi/cd.c: revision 1.341
sys/dev/scsipi/scsi_base.c: revision 1.92
sys/dev/scsipi/scsiconf.c: revision 1.280
sys/dev/scsipi/scsipi_base.c: revisions 1.176, 1.177
sys/dev/scsipi/sd.c: revision 1.325
sys/dev/scsipi/ss.c: revision 1.89
sys/dev/scsipi/st.c: revision 1.231
The atapibus detach path did hold the channel mutex while calling into autoconf,
which would trigger a panic when unplugging a USB ATAPI CDROM.
Align detach code for scsibus and atapibus to fix this.
Also avoid races when detaching devices by replacing callout_stop with
callout_halt.
--
pass config_detach error to caller.
 1.178.6.4 21-Apr-2020  martin Sync with HEAD
 1.178.6.3 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.178.6.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.178.6.1 10-Jun-2019  christos Sync with HEAD
 1.178.4.2 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.178.4.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.178.2.2 14-Jul-2017  christos 2975092
 1.178.2.1 14-Jul-2017  christos file scsipi_base.c was added on branch perseant-stdc-iso10646 on 2017-07-14 17:50:12 +0000
 1.182.4.1 31-Jan-2020  martin Pull up following revision(s) (requested by msaitoh in ticket #672):

sys/dev/ic/tulip.c: revision 1.198
sys/dev/pci/if_jme.c: revision 1.45
sys/dev/pci/agp.c: revision 1.86
sys/dev/pci/if_lii.c: revision 1.27
sys/dev/acpi/thinkpad_acpi.c: revision 1.47
sys/dev/scsipi/scsipi_base.c: revision 1.183
sys/dev/ic/aic6915reg.h: revision 1.6

Fix undefined behavior in thinkpad_mask_init(). Found by kUBSan.

Use unsigned when initialize the transmit completion ring to avoid undefined
behavior. Found by kUBSan.

Avoid undefined behavior when setting multicast address. found by kUBSan.

Use unsigned to avoid undefined behavior in agpattach(). Found by kUBSan.

Avoid undefined behavior in jme_mii_write(). Found by kUBSan.

Use unsigned to avoid undefined behavior in lii_setmulti().

Use unsigned to avoid undefined behavior in scsipi_{get,put}_tag().

Found by kUBSan.
 1.184.2.1 29-Feb-2020  ad Sync with head.
 1.185.4.1 20-Apr-2020  bouyer Sync with HEAD
 1.190.2.1 02-Aug-2025  perseant Sync with HEAD
 1.24 26-Feb-2017  maya Add a DEBUG assert for the problem noted in PR kern/51731
of some drivers not sufficiently locking.

The relevant drivers might not work, but if someone gets around to them
then the problem could be visible.

sd doesn't trip the assert.
 1.23 20-Nov-2016  mlelstv branches: 1.23.2;
Make scsipi framework MPSAFE.

Data structures are now protected by a per-adapter mutex at IPL_BIO
that is created by the scsibus or atapibus instance when the adapter
is configured.
The enable reference counter and the channel freeze counter which are
currently used by HBA code before the adapter is configured, are made
atomic.
The target drivers are now all tagged as D_MPSAFE.

Almost all HBA drivers still require the kernel lock to present,
so all callbacks into HBA code are still protected by kernel lock
unless the driver is tagged as SCSIPI_ADAPT_MPSAFE.

TODO: refactor sd and cd to use dksubr.
 1.22 28-Apr-2008  martin branches: 1.22.44; 1.22.64; 1.22.68;
Remove clause 3 and 4 from TNF licenses
 1.21 16-Nov-2006  christos branches: 1.21.48; 1.21.50; 1.21.52;
__unused removal on arguments; approved by core.
 1.20 16-Feb-2006  perry branches: 1.20.14; 1.20.16;
Change "inline" back to "__inline" in .h files -- C99 is still too
new, and some apps compile things in C89 mode. C89 keywords stay.

As per core@.
 1.19 24-Dec-2005  perry branches: 1.19.2; 1.19.4; 1.19.6;
Remove leading __ from __(const|inline|signed|volatile) -- it is obsolete.
 1.18 11-Dec-2005  christos merge ktrace-lwp.
 1.17 01-Feb-2005  reinoud branches: 1.17.6;
Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.16 31-Jan-2005  reinoud As part of cleaning up sys/scsipi, replace all u_char by uint8_t and
replace all `short' with int16_t.
 1.15 18-Sep-2004  mycroft branches: 1.15.4;
Standardize some variable names and the calling pattern for scsipi_command().
Use void pointer casts.
 1.14 21-Aug-2004  thorpej - De-__P.
- Use ANSI function decls.
 1.13 06-Jan-2003  matt branches: 1.13.2;
Add multiple inclusion protection.
 1.12 18-Jul-2001  thorpej bcopy -> memcpy
 1.11 25-Apr-2001  bouyer branches: 1.11.2;
Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.10 28-Feb-2000  thorpej branches: 1.10.6;
Make sure to initialize xs->req_sense_length (to 0). This appears to be
a historical leftover, and very few drivers actually look at it. But the
PCI `ncr' driver does, and if this field is not initialized to its default
value, that driver will fall over flat on its face when it attempts to get
sense data from a target.
 1.9 08-Dec-1998  thorpej branches: 1.9.4; 1.9.10;
Remove xs_free_list; it no longer exists.
 1.8 15-Aug-1998  mycroft Assign my copyrights to TNF.
 1.7 13-Feb-1998  enami Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.6 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.5 15-Jan-1998  cgd add support for an ATAPI attachment for 'sd'.
fix 'cd' driver's NCD_SCSI bogosity (was using testing wrong macro!)
clean up in various ways:
* make common atapi_mode_{sense,select}() functions.
* put ATAPI data structures in more sensible headers, split up by
device type.
* include headers a bit more carefully.
* pass flags to attachment-specific cd functions, and use them.
* get rid of SCSI bits in scsipi_base.h's scsipi_make_xs(), move
them into the correct place in scsi_base.c.
* fix minor typo in struct name in scsipiconf.h (which was apparently
never used except in a #define later in the same file).
* use __attribute__ to force 4-byte alignment for xs command store,
so that architectures trying to bus_space_write_multi_N() (where
N > 1) that data to a controller won't lose.
* clean up a few comments in typos, and make a few #defines easier to
understand/maintain.
* rename cd_link.h to cdvar.h (via repository copy). This is exactly
what a 'var' file is supposed to be.
 1.4 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.3 19-Sep-1997  cgd mark scsipi_make_xs() as potentially unused, so that this can be
compiled with -Wunused without optimization/inlining enabled.
Also, make it a little bit closer to KNF.
 1.2 27-Aug-1997  bouyer branches: 1.2.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.1 01-Jul-1997  bouyer branches: 1.1.2;
file scsipi_base.h was initially added on branch bouyer-scsipi.
 1.1.2.1 01-Jul-1997  bouyer New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
scsibus at aha
sd at scsibus
cd at scsibus
atapibus at wdc
cd at atapibus
cd has bus-specific config and function front-end in scsicd.c and atapicd.c
The call to theses functions from cd.c is conditionned to
NSCSICD and NATAPICD (all defined in cd.h by config).
 1.2.2.4 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.3 22-Sep-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.1 27-Aug-1997  thorpej file scsipi_base.h was added on branch marc-pcmcia on 1997-08-27 23:33:24 +0000
 1.9.10.4 15-Jan-2001  bouyer Handle CHECK CONDITION status in mid-layer:
the REQUEST_SENSE command is generated from scsipi_complete() so it can
tsleep() (we use a regular scsipi_command() call for this). Add a new
periph_flag, PERIPH_SENSE, and a new xs_control, XS_CTL_REQSENSE.
When PERIPH_SENSE is set only xfer with XS_CTL_REQSENSE may be sent
(same logic as PERIPH_RECOVERING but with higther priority). xfer with
XS_CTL_REQSENSE need to have XS_CTL_URGENT urgent too.
XS_CTL_USERCMD xfers are now handled in scsipi_complete().

We need to pay special attention to SCSI resets, as we may have:
- an aborted REQUEST_SENSE. In this case we need to requeue the original
command, not the request sense.
- sense pending for a command no longer in the queue but for which a
request sense has not yet been queued. In this case we should not issue
the request sense but requeue the original command instead.

For this:
- the xfer with the CHECK CONDITION status is stored in the scsipi_periph
(periph_xscheck); the CHECK CONDITION is tested in scsipi_done, PERIPH_SENSE
and periph_xscheck is set here.
- XS_CTL_REQSENSE xfers are not allowed to be requeued, and are terminated
with EINTR in case of reset.
- we have a new async event, "ASYNC_EVENT_RESET", which cleanup the
xfer in periph_xscheck.
- appropriate splbio/splx to avoid race condition (especially,
scsipi_request_sense() runs at splbio. Should not be a real problem as it
doesn't happen often.

While I'm there kill req_sense_length from struct scsipi_xfer, it's always set
to 0 and only checked by a few drivers.
 1.9.10.3 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.9.10.2 04-Feb-2000  thorpej Make sure the channel's completion thread exits when the channel
is detached, and completely encapsulate the way periph's are stored
by the channel.
 1.9.10.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.9.4.1 28-Feb-2000  he Pull up revision 1.10 (requested by thorpej):
Make sure to initialize the ``req_sense_length'' field. Fixes
a serious problem with the PCI ``ncr'' driver.
 1.10.6.3 07-Jan-2003  thorpej Sync with HEAD.
 1.10.6.2 24-Aug-2001  nathanw Catch up with -current.
 1.10.6.1 21-Jun-2001  nathanw Catch up to -current.
 1.11.2.1 03-Aug-2001  lukem update to -current
 1.13.2.3 21-Sep-2004  skrll Fix the sync with head I botched.
 1.13.2.2 18-Sep-2004  skrll Sync with HEAD.
 1.13.2.1 25-Aug-2004  skrll Sync with HEAD.
 1.15.4.1 29-Apr-2005  kent sync with -current
 1.17.6.1 30-Dec-2006  yamt sync with head.
 1.19.6.1 22-Apr-2006  simonb Sync with head.
 1.19.4.1 09-Sep-2006  rpaulo sync with head
 1.19.2.1 18-Feb-2006  yamt sync with head.
 1.20.16.1 10-Dec-2006  yamt sync with head.
 1.20.14.1 18-Nov-2006  ad Sync with head.
 1.21.52.1 16-May-2008  yamt sync with head.
 1.21.50.1 18-May-2008  yamt sync with head.
 1.21.48.1 02-Jun-2008  mjf Sync with HEAD.
 1.22.68.2 20-Mar-2017  pgoyette Sync with HEAD
 1.22.68.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.22.64.2 28-Aug-2017  skrll Sync with HEAD
 1.22.64.1 05-Dec-2016  skrll Sync with HEAD
 1.22.44.1 03-Dec-2017  jdolecek update from HEAD
 1.23.2.1 21-Apr-2017  bouyer Sync with HEAD
 1.21 01-Apr-2009  reinoud Implement MMC_TRACKINFO_DATA and MMC_TRACKINFO_AUDIO detection for cdrom and
dvdrom devices. These two compatibility codes were forgotten when those were
introduced.
 1.20 08-Sep-2008  gmcgarry branches: 1.20.2; 1.20.4; 1.20.8;
Replace most gcc-specific __attribute__ uses with BSD-style sys/cdef.h
preprocessor macros.
 1.19 02-May-2008  reinoud branches: 1.19.2; 1.19.6;
Add definitions for the close track/session, reserve track and repair
track SCSI commands.
 1.18 25-Dec-2007  perry branches: 1.18.6; 1.18.8; 1.18.10;
Convert many of the uses of __attribute__ to equivalent
__packed, __unused and __dead macros from cdefs.h
 1.17 01-Dec-2006  martin branches: 1.17.24; 1.17.30; 1.17.34; 1.17.38;
On Christos' request, reinstantiate the packed->__packed__ part of the
backed out change.
 1.16 01-Dec-2006  martin Back out align(4) for all struct decls - it breaks various things all
over the tree (for example esiop on sparc64 won't boot any more).
 1.15 30-Nov-2006  christos PR/34202: Dave Huang: Accessing cd device on alpha causes kernel trap

From the PR:
Since struct scsipi_read_discinfo_data is an odd number of bytes long
(35 bytes), ti ends up being unaligned. wdc_datain_pio() uses
bus_space_read_multi_4() to read the response from the drive, and
since ti is unaligned, it causes an unaligned access fault.

Add __aligned__(4) to all the structures.
 1.14 20-Dec-2005  reinoud branches: 1.14.20; 1.14.22;
The as `unused' specified field in the readdiscinfo SCSI command is really
application_code to be able to write to -RW media when its in restricted
mode.
 1.13 11-Dec-2005  simonb Remove blank line added to end of file by previous change.
 1.12 11-Dec-2005  reinoud Add SCSI calls for read discinfo and read trackinfo. Also add the various
read toc formats returned.
 1.11 11-Dec-2005  christos merge ktrace-lwp.
 1.10 01-Feb-2005  reinoud branches: 1.10.6;
Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.9 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.8 30-Jan-2005  reinoud The code asumes all programs want to know is TOC response format 0. Support
for TOC response format 1 and 2 are mandatory on CD/DVD too and provide
more information.

Next an IOCTL needs to be implemented that can read all TOC formats in a
generic way. This is pending.
 1.7 07-Sep-2003  mycroft branches: 1.7.8; 1.7.10;
Set PQUIRK_ONLYBIG in the wdc-atapi frontend, obviating the need to ever test
the "bus type" for this.

Merge all the code in the SCSI and ATAPI backends for "cd" devices. All of
the mode page handling and whatnot is general to SCSI MMC devices, and should
never have been separated to begin with. This fixes a variety of problems,
and adds load/unload support for SCSI-attached devices.
 1.6 02-Sep-2001  thorpej branches: 1.6.20;
Centralize the definition of the page code mask and page-savable bit
in the pages returned by MODE SENSE.
 1.5 13-Feb-1998  enami branches: 1.5.26; 1.5.28;
Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.4 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.3 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.2 27-Aug-1997  bouyer branches: 1.2.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.1 01-Jul-1997  bouyer branches: 1.1.2;
file scsipi_cd.h was initially added on branch bouyer-scsipi.
 1.1.2.2 01-Jul-1997  thorpej Purely cosmetic formatting changes: some spaces -> tabs, indenting
consistency in prototypes.
 1.1.2.1 01-Jul-1997  bouyer New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
scsibus at aha
sd at scsibus
cd at scsibus
atapibus at wdc
cd at atapibus
cd has bus-specific config and function front-end in scsicd.c and atapicd.c
The call to theses functions from cd.c is conditionned to
NSCSICD and NATAPICD (all defined in cd.h by config).
 1.2.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.1 27-Aug-1997  thorpej file scsipi_cd.h was added on branch marc-pcmcia on 1997-08-27 23:33:25 +0000
 1.5.28.1 13-Sep-2001  thorpej Update the kqueue branch to HEAD.
 1.5.26.1 21-Sep-2001  nathanw Catch up to -current.
 1.6.20.4 04-Feb-2005  skrll Sync with HEAD.
 1.6.20.3 21-Sep-2004  skrll Fix the sync with head I botched.
 1.6.20.2 18-Sep-2004  skrll Sync with HEAD.
 1.6.20.1 03-Aug-2004  skrll Sync with HEAD
 1.7.10.1 12-Feb-2005  yamt sync with head.
 1.7.8.1 29-Apr-2005  kent sync with -current
 1.10.6.3 21-Jan-2008  yamt sync with head
 1.10.6.2 30-Dec-2006  yamt sync with head.
 1.10.6.1 21-Jun-2006  yamt sync with head.
 1.14.22.1 10-Dec-2006  yamt sync with head.
 1.14.20.1 12-Jan-2007  ad Sync with head.
 1.17.38.1 02-Jan-2008  bouyer Sync with HEAD
 1.17.34.1 26-Dec-2007  ad Sync with head.
 1.17.30.1 18-Feb-2008  mjf Sync with HEAD.
 1.17.24.1 09-Jan-2008  matt sync with HEAD
 1.18.10.2 04-May-2009  yamt sync with head.
 1.18.10.1 16-May-2008  yamt sync with head.
 1.18.8.1 18-May-2008  yamt sync with head.
 1.18.6.2 28-Sep-2008  mjf Sync with HEAD.
 1.18.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.19.6.1 19-Oct-2008  haad Sync with HEAD.
 1.19.2.1 24-Sep-2008  wrstuden Merge in changes between wrstuden-revivesa-base-2 and
wrstuden-revivesa-base-3.
 1.20.8.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.20.4.1 08-Apr-2009  snj Pull up following revision(s) (requested by reinoud in ticket #633):
sys/dev/scsipi/cd.c: revision 1.291
sys/dev/scsipi/scsipi_cd.h: revision 1.21
Implement MMC_TRACKINFO_DATA and MMC_TRACKINFO_AUDIO detection for cdrom and
dvdrom devices. These two compatibility codes were forgotten when those were
introduced.
 1.20.2.1 28-Apr-2009  skrll Sync with HEAD.
 1.17 28-Apr-2008  martin Remove clause 3 and 4 from TNF licenses
 1.16 11-Dec-2005  christos branches: 1.16.70; 1.16.72; 1.16.74;
merge ktrace-lwp.
 1.15 27-Feb-2005  perry nuke trailing whitespace
 1.14 30-May-2001  mrg branches: 1.14.22; 1.14.30; 1.14.32;
use _KERNEL_OPT
 1.13 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.12 13-Feb-1998  enami branches: 1.12.14; 1.12.26;
Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.11 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.10 24-Oct-1997  christos PR/4328: Matthias Scheler: Syntax errors in debug macros.
 1.9 03-Oct-1997  enami branches: 1.9.2;
- Rename PIDEBUG back to SCSIDEBUG, since other files are still
using SCSIDEBUG.
- Don't #undef PIDEBUG unconditionally.
 1.8 27-Aug-1997  bouyer branches: 1.8.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.7 12-Oct-1996  christos revert previous kprintf change
 1.6 10-Oct-1996  christos printf -> kprintf, sprintf -> ksprintf
 1.5 28-Dec-1994  mycroft Numerous changes. Many bugs fixed, better autoconfig, a few new features.
 1.4 21-Nov-1994  mycroft Replace dev_unit with device_softc in scsi_link. Change argument to foostart()
to void*.
 1.3 29-Jun-1994  cgd New RCS ID's, take two. they're more aesthecially pleasant, and use 'NetBSD'
 1.2 22-Jan-1994  briggs If SCSI_DEBUG is on, a structure member wasn't updated from adapter_unit
to scsibus.
 1.1 24-Nov-1993  mycroft Under construction...
 1.8.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.8.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.8.2.1 27-Aug-1997  thorpej file scsipi_debug.h was added on branch marc-pcmcia on 1997-08-27 23:33:26 +0000
 1.9.2.1 24-Oct-1997  mellon Pull rev 1.10 up from trunk
 1.12.26.1 21-Jun-2001  nathanw Catch up to -current.
 1.12.14.1 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.14.32.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.14.30.1 29-Apr-2005  kent sync with -current
 1.14.22.1 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.16.74.1 16-May-2008  yamt sync with head.
 1.16.72.1 18-May-2008  yamt sync with head.
 1.16.70.1 02-Jun-2008  mjf Sync with HEAD.
 1.24 27-Feb-2025  jakllsch add more READ CAPACITY 16 data definitions
 1.23 04-Nov-2017  jdolecek branches: 1.23.42;
fix comment - scsipi_rw_big was renamed to scsipi_rw_10 a while ago
 1.22 05-Apr-2017  jdolecek expose disk device FUA/DPO support via DIOCGCACHE, and allow the flags
to be set for I/O; implement support in sd(4) and nvme(4)

discussed on tech-kern
 1.21 25-Dec-2007  perry branches: 1.21.54; 1.21.74; 1.21.78; 1.21.82;
Convert many of the uses of __attribute__ to equivalent
__packed, __unused and __dead macros from cdefs.h
 1.20 01-Jul-2007  tsutsui branches: 1.20.8; 1.20.14; 1.20.16; 1.20.20;
Add __attribute__((__packed__)) to all command structures.
 1.19 01-Dec-2006  martin branches: 1.19.8; 1.19.10;
On Christos' request, reinstantiate the packed->__packed__ part of the
backed out change.
 1.18 01-Dec-2006  martin Back out align(4) for all struct decls - it breaks various things all
over the tree (for example esiop on sparc64 won't boot any more).
 1.17 30-Nov-2006  christos PR/34202: Dave Huang: Accessing cd device on alpha causes kernel trap

From the PR:
Since struct scsipi_read_discinfo_data is an odd number of bytes long
(35 bytes), ti ends up being unaligned. wdc_datain_pio() uses
bus_space_read_multi_4() to read the response from the drive, and
since ti is unaligned, it causes an unaligned access fault.

Add __aligned__(4) to all the structures.
 1.16 11-Dec-2005  christos branches: 1.16.20; 1.16.22;
merge ktrace-lwp.
 1.15 01-Feb-2005  reinoud branches: 1.15.6;
Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.14 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.13 07-Dec-2004  thorpej branches: 1.13.2;
READ_CAPACITY -> READ_CAPACITY_10
 1.12 07-Dec-2004  thorpej USe more appropriate macro/struct names for READ/WRITE (6) and
READ/WRITE (10).
 1.11 04-Dec-2004  thorpej Use READ/WRITE (16) if the LBA is larger than 32 bits. Another bit
of kern/28514.
 1.10 03-Dec-2004  thorpej And thus spake SBC-3:

If the number of logical blocks exceeds the maximum value that is able to be
specified in the RETURNED LOGICAL BLOCK ADDRESS field, the device server
shall set the RETURNED LOGICAL BLOCK ADDRESS field to FFFFFFFFh. The
application client should then issue a READ CAPACITY (16) command (see 5.11)
to retrieve the READ CAPACITY (16) parameter data.

Implement this in scsipi_size().

First issue in kern/28514.
 1.9 17-Sep-2003  mycroft If READ CAPACITY fails, try a READ FORMAT CAPACITIES. Separate this into
another function, always doing a page 0 MODE SENSE to get the block
descriptor if we use READ CAPACITY, and use SMS_DBD on the page 4/5 MODE
SENSE. This does one extra command in some cases, but it separates and
simplifies the code a little.

Why do we prefer READ CAPACITY over READ FORMAT CAPACITIES? Two reasons:
1) It's much older and is much less likely to have had its command code
abused, and is thus "safer" to try first. 2) ALL of my USB flash readers
and pen drives screw up their capacity descriptors -- mostly off-by-one
errors in the size (they return the maximum LBA number instead, a la READ
CAPACITY, which has *never* been how READ FORMAT CAPACITIES was documented
in the MMC spec), and one returns the "no media" code on slots that have
media inserted (despite returning almost-correct data otherwise)!

F*** me with a chainsaw.
 1.8 06-Jan-2003  matt branches: 1.8.2;
Add multiple inclusion protection.
 1.7 19-Nov-2001  soren Remove duplicate struct scsipi_start_stop.
 1.6 02-Sep-2001  thorpej Add the Force Unit Access and Disable Page Out bits to the
READ(10) and WRITE(10) commands.
 1.5 13-Feb-1998  enami branches: 1.5.26; 1.5.28;
Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.4 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.3 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.2 27-Aug-1997  bouyer branches: 1.2.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.1 01-Jul-1997  bouyer branches: 1.1.2;
file scsipi_disk.h was initially added on branch bouyer-scsipi.
 1.1.2.1 01-Jul-1997  bouyer New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
scsibus at aha
sd at scsibus
cd at scsibus
atapibus at wdc
cd at atapibus
cd has bus-specific config and function front-end in scsicd.c and atapicd.c
The call to theses functions from cd.c is conditionned to
NSCSICD and NATAPICD (all defined in cd.h by config).
 1.2.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.1 27-Aug-1997  thorpej file scsipi_disk.h was added on branch marc-pcmcia on 1997-08-27 23:33:27 +0000
 1.5.28.2 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.5.28.1 13-Sep-2001  thorpej Update the kqueue branch to HEAD.
 1.5.26.3 07-Jan-2003  thorpej Sync with HEAD.
 1.5.26.2 08-Jan-2002  nathanw Catch up to -current.
 1.5.26.1 21-Sep-2001  nathanw Catch up to -current.
 1.8.2.4 18-Dec-2004  skrll Sync with HEAD.
 1.8.2.3 21-Sep-2004  skrll Fix the sync with head I botched.
 1.8.2.2 18-Sep-2004  skrll Sync with HEAD.
 1.8.2.1 03-Aug-2004  skrll Sync with HEAD
 1.13.2.1 29-Apr-2005  kent sync with -current
 1.15.6.3 21-Jan-2008  yamt sync with head
 1.15.6.2 03-Sep-2007  yamt sync with head.
 1.15.6.1 30-Dec-2006  yamt sync with head.
 1.16.22.1 10-Dec-2006  yamt sync with head.
 1.16.20.1 12-Jan-2007  ad Sync with head.
 1.19.10.1 11-Jul-2007  mjf Sync with head.
 1.19.8.1 15-Jul-2007  ad Sync with head.
 1.20.20.1 02-Jan-2008  bouyer Sync with HEAD
 1.20.16.1 26-Dec-2007  ad Sync with head.
 1.20.14.1 18-Feb-2008  mjf Sync with HEAD.
 1.20.8.1 09-Jan-2008  matt sync with HEAD
 1.21.82.1 21-Apr-2017  bouyer Sync with HEAD
 1.21.78.1 26-Apr-2017  pgoyette Sync with HEAD
 1.21.74.1 28-Aug-2017  skrll Sync with HEAD
 1.21.54.1 03-Dec-2017  jdolecek update from HEAD
 1.23.42.1 02-Aug-2025  perseant Sync with HEAD
 1.73 27-Dec-2019  msaitoh s/transfered/transferred/
 1.72 30-May-2019  mlelstv use correct size when copying outgoing sense data.
 1.71 26-May-2019  mlelstv Add sanity checks to SCIOCCOMMAND, adapter drivers might be confused or trigger
assertions (e.g. umass).
 1.70 03-Sep-2018  riastradh Rename min/max -> uimin/uimax for better honesty.

These functions are defined on unsigned int. The generic name
min/max should not silently truncate to 32 bits on 64-bit systems.
This is purely a name change -- no functional change intended.

HOWEVER! Some subsystems have

#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))

even though our standard name for that is MIN/MAX. Although these
may invite multiple evaluation bugs, these do _not_ cause integer
truncation.

To avoid `fixing' these cases, I first changed the name in libkern,
and then compile-tested every file where min/max occurred in order to
confirm that it failed -- and thus confirm that nothing shadowed
min/max -- before changing it.

I have left a handful of bootloaders that are too annoying to
compile-test, and some dead code:

cobalt ews4800mips hp300 hppa ia64 luna68k vax
acorn32/if_ie.c (not included in any kernels)
macppc/if_gm.c (superseded by gem(4))

It should be easy to fix the fallout once identified -- this way of
doing things fails safe, and the goal here, after all, is to _avoid_
silent integer truncations, not introduce them.

Maybe one day we can reintroduce min/max as type-generic things that
never silently truncate. But we should avoid doing that for a while,
so that existing code has a chance to be detected by the compiler for
conversion to uimin/uimax without changing the semantics until we can
properly audit it all. (Who knows, maybe in some cases integer
truncation is actually intended!)
 1.69 20-Nov-2016  mlelstv branches: 1.69.8; 1.69.14; 1.69.16;
Make scsipi framework MPSAFE.

Data structures are now protected by a per-adapter mutex at IPL_BIO
that is created by the scsibus or atapibus instance when the adapter
is configured.
The enable reference counter and the channel freeze counter which are
currently used by HBA code before the adapter is configured, are made
atomic.
The target drivers are now all tagged as D_MPSAFE.

Almost all HBA drivers still require the kernel lock to present,
so all callbacks into HBA code are still protected by kernel lock
unless the driver is tagged as SCSIPI_ADAPT_MPSAFE.

TODO: refactor sd and cd to use dksubr.
 1.68 24-Aug-2015  pooka branches: 1.68.2;
to garnish, dust with _KERNEL_OPT
 1.67 19-Apr-2012  bouyer branches: 1.67.2; 1.67.14; 1.67.16; 1.67.18; 1.67.22;
Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.

This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.

In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.

Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.66 14-Jul-2008  drochner branches: 1.66.28; 1.66.32; 1.66.34;
set the buffer's "resid" if a raw SCSI command fails, otherwise a
diagnostic check in kern_physio is set up
 1.65 28-Apr-2008  martin branches: 1.65.2; 1.65.4; 1.65.6;
Remove clause 3 and 4 from TNF licenses
 1.64 02-Jan-2008  ad branches: 1.64.6; 1.64.8; 1.64.10;
Merge vmlocking2 to head.
 1.63 29-Jul-2007  ad branches: 1.63.6; 1.63.12; 1.63.14; 1.63.18; 1.63.22;
It's not a good idea for device drivers to modify b_flags, as they don't
need to understand the locking around that field. Instead of setting
B_ERROR, set b_error instead. b_error is 'owned' by whoever completes
the I/O request.
 1.62 04-Mar-2007  christos branches: 1.62.2; 1.62.10;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.61 14-Apr-2006  christos branches: 1.61.14;
Coverity CID 1101: Protect against address argument being NULL.
 1.60 28-Mar-2006  thorpej Use device_unit().
 1.59 25-Mar-2006  thorpej Use device_parent().
 1.58 01-Mar-2006  yamt branches: 1.58.2; 1.58.4; 1.58.6;
merge yamt-uio_vmspace branch.

- use vmspace rather than proc or lwp where appropriate.
the latter is more natural to specify an address space.
(and less likely to be abused for random purposes.)
- fix a swdmover race.
 1.57 14-Dec-2005  reinoud branches: 1.57.2; 1.57.4; 1.57.6;
Allow SCIOCCOMMAND ioctl that executes a SCSI command to also be issued
within the kernel when the FKIOCTL flag is passed to the ioctl.

No code actually uses this yet but is committed for completion.
 1.56 11-Dec-2005  christos merge ktrace-lwp.
 1.55 01-Nov-2005  martin Rework the funny "user_strat: No ioctl" error messages - the functions
have not been called user_strat for quite some time and the message is
confusing if you do not know the code.
 1.54 01-Nov-2005  bouyer Don't claim there is a data in or out phase if the datalen is 0 (userland
shouldn't claim it either, but a buggy software shouldn't be able to crash
the kernel anyway). Should fix port-sparc64/31925 by Johan A.van Zanten
(which should really be kern/31925).
Analysed and patch tested by Martin Husemann.
 1.53 31-Oct-2005  yamt fix ioctl problems after the recent physio changes
in some drivers including wd and scsi.

- physio: if a caller provided a buf, stick to use it
because some drivers use it as an identifier.
- sprinkle simple_locks.
- scsistrategy: rather than issueing an async request and
waiting for its completion, simply issue a sync request.
the way to wait for the completion had an assumption that
B_CALL is never used. it isn't the case after the recent
physio() changes.

pointed/analyzed/tested by Martin Husemann.
 1.52 01-Feb-2005  reinoud branches: 1.52.4; 1.52.6; 1.52.8;
Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.51 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.50 18-Sep-2004  mycroft branches: 1.50.4;
Standardize some variable names and the calling pattern for scsipi_command().
Use void pointer casts.
 1.49 17-Sep-2004  mycroft Remove the "xfer" argument to scsipi_command().
 1.48 09-Sep-2004  bouyer Make the xxstart() functions reentrant again, as some drivers HBA can call
scsipi_done() from their scsipi_request().
For this, add a struct scsipi_xfer * argument to scsipi_command().
If not NULL scsipi_command() will use this to enqueue this xfer, otherwise
it'll try to allocate a new one. This scsipi_xfer has to be allocated
and initialised by scsipi_make_xs() or equivalent.
In xxstart(), allocate a scsipi_xfer using scsipi_make_xs(), and if not NULL,
dequeue the buffer before calling scsipi_command(). This makes sure that
scsipi_command() will not fail, and also makes sure that xxstart() won't
be called again between the BUFQ_PEEK() and BUFQ_GET().

Fix "dequeued wrong buf" panics reported by Juergen Hannken-Illjes in
private mail and Andreas Wrede on current-users@.
Thanks to Jason Thorpe and Chuck Silver for review, and Andreas Wrede for
testing the patch.
 1.47 21-Aug-2004  thorpej Use ANSI function decls and make use of static.
 1.46 29-Jun-2003  fvdl branches: 1.46.2; 1.46.4;
Back out the lwp/ktrace changes. They contained a lot of colateral damage,
and need to be examined and discussed more.
 1.45 28-Jun-2003  darrenr Pass lwp pointers throughtout the kernel, as required, so that the lwpid can
be inserted into ktrace records. The general change has been to replace
"struct proc *" with "struct lwp *" in various function prototypes, pass
the lwp through and use l_proc to get the process pointer when needed.

Bump the kernel rev up to 1.6V
 1.44 23-Feb-2003  yamt initialize b_interlock properly.
(for ioctl)
 1.43 12-Jan-2002  tsutsui branches: 1.43.10;
Call malloc(9) with M_ZERO flag instead of memset() after malloc().
 1.42 15-Nov-2001  lukem don't need <sys/types.h> when including <sys/param.h>
 1.41 13-Nov-2001  lukem add RCSIDs
 1.40 18-Jul-2001  thorpej branches: 1.40.2;
bcopy -> memcpy
 1.39 18-Jul-2001  thorpej bzero -> memset
 1.38 25-Apr-2001  bouyer branches: 1.38.2;
Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.37 30-Sep-1999  thorpej branches: 1.37.2; 1.37.8; 1.37.14;
Cleanup the scsipi_xfer flags:
- `flags' is now gone, replaced with `xs_control' and `xs_status'.
- Massive cleanup of the control flags. Now we explicitly say that
a job is to complete asynchronously, rather than relying on side-effects,
and use a new flag to now that device discovery is being performed.
- Do SCSI device discovery interrupt-driven.
 1.36 11-Jan-1999  thorpej Add missing XS_SELTIMEOUT case in user command completion path, returning
SCCMD_TIMEOUT. Based on PR #6720, Andreas Wrede.
 1.35 17-Nov-1998  bouyer Rename scsi_interpret_sense() to scsipi_interpret_sense() and move it from
scsi_base.c to scsipi_base.c. Rename the functions from scsi_verbose.c
too, and rename the file itself. Cleaup includes too (scsi_*.h should not
be #included in scsipi_*.h files, which are supposed to be
common to atapi and scsi).
 1.34 10-Oct-1998  thorpej Garbage-collect the SCIOCREPROBE and OSCIOCREPROBE ioctls. This is a
bus-oriented command. (How much sense does it make to open a device
to rescan the bus?!)
 1.33 17-Aug-1998  mycroft Assign my copyrights to TNF.
 1.32 15-Aug-1998  mycroft Make copyright notices with my name consistent.
 1.31 05-Jul-1998  jonathan * defopt COMPAT_{09,10,11,12,13} and COMPAT_NOMID.
TODO: revisit interaction between native compat and emul compat usage.
 1.30 25-Jun-1998  thorpej defopt COMPAT_FREEBSD
 1.29 15-Jun-1998  bouyer Treat OSCIOCIDENTIFY just as SCIOCIDENTIFY regarding permissions, don't
require the file descriptor to be open read/write. Fix PR kern/5592
from John F. Woods.
 1.28 18-Oct-1997  thorpej Implement two macros, scsipi_command() and scsipi_command_direct(), and
use them to hide the structure of the function pointers we jump through
to issue a command.
 1.27 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.26 19-Sep-1997  enami Add new ioctl OSCIOCREPROBE and OSCIOCIDENTIFY to support binary
compatibility with NetBSD 1.2.
 1.25 27-Aug-1997  bouyer branches: 1.25.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.24 26-Apr-1997  augustss Don't require that a device is open for writing to perform a raw
SCSI command that just reads.
 1.23 12-Oct-1996  christos revert previous kprintf change
 1.22 10-Oct-1996  christos printf -> kprintf, sprintf -> ksprintf
 1.21 13-Sep-1996  thorpej For ioctl commands which may change the device's state, ensure that
the caller has the device open for writing.
 1.20 14-Feb-1996  christos branches: 1.20.4;
scsi prototypes
 1.19 26-Sep-1995  thorpej Don't declare Debugger(). It's handled in <sys/systm.h>.
#include <sys/systm.h> where necessary, as suggested by Jonathan Stone.
Fixes PR #1511.
 1.18 30-Jan-1995  mycroft Fix invocation of scsi_scsi_cmd(), per John Kohl.
 1.17 23-Jan-1995  mycroft Remove old, misleading comment.
 1.16 28-Dec-1994  mycroft Numerous changes. Many bugs fixed, better autoconfig, a few new features.
 1.15 01-Dec-1994  mycroft Set UIO residual count.
 1.14 01-Dec-1994  mycroft Always set the device number correctly, even though we don't need it.
 1.13 01-Dec-1994  mycroft Redux.
 1.12 01-Dec-1994  mycroft Uncomment call to scsi_user_done(), and remove a duplicate biodone() (and
another si_free()).
 1.11 01-Dec-1994  mycroft Remove the silly statically allocated array, and simply malloc the data
structures as needed. Also, put the buf in the malloc()ed area. Remove a
duplicate si_free() (though this was harmless before).
 1.10 30-Oct-1994  cgd be more careful with types, also pull in headers where necessary.
 1.9 23-Oct-1994  mycroft Make this more likely to work.
 1.8 20-Oct-1994  mycroft First cut at making user-level SCSI commands work. This is untested.
Partly from John Brezak.
 1.7 29-Jun-1994  cgd New RCS ID's, take two. they're more aesthecially pleasant, and use 'NetBSD'
 1.6 16-Jun-1994  mycroft b_un.b_addr -> b_data
 1.5 11-Apr-1994  mycroft Fix various types. Remove some outdated flags.
 1.4 29-Mar-1994  mycroft New SCSI system, based on Julian's more recent work.
 1.3 29-Nov-1993  mycroft Use new struct buf fields.
 1.2 24-Nov-1993  mycroft Still under construction...
 1.1 24-Nov-1993  mycroft Under construction...
 1.20.4.1 14-Nov-1996  thorpej Pull up from trunk:

> For ioctl commands which may change the device's state, ensure that
> the caller has the device open for writing.
 1.25.2.4 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.25.2.3 22-Sep-1997  thorpej Update marc-pcmcia branch from trunk.
 1.25.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.25.2.1 27-Aug-1997  thorpej file scsipi_ioctl.c was added on branch marc-pcmcia on 1997-08-27 23:33:28 +0000
 1.37.14.5 28-Feb-2002  nathanw Catch up to -current.
 1.37.14.4 08-Jan-2002  nathanw Catch up to -current.
 1.37.14.3 14-Nov-2001  nathanw Catch up to -current.
 1.37.14.2 24-Aug-2001  nathanw Catch up with -current.
 1.37.14.1 21-Jun-2001  nathanw Catch up to -current.
 1.37.8.1 21-Dec-1999  wrstuden Initial commit of recent changes to make DEV_BSIZE go away.

Runs on i386, needs work on other arch's. Main kernel routines should be
fine, but a number of the stand programs need help.

cd, fd, ccd, wd, and sd have been updated. sd has been tested with non-512
byte block devices. vnd, raidframe, and lfs need work.

Non 2**n block support is automatic for LKM's and conditional for kernels
on "options NON_PO2_BLOCKS".
 1.37.2.2 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.37.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.38.2.3 11-Feb-2002  jdolecek Sync w/ -current.
 1.38.2.2 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.38.2.1 03-Aug-2001  lukem update to -current
 1.40.2.1 07-Sep-2001  thorpej Commit my "devvp" changes to the thorpej-devvp branch. This
replaces the use of dev_t in most places with a struct vnode *.

This will form the basic infrastructure for real cloning device
support (besides being architecurally cleaner -- it'll be good
to get away from using numbers to represent objects).
 1.43.10.1 03-Nov-2005  riz Pull up following revision(s) (requested by bouyer in ticket #5929):
sys/dev/scsipi/scsipi_ioctl.c: revision 1.54
Don't claim there is a data in or out phase if the datalen is 0 (userland
shouldn't claim it either, but a buggy software shouldn't be able to crash
the kernel anyway). Should fix port-sparc64/31925 by Johan A.van Zanten
(which should really be kern/31925).
Analysed and patch tested by Martin Husemann.
 1.46.4.2 03-Nov-2005  riz Pull up following revision(s) (requested by bouyer in ticket #5961):
sys/dev/scsipi/scsipi_ioctl.c: revision 1.54
Don't claim there is a data in or out phase if the datalen is 0 (userland
shouldn't claim it either, but a buggy software shouldn't be able to crash
the kernel anyway). Should fix port-sparc64/31925 by Johan A.van Zanten
(which should really be kern/31925).
Analysed and patch tested by Martin Husemann.
 1.46.4.1 11-Sep-2004  he branches: 1.46.4.1.2; 1.46.4.1.4;
Pull up revision 1.48 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.46.4.1.4.1 03-Nov-2005  riz Pull up following revision(s) (requested by bouyer in ticket #5961):
sys/dev/scsipi/scsipi_ioctl.c: revision 1.54
Don't claim there is a data in or out phase if the datalen is 0 (userland
shouldn't claim it either, but a buggy software shouldn't be able to crash
the kernel anyway). Should fix port-sparc64/31925 by Johan A.van Zanten
(which should really be kern/31925).
Analysed and patch tested by Martin Husemann.
 1.46.4.1.2.1 03-Nov-2005  riz Pull up following revision(s) (requested by bouyer in ticket #5961):
sys/dev/scsipi/scsipi_ioctl.c: revision 1.54
Don't claim there is a data in or out phase if the datalen is 0 (userland
shouldn't claim it either, but a buggy software shouldn't be able to crash
the kernel anyway). Should fix port-sparc64/31925 by Johan A.van Zanten
(which should really be kern/31925).
Analysed and patch tested by Martin Husemann.
 1.46.2.5 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.46.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.46.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.46.2.2 25-Aug-2004  skrll Sync with HEAD.
 1.46.2.1 02-Jul-2003  darrenr Apply the aborted ktrace-lwp changes to a specific branch. This is just for
others to review, I'm concerned that patch fuziness may have resulted in some
errant code being generated but I'll look at that later by comparing the diff
from the base to the branch with the file I attempt to apply to it. This will,
at the very least, put the changes in a better context for others to review
them and attempt to tinker with removing passing of 'struct lwp' through
the kernel.
 1.50.4.1 29-Apr-2005  kent sync with -current
 1.52.8.1 02-Nov-2005  yamt sync with head.
 1.52.6.3 21-Jan-2008  yamt sync with head
 1.52.6.2 03-Sep-2007  yamt sync with head.
 1.52.6.1 21-Jun-2006  yamt sync with head.
 1.52.4.1 03-Nov-2005  tron Pull up following revision(s) (requested by bouyer in ticket #928):
sys/dev/scsipi/scsipi_ioctl.c: revision 1.54
Don't claim there is a data in or out phase if the datalen is 0 (userland
shouldn't claim it either, but a buggy software shouldn't be able to crash
the kernel anyway). Should fix port-sparc64/31925 by Johan A.van Zanten
(which should really be kern/31925).
Analysed and patch tested by Martin Husemann.
 1.57.6.1 22-Apr-2006  simonb Sync with head.
 1.57.4.1 09-Sep-2006  rpaulo sync with head
 1.57.2.1 05-Feb-2006  yamt adapt scsipi.
 1.58.6.3 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.58.6.2 31-Mar-2006  tron Merge 2006-03-31 NetBSD-current into the "peter-altq" branch.
 1.58.6.1 28-Mar-2006  tron Merge 2006-03-28 NetBSD-current into the "peter-altq" branch.
 1.58.4.1 19-Apr-2006  elad sync with head.
 1.58.2.2 24-May-2006  yamt sync with head.
 1.58.2.1 01-Apr-2006  yamt sync with head.
 1.61.14.1 12-Mar-2007  rmind Sync with HEAD.
 1.62.10.1 15-Aug-2007  skrll Sync with HEAD.
 1.62.2.6 09-Oct-2007  ad Sync with head.
 1.62.2.5 20-Aug-2007  ad Sync with HEAD.
 1.62.2.4 19-Aug-2007  ad - Back out the biodone() changes.
- Eliminate B_ERROR (from HEAD).
 1.62.2.3 17-Jun-2007  ad - Increase the number of thread priorities from 128 to 256. How the space
is set up is to be revisited.
- Implement soft interrupts as kernel threads. A generic implementation
is provided, with hooks for fast-path MD code that can run the interrupt
threads over the top of other threads executing in the kernel.
- Split vnode::v_flag into three fields, depending on how the flag is
locked (by the interlock, by the vnode lock, by the file system).
- Miscellaneous locking fixes and improvements.
 1.62.2.2 13-May-2007  ad - Pass the error number and residual count to biodone(), and let it handle
setting error indicators. Prepare to eliminate B_ERROR.
- Add a flag argument to brelse() to be set into the buf's flags, instead
of doing it directly. Typically used to set B_INVAL.
- Add a "struct cpu_info *" argument to kthread_create(), to be used to
create bound threads. Change "bool mpsafe" to "int flags".
- Allow exit of LWPs in the IDL state when (l != curlwp).
- More locking fixes & conversion to the new API.
 1.62.2.1 13-Mar-2007  ad Pull in the initial set of changes for the vmlocking branch.
 1.63.22.2 29-Jul-2007  ad It's not a good idea for device drivers to modify b_flags, as they don't
need to understand the locking around that field. Instead of setting
B_ERROR, set b_error instead. b_error is 'owned' by whoever completes
the I/O request.
 1.63.22.1 29-Jul-2007  ad file scsipi_ioctl.c was added on branch matt-mips64 on 2007-07-29 12:50:24 +0000
 1.63.18.1 02-Jan-2008  bouyer Sync with HEAD
 1.63.14.1 04-Dec-2007  ad Pull the vmlocking changes into a new branch.
 1.63.12.1 18-Feb-2008  mjf Sync with HEAD.
 1.63.6.1 09-Jan-2008  matt sync with HEAD
 1.64.10.2 04-May-2009  yamt sync with head.
 1.64.10.1 16-May-2008  yamt sync with head.
 1.64.8.1 18-May-2008  yamt sync with head.
 1.64.6.2 28-Sep-2008  mjf Sync with HEAD.
 1.64.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.65.6.1 19-Oct-2008  haad Sync with HEAD.
 1.65.4.1 18-Jul-2008  simonb Sync with head.
 1.65.2.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.66.34.1 23-Apr-2012  riz Pull up following revision(s) (requested by bouyer in ticket #192):
sys/dev/scsipi/cd.c: revision 1.307
sys/dev/scsipi/scsiconf.c: revision 1.266
sys/dev/scsipi/sd.c: revision 1.298
sys/dev/scsipi/st_scsi.c: revision 1.35
sys/dev/scsipi/atapiconf.c: revision 1.85
sys/dev/scsipi/scsipiconf.h: revision 1.120
sys/dev/usb/umass_scsipi.c: revision 1.44
sys/dev/scsipi/scsiconf.h: revision 1.57
sys/dev/scsipi/st_atapi.c: revision 1.29
sys/dev/scsipi/scsipi_base.c: revision 1.158
sys/dev/scsipi/st.c: revision 1.221
sys/dev/scsipi/scsipi_ioctl.c: revision 1.67
Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.
This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.
In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.
Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.66.32.1 29-Apr-2012  mrg sync to latest -current.
 1.66.28.1 23-May-2012  yamt sync with head.
 1.67.22.1 31-May-2019  martin Pull up following revision(s) (requested by mlelstv in ticket #1698):

sys/dev/scsipi/scsipi_ioctl.c: revision 1.72 (via patch)

use correct size when copying outgoing sense data.
 1.67.18.1 31-May-2019  martin Pull up following revision(s) (requested by mlelstv in ticket #1698):

sys/dev/scsipi/scsipi_ioctl.c: revision 1.72 (via patch)

use correct size when copying outgoing sense data.
 1.67.16.2 05-Dec-2016  skrll Sync with HEAD
 1.67.16.1 22-Sep-2015  skrll Sync with HEAD
 1.67.14.1 31-May-2019  martin Pull up following revision(s) (requested by mlelstv in ticket #1698):

sys/dev/scsipi/scsipi_ioctl.c: revision 1.72 (via patch)

use correct size when copying outgoing sense data.
 1.67.2.1 03-Dec-2017  jdolecek update from HEAD
 1.68.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.69.16.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.69.16.1 10-Jun-2019  christos Sync with HEAD
 1.69.14.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.69.8.1 31-May-2019  martin Pull up following revision(s) (requested by mlelstv in ticket #1278):

sys/dev/scsipi/scsipi_ioctl.c: revision 1.72 (via patch)

use correct size when copying outgoing sense data.
 1.37 27-Jun-2025  andvar Grammar and spelling fixes, mainly in comments. A few in documentation,
logging, test description, and SCSI ASC/ASCQ assignment descriptions.
 1.36 28-Dec-2023  andvar branches: 1.36.2;
s/informtion/information/ in comments.
 1.35 05-Jun-2021  pgoyette As with usbverbose and pciverbose, these modules are not safe to be
auto-unloaded. Disable for now.

All of these need to be updated with an appropriate refcount mechanism
to ensure that the code and/or tables aren't unloaded while they are
being used.
 1.34 16-Sep-2018  mrg branches: 1.34.16; 1.34.20;
don't print a blank line that ends up in logs and console output.
 1.33 17-Sep-2016  kardel branches: 1.33.8; 1.33.14; 1.33.16;
ASC/ASCQ table updated from http://www.t10.org/lists/asc-num.txt 2016-07-30
 1.32 25-Jul-2010  pgoyette branches: 1.32.18; 1.32.36; 1.32.40;
Move setting of scsi_verbose_loaded flag into the module's init routine.
This ensures that the flag is set even if the module was manually loaded
by the user rather than just auto-loaded.
 1.31 07-Jun-2010  pgoyette Update scsiverbose module to use module_autoload() rather than module_load().
Load the module right before each attempt to use its features, and let the
module subsystem handle unloading.
 1.30 30-May-2010  pgoyette Extract SCSIVERBOSE into a kernel module. The module can be builtin
by defining 'options SCSIVERBOSE' in the kernel config file (no change
from current behavior), or it can be loaded at boot time on those
architectures that support the boot loader's "load" command.

The module is built for all architectures, whether or not SCSI or
atapi support exists.
 1.29 28-Apr-2008  martin branches: 1.29.20; 1.29.22;
Remove clause 3 and 4 from TNF licenses
 1.28 11-Dec-2005  christos branches: 1.28.70; 1.28.72; 1.28.74;
merge ktrace-lwp.
 1.27 29-May-2005  christos - Sprinkle const
- Avoid variable shadowing.
- Eliminate some caddr_t abuse.
 1.26 21-Feb-2005  thorpej Part 1 of a cleanup pass over the SCSI subsystem. The aim is to name
everything "scsi_*", since we really are talking about the SCSI command
set, ATAPI transport not withstanding. Improve the names of many structures,
and prepend "SCSI_" onto all SCSI command opcodes. Place items described
by the SCSI Primary Commands document into scsi_spc.h.
 1.25 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.24 31-Jan-2005  reinoud As part of cleaning up sys/scsipi, replace all u_char by uint8_t and
replace all `short' with int16_t.
 1.23 21-Aug-2004  thorpej branches: 1.23.4; 1.23.6;
Use ANSI function decls and make use of static.
 1.22 23-Apr-2004  itojun use bounded string ops
 1.21 22-Apr-2004  itojun sprintf -> snprintf
 1.20 16-Mar-2004  bouyer cbd -> cdb
Command Block Descriptor -> Command Descriptor Block
Pointed out by Allen Briggs.
 1.19 15-Mar-2004  bouyer Extract the code printing the CBD from scsipi_print_sense(), so that it's
usable in other context.
Use the new scsipi_print_cbd() to dump the command in case of timeout
in siop/esiop.
 1.18 03-Dec-2003  bjh21 Synchronise with <ftp://ftp.t10.org/t10/drafts/spc3/asc-num.txt>:

SCSI ASC/ASCQ Assignments
Numeric Sorted Listing
as of 9/12/03
 1.17 22-Oct-2003  mycroft Add a code that appeared in a PR recently.
 1.16 28-May-2002  drochner branches: 1.16.6;
add translations for two ASC/ASCQs which occur before
the DVD player's region code is dealt with
 1.15 15-Nov-2001  lukem branches: 1.15.8;
don't need <sys/types.h> when including <sys/param.h>
 1.14 13-Nov-2001  lukem add RCSIDs
 1.13 20-Aug-2001  ad Add descriptions for Medium Not Present ASCQs.
 1.12 25-Apr-2001  bouyer branches: 1.12.2;
Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.11 30-Mar-2000  augustss branches: 1.11.6;
Get rid of register declarations.
 1.10 24-Feb-1999  jwise branches: 1.10.8;
Fix typo.

Fixes last part of PR bin/6883 from Soren S. Jorvang <soren@t.dk>.
 1.9 17-Nov-1998  bouyer Rename scsi_interpret_sense() to scsipi_interpret_sense() and move it from
scsi_base.c to scsipi_base.c. Rename the functions from scsi_verbose.c
too, and rename the file itself. Cleaup includes too (scsi_*.h should not
be #included in scsipi_*.h files, which are supposed to be
common to atapi and scsi).
 1.8 15-Aug-1998  mycroft Assign my copyrights to TNF.
 1.7 30-Jul-1998  mjacob opt_scsiverbose.h now is opt_scsi.h and contains the defopted
SCSIVERBOSE and the new ST_ENABLE_EARLYWARN.
 1.6 11-Jul-1998  mjacob Clarify that Sense Key 1 is a "RECOVERED ERROR"- not just an error.
 1.5 10-Jul-1998  mjacob hmm, char sign extension bit me
 1.4 01-Jul-1998  mjacob Align output for Tape specific bits and use new Sense Key defines (for clarity)
 1.3 24-Jun-1998  mjacob When printing an error, print more than just the opcode. Print the
entire CDB, whose length is actually deterministic. This makes a *big*
difference when actually trying to bind errors to commands.
 1.2 15-Apr-1998  mjacob a tab crept in
 1.1 28-Mar-1998  christos Split out the verbose printing of scsi messages, so that user programs
can use the printing routines.
 1.10.8.2 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.10.8.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.11.6.5 20-Jun-2002  nathanw Catch up to -current.
 1.11.6.4 08-Jan-2002  nathanw Catch up to -current.
 1.11.6.3 14-Nov-2001  nathanw Catch up to -current.
 1.11.6.2 24-Aug-2001  nathanw Catch up with -current.
 1.11.6.1 21-Jun-2001  nathanw Catch up to -current.
 1.12.2.3 23-Jun-2002  jdolecek catch up with -current on kqueue branch
 1.12.2.2 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.12.2.1 25-Aug-2001  thorpej Merge Aug 24 -current into the kqueue branch.
 1.15.8.1 30-May-2002  gehenna Catch up with -current.
 1.16.6.6 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.16.6.5 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.16.6.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.16.6.3 18-Sep-2004  skrll Sync with HEAD.
 1.16.6.2 25-Aug-2004  skrll Sync with HEAD.
 1.16.6.1 03-Aug-2004  skrll Sync with HEAD
 1.23.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.23.4.1 29-Apr-2005  kent sync with -current
 1.28.74.2 11-Aug-2010  yamt sync with head.
 1.28.74.1 16-May-2008  yamt sync with head.
 1.28.72.1 18-May-2008  yamt sync with head.
 1.28.70.1 02-Jun-2008  mjf Sync with HEAD.
 1.29.22.2 05-Mar-2011  rmind sync with head
 1.29.22.1 03-Jul-2010  rmind sync with head
 1.29.20.1 17-Aug-2010  uebayasi Sync with HEAD.
 1.32.40.1 04-Nov-2016  pgoyette Sync with HEAD
 1.32.36.1 05-Oct-2016  skrll Sync with HEAD
 1.32.18.1 03-Dec-2017  jdolecek update from HEAD
 1.33.16.1 10-Jun-2019  christos Sync with HEAD
 1.33.14.1 30-Sep-2018  pgoyette Ssync with HEAD
 1.33.8.1 23-Sep-2018  martin Pull up following revision(s) (requested by mrg in ticket #1023):

sys/dev/scsipi/scsipi_verbose.c: revision 1.34

don't print a blank line that ends up in logs and console output.
 1.34.20.1 06-Jun-2021  cjep sync with head
 1.34.16.1 17-Jun-2021  thorpej Sync w/ HEAD.
 1.36.2.1 02-Aug-2025  perseant Sync with HEAD
 1.45 28-Mar-2019  kardel Add reading of supported opcodes and their timeouts
at attachment time. Though this information is optional,
it allows to override our fixed timeouts with device
provided timeouts. These timeouts will override the
hardcoded values if the device provided timeouts
exceed the hardcoded values and are less than a day.

Using the device provided timeouts avoids premature
device resets and unreliable operation due to
inadequate timeouts.

Due to the limited implementations of USB
umass devices this feature is disabled for all
umass attached devices.
 1.44 11-Apr-2017  christos branches: 1.44.12;
Put back module_autoload code; the module_autoload code now checks that
root is mounted.
 1.43 10-Apr-2017  jdolecek just do not autoload scsiverbose module, it causes deadlock if it happens
while root fs is being mounted

adresses second part of PR kern/52147 by Michael van Elst, thank you
 1.42 20-Nov-2016  mlelstv branches: 1.42.2;
Make scsipi framework MPSAFE.

Data structures are now protected by a per-adapter mutex at IPL_BIO
that is created by the scsibus or atapibus instance when the adapter
is configured.
The enable reference counter and the channel freeze counter which are
currently used by HBA code before the adapter is configured, are made
atomic.
The target drivers are now all tagged as D_MPSAFE.

Almost all HBA drivers still require the kernel lock to present,
so all callbacks into HBA code are still protected by kernel lock
unless the driver is tagged as SCSIPI_ADAPT_MPSAFE.

TODO: refactor sd and cd to use dksubr.
 1.41 02-May-2016  christos branches: 1.41.2;
move scsipi_strvis -> libkern:strnvisx()
change the prototype to match userland
fix sizes of strings passed to it
 1.40 21-Aug-2010  pgoyette branches: 1.40.18; 1.40.36;
Update the various xxx_verbose modules to conform to the module subsystem's
new locking protocol.
 1.39 25-Jul-2010  pgoyette Move setting of scsi_verbose_loaded flag into the module's init routine.
This ensures that the flag is set even if the module was manually loaded
by the user rather than just auto-loaded.
 1.38 07-Jun-2010  pgoyette Update scsiverbose module to use module_autoload() rather than module_load().
Load the module right before each attempt to use its features, and let the
module subsystem handle unloading.
 1.37 30-May-2010  pgoyette Extract SCSIVERBOSE into a kernel module. The module can be builtin
by defining 'options SCSIVERBOSE' in the kernel config file (no change
from current behavior), or it can be loaded at boot time on those
architectures that support the boot loader's "load" command.

The module is built for all architectures, whether or not SCSI or
atapi support exists.
 1.36 28-Apr-2008  martin branches: 1.36.20; 1.36.22;
Remove clause 3 and 4 from TNF licenses
 1.35 09-Jul-2007  ad branches: 1.35.28; 1.35.30; 1.35.32;
Merge some of the less invasive changes from the vmlocking branch:

- kthread, callout, devsw API changes
- select()/poll() improvements
- miscellaneous MT safety improvements
 1.34 11-Dec-2005  christos branches: 1.34.30; 1.34.32;
merge ktrace-lwp.
 1.33 30-May-2005  christos branches: 1.33.2;
- remove bogus casts
- add more const
 1.32 29-May-2005  christos - Sprinkle const
- Avoid variable shadowing.
- Eliminate some caddr_t abuse.
 1.31 27-Feb-2005  perry nuke trailing whitespace
 1.30 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.29 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.28 31-Jan-2005  reinoud As part of cleaning up sys/scsipi, replace all u_char by uint8_t and
replace all `short' with int16_t.
 1.27 18-Sep-2004  mycroft branches: 1.27.4; 1.27.6;
Minor rearrangement. Whitespace and #include cleanup.
 1.26 17-Sep-2004  mycroft Remove the "xfer" argument to scsipi_command().
 1.25 17-Sep-2004  mycroft Change the way bustype_cmd is used. Rather than having it be responsible for
calling scsipi_make_xs() and scsipi_execute_xs(), instead push these into
scsipi_command. Make bustype_cmd and PHOLD/PRELE be called from
scsipi_execute_xs(). This allows us to create a xfer structure -- possibly on
the stack -- and call scsipi_execute_xs() directly.
 1.24 09-Sep-2004  bouyer Make the xxstart() functions reentrant again, as some drivers HBA can call
scsipi_done() from their scsipi_request().
For this, add a struct scsipi_xfer * argument to scsipi_command().
If not NULL scsipi_command() will use this to enqueue this xfer, otherwise
it'll try to allocate a new one. This scsipi_xfer has to be allocated
and initialised by scsipi_make_xs() or equivalent.
In xxstart(), allocate a scsipi_xfer using scsipi_make_xs(), and if not NULL,
dequeue the buffer before calling scsipi_command(). This makes sure that
scsipi_command() will not fail, and also makes sure that xxstart() won't
be called again between the BUFQ_PEEK() and BUFQ_GET().

Fix "dequeued wrong buf" panics reported by Juergen Hannken-Illjes in
private mail and Andreas Wrede on current-users@.
Thanks to Jason Thorpe and Chuck Silver for review, and Andreas Wrede for
testing the patch.
 1.23 21-Aug-2004  thorpej Oops, missed one in last.
 1.22 21-Aug-2004  thorpej Const poison scsipi_dtype().
 1.21 21-Aug-2004  thorpej Use ANSI function decls and make use of static.
 1.20 18-Jan-2003  martin branches: 1.20.2; 1.20.4;
Merge botch.
 1.19 09-Nov-2002  thorpej Fix signed/unsigned comparison warnings.
 1.18 04-Oct-2002  soren Slightly simplicy the SCSI device attachment message:
- Use the plain words 'disk' and 'tape' instead of 'direct' and 'sequential'.
- Media status will be printed in the frontend, so don't bother with it.
- Don't bother printing the SCSI version, which is fixed these days, or the
numberic device type.
 1.17 12-Jan-2002  tsutsui Call malloc(9) with M_ZERO flag instead of memset() after malloc().
 1.16 15-Nov-2001  lukem don't need <sys/types.h> when including <sys/param.h>
 1.15 13-Nov-2001  lukem add RCSIDs
 1.14 18-Jul-2001  thorpej bcmp -> memcmp
 1.13 25-Apr-2001  bouyer branches: 1.13.2;
Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.12 31-Dec-2000  ad branches: 1.12.2;
PR 6402: some SCSI devices use \377 as blank, so adjust scsipi_strvis().
 1.11 28-Jun-2000  mrg <vm/vm.h> -> <uvm/uvm_extern.h>
 1.10 09-Jun-2000  enami Prevent a process being swapped out during I/O if the data buffer is
allocated on stack. This potential problem is noticed by Noriyuki Soda
and the idea and sample code to fix is given by Jason R. Thorpe.
 1.9 15-May-2000  dante branches: 1.9.2;
Add few peripheral device type
Remove "???" from T_IT8_1/2. They actually are pre-press devices for graphic arts as described by ASC IT8
Zeros and blanks scsipi_inquiry_data from byte 58 to byte 74 if additional_length is less than 58
 1.8 17-Nov-1998  bouyer branches: 1.8.10;
Rename scsi_interpret_sense() to scsipi_interpret_sense() and move it from
scsi_base.c to scsipi_base.c. Rename the functions from scsi_verbose.c
too, and rename the file itself. Cleaup includes too (scsi_*.h should not
be #included in scsipi_*.h files, which are supposed to be
common to atapi and scsi).
 1.7 17-Aug-1998  mycroft Assign my copyrights to TNF.
 1.6 15-Aug-1998  mycroft Make copyright notices with my name consistent.
 1.5 05-Aug-1998  drochner Improve generation of default disklabels:
-store printable product ID in cd's and sd's softc, use it as "typename"
-for this, add a "destination buffer length" argument to scsipi_strvis()
-return ATAPI device type for ATAPI devices
 1.4 03-Oct-1997  thorpej Clean up and comment the device types, and add the "enclosure services",
"storage array", and "IT8" types.
 1.3 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.2 27-Aug-1997  bouyer branches: 1.2.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.1 01-Jul-1997  bouyer branches: 1.1.2;
file scsipiconf.c was initially added on branch bouyer-scsipi.
 1.1.2.1 01-Jul-1997  bouyer New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
scsibus at aha
sd at scsibus
cd at scsibus
atapibus at wdc
cd at atapibus
cd has bus-specific config and function front-end in scsicd.c and atapicd.c
The call to theses functions from cd.c is conditionned to
NSCSICD and NATAPICD (all defined in cd.h by config).
 1.2.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.1 27-Aug-1997  thorpej file scsipiconf.c was added on branch marc-pcmcia on 1997-08-27 23:33:29 +0000
 1.8.10.4 05-Jan-2001  bouyer Sync with HEAD
 1.8.10.3 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.8.10.2 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.8.10.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.9.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.12.2.10 11-Nov-2002  nathanw Catch up to -current
 1.12.2.9 18-Oct-2002  nathanw Catch up to -current.
 1.12.2.8 12-Jul-2002  nathanw No longer need to pull in lwp.h; proc.h pulls it in for us.
 1.12.2.7 24-Jun-2002  nathanw Curproc->curlwp renaming.

Change uses of "curproc->l_proc" back to "curproc", which is more like the
original use. Bare uses of "curproc" are now "curlwp".

"curproc" is now #defined in proc.h as ((curlwp) ? (curlwp)->l_proc) : NULL)
so that it is always safe to reference curproc (*de*referencing curproc
is another story, but that's always been true).
 1.12.2.6 28-Feb-2002  nathanw Catch up to -current.
 1.12.2.5 08-Jan-2002  nathanw Catch up to -current.
 1.12.2.4 14-Nov-2001  nathanw Catch up to -current.
 1.12.2.3 24-Aug-2001  nathanw Catch up with -current.
 1.12.2.2 21-Jun-2001  nathanw Catch up to -current.
 1.12.2.1 05-Mar-2001  nathanw Initial commit of scheduler activations and lightweight process support.
 1.13.2.3 11-Feb-2002  jdolecek Sync w/ -current.
 1.13.2.2 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.13.2.1 03-Aug-2001  lukem update to -current
 1.20.4.1 11-Sep-2004  he Pull up revision 1.24 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.20.2.5 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.20.2.4 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.20.2.3 21-Sep-2004  skrll Fix the sync with head I botched.
 1.20.2.2 18-Sep-2004  skrll Sync with HEAD.
 1.20.2.1 25-Aug-2004  skrll Sync with HEAD.
 1.27.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.27.4.1 29-Apr-2005  kent sync with -current
 1.33.2.1 03-Sep-2007  yamt sync with head.
 1.34.32.1 11-Jul-2007  mjf Sync with head.
 1.34.30.1 01-Jul-2007  ad Adapt to callout API change.
 1.35.32.3 09-Oct-2010  yamt sync with head
 1.35.32.2 11-Aug-2010  yamt sync with head.
 1.35.32.1 16-May-2008  yamt sync with head.
 1.35.30.1 18-May-2008  yamt sync with head.
 1.35.28.1 02-Jun-2008  mjf Sync with HEAD.
 1.36.22.2 05-Mar-2011  rmind sync with head
 1.36.22.1 03-Jul-2010  rmind sync with head
 1.36.20.2 22-Oct-2010  uebayasi Sync with HEAD (-D20101022).
 1.36.20.1 17-Aug-2010  uebayasi Sync with HEAD.
 1.40.36.2 05-Dec-2016  skrll Sync with HEAD
 1.40.36.1 29-May-2016  skrll Sync with HEAD
 1.40.18.1 03-Dec-2017  jdolecek update from HEAD
 1.41.2.2 26-Apr-2017  pgoyette Sync with HEAD
 1.41.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.42.2.1 21-Apr-2017  bouyer Sync with HEAD
 1.44.12.1 10-Jun-2019  christos Sync with HEAD
 1.133 29-Oct-2024  nat Revert to previous - scsipi_done_once is not necessary.

A follow up commit will contain a simpler change (from riastradh@) to
ncr5380sbc.
 1.132 28-Oct-2024  nat Introduce scsipi_done_once.

This allows for transfers to be sucessfully aborted on the ncr5380sbc(4).

This may be usefull in future for other scsi controllers.

Callers of scsipi_done are not affected by this change.

Part of kern/58452.

As posted to tech-kern:
https://mail-index.netbsd.org/tech-kern/2024/08/02/msg029652.html

Ok thorpej@.
 1.131 22-Jun-2024  palle branches: 1.131.2;
Add quirk for devices that does not handle READ_DISCINFO
 1.130 28-Mar-2019  kardel Add reading of supported opcodes and their timeouts
at attachment time. Though this information is optional,
it allows to override our fixed timeouts with device
provided timeouts. These timeouts will override the
hardcoded values if the device provided timeouts
exceed the hardcoded values and are less than a day.

Using the device provided timeouts avoids premature
device resets and unreliable operation due to
inadequate timeouts.

Due to the limited implementations of USB
umass devices this feature is disabled for all
umass attached devices.
 1.129 07-Oct-2018  christos Handle the SATA to USB external enclosure sold by "Sabrent" and
made by JMicro (vendor=0x152d product=0x0578). This bridge does
not understand FUA, so add a quirk for it.
 1.128 01-Sep-2018  mlelstv Wait in detach if the discovery thread is still running. Avoids crashes
when a device is attached/detached rapidly.
 1.127 04-Jul-2018  kamil Avoid undefined behavior in scsipiconf.h in _4ltol() and _4btol()

Do not shift (through integer promotion) a signed value in an operation
than can change the bit of signedness.

sys/dev/scsipi/scsipiconf.h:808:17, left shift of 255 by 24 places cannot be represented in type 'int'

Detected with Kernel Undefined Behavior Sanitizer.

Reported by <Harry Pantazis>
 1.126 29-Nov-2016  mlelstv branches: 1.126.14; 1.126.16;
reference count adapter mutex possibly shared by multiple channels.

fix error in atapibusdetach, when a child device cannot be detached,
keep atapibus instance alive.
 1.125 25-Nov-2016  christos Move the #endif down to include other kernel only routines.
 1.124 20-Nov-2016  mlelstv Make scsipi framework MPSAFE.

Data structures are now protected by a per-adapter mutex at IPL_BIO
that is created by the scsibus or atapibus instance when the adapter
is configured.
The enable reference counter and the channel freeze counter which are
currently used by HBA code before the adapter is configured, are made
atomic.
The target drivers are now all tagged as D_MPSAFE.

Almost all HBA drivers still require the kernel lock to present,
so all callbacks into HBA code are still protected by kernel lock
unless the driver is tagged as SCSIPI_ADAPT_MPSAFE.

TODO: refactor sd and cd to use dksubr.
 1.123 02-May-2016  christos branches: 1.123.2;
move scsipi_strvis -> libkern:strnvisx()
change the prototype to match userland
fix sizes of strings passed to it
 1.122 13-Jul-2014  dholland branches: 1.122.4;
"peripheral", not "peripherial".
 1.121 20-Apr-2012  bouyer branches: 1.121.2; 1.121.12;
Add a bustype_async_event_xfer_mode() callback to scsipi_bustype (which can
be NULL), so that transport-specific details of transfer mode setting/printing
can be handled more easily.
Move scsipi_async_event_xfer_mode() and scsipi_print_xfer_mode() to
scsi_base.c and split in parallel scsi and FC/SAS parts.
size of struct scsipi_bustype has changed, welcome to 6.99.5
 1.120 19-Apr-2012  bouyer Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.

This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.

In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.

Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.119 06-Apr-2012  christos Add a quirk for the Apple iPod whose mode sense commands fails with not ready.
Seems to work just fine if we send a start command first...
 1.118 07-Jun-2010  pgoyette branches: 1.118.8; 1.118.12; 1.118.14;
Update scsiverbose module to use module_autoload() rather than module_load().
Load the module right before each attempt to use its features, and let the
module subsystem handle unloading.
 1.117 30-May-2010  pgoyette Extract SCSIVERBOSE into a kernel module. The module can be builtin
by defining 'options SCSIVERBOSE' in the kernel config file (no change
from current behavior), or it can be loaded at boot time on those
architectures that support the boot loader's "load" command.

The module is built for all architectures, whether or not SCSI or
atapi support exists.
 1.116 21-Oct-2009  rmind branches: 1.116.2; 1.116.4;
Remove uarea swap-out functionality:

- Addresses the issue described in PR/38828.
- Some simplification in threading and sleepq subsystems.
- Eliminates pmap_collect() and, as a side note, allows pmap optimisations.
- Eliminates XS_CTL_DATA_ONSTACK in scsipi code.
- Avoids few scans on LWP list and thus potentially long holds of proc_lock.
- Cuts ~1.5k lines of code. Reduces amd64 kernel size by ~4k.
- Removes __SWAP_BROKEN cases.

Tested on x86, mips, acorn32 (thanks <mpumford>) and partly tested on
acorn26 (thanks to <bjh21>).

Discussed on <tech-kern>, reviewed by <ad>.
 1.115 13-May-2009  christos sprinkle #ifdef _KERNEL to make scsictl compile.
 1.114 12-May-2009  cegger struct device * -> device_t, no functional changes intended.
 1.113 08-Sep-2008  gmcgarry branches: 1.113.8;
Replace most gcc-specific __attribute__ uses with BSD-style sys/cdef.h
preprocessor macros.
 1.112 11-May-2008  mlelstv branches: 1.112.4;
Reintroduce the NODOORLOCK quirk. Helps with PR kern/23875.
 1.111 28-Apr-2008  martin branches: 1.111.2;
Remove clause 3 and 4 from TNF licenses
 1.110 09-Jul-2007  ad branches: 1.110.28; 1.110.30; 1.110.32;
Merge some of the less invasive changes from the vmlocking branch:

- kthread, callout, devsw API changes
- select()/poll() improvements
- miscellaneous MT safety improvements
 1.109 04-Mar-2007  christos branches: 1.109.2; 1.109.4;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.108 26-Nov-2006  itohy branches: 1.108.4;
If the block size reported by Read Capacity looks valid, just use it.
Use Request Sense only if Read Capacity succeeded and did not return
valid block size.
Discussed on tech-kern.
Fix the easier part of NetBSD PR kern/26537.
(The harder part is the device hangs on large (>= 8KB) transfer.
Possibly umass BBB problem?)

Remove scsipi_size() and scsipi_validate_secsize() from scsipi_base.c
and add their functions to sd.c since they are used only by sd.c.

Use SCSI term `block' instead of `sector' where applicable.
 1.107 16-Nov-2006  christos __unused removal on arguments; approved by core.
 1.106 30-Oct-2006  skrll Remove the boot-from-disk hppa hack now that a real fix has been found.
 1.105 20-Oct-2006  scw Validate the sector size returned by READ CAPACITY. If it looks bogus
print a warning and fallback to a suitable default.

Fixes a problem on hp700 reported by skrll@
 1.104 09-Oct-2006  scw Some removable umass(4) devices don't respond to mode sense page 6, or
simply return zero for logical block size. In either case, use the sector
length reported by READ_CAPACITY instead of defaulting to 512 bytes.

This partially addresses the problems reported in PR port-i386/34707 and
PR kern/34737. Namely the incorrectly reported drive geometry and the
'hanging' issue.

However, since the device in question reports 2048-byte physical sectors
it will remain unusable until DEV_BSIZE is banished.
 1.103 16-Feb-2006  perry branches: 1.103.14; 1.103.16;
Change "inline" back to "__inline" in .h files -- C99 is still too
new, and some apps compile things in C89 mode. C89 keywords stay.

As per core@.
 1.102 24-Dec-2005  perry branches: 1.102.2; 1.102.4; 1.102.6;
Remove leading __ from __(const|inline|signed|volatile) -- it is obsolete.
 1.101 11-Dec-2005  christos merge ktrace-lwp.
 1.100 30-May-2005  christos branches: 1.100.2;
- remove bogus casts
- add more const
 1.99 29-May-2005  christos - Sprinkle const
- Avoid variable shadowing.
- Eliminate some caddr_t abuse.
 1.98 18-May-2005  chs as a stop-gap measure to allow boot-from-disk to work on hppa,
add some padding to the end of struct scsipi_xfer on that platform.
 1.97 27-Feb-2005  perry nuke trailing whitespace
 1.96 21-Feb-2005  thorpej Part 1 of a cleanup pass over the SCSI subsystem. The aim is to name
everything "scsi_*", since we really are talking about the SCSI command
set, ATAPI transport not withstanding. Improve the names of many structures,
and prepend "SCSI_" onto all SCSI command opcodes. Place items described
by the SCSI Primary Commands document into scsi_spc.h.
 1.95 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.94 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.93 31-Jan-2005  reinoud As part of cleaning up sys/scsipi, replace all u_char by uint8_t and
replace all `short' with int16_t.
 1.92 04-Dec-2004  thorpej branches: 1.92.4; 1.92.6;
Use READ/WRITE (16) if the LBA is larger than 32 bits. Another bit
of kern/28514.
 1.91 03-Dec-2004  thorpej And thus spake SBC-3:

If the number of logical blocks exceeds the maximum value that is able to be
specified in the RETURNED LOGICAL BLOCK ADDRESS field, the device server
shall set the RETURNED LOGICAL BLOCK ADDRESS field to FFFFFFFFh. The
application client should then issue a READ CAPACITY (16) command (see 5.11)
to retrieve the READ CAPACITY (16) parameter data.

Implement this in scsipi_size().

First issue in kern/28514.
 1.90 17-Sep-2004  mycroft Remove the "xfer" argument to scsipi_command().
 1.89 17-Sep-2004  mycroft Change the way bustype_cmd is used. Rather than having it be responsible for
calling scsipi_make_xs() and scsipi_execute_xs(), instead push these into
scsipi_command. Make bustype_cmd and PHOLD/PRELE be called from
scsipi_execute_xs(). This allows us to create a xfer structure -- possibly on
the stack -- and call scsipi_execute_xs() directly.
 1.88 17-Sep-2004  mycroft Do not manipulate xs->bp in "generic" code -- do it only in the psw_done
routine. As part of this, pass down our pre-parsed error code -- though this
interface will probably change later to accomodate better error handling.
 1.87 09-Sep-2004  bouyer Make the xxstart() functions reentrant again, as some drivers HBA can call
scsipi_done() from their scsipi_request().
For this, add a struct scsipi_xfer * argument to scsipi_command().
If not NULL scsipi_command() will use this to enqueue this xfer, otherwise
it'll try to allocate a new one. This scsipi_xfer has to be allocated
and initialised by scsipi_make_xs() or equivalent.
In xxstart(), allocate a scsipi_xfer using scsipi_make_xs(), and if not NULL,
dequeue the buffer before calling scsipi_command(). This makes sure that
scsipi_command() will not fail, and also makes sure that xxstart() won't
be called again between the BUFQ_PEEK() and BUFQ_GET().

Fix "dequeued wrong buf" panics reported by Juergen Hannken-Illjes in
private mail and Andreas Wrede on current-users@.
Thanks to Jason Thorpe and Chuck Silver for review, and Andreas Wrede for
testing the patch.
 1.86 21-Aug-2004  thorpej Const poison scsipi_dtype().
 1.85 21-Aug-2004  thorpej - De-__P.
- Use ANSI function decls.
 1.84 16-Mar-2004  bouyer branches: 1.84.2;
cbd -> cdb
Command Block Descriptor -> Command Descriptor Block
Pointed out by Allen Briggs.
 1.83 15-Mar-2004  bouyer Extract the code printing the CBD from scsipi_print_sense(), so that it's
usable in other context.
Use the new scsipi_print_cbd() to dump the command in case of timeout
in siop/esiop.
 1.82 15-Mar-2004  bouyer Indent.
 1.81 22-Feb-2004  mycroft Remove PQUIRK_CDROM. It is definitely not correct -- witness that no other
OS needs such a hack, and the same drive works fine on Suns -- and is much
more likely to be a bug in the host adapter driver (which is corroborated by
the PQUIRK_NOLUNS).
 1.80 18-Sep-2003  mycroft Merge the geometry and cache handling code for all direct access and optical
devices, as it's general to all SCSI MMC devices. In the process, remove
PQUIRK_NO_FLEX_PAGE.
 1.79 17-Sep-2003  mycroft Remove PQUIRK_BYTE5_ZERO.
 1.78 09-Sep-2003  mycroft Exorcise PQUIRK_NODOORLOCK.
 1.77 08-Sep-2003  mycroft Do a START UNIT only if the TEST UNIT READY reports that the device is not
ready. This avoids gratuitously starting the motor on floppy and CD-ROM
drives, and eliminates the need for the audio playing test in cdopen().

Therefore, also remove PQUIRK_NOSTARTUNIT.
 1.76 29-Jun-2003  fvdl branches: 1.76.2;
Back out the lwp/ktrace changes. They contained a lot of colateral damage,
and need to be examined and discussed more.
 1.75 28-Jun-2003  darrenr Pass lwp pointers throughtout the kernel, as required, so that the lwpid can
be inserted into ktrace records. The general change has been to replace
"struct proc *" with "struct lwp *" in various function prototypes, pass
the lwp through and use l_proc to get the process pointer when needed.

Bump the kernel rev up to 1.6V
 1.74 03-May-2003  wiz DMA, not dma nor Dma.
 1.73 19-Apr-2003  fvdl Add a PCAP_NODT quirk. I have two older Quantum Atlas drives that work
fine at U2W, but barf at U160, with different controllers. Unfortunately
sometimes just being initialized at U160 does the trick, so setting them
to lower speed in the BIOS may also be needed.
 1.72 03-Apr-2003  fvdl Check RAW_PART against the media size instead of the disklabel.
Add the media size in 512-byte sectors to the softc, to avoid
some 64 bit computations. Bump the capacity stored in softcs
for disks to 64 bits.
 1.71 19-Oct-2002  mjacob Fixes kern/18709 (correcting some typos in comments)
 1.70 19-Sep-2002  jmc Force the initial probes to happen within the newly forked off kthread.
This eliminates problems where the underlying interrupt handler isn't the
specific layer calling scsipi_complete() for a given scsi transaction.
This avoids deadlocks where the kthread that called the autoconf routines
to configure a scsibus shouldn't be the one put to sleep waiting on a
scsipi_complete (only the scsibus's kthread should be doing that).

To avoid jitter this will force the scsibus's to probe in the order they
run through autoconf (so machines with multiple bus's don't move sd* devices
around on every reboot).
 1.69 16-May-2002  thorpej Don't use a 2-dimensional array for the channel's periph table. Instead,
hash the target and lun together and use a hash table. This will be
necessary in order to support very large (64-bit) LUN number spaces.
 1.68 15-May-2002  bouyer branches: 1.68.2;
Change PQUIRK_CAP_SYNC to set PERIPH_CAP_SYNC aslo for devices that
improperly claim to only support DT clocking.
Add a PQUIRK_CAP_WIDE16 quirk.
Add a quirk for IBM DXHS36D SCSI disk (claims to only support DT).
 1.67 23-Apr-2002  bouyer Report SPC-2 features (DT clocking, quick arbitration and selection,
information unit transfers) though periph_cap.
 1.66 01-Apr-2002  bouyer Add a chan_name to struct scsipi_channel, holding the channel's name.
Set this to dv_xname for scsibus and atapibus.
Set the name of the kernel thread to chan_name instead of controller's
name:channel number (so that we can use this name for controller-specific
threads).
 1.65 02-Dec-2001  bouyer Clean up attach of wd/atapibus:
kill ata_atapi_attach. Change atapibus to use a struct scsipi_channel instead
of ata_atapi_attach as attach arch. Create a ata_device, compatible with
scsipi_channel, to attach wd.
 1.64 19-Nov-2001  tsutsui Add a new quirk value PQUIRK_CAP_SYNC for SCSI1 devices which support
sync transfers, and set PERIPH_CAP_SYNC for them in scsi_probe_device().
As per discussion on tech-kern.
 1.63 18-Nov-2001  tsutsui Fix formatting of scsi_quirk_patterns[] and wrap long lines.
 1.62 14-Oct-2001  bouyer Call ADAPTER_REQ_GROW_RESOURCES from the completion thread, if possible.
This allows HBA drivers to call bus_dmamem_map() safely.
 1.61 14-Oct-2001  bouyer Split channel flags in chan_flags used for communications between
scsipi and HBA, and chan_tflags used for communications between scsipi
and its kernel thread. No functionnal change.
 1.60 27-Sep-2001  mjacob Restore previous definitions.
 1.59 21-Sep-2001  fvdl Unfortunately, the previous change seems to make most (all?) configurations
using the ahc driver hang when probing scsi devices. The problem may be in
the ahc driver and not in this change, but back it out until this has been
fixed anyway.
 1.58 18-Sep-2001  mjacob Rather than run a periph's queue from scsipi_periph_timed_thaw which is
called via a callout, kick the completion thread to run it for us (uses
a new flag, SCSIPI_CHAN_KICK).

If we've received BUSY status and we haven't started the completion
thread yet, don't freeze do a callout to scsipi_periph_timed_thaw which
then will try and kick the completion thread- instead treat the command
as if it were a polled command and just call delay for 1 second.

If DIAGNOSTIC is defined, and the periph qfreeze count is less than
zero, panic because some HBA has corrupted the periph structure's
accounting.
 1.57 02-Sep-2001  tsutsui branches: 1.57.2;
Use be16toh(), be32toh(), le16toh() and le32toh() rather than
ntohs(), ntohl() or homegrown bswap() functions.

XXX Does anyone use drives with PQUIRK_LITTLETOC on big endian machines?
 1.56 31-Aug-2001  augustss Add a new quirk, PQUIRK_NOBIGMODESENSE, for devices that need big
SCSI ops, but still fails on big mode sense.
Makes M-Sys DiskOnKey work.
 1.55 18-Jul-2001  bouyer Adn scsipi_target_detach() and scsipi_thread_call_callback() as discussed
on tech-kern. scsipi_target_detach() accept wildcard target/lun as requested.
 1.54 26-Jun-2001  bouyer branches: 1.54.2;
Add a XS_CTL_SILENT_NODEV flag: if the sense info is "not ready, medium
not present" don't print any message but still return ENODEV.
Use this in cd driver to allow open of character raw partition even if
the drive is empty (older drives fails at LOAD_UNLOAD time, newer
ones fail at TEST_UNIT_READY time).
 1.53 13-Jun-2001  bjh21 Add explicit support for IDE and SCSI adaptors which don't support interrupts.
On such adaptors, all transfers are done in polling mode.

OK'ed by Manuel on tech-kern.
 1.52 21-May-2001  mjacob Remove reference to adapter_q in scsipi structure. Comment about the
usage of channel_q.
 1.51 14-May-2001  bouyer Use SCSI/ATAPI common definition for MODE_{SELECT,SENSE}{,_BIG}. Define
functions to send theses commands in scsipi_base.c and use them instead
of ad-hoc commands setups.
 1.50 02-May-2001  bouyer Use same type for first field of struct ata_atapi_attach and struct
scsipi_channel, so umass has better chances of working on big-endian systems.
 1.49 30-Apr-2001  lukem remove some lint
 1.48 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.47 20-Mar-2001  augustss Extend quirks to 32 bits. Add two new ATAPI quirks, one to
deal with a bad byte5 of the capacity descriptor, one to
handle devices that lack the flex geometry page.
 1.46 22-Sep-2000  ad branches: 1.46.2;
Add a new, optional method to scsipi_adapter (scsipi_accesschk()), and use
it when considering whether to attach devices. This is to facilitate
`non-SCSI' RAID controller drivers that want to provide SCSI pass-through
services to the kernel.
 1.45 06-Jul-2000  mjacob Reserve top nibble of status for use by the HBA. It's up to the
HBA to set or clear it.
 1.44 09-Jun-2000  enami Prevent a process being swapped out during I/O if the data buffer is
allocated on stack. This potential problem is noticed by Noriyuki Soda
and the idea and sample code to fix is given by Jason R. Thorpe.
 1.43 31-May-2000  augustss Add a method, scsipi_getgeom, to the adapter struct. If this method is
present it will be used to get the drive geometry if the MODE SENSE fails.
 1.42 30-May-2000  augustss Add a quirk, SDEV_ONLYBIG, which implies that the device cannot handle
the 6 byte versions of READ, WRITE, and MODE_SENSE.
This greatly simplifies the UFI (USB Floppy) handling.
 1.41 29-May-2000  bouyer ADEV_CDROM -> SDEV_CDROM, for consistency.
 1.40 02-Apr-2000  augustss branches: 1.40.2;
Change the initial field in struct ata_atapi_attach and struct scsipi_link
slightly to allow scsibus and atapibus to attach to the same device.
Furthermore, only attach a scsibus when the bus type is BUS_SCSI.
 1.39 30-Mar-2000  augustss Get rid of register declarations.
 1.38 23-Mar-2000  thorpej New callout mechanism with two major improvements over the old
timeout()/untimeout() API:
- Clients supply callout handle storage, thus eliminating problems of
resource allocation.
- Insertion and removal of callouts is constant time, important as
this facility is used quite a lot in the kernel.

The old timeout()/untimeout() API has been removed from the kernel.
 1.37 20-Jan-2000  mjacob Nobody said no to adding a pointer to original scsi inquiry data
to the scsibus attach args. Make sure it's nulled for ATAPI. Also,
for scsiconf.c, modify SENA's quirk entry.
 1.36 17-Jan-2000  bouyer Use SCSIPIRETRIES instead of hard-coded value '2' for number of retries in
common routines. Define SCSIPIRETRIES as 4, so that the command will finally
succeed after several consecutive downgrades from UDMA2 to PIO4.
 1.35 11-Jan-2000  pk <machine/cpu.h> is not needed here.
 1.34 23-Dec-1999  leo Implement the DIOCKLABEL ioctl. Through this ioctl it it possible keep the
disklabel on the last close or to drop it.
 1.33 20-Oct-1999  enami Cancel active transfers on aic/wdc detach.
Also makes LS-120 drive works for me again.
 1.32 30-Sep-1999  thorpej branches: 1.32.2; 1.32.4; 1.32.6;
Cleanup the scsipi_xfer flags:
- `flags' is now gone, replaced with `xs_control' and `xs_status'.
- Massive cleanup of the control flags. Now we explicitly say that
a job is to complete asynchronously, rather than relying on side-effects,
and use a new flag to now that device discovery is being performed.
- Do SCSI device discovery interrupt-driven.
 1.31 11-Sep-1999  thorpej Implement a function to kill off all commands pending for a given
scsipi_link.
 1.30 08-Apr-1999  bouyer Fix a long-standing bug with the ACAP_* flags:
- the cap field is a u_int8_t, so none of the defined flags would fit in.
Looks like nobody had a drive using 16 bytes commands.
- the ACAP_DRQ_* flags are all wrong. Just remove them and use the definitions
from ata/atareg.h, there's no need to duplicate theses. The effect of this
was that we were always polling for the command phase, even for drives
with interrupt DRQ. This didn't break until the code was changed to support
shared interrupts.

Should fix the lookup problems or 'boot hangs' reported by some users, and
kern/7111.
 1.29 19-Jan-1999  bouyer branches: 1.29.2;
Move test for SDEV_NOSTARTUNIT quirk from sd.c to scsipi_start().
Add a SDEV_NOSTARTUNIT quirk entry for BCD-16X 1997-04-25", "", "VER 2.2"
CD-rom (from Michael Santos).
 1.28 17-Dec-1998  bouyer Add a new quirk flags, "ADEV_NOSENSE", for devices that don't
handle properly the request sense command. Add <FX320S, , q01> as being
sense-unfriendly in the quirk table.
 1.27 08-Dec-1998  thorpej - Remove the "free_list" member from scsipi_xfer. Add "adapter_q" and
"device_q" TAILQ entries. The former is for use by the adapter driver,
as it sees fit. The latter is for the scsipi middle layer to track
pending xfers per device.
- Add a pending_xfers queue to scsipi_link, to track pending xfers per
device.
- Grow scsipi_link's flags to int, and add SDEV_WAITDRAIN, to indicate that
we're waiting for the pending_xfers queue to empty.
 1.26 05-Dec-1998  mjacob Add a max_lun property to match with the max_target property. Shorten
max_target to int16.
 1.25 19-Nov-1998  thorpej Add support for reference counting and enabling/disabling SCSI and
ATAPI controllers. This will eventually be used for power management
of e.g. PCMCIA SCSI and IDE controller cards.
 1.24 17-Nov-1998  bouyer Rename scsi_interpret_sense() to scsipi_interpret_sense() and move it from
scsi_base.c to scsipi_base.c. Rename the functions from scsi_verbose.c
too, and rename the file itself. Cleaup includes too (scsi_*.h should not
be #included in scsipi_*.h files, which are supposed to be
common to atapi and scsi).
 1.23 15-Oct-1998  thorpej Some const poisioning.
 1.22 15-Oct-1998  thorpej Protect kernel prototypes w/ #ifdef _KERNEL.
 1.21 15-Oct-1998  thorpej Forward decl of struct proc.
 1.20 12-Oct-1998  bouyer Merge bouyer-ide
 1.19 10-Oct-1998  thorpej Define an additional host adapter return code, XS_RESET, indicating
that the bus was reset while the command was pending. The middle
layer may choose to retry the command.
 1.18 10-Oct-1998  thorpej Garbage-collect the open_target_lu and close_target_lu entry points from
struct scsipi_adapter; they were not used.

Add a scsipi_ioctl entry point to struct scsipi_adapter. This will be
used to issue ioctl commands to the host adapters.

Inspired by PR #6090, from Matt Jacob.
 1.17 08-Oct-1998  thorpej Add a "NOSYNCCACHE" quirk, as suggested by Matt Jacob in PR #6027.
 1.16 18-Sep-1998  scottr Define a SCSIPI_XFER_ASYNC macro to test whether a request will be
completed asynchronously. Eliminate SCSI_ASYNCREQ, which is actually
redundant information.
 1.15 14-Sep-1998  scottr Fix a problem uncovered when we started to use the pool allocator to manage
scsipi_xfer structures.

When scsipi_execute_xs() calls the driver's scsi_cmd function, it assumes
that it can still dereference a pointer to the scsipi_xfer struct. Since
scsipi_done() has already been called, which in turn has called
scsipi_free_xs(), the struct has already been returned the structure to
the pool! In other words, xs->flags has been compromised, but we are still
testing it.

These changes resolve the problem by doing the following:

- In scsipi_execute_xs(), if the hardware driver's scsi_cmd function
returns SUCCESSFULLY_QUEUED, set a new flag (SCSI_ASYNCREQ) in xs->flags.
Since the request will be handled asynchronously, we will need the
scsipi_xfer struct to be freed in scsipi_done().

If the hardware driver's scsi_cmd function returns COMPLETE, we now
simply return any actual errors, or 0 if none occurred. (Previously,
we may have returned EJUSTRETURN, of which the sole effect was to
avoid freeing the scsipi_xfer struct in our caller.)

- In scsipi_done(), only free the scsipi_xfer struct for async requests.
The contents of the struct will otherwise remain valid until the
function that initiated the transfer frees it.

With this change, responsibility for freeing the struct now lies in two
places, depending on the type of the request:

- For synchronous requests, the routine calling scsipi_execute_xs()
must clean up.

- For asynchronous requests, scsipi_done() cleans up (as it always has).

[Note: this change also corrects a problem with sddump(): scsipi_done()
was attempting to return a static scsipi_xfer struct to the pool! Since
dumps are performed synchronously, we now handle this correctly.]

This solution was provided by Jason Thorpe, after I got him to look at
some related (but insufficient) attempts of my own.
 1.14 08-Sep-1998  mjacob Clarify quirk flags somwhat- split SDEV_NOSYNCWIDE into SDEV_NOSYNC and
SDEV_NOWIDE and add SDEV_NOTAG.
 1.13 17-Aug-1998  mycroft Assign my copyrights to TNF.
 1.12 15-Aug-1998  mycroft Make copyright notices with my name consistent.
 1.11 05-Aug-1998  drochner Improve generation of default disklabels:
-store printable product ID in cd's and sd's softc, use it as "typename"
-for this, add a "destination buffer length" argument to scsipi_strvis()
-return ATAPI device type for ATAPI devices
 1.10 31-Jul-1998  thorpej Use the pool allocator for scsipi_xfer structures.
 1.9 15-Jul-1998  mjacob branches: 1.9.2;
1) Part of fix for kern/3835: add in enumerated return values for target
drivers' sense handlers to return. Coincidentally one of them ends up
being ERESTARTSYS.

2) Add a SCSI_URGENT flag to xs structure- this allows host adapters that
do command tagging to do the right thing wrt a tag.
 1.8 13-Feb-1998  enami branches: 1.8.2;
Fix or add protection for mutiple inclusion.
 1.7 15-Jan-1998  cgd add support for an ATAPI attachment for 'sd'.
fix 'cd' driver's NCD_SCSI bogosity (was using testing wrong macro!)
clean up in various ways:
* make common atapi_mode_{sense,select}() functions.
* put ATAPI data structures in more sensible headers, split up by
device type.
* include headers a bit more carefully.
* pass flags to attachment-specific cd functions, and use them.
* get rid of SCSI bits in scsipi_base.h's scsipi_make_xs(), move
them into the correct place in scsi_base.c.
* fix minor typo in struct name in scsipiconf.h (which was apparently
never used except in a #define later in the same file).
* use __attribute__ to force 4-byte alignment for xs command store,
so that architectures trying to bus_space_write_multi_N() (where
N > 1) that data to a controller won't lose.
* clean up a few comments in typos, and make a few #defines easier to
understand/maintain.
* rename cd_link.h to cdvar.h (via repository copy). This is exactly
what a 'var' file is supposed to be.
 1.6 15-Dec-1997  scottr Fix a typo in a comment.
 1.5 20-Nov-1997  thorpej Declare the scsipi_xfer's "flags" member to be volatile, to force it
to be reloaded every time it is checked. This avoids a condition where
it can be cached in a register in such a way that updates to the flags in
an interrupt handler to not be noticed, which in turn causes the process
doing the i/o to sleep forever. Bug report and suggested fix from
Hiroshi HORIMOTO <horimoto@cs-yuugao.cs.sist.ac.jp>, PR $4460.
 1.4 18-Oct-1997  thorpej branches: 1.4.2;
Implement two macros, scsipi_command() and scsipi_command_direct(), and
use them to hide the structure of the function pointers we jump through
to issue a command.
 1.3 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.2 27-Aug-1997  bouyer branches: 1.2.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.1 01-Jul-1997  bouyer branches: 1.1.2;
file scsipiconf.h was initially added on branch bouyer-scsipi.
 1.1.2.3 17-Jul-1997  bouyer Update from trunck: quirk entries, __attribute__ ((unused)) and
probe message of scsi disks.
 1.1.2.2 01-Jul-1997  thorpej Purely cosmetic formatting changes: some spaces -> tabs, indenting
consistency in prototypes.
 1.1.2.1 01-Jul-1997  bouyer New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
New merged scsi/atapi code. Tested on i386 and sparc. Commiting to a branch
for now, so that it can be tested on other ports too.
The config sheme is as follow:
scsibus at aha
sd at scsibus
cd at scsibus
atapibus at wdc
cd at atapibus
cd has bus-specific config and function front-end in scsicd.c and atapicd.c
The call to theses functions from cd.c is conditionned to
NSCSICD and NATAPICD (all defined in cd.h by config).
 1.2.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.2.2.1 27-Aug-1997  thorpej file scsipiconf.h was added on branch marc-pcmcia on 1997-08-27 23:33:30 +0000
 1.4.2.1 20-Nov-1997  thorpej Pull up from trunk: declare scsipi_xfer's "flags" to be volatile.
 1.8.2.7 12-Oct-1998  bouyer Sync with HEAD.
 1.8.2.6 20-Sep-1998  bouyer sync with HEAD
 1.8.2.5 11-Sep-1998  bouyer Sync with HEAD.
 1.8.2.4 21-Aug-1998  bouyer Sync with -current
 1.8.2.3 13-Aug-1998  bouyer - sync with HEAD
- better error detection and report for wd drives
- better wddump() support
- wdcwait() now takes a timout argument, so that we can honnor the
timeout argument of scsipi cmds.
 1.8.2.2 27-Jul-1998  bouyer Sync with -current
 1.8.2.1 04-Jun-1998  bouyer Commit changes to the IDE system in a branch. This allows a better separation
between higth-level and low-level (i.e. registers read/write) and generalize
the queue for all commands. This also add supports for IDE DMA.
 1.9.2.1 08-Aug-1998  eeh Revert cdevsw mmap routines to return int.
 1.29.2.3 23-Jan-2000  he Pull up revision 1.36 (via patch, requested by bouyer):
Use SCSIPIRETRIES instead of hard-coded value '2' for number of
retries in common routines, and define it as 4 so that ATAPI
command will succeed after several downgrade.
 1.29.2.2 16-Jan-2000  he Apply patch (requested by ad):
When probing a SCSI target, do not report an error if the target
indicates that LUNs are not supported.
 1.29.2.1 08-Apr-1999  bouyer branches: 1.29.2.1.2;
Pull up atapi_wdc.c rev 1.21 and scsipiconf.h rev 1.30:
Fix botch with atapi capability flags, preventing 16 bytes commands to be ever
sent, and interrupt DRQ capable devices to effectively use interrupts for
the CMD_OUT phase. Didn't break until the code was changed to handle shared
interrupts (and I guess nobody has a device that requires 16 bytes commands
yet). Should fix the hangs reported by some users at first access of an ATAPI
device.
 1.29.2.1.2.1 21-Jun-1999  thorpej Sync w/ -current.
 1.32.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.32.4.1 15-Nov-1999  fvdl Sync with -current
 1.32.2.15 23-Apr-2001  ad If SCSIPI_CHAN_NOSETTLE is set in the channel's flags, then don't bother
with SCSI_DELAY.
 1.32.2.14 22-Apr-2001  bouyer Keep track of the number of commands per periph really sent to the adapter.
In scsipi_run_queue(), compare this to periph_openings instead of periph_active
When a drive reject all queued commands with QUEUE FULL (my IBM DDRS34560D
does this on large writes) we end up with all commands in the queue, and
periph_active > periph_openings.
While doing this I found a bug in scsipi_async_event_channel_reset(): calling
scsipi_done() would corrupt accounting.
 1.32.2.13 11-Apr-2001  mjacob Make scsipi_channel_init a function returning an int- non-zero means
it failed to initialize the channel (this should be acceptable)- in
which case we complain and don't schedule bus probing for later.

We make the internal memory allocations for the periph and the chan_periphs
array M_NOWAIT- this way we have a hope of booting instead of silently hanging
during boot if we've run out of memory.
 1.32.2.12 03-Apr-2001  bouyer When we send a untagged command, block the queue until this command is
complete.
 1.32.2.11 27-Mar-2001  bouyer Sync with HEAD.
 1.32.2.10 22-Jan-2001  bouyer Don't forget to remove xfer from the channel's queue in
scsipi_async_event_channel_reset()
thaw the periph when request_sense failed(), and when calling user done,
as we're not going though the usual path.
Implement XS_REQUEUE, for drivers needing to requeue a xfer.
 1.32.2.9 15-Jan-2001  bouyer Handle CHECK CONDITION status in mid-layer:
the REQUEST_SENSE command is generated from scsipi_complete() so it can
tsleep() (we use a regular scsipi_command() call for this). Add a new
periph_flag, PERIPH_SENSE, and a new xs_control, XS_CTL_REQSENSE.
When PERIPH_SENSE is set only xfer with XS_CTL_REQSENSE may be sent
(same logic as PERIPH_RECOVERING but with higther priority). xfer with
XS_CTL_REQSENSE need to have XS_CTL_URGENT urgent too.
XS_CTL_USERCMD xfers are now handled in scsipi_complete().

We need to pay special attention to SCSI resets, as we may have:
- an aborted REQUEST_SENSE. In this case we need to requeue the original
command, not the request sense.
- sense pending for a command no longer in the queue but for which a
request sense has not yet been queued. In this case we should not issue
the request sense but requeue the original command instead.

For this:
- the xfer with the CHECK CONDITION status is stored in the scsipi_periph
(periph_xscheck); the CHECK CONDITION is tested in scsipi_done, PERIPH_SENSE
and periph_xscheck is set here.
- XS_CTL_REQSENSE xfers are not allowed to be requeued, and are terminated
with EINTR in case of reset.
- we have a new async event, "ASYNC_EVENT_RESET", which cleanup the
xfer in periph_xscheck.
- appropriate splbio/splx to avoid race condition (especially,
scsipi_request_sense() runs at splbio. Should not be a real problem as it
doesn't happen often.

While I'm there kill req_sense_length from struct scsipi_xfer, it's always set
to 0 and only checked by a few drivers.
 1.32.2.8 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.32.2.7 04-Feb-2000  thorpej Make sure the channel's completion thread exits when the channel
is detached, and completely encapsulate the way periph's are stored
by the channel.
 1.32.2.6 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.32.2.5 26-Oct-1999  thorpej Deal a little more gracefully with the fact that xfer mode parameters
are for the I_T Nexus, and make all xfer mode updates `async events'.
 1.32.2.4 20-Oct-1999  thorpej Sync w/ trunk.
 1.32.2.3 20-Oct-1999  thorpej Add channel freeze/thaw, and when a timed thaw occurs on either a
channel or periph, make sure to kick the channel queue.
 1.32.2.2 19-Oct-1999  thorpej Add an asynchronous event for the adapter to notify the midlayer that
xfer mode parameters have changed for an I_T Nexus.
 1.32.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.40.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.46.2.11 11-Nov-2002  nathanw Catch up to -current
 1.46.2.10 18-Oct-2002  nathanw Catch up to -current.
 1.46.2.9 20-Jun-2002  nathanw Catch up to -current.
 1.46.2.8 17-Apr-2002  nathanw Catch up to -current.
 1.46.2.7 08-Jan-2002  nathanw Catch up to -current.
 1.46.2.6 22-Oct-2001  nathanw Catch up to -current.
 1.46.2.5 08-Oct-2001  nathanw Catch up to -current.
 1.46.2.4 21-Sep-2001  nathanw Catch up to -current.
 1.46.2.3 24-Aug-2001  nathanw Catch up with -current.
 1.46.2.2 21-Jun-2001  nathanw Catch up to -current.
 1.46.2.1 09-Apr-2001  nathanw Catch up with -current.
 1.54.2.5 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.54.2.4 23-Jun-2002  jdolecek catch up with -current on kqueue branch
 1.54.2.3 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.54.2.2 13-Sep-2001  thorpej Update the kqueue branch to HEAD.
 1.54.2.1 03-Aug-2001  lukem update to -current
 1.57.2.2 01-Oct-2001  fvdl Catch up with -current.
 1.57.2.1 07-Sep-2001  thorpej Commit my "devvp" changes to the thorpej-devvp branch. This
replaces the use of dev_t in most places with a struct vnode *.

This will form the basic infrastructure for real cloning device
support (besides being architecurally cleaner -- it'll be good
to get away from using numbers to represent objects).
 1.68.2.1 30-May-2002  gehenna Catch up with -current.
 1.76.2.8 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.76.2.7 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.76.2.6 18-Dec-2004  skrll Sync with HEAD.
 1.76.2.5 21-Sep-2004  skrll Fix the sync with head I botched.
 1.76.2.4 18-Sep-2004  skrll Sync with HEAD.
 1.76.2.3 25-Aug-2004  skrll Sync with HEAD.
 1.76.2.2 03-Aug-2004  skrll Sync with HEAD
 1.76.2.1 02-Jul-2003  darrenr Apply the aborted ktrace-lwp changes to a specific branch. This is just for
others to review, I'm concerned that patch fuziness may have resulted in some
errant code being generated but I'll look at that later by comparing the diff
from the base to the branch with the file I attempt to apply to it. This will,
at the very least, put the changes in a better context for others to review
them and attempt to tinker with removing passing of 'struct lwp' through
the kernel.
 1.84.2.1 11-Sep-2004  he Pull up revision 1.87 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.92.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.92.4.1 29-Apr-2005  kent sync with -current
 1.100.2.3 03-Sep-2007  yamt sync with head.
 1.100.2.2 30-Dec-2006  yamt sync with head.
 1.100.2.1 21-Jun-2006  yamt sync with head.
 1.102.6.1 22-Apr-2006  simonb Sync with head.
 1.102.4.1 09-Sep-2006  rpaulo sync with head
 1.102.2.1 18-Feb-2006  yamt sync with head.
 1.103.16.2 10-Dec-2006  yamt sync with head.
 1.103.16.1 22-Oct-2006  yamt sync with head
 1.103.14.2 12-Jan-2007  ad Sync with head.
 1.103.14.1 18-Nov-2006  ad Sync with head.
 1.108.4.1 12-Mar-2007  rmind Sync with HEAD.
 1.109.4.1 11-Jul-2007  mjf Sync with head.
 1.109.2.2 01-Jul-2007  ad Adapt to callout API change.
 1.109.2.1 09-Apr-2007  ad - Add two new arguments to kthread_create1: pri_t pri, bool mpsafe.
- Fork kthreads off proc0 as new LWPs, not new processes.
 1.110.32.5 11-Aug-2010  yamt sync with head.
 1.110.32.4 11-Mar-2010  yamt sync with head
 1.110.32.3 16-May-2009  yamt sync with head
 1.110.32.2 04-May-2009  yamt sync with head.
 1.110.32.1 16-May-2008  yamt sync with head.
 1.110.30.1 18-May-2008  yamt sync with head.
 1.110.28.2 28-Sep-2008  mjf Sync with HEAD.
 1.110.28.1 02-Jun-2008  mjf Sync with HEAD.
 1.111.2.2 24-Sep-2008  wrstuden Merge in changes between wrstuden-revivesa-base-2 and
wrstuden-revivesa-base-3.
 1.111.2.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.112.4.1 19-Oct-2008  haad Sync with HEAD.
 1.113.8.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.116.4.1 03-Jul-2010  rmind sync with head
 1.116.2.1 17-Aug-2010  uebayasi Sync with HEAD.
 1.118.14.1 23-Apr-2012  riz Pull up following revision(s) (requested by bouyer in ticket #192):
sys/dev/scsipi/cd.c: revision 1.307
sys/dev/scsipi/scsiconf.c: revision 1.266
sys/dev/scsipi/sd.c: revision 1.298
sys/dev/scsipi/st_scsi.c: revision 1.35
sys/dev/scsipi/atapiconf.c: revision 1.85
sys/dev/scsipi/scsipiconf.h: revision 1.120
sys/dev/usb/umass_scsipi.c: revision 1.44
sys/dev/scsipi/scsiconf.h: revision 1.57
sys/dev/scsipi/st_atapi.c: revision 1.29
sys/dev/scsipi/scsipi_base.c: revision 1.158
sys/dev/scsipi/st.c: revision 1.221
sys/dev/scsipi/scsipi_ioctl.c: revision 1.67
Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.
This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.
In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.
Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.118.12.1 29-Apr-2012  mrg sync to latest -current.
 1.118.8.2 23-May-2012  yamt sync with head.
 1.118.8.1 17-Apr-2012  yamt sync with head
 1.121.12.1 10-Aug-2014  tls Rebase.
 1.121.2.2 03-Dec-2017  jdolecek update from HEAD
 1.121.2.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.122.4.2 05-Dec-2016  skrll Sync with HEAD
 1.122.4.1 29-May-2016  skrll Sync with HEAD
 1.123.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.126.16.1 10-Jun-2019  christos Sync with HEAD
 1.126.14.3 20-Oct-2018  pgoyette Sync with head
 1.126.14.2 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.126.14.1 28-Jul-2018  pgoyette Sync with HEAD
 1.131.2.1 02-Aug-2025  perseant Sync with HEAD
 1.347 11-Oct-2025  mlelstv Make probe command silent.
 1.346 11-Oct-2025  mlelstv For SPC-3 devices try READ_CAPACITY_16 first and fall back to older
READ_CAPACITY_10.

This fixes SCSI drivers like mpt(4) that announce SPC-3 conformity but
don't support READ_CAPACITY_16 in all configurations.
 1.345 13-Apr-2025  jakllsch Add physical sector and alignment info to struct disk_geom and the
geometry plist, and handle in partutil.

Bump version for disk_geom addition.

Collect DIOCGSECTORALIGN handling into one place.
 1.344 12-Apr-2025  mlelstv Use WRITE_16 for dumps when necessary.
 1.343 05-Mar-2025  jakllsch sd(4): print physical sector information
 1.342 02-Mar-2025  riastradh sd(4): Break overlong lines.

No functional change intended.
 1.341 27-Feb-2025  jakllsch sd(4): Translate discard to UNMAP (10) command
 1.340 27-Feb-2025  jakllsch Implement DIOCGSECTORALIGN for sd(4).

Use only READ CAPACITY 16 if peripheral claims conformance to SPC-3.
 1.339 27-Feb-2025  jakllsch whitespace fixes
 1.338 11-Feb-2025  mlelstv Handle unit startup issues also before "media is loaded".
 1.337 28-Sep-2024  mlelstv Don't artificially limit block size to 4096 bytes, use MAXPHYS.
 1.336 24-Feb-2024  mlelstv branches: 1.336.2;
Don't try to discover wedges when the unit isn't online.
 1.335 28-Aug-2022  mlelstv branches: 1.335.4;
Don't fetch data beyond end of inquiry buffer, which, here, is not
NUL-terminated.

Reduce target buffer to needed size (product name + NUL terminator).
 1.334 28-Mar-2022  riastradh sd(4): Use d_cfdriver/devtounit to avoid open/detach races.
 1.333 27-Jan-2022  jakllsch use powerof2() in sd_validate_blksize()
 1.332 30-May-2021  dholland typo in comment
 1.331 10-Feb-2021  christos branches: 1.331.4; 1.331.6;
PR/55986: Ryo Onodera: DK_BUSY must have mask as second argument. Make cd.c
consistent by also using __BIT()
 1.330 25-Sep-2020  jakllsch branches: 1.330.2;
spelling fixes in sd.c comments
 1.329 13-Apr-2020  maxv constify
 1.328 01-Mar-2020  riastradh branches: 1.328.4;
Add a flag to dk_dump for virtual disk devices.

If a disk is backed by a physical medium other than itself, such as
cgd(4), then it passes DK_DUMP_RECURSIVE to disable the recursion
detection for dk_dump.

If, however, a device represents a physical medium on its own, such
as wd(4), then it passes 0 instead.

With this, I can now dump to dk on cgd on dk on wd.
 1.327 19-Mar-2019  mlelstv branches: 1.327.4;
Set disk product name as disk type. The information can be queried through
drvctl(4).
 1.326 07-Oct-2018  christos Handle the SATA to USB external enclosure sold by "Sabrent" and
made by JMicro (vendor=0x152d product=0x0578). This bridge does
not understand FUA, so add a quirk for it.
 1.325 17-Jun-2017  mlelstv branches: 1.325.4; 1.325.6;
The atapibus detach path did hold the channel mutex while calling into autoconf,
which would trigger a panic when unplugging a USB ATAPI CDROM.

Align detach code for scsibus and atapibus to fix this.

Also avoid races when detaching devices by replacing callout_stop with
callout_halt.
 1.324 10-Apr-2017  jdolecek branches: 1.324.4; 1.324.6;
execute the cache page MODE SENSE with XS_CTL_SILENT; it's pretty normal
for e.g. USB sticks thus showing error is not really useful, and the pretty
printing triggers autoload of scsiverbose module and immediate deadlock when
the DIOCGCACHE call is made by WAPBL during root mount

adresses PR kern/52147 by Michael van Elst
 1.323 05-Apr-2017  jdolecek expose disk device FUA/DPO support via DIOCGCACHE, and allow the flags
to be set for I/O; implement support in sd(4) and nvme(4)

discussed on tech-kern
 1.322 21-Dec-2016  mlelstv branches: 1.322.2;
really keep request queued after resource shortage
 1.321 16-Dec-2016  mlelstv add comment about "missing" dk_start.
 1.320 10-Dec-2016  mlelstv Refactored sd and cd to use common disk subroutines.
 1.319 20-Nov-2016  mlelstv Make scsipi framework MPSAFE.

Data structures are now protected by a per-adapter mutex at IPL_BIO
that is created by the scsibus or atapibus instance when the adapter
is configured.
The enable reference counter and the channel freeze counter which are
currently used by HBA code before the adapter is configured, are made
atomic.
The target drivers are now all tagged as D_MPSAFE.

Almost all HBA drivers still require the kernel lock to present,
so all callbacks into HBA code are still protected by kernel lock
unless the driver is tagged as SCSIPI_ADAPT_MPSAFE.

TODO: refactor sd and cd to use dksubr.
 1.318 20-Nov-2016  pgoyette Avoid calling bufq_free() from critical code sections.
 1.317 24-Aug-2015  pooka branches: 1.317.2;
would you like some freshly ground _KERNEL_OPT with that?
yes? excellent choice, sir/madam.
 1.316 26-Apr-2015  mlelstv Use C99-style initializers for struct dkdriver.
 1.315 13-Apr-2015  riastradh Convert sys/dev to use <sys/rndsource.h>.
 1.314 02-Jan-2015  christos We have three sets of DTYPE_ constants in the kernel:
altq Drop Type
disklabel Disk Type
file Descriptor Type
(not to mention constants that contain the string DTYPE).
Let's make them two, by changing the disklabel one to be DisK TYPE since the
other disklabel constants seem to do that. Not many userland programs use
these constants (and the ones that they do are mostly in ifdefs). They will
be fixed shortly.
 1.313 31-Dec-2014  christos make more drivers use disk_ioctl, and add a dev parameter to it so that
we can merge the "easy" disklabel ioctls to it. Ultimately all this will
go do dk_ioctl once all the drivers have been converted.
 1.312 31-Dec-2014  christos Centralize wedge ioctls in disk_ioctl.
 1.311 31-Dec-2014  mlelstv disk_blocksize and disk_set_info relay the same information
to the disk subsystem.

Make disk_set_info also set blocksize shift values.
Remove every call to disk_blocksize.

Keep disk_blocksize for ABI compatibility, make it also set dg_secsize.
 1.310 04-Nov-2014  mlelstv branches: 1.310.2;
support DIOCMWEDGES ioctl.
 1.309 05-Sep-2014  matt Don't use new as a variable name.
 1.308 10-Aug-2014  tls branches: 1.308.2;
Merge tls-earlyentropy branch into HEAD.
 1.307 25-Jul-2014  dholland Add d_discard to all struct cdevsw instances I could find.

All have been set to "nodiscard"; some should get a real implementation.
 1.306 25-Jul-2014  dholland Add d_discard to all struct bdevsw instances I could find.

I've set them all to nodiscard. Some of them (wd, dk, vnd, ld,
raidframe, maybe cgd) should be implemented for real.
 1.305 16-Mar-2014  dholland branches: 1.305.2;
Change (mostly mechanically) every cdevsw/bdevsw I can find to use
designated initializers.

I have not built every extant kernel so I have probably broken at
least one build; however I've also found and fixed some wrong
cdevsw/bdevsw entries so even if so I think we come out ahead.
 1.304 25-Oct-2013  martin Mark a diagnostic-only variable
 1.303 28-Sep-2013  skrll Remove unnecessary initialisation of error
 1.302 26-Sep-2013  kiyohara Reset variable error to 0 before handle each ioctl cmd. Or returns EPASSTHROUGH
when some cmds. (e.g. ODIOCGDINFO)
 1.301 13-Jun-2013  tls branches: 1.301.2;
Convert the entropy pool framework from pseudo-callout-driven to
soft interrupt driven operation.

Add a polling mode of operation -- now we can ask hardware random number
generators to top us up just when we need it (bcm2835_rng and amdpm
converted as examples).

Fix a stall noticed with repeated reads from /dev/random while testing.
 1.300 29-May-2013  christos phase 1 of disk geometry cleanup:
- centralize the geometry -> plist code so that we don't have
n useless copies of it.
 1.299 16-Apr-2013  jakllsch Clamp sectors per unit in the default disklabel to the maximum value,
instead of allowing it to wrap, as is already done in wd(4).
 1.298 19-Apr-2012  bouyer branches: 1.298.2;
Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.

This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.

In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.

Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.297 06-Apr-2012  christos Add a quirk for the Apple iPod whose mode sense commands fails with not ready.
Seems to work just fine if we send a start command first...
 1.296 02-Feb-2012  tls branches: 1.296.2;
Entropy-pool implementation move and cleanup.

1) Move core entropy-pool code and source/sink/sample management code
to sys/kern from sys/dev.

2) Remove use of NRND as test for presence of entropy-pool code throughout
source tree.

3) Remove use of RND_ENABLED in device drivers as microoptimization to
avoid expensive operations on disabled entropy sources; make the
rnd_add calls do this directly so all callers benefit.

4) Fix bug in recent rnd_add_data()/rnd_add_uint32() changes that might
have lead to slight entropy overestimation for some sources.

5) Add new source types for environmental sensors, power sensors, VM
system events, and skew between clocks, with a sample implementation
for each.

ok releng to go in before the branch due to the difficulty of later
pullup (widespread #ifdef removal and moved files). Tested with release
builds on amd64 and evbarm and live testing on amd64.
 1.295 25-Nov-2011  joerg branches: 1.295.2;
Replace memset with incorrect size argument with M_ZERO.
 1.294 30-Jul-2011  jmcneill branches: 1.294.2;
Add an FSILENT flag and use it to suppress "Medium Not Present" scsipi
spam when trying to access offline drives at boot.
 1.293 14-Apr-2010  jakllsch Implement DIOC[GS]STRATEGY for sd(4).
 1.292 24-Feb-2010  dyoung branches: 1.292.2;
A pointer typedef entails trading too much flexibility to declare const
and non-const types, and the kernel uses both const and non-const
PMF qualifiers and device suspensors, so change the pmf_qual_t and
device_suspensor_t typedefs from "pointers to const" to non-pointer,
non-const types.
 1.291 08-Jan-2010  dyoung branches: 1.291.2;
Expand PMF_FN_* macros.
 1.290 21-Oct-2009  rmind Remove uarea swap-out functionality:

- Addresses the issue described in PR/38828.
- Some simplification in threading and sleepq subsystems.
- Eliminates pmap_collect() and, as a side note, allows pmap optimisations.
- Eliminates XS_CTL_DATA_ONSTACK in scsipi code.
- Avoids few scans on LWP list and thus potentially long holds of proc_lock.
- Cuts ~1.5k lines of code. Reduces amd64 kernel size by ~4k.
- Removes __SWAP_BROKEN cases.

Tested on x86, mips, acorn32 (thanks <mpumford>) and partly tested on
acorn26 (thanks to <bjh21>).

Discussed on <tech-kern>, reviewed by <ad>.
 1.289 03-Aug-2009  jnemeth Fix previous where the call of sd_set_properties was moved from
the end of sdattach() to the end of sd_get_parms(). The code at
the bottom of sd_get_parms() was only used for optical drives or
in the case where the drive geometry couldn't be retrived for some
reason, to create a fake geometry. The case for setting up the
real geometry was handled above that code.
 1.288 21-Jun-2009  jakllsch Move call of sd_set_properties() to end of sd_get_parms(), rather than
sdattach(). This allows DIOCGDISKINFO to do the right thing even when
the media has changed. Note that drvctl -p will only DTRT if disk has
been opened since the most recent media chenged.
 1.287 05-Jun-2009  haad Add support for DIOCGDISKINFO to disk like device drivers. Change
partutil.c::getdiskinfo to use it to get disk geometry info.
Use DIOCGWEDGEINFO ioctl to get information about partition size, if disk
driver doesn't support it use old DIOCGDINFO. This patch adds support for
wedge like devices(lvm logical volumes, ZFS zvol partitions) to newfs and
other tools.

No objections on tech-userlevel@.
 1.286 20-May-2009  dyoung On second thought, let's call disk_predetach() disk_begindetach().
Verbs are good.
 1.285 19-May-2009  dyoung Use disk_predetach().
 1.284 19-May-2009  dyoung Delete wdactivate() and sdactivate(). They were no-ops, but as a
side-effect of registering them, config_detach(9) cleared DVF_ACTIVE
before it called wddetach() or sddetach(). Even though sd(4)'s
detachment may have subsequently failed with EBUSY, we could not
begin new disk I/O on sd(4) because the device had been deactivated.

By analogy to sdstrategy(), test device_is_active() in wdstrategy()
and if it is false set b_errno to EIO instead of initiating new
disk I/O.

XXX We should decline more politely to start new I/O, since
XXX !device_is_active() may mean simply that the device is suspended.
XXX I suppose that EIO is safe as long as system suspension is
XXX all-or-nothing.
 1.283 17-May-2009  dyoung Reverse the sense of the DETACH_FORCE check so that we return EBUSY in
the correct circumstances.
 1.282 16-May-2009  dyoung Do not detach a sd(4) instance if it is open, but return EBUSY,
instead. If the detach is forced, do what sdclose() does on the
last close: flush the cache, delete our reference to the SCSI bus
device, et cetera.
 1.281 12-May-2009  cegger struct device * -> device_t, no functional changes intended.
 1.280 12-May-2009  cegger struct cfdata * -> cfdata_t, no functional changes intended.
 1.279 10-Apr-2009  dyoung Use a PMF shutdown hook instead of shutdownhook_establish(). This ought
to stop the crash during shutdown that yamt@ reports in kern/41182.
 1.278 07-Apr-2009  dyoung Detach atapibus(4), scsibus(4), cd(4), and sd(4) during shutdown.
Destroy sd->sc_callout in sddetach(). Delete some dead code in
cddetach().
 1.277 13-Jan-2009  yamt branches: 1.277.2;
g/c BUFQ_FOO() macros and use bufq_foo() directly.
 1.276 11-Jan-2009  cegger make this compile
 1.275 16-Jul-2008  drochner branches: 1.275.2; 1.275.4;
split device/softc for sd (tested with a USB stick)
 1.274 08-Jun-2008  tsutsui branches: 1.274.2; 1.274.4;
Use device_lookup_private() rather than using cd_devs[] directly to get softc.

XXX maybe we should change a type of cd_devs[] in struct cfdriver
from (void *) to device_t.
 1.273 28-Apr-2008  martin branches: 1.273.2;
Remove clause 3 and 4 from TNF licenses
 1.272 05-Apr-2008  cegger branches: 1.272.2; 1.272.4;
use aprint_*_dev and device_xname
 1.271 29-Feb-2008  dyoung Use PMF_FN_ARGS, PMF_FN_PROTO.
 1.270 21-Feb-2008  drochner put back the old shutdown handlers to flush the disk cache,
the pmf API can't deal with all the different suspend/resume/reboot
cases well yet, so better keep suspend/resume and reboot/halt/poweroff
clearly seperated
 1.269 25-Dec-2007  perry branches: 1.269.2; 1.269.6;
Convert many of the uses of __attribute__ to equivalent
__packed, __unused and __dead macros from cdefs.h
 1.268 09-Dec-2007  jmcneill branches: 1.268.2;
Merge jmcneill-pm branch.
 1.267 08-Oct-2007  ad branches: 1.267.4; 1.267.6; 1.267.8;
Merge disk init changes from the vmlocking branch. These seperate init /
destroy of 'struct disk' from attach / detach.
 1.266 01-Oct-2007  bouyer Properly initialize xs_callout in sddump(). We're using a static scsipi_xfer
here which didn't get through scsipi_get_xs().
Pointed out by Greg Oster.
 1.265 03-Aug-2007  tsutsui branches: 1.265.2; 1.265.4; 1.265.6; 1.265.8;
Don't allocate data buffer on stack in sd_read_capacity().

If data buffer is allocated on stack at the end of CPU cacheline,
the same cacheline region might be accessed via kernel stack even
after bus_dmamap_sync(9) against the data buffer in lower scsi drivers
so that read data via DMA could be lost.

Fixes occasional readdisklabel(9) failure at boot on ews4800mips
with osiop(4) driver and 128byte cacheline.

XXX: Other functions/drivers might have the similar problem.
 1.264 29-Jul-2007  ad It's not a good idea for device drivers to modify b_flags, as they don't
need to understand the locking around that field. Instead of setting
B_ERROR, set b_error instead. b_error is 'owned' by whoever completes
the I/O request.
 1.263 21-Jul-2007  ad Replace some uses of lockmgr().
 1.262 09-Jul-2007  ad branches: 1.262.2;
Merge some of the less invasive changes from the vmlocking branch:

- kthread, callout, devsw API changes
- select()/poll() improvements
- miscellaneous MT safety improvements
 1.261 04-Mar-2007  christos branches: 1.261.2; 1.261.4;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.260 03-Mar-2007  jnemeth add support devctl properties
 1.259 21-Feb-2007  thorpej Replace the Mach-derived boolean_t type with the C99 bool type. A
future commit will replace use of TRUE and FALSE with true and false.
 1.258 30-Nov-2006  christos branches: 1.258.2; 1.258.4; 1.258.6;
Prevent kernel crashes caused by umass devices that return bogus mode sense
data, such as the "Cooler Master". From Rhialto. While there, clean up some
debugging code and make the offset unsigned.
 1.257 29-Nov-2006  drochner restore a nice little feature which got lost in some previous commit:
the d_typename field of the default disklabel is initialized with
text from the SCSI inquiry, so that one gets at least a vague idea which
disk he is dealing with
 1.256 26-Nov-2006  itohy If the block size reported by Read Capacity looks valid, just use it.
Use Request Sense only if Read Capacity succeeded and did not return
valid block size.
Discussed on tech-kern.
Fix the easier part of NetBSD PR kern/26537.
(The harder part is the device hangs on large (>= 8KB) transfer.
Possibly umass BBB problem?)

Remove scsipi_size() and scsipi_validate_secsize() from scsipi_base.c
and add their functions to sd.c since they are used only by sd.c.

Use SCSI term `block' instead of `sector' where applicable.
 1.255 25-Nov-2006  scw - Call disk_blocksize(9) once we know the disk's physical block size.
- bounds_check_with_label() has been fixed, so no need to fake the
raw partition's size in terms of DEV_BSIZE units.
 1.254 16-Nov-2006  christos __unused removal on arguments; approved by core.
 1.253 20-Oct-2006  scw Validate the sector size returned by READ CAPACITY. If it looks bogus
print a warning and fallback to a suitable default.

Fixes a problem on hp700 reported by skrll@
 1.252 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.251 09-Oct-2006  scw Some removable umass(4) devices don't respond to mode sense page 6, or
simply return zero for logical block size. In either case, use the sector
length reported by READ_CAPACITY instead of defaulting to 512 bytes.

This partially addresses the problems reported in PR port-i386/34707 and
PR kern/34737. Namely the incorrectly reported drive geometry and the
'hanging' issue.

However, since the device in question reports 2048-byte physical sectors
it will remain unusable until DEV_BSIZE is banished.
 1.250 14-Sep-2006  reinoud branches: 1.250.2;
Fix same panic problem as with cd.c when a scsi error happends on
read/write action. It would panic on a diagnosic assertion. When a scsi
read/write command fails, the whole extent is gone bad so mark all as not
done.
 1.249 09-Jul-2006  drochner branches: 1.249.2; 1.249.4;
some fixes for removable media (USB floppies in particular):
-allow to open RAW_PART, even if readdisklabel() doesn't like what it sees
-fix PERIPH_MEDIA_LOADED toggling on every other open attempt
-Don't reset PERIPH_REMOVABLE if prevent/allow fails - this is normal
for floppy drives. The flag is required for media change detection.
Honour XS_CTL_SILENT for the "no door lock" printouts, and set that flag
where scsipi_prevent() is called during open/close.
Tested with a USB floppy and a USB cardreader.
 1.248 01-Jun-2006  nathanw branches: 1.248.2;
Change return type of sdgetdisklabel() from void to int; return an error
if readdisklabel() returned an error.

sdopen(): Return an error if sdgetdisklabel() returns an error. This prevents
a crash in spec_open() (and possibly elsewhere) if a sd device is detached in
the middle of sdopen().
 1.247 30-Mar-2006  thorpej branches: 1.247.2;
Use device_private().
 1.246 28-Mar-2006  thorpej Use device_unit().
 1.245 20-Feb-2006  thorpej branches: 1.245.2; 1.245.4; 1.245.6;
Use device_is_active() rather than testing dv_flags for DVF_ACTIVE
directly.
 1.244 11-Dec-2005  christos branches: 1.244.2; 1.244.4; 1.244.6;
merge ktrace-lwp.
 1.243 09-Dec-2005  bouyer Adapt previous change to recent scsipi. Pointed out by Simon Burge.
 1.242 08-Dec-2005  bouyer Some USB devices reports themselves as removable, but have no door and
so don't support the SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL command.
When an "Illegal field in CDB" is reported for this command, mark the
device as non-removable (which is always true for USB keys from the SCSI
point of view), print a message and ignore the error.
For DIOCLOCK, return ENOTTY if the device is not removable instead of
trying a command which will fail.

Fix a problem reported by Hubert Feyrer for some USB umass devices, patch
tested by him.
 1.241 15-Oct-2005  yamt - change the way to specify a bufq strategy. (by string rather than by number)
- rather than embedding bufq_state in driver softc,
have a pointer to the former.
- move bufq related functions from kern/subr_disk.c to kern/subr_bufq.c.
- rename method to strategy for consistency.
- move some definitions which don't need to be exposed to the rest of kernel
from sys/bufq.h to sys/bufq_impl.h.
(is it better to move it to kern/ or somewhere?)
- fix some obvious breakage in dev/qbus/ts.c. (not tested)
 1.240 29-May-2005  christos branches: 1.240.2;
- Sprinkle const
- Avoid variable shadowing.
- Eliminate some caddr_t abuse.
 1.239 25-Apr-2005  drochner Revert the part if 1.236 which comprises a functional change.
While I didn't observe a misbehaviour yet, this _must_ be
wrong because SSD_RCODE_VALID is used incorrectly.
 1.238 31-Mar-2005  yamt introduce a function to drain bufq and use it where appropriate.
 1.237 27-Feb-2005  perry branches: 1.237.2;
nuke trailing whitespace
 1.236 21-Feb-2005  thorpej Part 1 of a cleanup pass over the SCSI subsystem. The aim is to name
everything "scsi_*", since we really are talking about the SCSI command
set, ATAPI transport not withstanding. Improve the names of many structures,
and prepend "SCSI_" onto all SCSI command opcodes. Place items described
by the SCSI Primary Commands document into scsi_spc.h.
 1.235 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.234 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.233 31-Jan-2005  reinoud As part of cleaning up sys/scsipi, replace all u_char by uint8_t and
replace all `short' with int16_t.
 1.232 07-Dec-2004  thorpej branches: 1.232.2; 1.232.4;
SYNCHRONIZE_CACHE -> SYNCHRONIZE_CACHE_10
 1.231 07-Dec-2004  thorpej USe more appropriate macro/struct names for READ/WRITE (6) and
READ/WRITE (10).
 1.230 04-Dec-2004  thorpej Use READ/WRITE (16) if the LBA is larger than 32 bits. Another bit
of kern/28514.
 1.229 28-Oct-2004  yamt move buffer queue related stuffs from buf.h to their own header, bufq.h.
 1.228 26-Sep-2004  dogcow Fix debug message output args to match yamt's src/sys/sys/buf.h changes.
 1.227 25-Sep-2004  thorpej Add support for wedges to the SCSI disk driver.
 1.226 17-Sep-2004  mycroft Remove the "xfer" argument to scsipi_command().
 1.225 17-Sep-2004  mycroft In places where we've already called scsipi_make_xs(), call scsipi_execute_xs()
directly rather than going through scsipi_command().
 1.224 17-Sep-2004  mycroft Do not manipulate xs->bp in "generic" code -- do it only in the psw_done
routine. As part of this, pass down our pre-parsed error code -- though this
interface will probably change later to accomodate better error handling.
 1.223 09-Sep-2004  bouyer Make the xxstart() functions reentrant again, as some drivers HBA can call
scsipi_done() from their scsipi_request().
For this, add a struct scsipi_xfer * argument to scsipi_command().
If not NULL scsipi_command() will use this to enqueue this xfer, otherwise
it'll try to allocate a new one. This scsipi_xfer has to be allocated
and initialised by scsipi_make_xs() or equivalent.
In xxstart(), allocate a scsipi_xfer using scsipi_make_xs(), and if not NULL,
dequeue the buffer before calling scsipi_command(). This makes sure that
scsipi_command() will not fail, and also makes sure that xxstart() won't
be called again between the BUFQ_PEEK() and BUFQ_GET().

Fix "dequeued wrong buf" panics reported by Juergen Hannken-Illjes in
private mail and Andreas Wrede on current-users@.
Thanks to Jason Thorpe and Chuck Silver for review, and Andreas Wrede for
testing the patch.
 1.222 06-Sep-2004  bouyer Fix comment: xxstart() can also be called from xxrestart()
 1.221 27-Aug-2004  bouyer Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
Havard Eidnes's analysis of this problem is that the scsipi_xfer pool is
competing for resources with other pools, including the the inode and vnode
pools which can grow quite large.

*_scsipi_cmd(): don't biodone the buffer if scsipi_make_xs() fails, let the
caller deal with the problem
start function of block devices drivers: dequeue the buffer after the
scsipi_command() call. If scsipi_command() fails with ENOMEM don't dequeue
the buffer, and schedule a callout to call the start function after
some delay.
scsipi_init(): prime the scsipi_xfer_pool with one page. This ensure that
there is always some scsipi_xfer to play with. If scsipi_command() fails
because of pool_get(), we're sure there will be resources available later,
when the pending commands have completed.

Reviewed by Jason Thorpe and Havard Eidnes.
Todo: remove the "unable to allocate scsipi_xfer" and "not queued, error %d"
printfs, but I choose to keep them for now, to help make sure the code does
what it should.
 1.220 21-Aug-2004  thorpej Use ANSI function decls and make use of static.
 1.219 28-Jun-2004  martin Add prototypes for the new functions added in previous commit.
 1.218 28-Jun-2004  mycroft Separate page 4/5 reading into separate functions. For "removable" devices,
try page 5 (flex geometry) first. This fixes problems with some broken USB
devices that do not handle page 4 correctly (rather than returning nothing,
or stalling, they return the CSW where there should be data).
 1.217 21-May-2004  bouyer If READ_FORMAT_CAPACITIES fails with "Medium Format Corrupted" ASC/ASQ error,
handle it as if the drive was not formatted, so that "scsictl sdx format"
can open the raw device.
 1.216 14-Mar-2004  thorpej branches: 1.216.2;
Use scsipi_adapter_minphys().
 1.215 28-Feb-2004  yamt change the way to handle NEW_BUFQ_STRATEGY option.
instead of putting #ifdefs into each drivers,
use a global variable to indicate default strategy.

XXX should have a way to specify other strategies.
 1.214 23-Dec-2003  pk Some older devices do not understand the `disable block descriptors' bit in
the mode sense request. So fall back on mode sense data including a block
descriptor section.

XXX this applies to the `page 4' case. The `page 5' didn't consider the
possible presence of a block descriptor at all, though it did allow the
device to return one. While that's fixed now, the `page 5' mode sense
does not use the `disable block descriptors' bit.

I'm not sure we should bother with this at all..
 1.213 29-Oct-2003  mycroft Back out the bogus initializer -- the compiler bug is fixed.
 1.212 28-Oct-2003  cl note 'm68k {u,}int64_t used uninitialized' bug.
add reference to gcc bug report.
mark all (known) occurrences.
 1.211 27-Oct-2003  fredb Appease gcc3 -Wno-uninitialized (even though blkno is clearly assigned in
each branch of a conditional).
 1.210 18-Sep-2003  mycroft Whoops, make sure 6-byte commands work again.
 1.209 18-Sep-2003  mycroft Merge the geometry and cache handling code for all direct access and optical
devices, as it's general to all SCSI MMC devices. In the process, remove
PQUIRK_NO_FLEX_PAGE.
 1.208 13-Sep-2003  mycroft Don't be silent if there is no media present.
 1.207 13-Sep-2003  mycroft Make sure the "raw partition" can always be opened again.
 1.206 09-Sep-2003  mycroft In the test for whether to start the unit, I used the wrong error code.
ENODEV is only returned when we get "medium not present," which we can fail
immediately on. All other "not ready" cases return EIO.
 1.205 08-Sep-2003  mycroft Fix an error path where we might scsipi_prevent() on a non-removable device.
 1.204 08-Sep-2003  mycroft Do a START UNIT only if the TEST UNIT READY reports that the device is not
ready. This avoids gratuitously starting the motor on floppy and CD-ROM
drives, and eliminates the need for the audio playing test in cdopen().

Therefore, also remove PQUIRK_NOSTARTUNIT.
 1.203 07-Sep-2003  mycroft Set PQUIRK_ONLYBIG in the wdc-atapi frontend, obviating the need to ever test
the "bus type" for this.

Merge all the code in the SCSI and ATAPI backends for "cd" devices. All of
the mode page handling and whatnot is general to SCSI MMC devices, and should
never have been separated to begin with. This fixes a variety of problems,
and adds load/unload support for SCSI-attached devices.
 1.202 29-Jun-2003  fvdl branches: 1.202.2;
Back out the lwp/ktrace changes. They contained a lot of colateral damage,
and need to be examined and discussed more.
 1.201 28-Jun-2003  darrenr Pass lwp pointers throughtout the kernel, as required, so that the lwpid can
be inserted into ktrace records. The general change has been to replace
"struct proc *" with "struct lwp *" in various function prototypes, pass
the lwp through and use l_proc to get the process pointer when needed.

Bump the kernel rev up to 1.6V
 1.200 13-May-2003  thorpej Use aprint*().
 1.199 10-May-2003  thorpej Change bounds_check_with_label() to take a pointer to the disk structure,
rather than the label itself. This paves the way for some future changes.
 1.198 02-May-2003  dsl Change return type of readdisklabel() to const char *
I hope I've found all the correct places!
 1.197 03-Apr-2003  fvdl Check RAW_PART against the media size instead of the disklabel.
Add the media size in 512-byte sectors to the softc, to avoid
some 64 bit computations. Bump the capacity stored in softcs
for disks to 64 bits.
 1.196 20-Mar-2003  dbj use PRId64 to printf bp->b_blkno, which is of type daddr_t
 1.195 03-Feb-2003  thorpej Test callout_pending(), not callout_active(), and eliminate now-unnecessary
callout_deactivate() calls.
 1.194 23-Jan-2003  bad Being able to make image backups of your whole disk, not to mention not
causing certain ata disks to lock up by reading beyond the last block,
beats catering to broken devices.

bounds_check_with_label() RAW_PART too.
 1.193 06-Jan-2003  wiz writable, not writeable.
 1.192 15-Dec-2002  fvdl Don't waste too much kernel stack space on (infrequent) ioctl operations,
use malloc instead for temp space.
 1.191 01-Nov-2002  mrg implement separate read/write disk statistics:
- disk_unbusy() gets a new parameter to tell the IO direction.
- struct disk_sysctl gets 4 new members for read/write bytes/transfers.
when processing hw.diskstats, add the read&write bytes/transfers for
the old combined stats to attempt to keep backwards compatibility.

unfortunately, due to multiple bugs, this will cause new kernels and old
vmstat/iostat/systat programs to fail. however, the next time this is
change it will not fail again.

this is just the kernel portion.
 1.190 23-Oct-2002  jdolecek merge kqueue branch into -current

kqueue provides a stateful and efficient event notification framework
currently supported events include socket, file, directory, fifo,
pipe, tty and device changes, and monitoring of processes and signals

kqueue is supported by all writable filesystems in NetBSD tree
(with exception of Coda) and all device drivers supporting poll(2)

based on work done by Jonathan Lemon for FreeBSD
initial NetBSD port done by Luke Mewburn and Jason Thorpe
 1.189 18-Oct-2002  junyoung Add option NEW_BUFQ_STRATEGY, a new buffer queue strategy for better
system responsiness under high disk I/O load.

OK'ed by Juergen Hannken-Illjes.
 1.188 18-Sep-2002  chs remove all vesitages of dk_establish().
 1.187 06-Sep-2002  gehenna Merge the gehenna-devsw branch into the trunk.

This merge changes the device switch tables from static array to
dynamically generated by config(8).

- All device switches is defined as a constant structure in device drivers.

- The new grammer ``device-major'' is introduced to ``files''.

device-major <prefix> char <num> [block <num>] [<rules>]

- All device major numbers must be listed up in port dependent majors.<arch>
by using this grammer.

- Added the new naming convention.
The name of the device switch must be <prefix>_[bc]devsw for auto-generation
of device switch tables.

- The backward compatibility of loading block/character device
switch by LKM framework is broken. This is necessary to convert
from block/character device major to device name in runtime and vice versa.

- The restriction to assign device major by LKM is completely removed.
We don't need to reserve LKM entries for dynamic loading of device switch.

- In compile time, device major numbers list is packed into the kernel and
the LKM framework will refer it to assign device major number dynamically.
 1.186 30-Aug-2002  hannken Remove the old device buffer queue interface.

Approved by: Jason R. Thorpe <thorpej@wasabisystems.com>
 1.185 21-Jul-2002  hannken Rename bufq_init() to bufq_alloc().
Add bufq_free() to remove a buffer queue.
Avoid MALLOC while holding a spinlock.

From Chuck Silvers.
 1.184 16-Jul-2002  hannken Implement a new device buffer queue interface.
One basic struct, a function to setup a queue with a specific strategy and
three macros to put buf's into the queue, get and remove the next buf or
get the next buf without removal.

The BUFQ_XXX interface will be removed in the future.
The B_ORDERED flag is not longer supported.

Approved by: Jason R. Thorpe <thorpej@wasabisystems.com>
 1.183 15-May-2002  bouyer branches: 1.183.2;
The number of sector is unsigned long, so use %lu to print it.
 1.182 05-May-2002  bouyer If periph->periph_callout is already active, don't freeze the periph again:
scispi_periph_timed_thaw() will be called only one time anyway.
 1.181 09-Jan-2002  thorpej Add ioctls to get (DIOCGCACHE) and set (DIOCSCACHE) cache enables
on disks in a generic way. Implement these ioctls for SCSI disks.

This is not fully fleshed-out yet, but it allows people to experiment
with disk caches more easily.
 1.180 15-Nov-2001  lukem don't need <sys/types.h> when including <sys/param.h>
 1.179 13-Nov-2001  lukem add RCSIDs
 1.178 18-Jul-2001  thorpej branches: 1.178.2;
bzero -> memset
 1.177 01-Jun-2001  bouyer branches: 1.177.2;
Fix typo in sddump() which caused core dumps to fail with ENXIO.
 1.176 29-May-2001  chs fix some problems in sddump() with uninitialized variables.
 1.175 22-May-2001  mjacob Make SDRETRIES an option you can dig out of opt_scsi.h.

Add SD_IO_TIMEOUT (an option in opt_scsi.h) also, which defaults to the
normal 60 * 1000 timeout for normal read/write commamds. This allows you
to shorten or lengthen the timeout as needed.
 1.174 06-May-2001  drochner Since __HAVE_OLD_DISKLABEL, DISKMINOR(u, p) isn't continuous over "p"
anymore. So we can't pass a range to vdevgone() but have to loop over the
partitions explicitely.
(otherwise the next device unit will be killed)
 1.173 28-Apr-2001  tsutsui Don't forget to set XS_CTL_DATA_OUT (that was removed in previous).
 1.172 28-Apr-2001  thorpej Use simple tags for read and write, unless B_ORDERED is set, in
which case we use an ordered tag.
 1.171 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.170 09-Mar-2001  explorer Only lock the pack in if the disk device is a removable one. This fixes a
problem with Hitachi SCA drives
 1.169 08-Jan-2001  fvdl branches: 1.169.2;
Return error in the case of using ODIOCGDINFO or ODIOCGDEFLABEL when
the number of partitions is > OLDMAXPARTITIONS. This is better
than silently truncating the label (don't want to silently throw
away partitions when using an old disklabel binary on a label with
> 8 partitions). From Enami Tsugutomo.
 1.168 07-Jan-2001  fvdl Adapt all disk devices in MI directories to handle ODIOC* calls
for ports that have bumped MAXPARTITIONS (and thus define
__HAVE_OLD_DISKLABEL).
 1.167 08-Dec-2000  augustss Don't access (in core) disk label of a removable disk that is not there.
 1.166 24-Nov-2000  chs avoid division and remainder operations for devices where
the sector size is DEV_BSIZE (ie. practically everything).
 1.165 22-Nov-2000  soren Remove obsolete comment.
 1.164 05-Jul-2000  thorpej Garbage-collect __BDEVSW_DUMP_OLD_TYPE.
 1.163 09-Jun-2000  enami branches: 1.163.2;
Prevent a process being swapped out during I/O if the data buffer is
allocated on stack. This potential problem is noticed by Noriyuki Soda
and the idea and sample code to fix is given by Jason R. Thorpe.
 1.162 30-May-2000  augustss Add a quirk, SDEV_ONLYBIG, which implies that the device cannot handle
the 6 byte versions of READ, WRITE, and MODE_SENSE.
This greatly simplifies the UFI (USB Floppy) handling.
 1.161 23-May-2000  bouyer branches: 1.161.2;
sd_interpret_sense() can be called from interrupt context; call scsipi_start()
with XS_CTL_ASYNC if the failed command was called with XS_CTL_ASYNC.
Add a SDF_RESTART flag to keep state, cleared in sddone().
A mounted disk can now spin down, it will propely spin up at the next access.
 1.160 16-May-2000  thorpej Nuke dk_establish() from orbit except from those ports which still use
it to determine the boot device: mvme68k, pc532, macppc, ofppc. Those
platforms should be changed to use device_register(). In the mean time,
those ports defined __BROKEN_DK_ESTABLISH.
 1.159 30-Mar-2000  augustss Get rid of register declarations.
 1.158 13-Mar-2000  soren Fix doubled 'the's in comments.
 1.157 07-Feb-2000  thorpej Fix a bug in disksort_*() which caused non-optimal ordering when multiple
active partitions were on a single spindle. Add a b_rawblkno member to
struct buf which contains the non-partition-relative block number to sort
by.
 1.156 25-Jan-2000  drochner try to dtrt for sector sizes <DEV_BSIZE (at least, don't divide by 0)
 1.155 21-Jan-2000  thorpej Update for sys/buf.h/disksort_*() changes.
 1.154 23-Dec-1999  leo Implement the DIOCKLABEL ioctl. Through this ioctl it it possible keep the
disklabel on the last close or to drop it.
 1.153 03-Nov-1999  matt use __vax__ and __i386__ instead of vax and i386
 1.152 30-Oct-1999  bouyer In sddump(), check sd->sc_dev.dv_flags once sd has been initialised.
Patch supplied by tsutsui@ceres.dti.ne.jp in kern/8708.
 1.151 17-Oct-1999  ragge branches: 1.151.2; 1.151.4;
Don't call dk_establish() on vax either.
 1.150 17-Oct-1999  enami Cosmetic changes.
 1.149 30-Sep-1999  thorpej branches: 1.149.2;
Cleanup the scsipi_xfer flags:
- `flags' is now gone, replaced with `xs_control' and `xs_status'.
- Massive cleanup of the control flags. Now we explicitly say that
a job is to complete asynchronously, rather than relying on side-effects,
and use a new flag to now that device discovery is being performed.
- Do SCSI device discovery interrupt-driven.
 1.148 21-Sep-1999  enami Calculate the correct device minor of the first partition.
 1.147 11-Sep-1999  thorpej Implement detaching SCSI and ATAPI disks.
 1.146 26-Aug-1999  hannken Make `sc_ops->sdo_flush' synchronous. The flags `SDF_FLUSHING' and
`SDF_DIRTY' were never reset because `sddone' doesn't get called from
synchronous scsi commands.
 1.145 31-May-1999  lukem * use format_bytes() to display the disk size
* wrap a couple of printf()s
 1.144 28-Feb-1999  explorer branches: 1.144.4;
Update to slightly altered rnd_attach_source() api
 1.143 10-Feb-1999  bouyer Abort transfer if b_blkno is negative. Closes PR kern/5553 by Johan Danielsson.
 1.142 08-Feb-1999  bouyer Change DIOCEJECT to do what's needed to eject a device before the eject
command (unlock for sd and cd) if no other partitions are open, return
EBUSY otherwise. DIOCEJECT will have the old semantic if its argument is not
0. The old ioctl has been renamed to ODIOCEJECT for binary compatibility.
 1.141 29-Jan-1999  bouyer Return ENODEV instead of EIO when we are trying to open a device without media
in the drive. restrict "opening of empty drive" to character devices only
(reading a block device returns a short read instead of ENODEV, which can lead
to confusion).
 1.140 26-Jan-1999  bouyer Allows the raw partition to be open()'ed, even when scsipi_start() fails
(no media or other ...) so that we can always send ioctl's to the device.
 1.139 19-Jan-1999  bouyer Move test for SDEV_NOSTARTUNIT quirk from sd.c to scsipi_start().
Add a SDEV_NOSTARTUNIT quirk entry for BCD-16X 1997-04-25", "", "VER 2.2"
CD-rom (from Michael Santos).
 1.138 08-Dec-1998  thorpej When closing, wait for pending xfers to drain before unlocking the door,
and wait again before deleting the reference to the adapter.
 1.137 20-Nov-1998  thorpej Add adapter reference counting for SCSI and ATAPI devices.
 1.136 17-Nov-1998  bouyer The ATAPI-specific hack is not needed any more.
 1.135 17-Aug-1998  thorpej Only mark the disk dirty if we write.
 1.134 17-Aug-1998  mycroft Assign my copyrights to TNF.
 1.133 15-Aug-1998  thorpej Should not use SCSI_POLL if the system is not cold.
 1.132 15-Aug-1998  thorpej Keep track if the disk has had done i/o, making it dirty. On last close,
or at shutdown, flush the cache if the disk is still dirty and it support
cache flushing. Fixes PRs #5588 (Jim Bernard) and #5589 (Justin Gibbs).
 1.131 05-Aug-1998  drochner Improve generation of default disklabels:
-store printable product ID in cd's and sd's softc, use it as "typename"
-for this, add a "destination buffer length" argument to scsipi_strvis()
-return ATAPI device type for ATAPI devices
 1.130 31-Jul-1998  thorpej Typo.
 1.129 30-Jul-1998  thorpej From cd.c: call disk_unbusy() if we are unable to enqueue our command.
 1.128 30-Jul-1998  mjacob branches: 1.128.2;
opt_scsiverbose.h now is opt_scsi.h and contains the defopted
SCSIVERBOSE and the new ST_ENABLE_EARLYWARN.
 1.127 15-Jul-1998  mjacob Fix for kern/3835: add an sd sense handler. If we get a check condition/sense
data ASC/ASCQ of 0x04/0x01 (logical unit not ready, initialization in progress),
hang out for 5 seconds and return a RETRY THE OPERATION command. If we
get a check condition/sense data ASC/ASCQ of 0x04/0x02 (logical unit not
ready, initialization command required), send a polled/nosleep START UNIT
command and return a RETRY THE OPERATION command if that succeeds.

Don't send a START UNIT to a disk quirked as SDEV_NO_START. Don't send
a START UNIT to removable media. The reason for the latter is to not
just blindly spin up new (maybe changed) media.

I should note that I've successfully made this work with the ISP host
adapter so far. Other host adapters will need some work to be able
to manage or reasonably fail NOSLEEP/POLL commands while in this
state. Alternatively, the internal SCSI midlayer structure has got
to allow for more controlled error recovery (e.g., restart queues
controlled by the target driver).
 1.126 03-Mar-1998  cgd in the ATAPI bug workaround code ("HACK"), don't forget to set
SCSI_AUTOCONF when executing SCSI commands.
 1.125 15-Jan-1998  cgd #if some bug workaround code which only needs to be compiled if
ATAPI 'sd's are present.
 1.124 15-Jan-1998  cgd add support for an ATAPI attachment for 'sd'.
fix 'cd' driver's NCD_SCSI bogosity (was using testing wrong macro!)
clean up in various ways:
* make common atapi_mode_{sense,select}() functions.
* put ATAPI data structures in more sensible headers, split up by
device type.
* include headers a bit more carefully.
* pass flags to attachment-specific cd functions, and use them.
* get rid of SCSI bits in scsipi_base.h's scsipi_make_xs(), move
them into the correct place in scsi_base.c.
* fix minor typo in struct name in scsipiconf.h (which was apparently
never used except in a #define later in the same file).
* use __attribute__ to force 4-byte alignment for xs command store,
so that architectures trying to bus_space_write_multi_N() (where
N > 1) that data to a controller won't lose.
* clean up a few comments in typos, and make a few #defines easier to
understand/maintain.
* rename cd_link.h to cdvar.h (via repository copy). This is exactly
what a 'var' file is supposed to be.
 1.123 12-Jan-1998  thorpej Adjust for changes to config.
 1.122 18-Oct-1997  thorpej Implement two macros, scsipi_command() and scsipi_command_direct(), and
use them to hide the structure of the function pointers we jump through
to issue a command.
 1.121 13-Oct-1997  explorer o Make usage of /dev/random dependant on
pseudo-device rnd # /dev/random and in-kernel generator
in config files.

o Add declaration to all architectures.

o Clean up copyright message in rnd.c, rnd.h, and rndpool.c to include
that this code is derived in part from Ted Tyso's linux code.
 1.120 10-Oct-1997  explorer Add hooks to insert timing info into the random system
 1.119 08-Oct-1997  thorpej Implement DIOCGDEFLABEL.
 1.118 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.117 20-Sep-1997  mjacob Allow SDOUTSTANDING to be defined elsewhere (e.g., config file).
 1.116 27-Aug-1997  bouyer branches: 1.116.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.115 04-Aug-1997  fair trivial change for PR#3815
 1.114 02-Jul-1997  phil branches: 1.114.2;
Report actual disksize on finding a disk.
(From: der Mouse <mouse@Rodents.Montreal.QC.CA>)
 1.113 24-Jun-1997  thorpej foosize()'s return value is in DEV_BSIZE units; adjust the size obtained
from the disklabel accordingly.
 1.112 18-Jun-1997  pk sdsize(): only call sdopen()/sdclose() if not already open, per the
current openmask.
 1.111 02-Apr-1997  mycroft Push the buffer cleanup code into scsi_done(), and split it so that biodone()
is called *after* the driver `done' routine. This fixes disk I/O statistics
on SCSI devices.

Also, calling the `done' routine with a `complete' argument of 0 and actually
having it do anything meaningful loses in at least 3 ways, so just nuke the
argument altogether and don't call it this way. If the driver needs to do
some error handling, that's what `err_handler' is for.
 1.110 04-Mar-1997  mikel garbage collect unused variable (was this struct declaration orginally
a typedef?); from Enami Tsugutomo in PR kern/3282.
 1.109 21-Feb-1997  thorpej If posting an error condition because the media has been unloaded,
make sure to set the residual count to reflect that no data was
transfered.

From Naofumi HONDA / MINOURA Makoto, PR #3007.
 1.108 21-Feb-1997  thorpej Handle optical disks a little better, from Naofumi HONDA / MINOURA Makoto
in PR #3009, with some slight KNF from me.
 1.107 05-Dec-1996  cgd branches: 1.107.6;
update these so they compile whether or not __BROKEN_INDIRECT_CONFIG
is defined.
 1.106 29-Nov-1996  thorpej Start a mode sense with a clean sense buffer.
Fixes PR #2953, from David M. Stanhope <dms@celtech.com>.
 1.105 23-Oct-1996  matthias * At least the Teac FC-1 Shugart-SCSI bridgeboard does motor on/off
management by itself. But when it gets a start unit request, it keeps
the floppy motor running all the time. This adds code for dealing with
yet another quirk (SDEV_NOSTARTUNIT) that prevents sd.c from sending
start unit requests. A entry for the Teac FC-1 is added to the quirk
table.
 1.104 12-Oct-1996  christos revert previous kprintf change
 1.103 10-Oct-1996  christos printf -> kprintf, sprintf -> ksprintf
 1.102 05-Jul-1996  christos - Apply PR/2535: Add support for flex scsi disks.
- Add a quirk called SCSI_NOMODESENSE for drives like the iomega jaz,
that don't support mode sense.
 1.101 04-Jun-1996  thorpej In sddump(), don't check the SDEV_MEDIA_LOADED bit of the device, since
that bit might have been cleared by successful unmounting of all filesystems
on the dump device, e.g. in the case of "reboot -d".
 1.100 14-May-1996  leo branches: 1.100.4;
Clear SDEV_MEDIA_LOADED bit on the last close of a device. This causes the
geometry info & the disklabel to be reloaded on each 'first-open' of the
device.
 1.99 14-May-1996  thorpej Print the error code in the "not queued" message, to make debugging
a bit easier, and make sure a newline gets printed.
 1.98 05-May-1996  christos Cleanup the rest of the SCSIDEBUG printfs. From Bernd Ernesti.
 1.97 03-May-1996  christos - SCSIDEBUG printf() fixes.
 1.96 22-Apr-1996  christos remove include of <sys/cpu.h>
 1.95 30-Mar-1996  christos Eliminate scsi_conf.h.
 1.94 26-Mar-1996  mycroft Put back dk_establish() for now.
 1.93 26-Mar-1996  mycroft Increase *OUTSTANDING.
Remove old dk_establish() garbage.
Support the `ancient' bit in both drivers.
 1.92 24-Mar-1996  mycroft Fix trivial oversight in last.
 1.91 19-Mar-1996  mycroft Define a full set of [234][bl]tol() and lto[234][bl]() conversion functions,
inlined.
Use sized types in protocol structures.
Make the definition of scsi_sense_data less ugly.
 1.90 17-Mar-1996  pk Parentheses are your friends.
 1.89 17-Mar-1996  thorpej New device attachment scheme:

- split softc size and match/attach out from cfdriver into
a new struct cfattach.

- new "attach" directive for files.*. May specify the name of
the cfattach structure, so that devices may be easily attached
to parents with different autoconfiguration semantics.
 1.88 05-Mar-1996  thorpej Normalize SCSI autoconfiguration output.
From Chris Demetriou <cgd@NetBSD.ORG>. Fixes PR #1958.
 1.87 14-Feb-1996  christos scsi prototypes
 1.86 30-Jan-1996  thorpej Implement DIOCLOCK and DIOCEJECT. DIOCEJECT is limited to removable
media. Fixes PR #1975.
 1.85 12-Jan-1996  thorpej Handle cases like the following:
- controller calls scsi_done() with error XS_TIMEOUT
- scsi_done() calls sddone()
- sddone() calls disk_unbusy()
- scsi_done() calls controller to retry command (missing the
call to disk_busy())
- controller calls scsi_done()
- scsi_done() calls sddone()
- sddone() calls disk_busy(), which panics because of the imbalance.
Bug noticed by Leo Weppleman, who also suggested this fix; pass an additional
boolean argument ("complete") to the device's "done" routine, with a
value of `0' passed from the previous call to "done", and add an additional
call to "done" when the xfer resources are freed.
 1.84 07-Jan-1996  thorpej New generic disk framework. Highlights:

- New metrics handling. Metrics are now kept in the new
`struct disk'. Busy time is now stored as a timeval, and
transfer count in bytes.

- Storage for disklabels is now dynamically allocated, so that
the size of the disk structure is not machine-dependent.

- Several new functions for attaching and detaching disks, and
handling metrics calculation.

Old-style instrumentation is still supported in drivers that did it before.
However, old-style instrumentation is being deprecated, and will go away
once the userland utilities are updated for the new framework.

For usage and architectural details, see the forthcoming disk(9) manual
page.
 1.83 07-Dec-1995  thorpej Remove a couple of now unneeded assignments.
 1.82 07-Dec-1995  thorpej In sdminphys(), add a comment about the semantics of the "length" field
in a 6-byte read/write cdb (namely, length == 0 really means 256), and
that we handle this conservatively by limiting the transfer to 255 blocks.
*sigh* One just never knows how a pre-scsi-1 device is going to behave.
 1.81 07-Dec-1995  thorpej In both cd.c and sd.c:
If the read or write request can fit into a 6-byte cdb, then
use a 6-byte cdb, otherwise use the 10-byte as before.

In sd.c:
In sdattach(), make a note if the device is "ancient" (i.e.
inqbuf.version & SID_ANSII == 0).

Implement sdminphys(): if the device is "ancient", shorten the
transfer so it will fit into a 6-byte cdb.

These changes have eliminated the rejected read/write requests on my
Sun 3/60 with 2 ESDI disks behind an Emulex MD21.
 1.80 10-Oct-1995  mycroft branches: 1.80.2;
Return EINVAL if something other than a whole number of blocks is requested.
 1.79 30-Sep-1995  pk Specify SCSI_DATA_OUT in sddump() (from John Kohl; PR#1482).
 1.78 26-Sep-1995  thorpej Don't declare Debugger(). It's handled in <sys/systm.h>.
#include <sys/systm.h> where necessary, as suggested by Jonathan Stone.
Fixes PR #1511.
 1.77 12-Aug-1995  mycroft opri --> s
 1.76 12-Aug-1995  mycroft Fix oversight in previous.
 1.75 12-Aug-1995  mycroft minphys() functions really should return void.
 1.74 05-Aug-1995  mycroft Use an intermediate variable to shorten label initialization code.
 1.73 24-Jul-1995  cgd update SCSI minphys routines' definitions to match standard minphys()
definition and usage.
 1.72 04-Jul-1995  mycroft Make each disk and tape driver define its own read and write functions.
Deprecate rawread() and rawwrite() completely. Remove d_strategy from cdevsw to
force the abstraction barrier.
 1.71 26-Jun-1995  cgd implement new-style block device dump routine if __BDEVSW_DUMP_OLD_TYPE
not defined, otherwise use an "not implemented" stub.
 1.70 03-May-1995  cgd looks like some extra ')'s were left in here.
 1.69 03-May-1995  mycroft Make the byte-shifting code consistent.
 1.68 15-Apr-1995  mycroft Don't boundary check I/O to the `raw' partition.
 1.67 10-Apr-1995  mycroft kernel_pmap --> pmap_kernel()
 1.66 01-Apr-1995  mycroft Add a missing unlock.
 1.65 29-Mar-1995  mycroft Revamp the locking mechanism slightly.
 1.64 25-Mar-1995  mycroft Remove the write protect check altogether, and rely on the drive to do it.
 1.63 23-Mar-1995  mycroft Check for read-only media in open(), not write().
 1.62 23-Mar-1995  mycroft Rearrange DIOCWDINFO a little; closer to wd.c.
 1.61 23-Mar-1995  mycroft Allow DIOCWDINFO even if label is not `writable'.
 1.60 07-Mar-1995  mycroft Correct spelling of `fictitious'. Add patterns for optical memory devices.
From Alistair Crooks.
 1.59 30-Jan-1995  mycroft Some drives don't grok START with LoEj=1, either.
 1.58 30-Jan-1995  mycroft Set the LoadEject bit when issuing a START.
 1.57 30-Jan-1995  mycroft Some devices really do require the START before the PREVENT. This makes no
sense.
 1.56 26-Jan-1995  mycroft Update copyrights.
 1.55 26-Jan-1995  mycroft Redo the `wait for spin up' code. The Mach 3 method clearly doesn't work.
Also, rearrange the first open sequences a bit; SDEV_OPEN is no longer magic.
 1.54 23-Jan-1995  mycroft Do the PREVENT before the START.
 1.53 16-Jan-1995  mycroft If the disk reports a block size of 0, assume it really meant 512.
 1.52 13-Jan-1995  mycroft Always boundary check I/O.
 1.51 28-Dec-1994  mycroft Numerous changes. Many bugs fixed, better autoconfig, a few new features.
 1.50 16-Dec-1994  mycroft Remove DIOCSBAD handling. It's not actually handled, so why recognize it?
 1.49 14-Dec-1994  mycroft Remove dkbad.h.
 1.48 23-Nov-1994  mycroft There's no point in bothering to unlock what we didn't lock.
 1.47 22-Nov-1994  mycroft Add still more locking.
 1.46 21-Nov-1994  mycroft Replace dev_unit with device_softc in scsi_link. Change argument to foostart()
to void*.
 1.45 20-Nov-1994  mycroft Add some missing locking, and some general cleanup.
 1.44 15-Nov-1994  deraadt Enough Chinese torture already
 1.43 30-Oct-1994  cgd be more careful with types, also pull in headers where necessary.
 1.42 20-Oct-1994  mycroft First cut at making user-level SCSI commands work. This is untested.
Partly from John Brezak.
 1.41 20-Oct-1994  mycroft Back out change to print geometry from label at boot; can't do this reliably.
 1.40 20-Oct-1994  mycroft openpart -> openmask
 1.39 20-Oct-1994  mycroft Trivial stylistic changes.
 1.38 20-Oct-1994  mycroft Use the block size in the label in most cases.
 1.37 20-Oct-1994  mycroft Various changes:
* Get rid of unused `partflags'.
* Use dk_openpart, and remove `openparts'. Also, allow an open on both
the block and characters devices at the same time.
* If there was a label, print the geometry from the label at boot time.
* Turn `wlabel' into a flag, and *don't* set it automatically in
DIOCWLABEL; disklabel(8) does that.
Other minor changes.
 1.36 14-Oct-1994  cgd move arch dependence to archs. clean up a bit. deal with weird MAXPARTITONS.
 1.35 26-Jul-1994  mycroft Normalize label handling.
 1.34 04-Jul-1994  chopps branches: 1.34.2;
change to support other MAXPARTITIONS vals aside from 8
 1.33 29-Jun-1994  cgd New RCS ID's, take two. they're more aesthecially pleasant, and use 'NetBSD'
 1.32 16-Jun-1994  chopps resolve confusion over who owns the buf after calling scsi_scsi_cmd()
 1.31 16-Jun-1994  mycroft b_un.b_addr -> b_data
 1.30 09-May-1994  chopps remove union's from sense_data struct, conditionaly define RAW_PART
 1.29 11-Apr-1994  mycroft Fix various types. Remove some outdated flags.
 1.28 11-Apr-1994  mycroft Combine scsi_start_unit() and scsi_stop_unit(), and increase the start
timeout (again).
 1.27 06-Apr-1994  mycroft Redo the last change; it was compltely bogus.
 1.26 06-Apr-1994  mycroft Try to detect garbage data from page 4 sense.
 1.25 29-Mar-1994  mycroft New SCSI system, based on Julian's more recent work.
 1.24 24-Mar-1994  cgd print even more SCSI sense data; this should be in a function...
 1.23 24-Mar-1994  cgd print out lots of info on non-media hardware error...
 1.22 06-Feb-1994  mycroft Use b_actf, not av_forw.
 1.21 25-Jan-1994  deraadt print nice numbers for disks >2G. from <adrian@lemmings.cursci.com>
 1.20 11-Jan-1994  mycroft *strategy functions return void.
 1.19 17-Dec-1993  mycroft Canonicalize all #includes.
 1.18 01-Aug-1993  mycroft branches: 1.18.2;
Add RCS identifiers (this time on the correct side of the branch), and
incorporate recent changes in netbsd-0-9 branch.
 1.17 19-Jul-1993  cgd branches: 1.17.2;
patches from allen briggs to fix a minor bug in *attach()
 1.16 04-Jul-1993  andrew Reject non block-aligned transfers (where a block is 512 bytes in length);
they were causing scsi controller lockups. Replace an occurrence of "512"
with SECSIZE, which is #defined to be 512.
 1.15 27-Jun-1993  andrew ANSIfications.
 1.14 16-Jun-1993  deraadt whoops. typo.
 1.13 16-Jun-1993  deraadt fix to intuit the maximum number of scsi units available on a device
driver. this piece at least, should be safe from changing sizeof(dev_t)
 1.12 25-May-1993  deraadt patch00149 by Julian Elischer <julian@jules.dialix.oz.au> & Rodney Grimes.
When an error was encountered, the sd/cd drivers printed blockno&ff0000
rather that blockno.
 1.11 21-May-1993  davidb Changed when disks are started, to get DEC disks working.
 1.10 20-May-1993  deraadt more disklabel changes
 1.9 20-May-1993  cgd add rcsids and clean up file headers
 1.8 04-May-1993  deraadt support for making dev->id_alive be set, this is for iostat to
find disk devices. wee bit of a kludge. sub-device attach()
routines must now return 1 for successful attach(), 0 otherwise.
Other bsd's do this too..
 1.7 26-Apr-1993  deraadt cosmetic change
 1.6 20-Apr-1993  mycroft Display more meaningful message on SCSI `unit attention'.
 1.5 12-Apr-1993  deraadt fixed various bugs like cdattach() returning garbage.
 1.4 12-Apr-1993  deraadt small fixes
 1.3 12-Apr-1993  deraadt new scsi subsystem.
changes also in config/mkioconf.c
i386/isa/wd.c, fd.c, and all scsi drivers.
 1.2 10-Apr-1993  glass fixed to be compliant, subservient, and to take advantage of the newly
hacked config(8)
 1.1 21-Mar-1993  cgd after 0.2.2 "stable" patches applied
 1.17.2.1 31-Jul-1993  cgd give names, err, wmesg's, to my "pain" -- i.e. convert sleep() to tsleep()
 1.18.2.12 16-Feb-1994  mycroft More KNF-like.
 1.18.2.11 06-Feb-1994  mycroft Use b_actf, not av_forw.
 1.18.2.10 01-Feb-1994  mycroft Add scsi_stop_unit(), from Mac code. Other minor nits.
 1.18.2.9 29-Nov-1993  mycroft Use dk_establish().
 1.18.2.8 25-Nov-1993  mycroft Fix silly typo.
 1.18.2.7 25-Nov-1993  mycroft (Hopefully) make cd work. Make cd and sd more similar. Implement variable
block sizes in sd.
 1.18.2.6 24-Nov-1993  mycroft Final tweaks to get it going.
 1.18.2.5 24-Nov-1993  mycroft Fix unitialized variable.
 1.18.2.4 24-Nov-1993  mycroft More construction...
 1.18.2.3 24-Nov-1993  mycroft Still under construction...
 1.18.2.2 24-Nov-1993  mycroft Under construction...
 1.18.2.1 24-Sep-1993  mycroft cd.c, sd.c, st.c: strategy functions return void.
 1.34.2.1 26-Jul-1994  cgd from branch.
 1.80.2.1 04-Feb-1996  gwr Pull in rev 1.82 (Jason's fix to use 6-byte command blocks).
 1.100.4.3 04-Mar-1997  mycroft Pull up support for fetching geometry info on optical disks.
 1.100.4.2 29-Nov-1996  thorpej Start a mode sense with a clean sense buffer.
From David M. Stanhope <dms@celtech.com>, PR #2953.
 1.100.4.1 04-Jun-1996  thorpej Update from trunk:

In sddump(), don't check the SDEV_MEDIA_LOADED bit of the device, since
that bit might have been cleared by successful unmounting of all filesystems
on the dump device, e.g. in the case of "reboot -d".
 1.107.6.1 12-Mar-1997  is Merge in changes from Trunk
 1.114.2.1 23-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.116.2.4 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.116.2.3 22-Sep-1997  thorpej Update marc-pcmcia branch from trunk.
 1.116.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.116.2.1 27-Aug-1997  thorpej file sd.c was added on branch marc-pcmcia on 1997-08-27 23:33:32 +0000
 1.128.2.2 08-Aug-1998  eeh Revert cdevsw mmap routines to return int.
 1.128.2.1 30-Jul-1998  eeh file sd.c was added on branch eeh-paddr_t on 1998-08-08 03:06:52 +0000
 1.144.4.1 21-Jun-1999  thorpej Sync w/ -current.
 1.149.2.2 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.149.2.1 21-Dec-1999  wrstuden Initial commit of recent changes to make DEV_BSIZE go away.

Runs on i386, needs work on other arch's. Main kernel routines should be
fine, but a number of the stand programs need help.

cd, fd, ccd, wd, and sd have been updated. sd has been tested with non-512
byte block devices. vnd, raidframe, and lfs need work.

Non 2**n block support is automatic for LKM's and conditional for kernels
on "options NON_PO2_BLOCKS".
 1.151.4.1 15-Nov-1999  fvdl Sync with -current
 1.151.2.9 12-Mar-2001  bouyer Sync with HEAD.
 1.151.2.8 18-Jan-2001  bouyer Sync with head (for UBC+NFS fixes, mostly).
 1.151.2.7 13-Dec-2000  bouyer Make it compile again.
 1.151.2.6 13-Dec-2000  bouyer Sync with HEAD (for UBC fixes).
 1.151.2.5 08-Dec-2000  bouyer Sync with HEAD.
 1.151.2.4 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.151.2.3 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.151.2.2 20-Oct-1999  thorpej Remove an unnecessary comment regarding XS_CTL_NOSLEEP.
 1.151.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.161.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.163.2.3 06-May-2001  he Pull up revision 1.174 (requested by fvdl):
Compensate for the fact that after __HAVE_OLD_DISKLABEL,
DISKMINOR(u,p) is no longer contiguous over ``p'', so ranges
cannot be passed to vdevgone().
 1.163.2.2 01-May-2001  he Pull up revisions 1.167-1.168 (requested by fvdl):
Increase the number of BSD disklabel partitions on i386 to 16.
 1.163.2.1 11-Mar-2001  he Pull up revision 1.170 (requested by explorer):
Fix a problem with some Hitachi drives by only issuing the ``lock
media'' and ``unlock media'' commands to removable media.
 1.169.2.14 07-Jan-2003  thorpej Sync with HEAD.
 1.169.2.13 19-Dec-2002  thorpej Sync with HEAD.
 1.169.2.12 11-Nov-2002  nathanw Catch up to -current
 1.169.2.11 18-Oct-2002  nathanw Catch up to -current.
 1.169.2.10 17-Sep-2002  nathanw Catch up to -current.
 1.169.2.9 01-Aug-2002  nathanw Catch up to -current.
 1.169.2.8 20-Jun-2002  nathanw Catch up to -current.
 1.169.2.7 28-Feb-2002  nathanw Catch up to -current.
 1.169.2.6 11-Jan-2002  nathanw More catchup.
 1.169.2.5 08-Jan-2002  nathanw Catch up to -current.
 1.169.2.4 14-Nov-2001  nathanw Catch up to -current.
 1.169.2.3 24-Aug-2001  nathanw Catch up with -current.
 1.169.2.2 21-Jun-2001  nathanw Catch up to -current.
 1.169.2.1 09-Apr-2001  nathanw Catch up with -current.
 1.177.2.5 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.177.2.4 06-Sep-2002  jdolecek sync kqueue branch with HEAD
 1.177.2.3 23-Jun-2002  jdolecek catch up with -current on kqueue branch
 1.177.2.2 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.177.2.1 03-Aug-2001  lukem update to -current
 1.178.2.2 26-Sep-2001  fvdl * add a VCLONED vnode flag that indicates a vnode representing a cloned
device.
* rename REVOKEALL to REVOKEALIAS, and add a REVOKECLONE flag, to pass
to VOP_REVOKE
* the revoke system call will revoke all aliases, as before, but not the
clones
* vdevgone is called when detaching a device, so make it use REVOKECLONE
to get rid of all clones as well
* clean up all uses of VOP_OPEN wrt. locking.
* add a few VOPS to spec_vnops that need to do something when it's a
clone vnode (access and getattr)
* add a copy of the vnode vattr structure of the original 'master' vnode
to the specinfo of a cloned vnode. could possibly redirect getattr to
the 'master' vnode, but this has issues with revoke
* add a vdev_reassignvp function that disassociates a vnode from its
original device, and reassociates it with the specified dev_t. to be
used by cloning devices only, in case a new minor is allocated.
* change all direct references in drivers to v_devcookie and v_rdev
to vdev_privdata(vp) and vdev_rdev(vp). for diagnostic purposes
when debugging race conditions that still exist wrt. locking and
revoking vnodes.
* make the locking state of a vnode consistent when passed to
d_open and d_close (unlocked). locked would be better, but has
some deadlock issues
 1.178.2.1 07-Sep-2001  thorpej Commit my "devvp" changes to the thorpej-devvp branch. This
replaces the use of dev_t in most places with a struct vnode *.

This will form the basic infrastructure for real cloning device
support (besides being architecurally cleaner -- it'll be good
to get away from using numbers to represent objects).
 1.183.2.4 31-Aug-2002  gehenna catch up with -current.
 1.183.2.3 29-Aug-2002  gehenna catch up with -current.
 1.183.2.2 20-Jul-2002  gehenna catch up with -current.
 1.183.2.1 16-May-2002  gehenna Add the block/character device switches.
Replace the direct-access to devsw table with calling devsw API.
 1.202.2.13 11-Dec-2005  christos Sync with head.
 1.202.2.12 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.202.2.11 01-Apr-2005  skrll Sync with HEAD.
 1.202.2.10 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.202.2.9 18-Dec-2004  skrll Sync with HEAD.
 1.202.2.8 02-Nov-2004  skrll Sync with HEAD.
 1.202.2.7 19-Oct-2004  skrll Sync with HEAD
 1.202.2.6 21-Sep-2004  skrll Fix the sync with head I botched.
 1.202.2.5 18-Sep-2004  skrll Sync with HEAD.
 1.202.2.4 03-Sep-2004  skrll Sync with HEAD
 1.202.2.3 25-Aug-2004  skrll Sync with HEAD.
 1.202.2.2 03-Aug-2004  skrll Sync with HEAD
 1.202.2.1 02-Jul-2003  darrenr Apply the aborted ktrace-lwp changes to a specific branch. This is just for
others to review, I'm concerned that patch fuziness may have resulted in some
errant code being generated but I'll look at that later by comparing the diff
from the base to the branch with the file I attempt to apply to it. This will,
at the very least, put the changes in a better context for others to review
them and attempt to tinker with removing passing of 'struct lwp' through
the kernel.
 1.216.2.3 11-Sep-2004  he Pull up revisions 1.221-1.223 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.216.2.2 02-Jul-2004  he Pull up revisions 1.218-1.219 (requested by mycroft in ticket #572):
Several fixes mostly related to USB:
o Separate page 4/5 reading into separate functions. For
``removeable'' devices, try page 5 first. Fixes problem
with some broken USB devices which do not handle page 4
correctly.
o Add prototypes for recently added functions.
 1.216.2.1 29-May-2004  tron Pull up revision 1.217 (requested by bouyer in ticket #399):
If READ_FORMAT_CAPACITIES fails with "Medium Format Corrupted" ASC/ASQ error,
handle it as if the drive was not formatted, so that "scsictl sdx format"
can open the raw device.
 1.232.4.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.232.2.1 29-Apr-2005  kent sync with -current
 1.237.2.3 29-Dec-2005  riz Pull up following revision(s) (requested by bouyer in ticket #1045):
sys/dev/scsipi/sd.c: revision 1.243
Adapt previous change to recent scsipi. Pointed out by Simon Burge.
 1.237.2.2 29-Dec-2005  riz Pull up following revision(s) (requested by bouyer in ticket #1045):
sys/dev/scsipi/sd.c: revision 1.242
Some USB devices reports themselves as removable, but have no door and
so don't support the SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL command.
When an "Illegal field in CDB" is reported for this command, mark the
device as non-removable (which is always true for USB keys from the SCSI
point of view), print a message and ignore the error.
For DIOCLOCK, return ENOTTY if the device is not removable instead of
trying a command which will fail.
Fix a problem reported by Hubert Feyrer for some USB umass devices, patch
tested by him.
 1.237.2.1 06-Apr-2005  tron Pull up revision 1.238 (requested by yamt in ticket #112):
introduce a function to drain bufq and use it where appropriate.
 1.240.2.8 17-Mar-2008  yamt sync with head.
 1.240.2.7 27-Feb-2008  yamt sync with head.
 1.240.2.6 21-Jan-2008  yamt sync with head
 1.240.2.5 27-Oct-2007  yamt sync with head.
 1.240.2.4 03-Sep-2007  yamt sync with head.
 1.240.2.3 26-Feb-2007  yamt sync with head.
 1.240.2.2 30-Dec-2006  yamt sync with head.
 1.240.2.1 21-Jun-2006  yamt sync with head.
 1.244.6.2 01-Jun-2006  kardel Sync with head.
 1.244.6.1 22-Apr-2006  simonb Sync with head.
 1.244.4.1 09-Sep-2006  rpaulo sync with head
 1.244.2.1 01-Mar-2006  yamt sync with head.
 1.245.6.1 31-Mar-2006  tron Merge 2006-03-31 NetBSD-current into the "peter-altq" branch.
 1.245.4.1 19-Apr-2006  elad sync with head.
 1.245.2.3 11-Aug-2006  yamt sync with head
 1.245.2.2 26-Jun-2006  yamt sync with head.
 1.245.2.1 01-Apr-2006  yamt sync with head.
 1.247.2.1 19-Jun-2006  chap Sync with head.
 1.248.2.1 13-Jul-2006  gdamore Merge from HEAD.
 1.249.4.2 12-Jan-2007  ad Sync with head.
 1.249.4.1 18-Nov-2006  ad Sync with head.
 1.249.2.1 22-Sep-2006  riz Pull up following revision(s) (requested by reinoud in ticket #168):
sys/dev/scsipi/sd.c: revision 1.250
Fix same panic problem as with cd.c when a scsi error happends on
read/write action. It would panic on a diagnosic assertion. When a scsi
read/write command fails, the whole extent is gone bad so mark all as not
done.
 1.250.2.2 10-Dec-2006  yamt sync with head.
 1.250.2.1 22-Oct-2006  yamt sync with head
 1.258.6.1 03-Sep-2007  wrstuden Sync w/ NetBSD-4-RC_1
 1.258.4.2 12-Mar-2007  rmind Sync with HEAD.
 1.258.4.1 27-Feb-2007  yamt - sync with head.
- move sched_changepri back to kern_synch.c as it doesn't know PPQ anymore.
 1.258.2.1 31-Jul-2007  liamjfoy Pull up following revision(s) (requested by jnemeth in ticket #803):
sys/dev/scsipi/sd.c: revision 1.260
add support devctl properties
 1.261.4.1 11-Jul-2007  mjf Sync with head.
 1.261.2.5 09-Oct-2007  ad Sync with head.
 1.261.2.4 20-Aug-2007  ad - Alter disk attach/detach to fix a panic when closing a vnd device.
- Sync with HEAD.
 1.261.2.3 19-Aug-2007  ad - Back out the biodone() changes.
- Eliminate B_ERROR (from HEAD).
 1.261.2.2 01-Jul-2007  ad Adapt to callout API change.
 1.261.2.1 13-May-2007  ad - Pass the error number and residual count to biodone(), and let it handle
setting error indicators. Prepare to eliminate B_ERROR.
- Add a flag argument to brelse() to be set into the buf's flags, instead
of doing it directly. Typically used to set B_INVAL.
- Add a "struct cpu_info *" argument to kthread_create(), to be used to
create bound threads. Change "bool mpsafe" to "int flags".
- Allow exit of LWPs in the IDL state when (l != curlwp).
- More locking fixes & conversion to the new API.
 1.262.2.1 15-Aug-2007  skrll Sync with HEAD.
 1.265.8.2 03-Aug-2007  tsutsui Don't allocate data buffer on stack in sd_read_capacity().

If data buffer is allocated on stack at the end of CPU cacheline,
the same cacheline region might be accessed via kernel stack even
after bus_dmamap_sync(9) against the data buffer in lower scsi drivers
so that read data via DMA could be lost.

Fixes occasional readdisklabel(9) failure at boot on ews4800mips
with osiop(4) driver and 128byte cacheline.

XXX: Other functions/drivers might have the similar problem.
 1.265.8.1 03-Aug-2007  tsutsui file sd.c was added on branch matt-mips64 on 2007-08-03 13:56:38 +0000
 1.265.6.2 14-Oct-2007  yamt sync with head.
 1.265.6.1 06-Oct-2007  yamt sync with head.
 1.265.4.3 23-Mar-2008  matt sync with HEAD
 1.265.4.2 09-Jan-2008  matt sync with HEAD
 1.265.4.1 06-Nov-2007  matt sync with HEAD
 1.265.2.5 08-Dec-2007  jmcneill Rename pnp(9) -> pmf(9), as requested by many.
 1.265.2.4 06-Nov-2007  joerg Refactor PNP API:
- Make suspend/resume directly a device functionality. It consists of
three layers (class logic, device logic, bus logic), all of them being
optional. This replaces D0/D3 transitions.
- device_is_active returns true if the device was not disabled and was
not suspended (even partially), device_is_enabled returns true if the
device was enabled.
- Change pnp_global_transition into pnp_system_suspend and
pnp_system_resume. Before running any suspend/resume handlers, check
that all currently attached devices support power management and bail
out otherwise. The latter is not done for the shutdown/panic case.
- Make the former bus-specific generic network handlers a class handler.
- Make PNP message like volume up/down/toogle PNP events. Each device
can register what events they are interested in and whether the handler
should be global or not.
- Introduce device_active API for devices to mark themselve in use from
either the system or the device. Use this to implement the idle handling
for audio and input devices. This is intended to replace most ad-hoc
watchdogs as well.
- Fix somes situations in which audio resume would lose mixer settings.
- Make USB host controllers better deal with suspend in the light of
shared interrupts.
- Flush filesystem cache on suspend.
- Flush disk caches on suspend. Put ATA disks into standby on suspend as
well.
- Adopt drivers to use the new PNP API.
- Fix a critical bug in the generic cardbus layer that made D0->D3
break.
- Fix ral(4) to set if_stop.
- Convert cbb(4) to the new PNP API.
- Apply the PCI Express SCI fix on resume again.
 1.265.2.3 26-Oct-2007  joerg Sync with HEAD.

Follow the merge of pmap.c on i386 and amd64 and move
pmap_init_tmp_pgtbl into arch/x86/x86/pmap.c. Modify the ACPI wakeup
code to restore CR4 before jumping back into kernel space as the large
page option might cover that.
 1.265.2.2 02-Oct-2007  joerg Sync with HEAD.
 1.265.2.1 03-Aug-2007  joerg file sd.c was added on branch jmcneill-pm on 2007-10-02 18:28:39 +0000
 1.267.8.1 11-Dec-2007  yamt sync with head.
 1.267.6.1 26-Dec-2007  ad Sync with head.
 1.267.4.1 18-Feb-2008  mjf Sync with HEAD.
 1.268.2.1 02-Jan-2008  bouyer Sync with HEAD
 1.269.6.8 17-Jan-2009  mjf - Fix a CVS merge breakage
- Catch up with some driver changes
 1.269.6.7 17-Jan-2009  mjf Sync with HEAD.
 1.269.6.6 28-Sep-2008  mjf Sync with HEAD.
 1.269.6.5 29-Jun-2008  mjf Sync with HEAD.
 1.269.6.4 02-Jun-2008  mjf Sync with HEAD.
 1.269.6.3 06-Apr-2008  mjf - after some discussion with agc@ i agreed it would be a good idea to move
device_unregister_* to device_deregister_* to be more like the pmf(9)
functions, especially since a lot of the time the function calls are next
to each other.

- add device_register_name() support for dk(4).
 1.269.6.2 03-Apr-2008  mjf Sync with HEAD.
 1.269.6.1 29-Mar-2008  mjf - etc/devfsd.conf: Add some rules to give nodes like /dev/tty and
/dev/null better default modes, i.e. 0666.

- sbin/init: Run devfsd -s before going to multiuser.

- sys/arch: Provide arm32, i386, sparc with a mem_init() function to request
device nodes for /dev/null, /dev/zero, etc.

- sys/dev: Convert rnd, wd, agp, raid, cd, sd, wsdisplay, wskbd, wsmouse,
wsmux, tty, bpf, swap to devfs New World Order.

- sys/fs/devfs: Make the visibility attribute of device nodes configurable.
Also provide a function to mount a devfs on boot.

- sys/kern: Add a new boot flag, -n. This disables devfs support. Unless
the -n flag is specified the kernel will mount a devfs file
system on boot.
 1.269.2.1 24-Mar-2008  keiichi sync with head.
 1.272.4.8 11-Aug-2010  yamt sync with head.
 1.272.4.7 11-Mar-2010  yamt sync with head
 1.272.4.6 19-Aug-2009  yamt sync with head.
 1.272.4.5 18-Jul-2009  yamt sync with head.
 1.272.4.4 20-Jun-2009  yamt sync with head
 1.272.4.3 16-May-2009  yamt sync with head
 1.272.4.2 04-May-2009  yamt sync with head.
 1.272.4.1 16-May-2008  yamt sync with head.
 1.272.2.2 17-Jun-2008  yamt sync with head.
 1.272.2.1 18-May-2008  yamt sync with head.
 1.273.2.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.273.2.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.274.4.1 19-Oct-2008  haad Sync with HEAD.
 1.274.2.1 18-Jul-2008  simonb Sync with head.
 1.275.4.1 31-Oct-2012  riz sys/dev/scsipi/sd.c patch

Allow disk strategy to be queried and changed at runtime
for sd(4) disks.
[buhrow, ticket #1807]
 1.275.2.2 28-Apr-2009  skrll Sync with HEAD.
 1.275.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.277.2.2 23-Jul-2009  jym Sync with HEAD.
 1.277.2.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.291.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.292.2.1 30-May-2010  rmind sync with head
 1.294.2.3 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.294.2.2 23-May-2012  yamt sync with head.
 1.294.2.1 17-Apr-2012  yamt sync with head
 1.295.2.2 29-Apr-2012  mrg sync to latest -current.
 1.295.2.1 18-Feb-2012  mrg merge to -current.
 1.296.2.1 23-Apr-2012  riz Pull up following revision(s) (requested by bouyer in ticket #192):
sys/dev/scsipi/cd.c: revision 1.307
sys/dev/scsipi/scsiconf.c: revision 1.266
sys/dev/scsipi/sd.c: revision 1.298
sys/dev/scsipi/st_scsi.c: revision 1.35
sys/dev/scsipi/atapiconf.c: revision 1.85
sys/dev/scsipi/scsipiconf.h: revision 1.120
sys/dev/usb/umass_scsipi.c: revision 1.44
sys/dev/scsipi/scsiconf.h: revision 1.57
sys/dev/scsipi/st_atapi.c: revision 1.29
sys/dev/scsipi/scsipi_base.c: revision 1.158
sys/dev/scsipi/st.c: revision 1.221
sys/dev/scsipi/scsipi_ioctl.c: revision 1.67
Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.
This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.
In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.
Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.298.2.4 03-Dec-2017  jdolecek update from HEAD
 1.298.2.3 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.298.2.2 23-Jun-2013  tls resync from head
 1.298.2.1 12-Sep-2012  tls Initial snapshot of work to eliminate 64K MAXPHYS. Basically works for
physio (I/O to raw devices); needs more doing to get it going with the
filesystems, but it shouldn't damage data.

All work's been done on amd64 so far. Not hard to add support to other
ports. If others want to pitch in, one very helpful thing would be to
sort out when and how IDE disks can do 128K or larger transfers, and
adjust the various PCI IDE (or at least ahcisata) drivers and wd.c
accordingly -- it would make testing much easier. Another very helpful
thing would be to implement a smart minphys() for RAIDframe along the
lines detailed in the MAXPHYS-NOTES file.
 1.301.2.1 18-May-2014  rmind sync with head
 1.305.2.2 10-Aug-2014  tls Rebase.
 1.305.2.1 07-Apr-2014  tls Be a little more clear and consistent about harvesting entropy from devices:

1) deprecate RND_FLAG_NO_ESTIMATE

2) define RND_FLAG_COLLECT_TIME, RND_FLAG_COLLECT_VALUE

3) define RND_FLAG_ESTIMATE_TIME, RND_FLAG_ESTIMATE_VALUE

4) define RND_FLAG_DEFAULT: RND_FLAG_COLLECT_TIME|
RND_FLAG_COLLECT_VALUE|RND_FLAG_ESTIMATE_TIME

5) Make entropy harvesting from environmental sensors a little more generic
and remove it from individual sensor drivers.

6) Remove individual open-coded delta-estimators for values from a few
places in the tree (uvm, environmental drivers).

7) 0 -> RND_FLAG_DEFAULT, actually gather entropy from various drivers
that had stubbed out code, other minor cleanups.
 1.308.2.1 11-Nov-2014  martin Pull up following revision(s) (requested by mlelstv in ticket #201):
sbin/dkctl/dkctl.8: revision 1.24
sbin/dkctl/dkctl.8: revision 1.25
sys/dev/scsipi/sd.c: revision 1.310
sys/dev/ata/wd.c: revision 1.415
sbin/dkctl/dkctl.c: revision 1.21
sys/dev/raidframe/rf_netbsdkintf.c: revision 1.315
sys/dev/ld.c: revision 1.78
sys/dev/vnd.c: revision 1.234
sys/dev/dksubr.c: revision 1.54
sys/sys/dkio.h: revision 1.20
sys/dev/dkwedge/dk.c: revision 1.74
Add ioctl to autodiscover wedges.
Implement DIOCMWEDGES ioctl that triggers wedge autodiscovery.
Also fix a reference counting bug and clean up some code.
support DIOCMWEDGES ioctl.
Add 'makewedges' option to autodiscover wedges from a changed label.
New sentence, new line. Bump date for previous.
 1.310.2.6 28-Aug-2017  skrll Sync with HEAD
 1.310.2.5 05-Feb-2017  skrll Sync with HEAD
 1.310.2.4 05-Dec-2016  skrll Sync with HEAD
 1.310.2.3 22-Sep-2015  skrll Sync with HEAD
 1.310.2.2 06-Jun-2015  skrll Sync with HEAD
 1.310.2.1 06-Apr-2015  skrll Sync with HEAD
 1.317.2.5 26-Apr-2017  pgoyette Sync with HEAD
 1.317.2.4 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.317.2.3 26-Jul-2016  pgoyette Rename LOCALCOUNT_INITIALIZER to DEVSW_MODULE_INIT. This better describes
what we're doing, and why.
 1.317.2.2 19-Jul-2016  pgoyette Instead of repeatedly typing the conditional initialization of the
.d_localcount members in the various {b,c}devsw, define an initializer
macro and use it. This also removes the need for defining new symbols
for each 'struct localcount'.

As suggested by riastradh@
 1.317.2.1 18-Jul-2016  pgoyette Rump drivers are always installed via devsw_attach() so we need to
always allocate a 'struct localcount' for these drivers whenever they
are built as modules.
 1.322.2.1 21-Apr-2017  bouyer Sync with HEAD
 1.324.6.2 11-Feb-2021  martin Pull up following revision(s) (requested by ryoon in ticket #1652):

sys/dev/scsipi/cd.c: revision 1.350
sys/dev/scsipi/sd.c: revision 1.331

PR 55986: Ryo Onodera: DK_BUSY must have mask as second argument. Make cd.c
consistent by also using __BIT()
 1.324.6.1 21-Jun-2017  snj Pull up following revision(s) (requested by mlelstv in ticket #53):
sys/dev/scsipi/atapiconf.c: revision 1.91
sys/dev/scsipi/cd.c: revision 1.341
sys/dev/scsipi/scsi_base.c: revision 1.92
sys/dev/scsipi/scsiconf.c: revision 1.280
sys/dev/scsipi/scsipi_base.c: revisions 1.176, 1.177
sys/dev/scsipi/sd.c: revision 1.325
sys/dev/scsipi/ss.c: revision 1.89
sys/dev/scsipi/st.c: revision 1.231
The atapibus detach path did hold the channel mutex while calling into autoconf,
which would trigger a panic when unplugging a USB ATAPI CDROM.
Align detach code for scsibus and atapibus to fix this.
Also avoid races when detaching devices by replacing callout_stop with
callout_halt.
--
pass config_detach error to caller.
 1.324.4.1 27-Apr-2017  pgoyette Restore all work from the former pgoyette-localcount branch (which is
now abandoned doe to cvs merge botch).

The branch now builds, and installs via anita. There are still some
problems (cgd is non-functional and all atf tests time-out) but they
will get resolved soon.
 1.325.6.3 21-Apr-2020  martin Sync with HEAD
 1.325.6.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.325.6.1 10-Jun-2019  christos Sync with HEAD
 1.325.4.1 20-Oct-2018  pgoyette Sync with head
 1.327.4.3 29-Aug-2022  martin Pull up following revision(s) (requested by mlelstv in ticket #1509):

sys/dev/scsipi/sdvar.h: revision 1.40
sys/dev/scsipi/sd.c: revision 1.335

Don't fetch data beyond end of inquiry buffer, which, here, is not
NUL-terminated.

Reduce target buffer to needed size (product name + NUL terminator).
 1.327.4.2 11-Feb-2021  martin Pull up following revision(s) (requested by ryoon in ticket #1203):

sys/dev/scsipi/cd.c: revision 1.350
sys/dev/scsipi/sd.c: revision 1.331

PR 55986: Ryo Onodera: DK_BUSY must have mask as second argument. Make cd.c
consistent by also using __BIT()
 1.327.4.1 21-Mar-2020  martin Pull up following revision(s) (requested by riastradh in ticket #795):

sys/dev/dksubr.c: revision 1.112
sys/arch/xen/xen/xbd_xenbus.c: revision 1.95
sys/dev/scsipi/sd.c: revision 1.328
sys/dev/dkvar.h: revision 1.32
sys/dev/ld.c: revision 1.109
sys/dev/cgd.c: revision 1.120
sys/dev/raidframe/rf_netbsdkintf.c: revision 1.380
sys/dev/ata/wd.c: revision 1.458

Add a flag to dk_dump for virtual disk devices.

If a disk is backed by a physical medium other than itself, such as
cgd(4), then it passes DK_DUMP_RECURSIVE to disable the recursion
detection for dk_dump.

If, however, a device represents a physical medium on its own, such
as wd(4), then it passes 0 instead.

With this, I can now dump to dk on cgd on dk on wd.
 1.328.4.1 20-Apr-2020  bouyer Sync with HEAD
 1.330.2.1 03-Apr-2021  thorpej Sync with HEAD.
 1.331.6.1 31-May-2021  cjep sync with head
 1.331.4.1 17-Jun-2021  thorpej Sync w/ HEAD.
 1.335.4.1 12-Sep-2024  martin Pull up following revision(s) (requested by rin in ticket #838):

sys/dev/scsipi/sd.c: revision 1.336

Don't try to discover wedges when the unit isn't online.
 1.336.2.1 02-Aug-2025  perseant Sync with HEAD
 1.21 18-Sep-2003  mycroft Merge the geometry and cache handling code for all direct access and optical
devices, as it's general to all SCSI MMC devices. In the process, remove
PQUIRK_NO_FLEX_PAGE.
 1.20 17-Sep-2003  mycroft Remove PQUIRK_BYTE5_ZERO.
 1.19 08-Sep-2003  mycroft Use the const shaker.
 1.18 03-Apr-2003  fvdl branches: 1.18.2;
Check RAW_PART against the media size instead of the disklabel.
Add the media size in 512-byte sectors to the softc, to avoid
some 64 bit computations. Bump the capacity stored in softcs
for disks to 64 bits.
 1.17 02-Oct-2002  thorpej Add trailing ; to CFATTACH_DECL.
 1.16 30-Sep-2002  thorpej Use CFATTACH_DECL().
 1.15 27-Sep-2002  thorpej Declare all cfattach structures const.
 1.14 09-Jan-2002  drochner -don't apply scsipi_strvis() twice, it tries to octal-print the string
terminating zero and uninitialized dada following
-use use the "vendor" field for the name which is used in the default
disklabel, the "product" field is meaningless or not existing for ATAPI
(serial number)
 1.13 15-Nov-2001  lukem don't need <sys/types.h> when including <sys/param.h>
 1.12 13-Nov-2001  lukem add RCSIDs
 1.11 18-Jul-2001  thorpej bzero -> memset
 1.10 14-May-2001  bouyer branches: 1.10.2;
Use SCSI/ATAPI common definition for MODE_{SELECT,SENSE}{,_BIG}. Define
functions to send theses commands in scsipi_base.c and use them instead
of ad-hoc commands setups.
 1.9 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.8 20-Mar-2001  augustss Extend quirks to 32 bits. Add two new ATAPI quirks, one to
deal with a bad byte5 of the capacity descriptor, one to
handle devices that lack the flex geometry page.
 1.7 09-Jun-2000  enami branches: 1.7.4;
Prevent a process being swapped out during I/O if the data buffer is
allocated on stack. This potential problem is noticed by Noriyuki Soda
and the idea and sample code to fix is given by Jason R. Thorpe.
 1.6 29-Mar-2000  simonb branches: 1.6.2;
Don't need to include <sys/conf.h> here.
 1.5 30-Sep-1999  thorpej branches: 1.5.2;
Cleanup the scsipi_xfer flags:
- `flags' is now gone, replaced with `xs_control' and `xs_status'.
- Massive cleanup of the control flags. Now we explicitly say that
a job is to complete asynchronously, rather than relying on side-effects,
and use a new flag to now that device discovery is being performed.
- Do SCSI device discovery interrupt-driven.
 1.4 11-Sep-1999  thorpej Implement detaching SCSI and ATAPI disks.
 1.3 31-Aug-1998  cgd kill the last remnants of __BROKEN_INDIRECT_CONFIG. (only the pica port
used it, and it's non-working and apparently slated for replacement.)
 1.2 05-Aug-1998  drochner Improve generation of default disklabels:
-store printable product ID in cd's and sd's softc, use it as "typename"
-for this, add a "destination buffer length" argument to scsipi_strvis()
-return ATAPI device type for ATAPI devices
 1.1 15-Jan-1998  cgd add support for an ATAPI attachment for 'sd'.
fix 'cd' driver's NCD_SCSI bogosity (was using testing wrong macro!)
clean up in various ways:
* make common atapi_mode_{sense,select}() functions.
* put ATAPI data structures in more sensible headers, split up by
device type.
* include headers a bit more carefully.
* pass flags to attachment-specific cd functions, and use them.
* get rid of SCSI bits in scsipi_base.h's scsipi_make_xs(), move
them into the correct place in scsi_base.c.
* fix minor typo in struct name in scsipiconf.h (which was apparently
never used except in a #define later in the same file).
* use __attribute__ to force 4-byte alignment for xs command store,
so that architectures trying to bus_space_write_multi_N() (where
N > 1) that data to a controller won't lose.
* clean up a few comments in typos, and make a few #defines easier to
understand/maintain.
* rename cd_link.h to cdvar.h (via repository copy). This is exactly
what a 'var' file is supposed to be.
 1.5.2.4 27-Mar-2001  bouyer Sync with HEAD.
 1.5.2.3 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.5.2.2 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.5.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.6.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.7.4.8 18-Oct-2002  nathanw Catch up to -current.
 1.7.4.7 28-Feb-2002  nathanw Catch up to -current.
 1.7.4.6 11-Jan-2002  nathanw More catchup.
 1.7.4.5 08-Jan-2002  nathanw Catch up to -current.
 1.7.4.4 14-Nov-2001  nathanw Catch up to -current.
 1.7.4.3 24-Aug-2001  nathanw Catch up with -current.
 1.7.4.2 21-Jun-2001  nathanw Catch up to -current.
 1.7.4.1 09-Apr-2001  nathanw Catch up with -current.
 1.10.2.3 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.10.2.2 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.10.2.1 03-Aug-2001  lukem update to -current
 1.18.2.1 03-Aug-2004  skrll Sync with HEAD
 1.37 18-Sep-2003  mycroft Merge the geometry and cache handling code for all direct access and optical
devices, as it's general to all SCSI MMC devices. In the process, remove
PQUIRK_NO_FLEX_PAGE.
 1.36 17-Sep-2003  mycroft If READ CAPACITY fails, try a READ FORMAT CAPACITIES. Separate this into
another function, always doing a page 0 MODE SENSE to get the block
descriptor if we use READ CAPACITY, and use SMS_DBD on the page 4/5 MODE
SENSE. This does one extra command in some cases, but it separates and
simplifies the code a little.

Why do we prefer READ CAPACITY over READ FORMAT CAPACITIES? Two reasons:
1) It's much older and is much less likely to have had its command code
abused, and is thus "safer" to try first. 2) ALL of my USB flash readers
and pen drives screw up their capacity descriptors -- mostly off-by-one
errors in the size (they return the maximum LBA number instead, a la READ
CAPACITY, which has *never* been how READ FORMAT CAPACITIES was documented
in the MMC spec), and one returns the "no media" code on slots that have
media inserted (despite returning almost-correct data otherwise)!

F*** me with a chainsaw.
 1.35 09-Sep-2003  mycroft Make similar changes as with cd_mode_{sense,select}().
 1.34 08-Sep-2003  mycroft Rework sd_scsibus_get_parms() a little. Try to get a block descriptor even
if our other MODE SENSEs fail. Use this code for the optical device case,
at least for now. (We could query the optical media type and do a table
lookup for the geometry, but why bother? Actually, why bother with geometry
at all, but I digress...)
 1.33 08-Sep-2003  mycroft RBC devices do in fact requre using 6-byte MODE SENSE/SELECT. This is totally
the wrong way to deal with that, but... I'll get back to it later.
 1.32 08-Sep-2003  mycroft Use the const shaker.
 1.31 07-Sep-2003  mycroft Set PQUIRK_ONLYBIG in the wdc-atapi frontend, obviating the need to ever test
the "bus type" for this.

Merge all the code in the SCSI and ATAPI backends for "cd" devices. All of
the mode page handling and whatnot is general to SCSI MMC devices, and should
never have been separated to begin with. This fixes a variety of problems,
and adds load/unload support for SCSI-attached devices.
 1.30 05-Sep-2003  mycroft In addition to skipping a variable-length list of block descriptors, we also
have to adjust for whether he have a "small" or "large" mode sense response.

sd_scsibus_getcache(), sd_scsibus_setache(): Set SMS_DBD so that we don't
have to deal with block descriptors here.

sd_scsibus_mode_sense(): Add a new parameter, "int *big", which tells the
caller whether it's a small or large header.

sd_scsibus_get_parms(), sd_scsibus_getcache(), sd_scsibus_setcache(): Use
the "big" value to find the data following the mode sense header correctly.

sd_scsibus_mode_sense(): Disable PQUIRK_NOBIGMODESENSE for now, as the
reasons for it were dubious and most likely fixed now.

sd_scsibus_get_parms(): Now that we're actually locating the right data,
check the returned page code to see if it's correct before using the mode
page info.

sd_scsibus_get_parms(): Get the rotational rate from the flex geometry page
too, if present.

XXX sd_scsibus_get_{opt,simplifiedparms}() need some work.
 1.29 05-Sep-2003  mycroft sd_scsibus_get_parms():
* Use the "block descriptor length" to find the beginning of the mode page
data. This is critical, since some devices do not return a block descriptor
at all. (XXX There is disgusting structure hackery here.)
* Always use scsipi_size() (i.e. READ CAPACITY) to get the total disk size.
* Get the rotational rate from the rigid geometry page.
* Don't read the block size from the block descriptor if there was none.
* Get the block size from the flexible geometry page if possible.
* If we don't like the way page 4 smells, try page 5, rather than just punting.
 1.28 03-Apr-2003  fvdl branches: 1.28.2;
Check RAW_PART against the media size instead of the disklabel.
Add the media size in 512-byte sectors to the softc, to avoid
some 64 bit computations. Bump the capacity stored in softcs
for disks to 64 bits.
 1.27 07-Mar-2003  drochner Add support for "simplified direct" (aka RBC) devices.

This reveals some ugly points in the sd driver, as
the PQUIRK_ONLYBIG flag (there are 6, 10, 12 and 16 byte
CDBs for r/w) and the handling of small/big mode
select/sense. This should be cleaned up.
Also the "long" struct disk_parms.disksize is worth
a thought...
 1.26 04-Oct-2002  soren Pass on indication of absent media in the general case as in the optical
device case.
 1.25 02-Oct-2002  thorpej Add trailing ; to CFATTACH_DECL.
 1.24 30-Sep-2002  thorpej Use CFATTACH_DECL().
 1.23 27-Sep-2002  thorpej Declare all cfattach structures const.
 1.22 09-Jan-2002  thorpej Add ioctls to get (DIOCGCACHE) and set (DIOCSCACHE) cache enables
on disks in a generic way. Implement these ioctls for SCSI disks.

This is not fully fleshed-out yet, but it allows people to experiment
with disk caches more easily.
 1.21 15-Nov-2001  lukem don't need <sys/types.h> when including <sys/param.h>
 1.20 13-Nov-2001  lukem add RCSIDs
 1.19 31-Aug-2001  augustss Add a new quirk, PQUIRK_NOBIGMODESENSE, for devices that need big
SCSI ops, but still fails on big mode sense.
Makes M-Sys DiskOnKey work.
 1.18 18-Jul-2001  thorpej bzero -> memset
 1.17 14-May-2001  bouyer branches: 1.17.2;
Use SCSI/ATAPI common definition for MODE_{SELECT,SENSE}{,_BIG}. Define
functions to send theses commands in scsipi_base.c and use them instead
of ad-hoc commands setups.
 1.16 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.15 09-Jun-2000  enami branches: 1.15.4;
Prevent a process being swapped out during I/O if the data buffer is
allocated on stack. This potential problem is noticed by Noriyuki Soda
and the idea and sample code to fix is given by Jason R. Thorpe.
 1.14 31-May-2000  augustss Add a method, scsipi_getgeom, to the adapter struct. If this method is
present it will be used to get the drive geometry if the MODE SENSE fails.
 1.13 30-May-2000  augustss Add a quirk, SDEV_ONLYBIG, which implies that the device cannot handle
the 6 byte versions of READ, WRITE, and MODE_SENSE.
This greatly simplifies the UFI (USB Floppy) handling.
 1.12 29-Mar-2000  simonb branches: 1.12.2;
Don't need to include <sys/conf.h> here.
 1.11 30-Sep-1999  thorpej branches: 1.11.2;
Cleanup the scsipi_xfer flags:
- `flags' is now gone, replaced with `xs_control' and `xs_status'.
- Massive cleanup of the control flags. Now we explicitly say that
a job is to complete asynchronously, rather than relying on side-effects,
and use a new flag to now that device discovery is being performed.
- Do SCSI device discovery interrupt-driven.
 1.10 11-Sep-1999  thorpej Implement detaching SCSI and ATAPI disks.
 1.9 26-Aug-1999  hannken Make `sc_ops->sdo_flush' synchronous. The flags `SDF_FLUSHING' and
`SDF_DIRTY' were never reset because `sddone' doesn't get called from
synchronous scsi commands.
 1.8 08-Oct-1998  thorpej Handle devices that have the SDEV_NOSYNCCACHE quirk.
 1.7 31-Aug-1998  cgd kill the last remnants of __BROKEN_INDIRECT_CONFIG. (only the pica port
used it, and it's non-working and apparently slated for replacement.)
 1.6 17-Aug-1998  mycroft Assign my copyrights to TNF.
 1.5 15-Aug-1998  thorpej Keep track if the disk has had done i/o, making it dirty. On last close,
or at shutdown, flush the cache if the disk is still dirty and it support
cache flushing. Fixes PRs #5588 (Jim Bernard) and #5589 (Justin Gibbs).
 1.4 12-Aug-1998  thorpej Run the shutdown hook w/ SCSI_AUTOCONF.
 1.3 05-Aug-1998  drochner Improve generation of default disklabels:
-store printable product ID in cd's and sd's softc, use it as "typename"
-for this, add a "destination buffer length" argument to scsipi_strvis()
-return ATAPI device type for ATAPI devices
 1.2 10-Jun-1998  thorpej Add a shutdown hook for SCSI-2 and higher disks that issues a SYNCHRONIZE CACHE
operation with address 0 length 0, which, according to the SCSI-2 spec, should
be interpreted as "synchronize all remaining blocks beginning at address 0".
 1.1 15-Jan-1998  cgd add support for an ATAPI attachment for 'sd'.
fix 'cd' driver's NCD_SCSI bogosity (was using testing wrong macro!)
clean up in various ways:
* make common atapi_mode_{sense,select}() functions.
* put ATAPI data structures in more sensible headers, split up by
device type.
* include headers a bit more carefully.
* pass flags to attachment-specific cd functions, and use them.
* get rid of SCSI bits in scsipi_base.h's scsipi_make_xs(), move
them into the correct place in scsi_base.c.
* fix minor typo in struct name in scsipiconf.h (which was apparently
never used except in a #define later in the same file).
* use __attribute__ to force 4-byte alignment for xs command store,
so that architectures trying to bus_space_write_multi_N() (where
N > 1) that data to a controller won't lose.
* clean up a few comments in typos, and make a few #defines easier to
understand/maintain.
* rename cd_link.h to cdvar.h (via repository copy). This is exactly
what a 'var' file is supposed to be.
 1.11.2.3 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.11.2.2 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.11.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.12.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.15.4.8 18-Oct-2002  nathanw Catch up to -current.
 1.15.4.7 28-Feb-2002  nathanw Catch up to -current.
 1.15.4.6 11-Jan-2002  nathanw More catchup.
 1.15.4.5 08-Jan-2002  nathanw Catch up to -current.
 1.15.4.4 14-Nov-2001  nathanw Catch up to -current.
 1.15.4.3 21-Sep-2001  nathanw Catch up to -current.
 1.15.4.2 24-Aug-2001  nathanw Catch up with -current.
 1.15.4.1 21-Jun-2001  nathanw Catch up to -current.
 1.17.2.4 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.17.2.3 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.17.2.2 13-Sep-2001  thorpej Update the kqueue branch to HEAD.
 1.17.2.1 03-Aug-2001  lukem update to -current
 1.28.2.1 03-Aug-2004  skrll Sync with HEAD
 1.42 27-Feb-2025  jakllsch sd(4): Translate discard to UNMAP (10) command
 1.41 27-Feb-2025  jakllsch Implement DIOCGSECTORALIGN for sd(4).

Use only READ CAPACITY 16 if peripheral claims conformance to SPC-3.
 1.40 28-Aug-2022  mlelstv branches: 1.40.10;
Don't fetch data beyond end of inquiry buffer, which, here, is not
NUL-terminated.

Reduce target buffer to needed size (product name + NUL terminator).
 1.39 19-Mar-2019  mlelstv branches: 1.39.4;
Set disk product name as disk type. The information can be queried through
drvctl(4).
 1.38 10-Dec-2016  mlelstv branches: 1.38.16;
Refactored sd and cd to use common disk subroutines.
 1.37 24-Aug-2015  pooka branches: 1.37.2;
would you like some freshly ground _KERNEL_OPT with that?
yes? excellent choice, sir/madam.
 1.36 13-Apr-2015  riastradh Convert sys/dev to use <sys/rndsource.h>.
 1.35 05-Sep-2014  matt branches: 1.35.2;
Don't nest structure definitions.
 1.34 02-Feb-2012  tls branches: 1.34.6;
Entropy-pool implementation move and cleanup.

1) Move core entropy-pool code and source/sink/sample management code
to sys/kern from sys/dev.

2) Remove use of NRND as test for presence of entropy-pool code throughout
source tree.

3) Remove use of RND_ENABLED in device drivers as microoptimization to
avoid expensive operations on disabled entropy sources; make the
rnd_add calls do this directly so all callers benefit.

4) Fix bug in recent rnd_add_data()/rnd_add_uint32() changes that might
have lead to slight entropy overestimation for some sources.

5) Add new source types for environmental sensors, power sensors, VM
system events, and skew between clocks, with a sample implementation
for each.

ok releng to go in before the branch due to the difficulty of later
pullup (widespread #ifdef removal and moved files). Tested with release
builds on amd64 and evbarm and live testing on amd64.
 1.33 19-Nov-2011  tls branches: 1.33.2;
First step of random number subsystem rework described in
<20111022023242.BA26F14A158@mail.netbsd.org>. This change includes
the following:

An initial cleanup and minor reorganization of the entropy pool
code in sys/dev/rnd.c and sys/dev/rndpool.c. Several bugs are
fixed. Some effort is made to accumulate entropy more quickly at
boot time.

A generic interface, "rndsink", is added, for stream generators to
request that they be re-keyed with good quality entropy from the pool
as soon as it is available.

The arc4random()/arc4randbytes() implementation in libkern is
adjusted to use the rndsink interface for rekeying, which helps
address the problem of low-quality keys at boot time.

An implementation of the FIPS 140-2 statistical tests for random
number generator quality is provided (libkern/rngtest.c). This
is based on Greg Rose's implementation from Qualcomm.

A new random stream generator, nist_ctr_drbg, is provided. It is
based on an implementation of the NIST SP800-90 CTR_DRBG by
Henric Jungheim. This generator users AES in a modified counter
mode to generate a backtracking-resistant random stream.

An abstraction layer, "cprng", is provided for in-kernel consumers
of randomness. The arc4random/arc4randbytes API is deprecated for
in-kernel use. It is replaced by "cprng_strong". The current
cprng_fast implementation wraps the existing arc4random
implementation. The current cprng_strong implementation wraps the
new CTR_DRBG implementation. Both interfaces are rekeyed from
the entropy pool automatically at intervals justifiable from best
current cryptographic practice.

In some quick tests, cprng_fast() is about the same speed as
the old arc4randbytes(), and cprng_strong() is about 20% faster
than rnd_extract_data(). Performance is expected to improve.

The AES code in src/crypto/rijndael is no longer an optional
kernel component, as it is required by cprng_strong, which is
not an optional kernel component.

The entropy pool output is subjected to the rngtest tests at
startup time; if it fails, the system will reboot. There is
approximately a 3/10000 chance of a false positive from these
tests. Entropy pool _input_ from hardware random numbers is
subjected to the rngtest tests at attach time, as well as the
FIPS continuous-output test, to detect bad or stuck hardware
RNGs; if any are detected, they are detached, but the system
continues to run.

A problem with rndctl(8) is fixed -- datastructures with
pointers in arrays are no longer passed to userspace (this
was not a security problem, but rather a major issue for
compat32). A new kernel will require a new rndctl.

The sysctl kern.arandom() and kern.urandom() nodes are hooked
up to the new generators, but the /dev/*random pseudodevices
are not, yet.

Manual pages for the new kernel interfaces are forthcoming.
 1.32 10-Apr-2009  dyoung branches: 1.32.12;
Use a PMF shutdown hook instead of shutdownhook_establish(). This ought
to stop the crash during shutdown that yamt@ reports in kern/41182.
 1.31 16-Jul-2008  drochner branches: 1.31.2; 1.31.8;
split device/softc for sd (tested with a USB stick)
 1.30 28-Apr-2008  martin branches: 1.30.2; 1.30.4; 1.30.6;
Remove clause 3 and 4 from TNF licenses
 1.29 09-Jul-2007  ad branches: 1.29.28; 1.29.30; 1.29.32;
Merge some of the less invasive changes from the vmlocking branch:

- kthread, callout, devsw API changes
- select()/poll() improvements
- miscellaneous MT safety improvements
 1.28 11-Dec-2005  christos branches: 1.28.30; 1.28.32;
merge ktrace-lwp.
 1.27 15-Oct-2005  yamt - change the way to specify a bufq strategy. (by string rather than by number)
- rather than embedding bufq_state in driver softc,
have a pointer to the former.
- move bufq related functions from kern/subr_disk.c to kern/subr_bufq.c.
- rename method to strategy for consistency.
- move some definitions which don't need to be exposed to the rest of kernel
from sys/bufq.h to sys/bufq_impl.h.
(is it better to move it to kern/ or somewhere?)
- fix some obvious breakage in dev/qbus/ts.c. (not tested)
 1.26 01-Feb-2005  reinoud branches: 1.26.6;
Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.25 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.24 01-Oct-2004  thorpej branches: 1.24.4;
Don't need the lock in the softc anymore.
 1.23 17-Sep-2004  mycroft Do not manipulate xs->bp in "generic" code -- do it only in the psw_done
routine. As part of this, pass down our pre-parsed error code -- though this
interface will probably change later to accomodate better error handling.
 1.22 27-Aug-2004  bouyer Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
Havard Eidnes's analysis of this problem is that the scsipi_xfer pool is
competing for resources with other pools, including the the inode and vnode
pools which can grow quite large.

*_scsipi_cmd(): don't biodone the buffer if scsipi_make_xs() fails, let the
caller deal with the problem
start function of block devices drivers: dequeue the buffer after the
scsipi_command() call. If scsipi_command() fails with ENOMEM don't dequeue
the buffer, and schedule a callout to call the start function after
some delay.
scsipi_init(): prime the scsipi_xfer_pool with one page. This ensure that
there is always some scsipi_xfer to play with. If scsipi_command() fails
because of pool_get(), we're sure there will be resources available later,
when the pending commands have completed.

Reviewed by Jason Thorpe and Havard Eidnes.
Todo: remove the "unable to allocate scsipi_xfer" and "not queued, error %d"
printfs, but I choose to keep them for now, to help make sure the code does
what it should.
 1.21 24-Feb-2004  wiz branches: 1.21.2;
parameter with two es. From Peter Postma.
 1.20 18-Sep-2003  mycroft Merge the geometry and cache handling code for all direct access and optical
devices, as it's general to all SCSI MMC devices. In the process, remove
PQUIRK_NO_FLEX_PAGE.
 1.19 03-Apr-2003  fvdl branches: 1.19.2;
Check RAW_PART against the media size instead of the disklabel.
Add the media size in 512-byte sectors to the softc, to avoid
some 64 bit computations. Bump the capacity stored in softcs
for disks to 64 bits.
 1.18 06-Jan-2003  matt Add multiple inclusion protection.
 1.17 16-Jul-2002  hannken Implement a new device buffer queue interface.
One basic struct, a function to setup a queue with a specific strategy and
three macros to put buf's into the queue, get and remove the next buf or
get the next buf without removal.

The BUFQ_XXX interface will be removed in the future.
The B_ORDERED flag is not longer supported.

Approved by: Jason R. Thorpe <thorpej@wasabisystems.com>
 1.16 09-Jan-2002  thorpej branches: 1.16.8;
Add ioctls to get (DIOCGCACHE) and set (DIOCSCACHE) cache enables
on disks in a generic way. Implement these ioctls for SCSI disks.

This is not fully fleshed-out yet, but it allows people to experiment
with disk caches more easily.
 1.15 23-May-2001  chs branches: 1.15.2;
fix typo in previous revision so that the scsi code compiles again.
 1.14 22-May-2001  mjacob Make SDRETRIES an option you can dig out of opt_scsi.h.

Add SD_IO_TIMEOUT (an option in opt_scsi.h) also, which defaults to the
normal 60 * 1000 timeout for normal read/write commamds. This allows you
to shorten or lengthen the timeout as needed.
 1.13 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.12 23-May-2000  bouyer branches: 1.12.6;
sd_interpret_sense() can be called from interrupt context; call scsipi_start()
with XS_CTL_ASYNC if the failed command was called with XS_CTL_ASYNC.
Add a SDF_RESTART flag to keep state, cleared in sddone().
A mounted disk can now spin down, it will propely spin up at the next access.
 1.11 22-Jan-2000  drochner include "rnd.h" explicitely since the size of sd_softc depends on it
(sd_scsi.c and sd_atapi.c missed it, leading to random memory corruption)
 1.10 21-Jan-2000  thorpej Update for sys/buf.h/disksort_*() changes.
 1.9 11-Sep-1999  thorpej branches: 1.9.2;
Implement detaching SCSI and ATAPI disks.
 1.8 26-Aug-1999  hannken Make `sc_ops->sdo_flush' synchronous. The flags `SDF_FLUSHING' and
`SDF_DIRTY' were never reset because `sddone' doesn't get called from
synchronous scsi commands.
 1.7 17-Aug-1998  mycroft branches: 1.7.6;
Assign my copyrights to TNF.
 1.6 15-Aug-1998  thorpej Keep track if the disk has had done i/o, making it dirty. On last close,
or at shutdown, flush the cache if the disk is still dirty and it support
cache flushing. Fixes PRs #5588 (Jim Bernard) and #5589 (Justin Gibbs).
 1.5 05-Aug-1998  drochner Improve generation of default disklabels:
-store printable product ID in cd's and sd's softc, use it as "typename"
-for this, add a "destination buffer length" argument to scsipi_strvis()
-return ATAPI device type for ATAPI devices
 1.4 10-Jun-1998  thorpej Add a shutdown hook for SCSI-2 and higher disks that issues a SYNCHRONIZE CACHE
operation with address 0 length 0, which, according to the SCSI-2 spec, should
be interpreted as "synchronize all remaining blocks beginning at address 0".
 1.3 13-Feb-1998  enami Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.2 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.1 15-Jan-1998  cgd add support for an ATAPI attachment for 'sd'.
fix 'cd' driver's NCD_SCSI bogosity (was using testing wrong macro!)
clean up in various ways:
* make common atapi_mode_{sense,select}() functions.
* put ATAPI data structures in more sensible headers, split up by
device type.
* include headers a bit more carefully.
* pass flags to attachment-specific cd functions, and use them.
* get rid of SCSI bits in scsipi_base.h's scsipi_make_xs(), move
them into the correct place in scsi_base.c.
* fix minor typo in struct name in scsipiconf.h (which was apparently
never used except in a #define later in the same file).
* use __attribute__ to force 4-byte alignment for xs command store,
so that architectures trying to bus_space_write_multi_N() (where
N > 1) that data to a controller won't lose.
* clean up a few comments in typos, and make a few #defines easier to
understand/maintain.
* rename cd_link.h to cdvar.h (via repository copy). This is exactly
what a 'var' file is supposed to be.
 1.7.6.1 23-Jan-2000  he Pull up revision 1.11 (requested by drochner):
Fix a size inconsistency of "struct sd_softc" if the "random"
pseudo-device is enabled, leading to data corruption.
 1.9.2.2 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.9.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.12.6.5 07-Jan-2003  thorpej Sync with HEAD.
 1.12.6.4 01-Aug-2002  nathanw Catch up to -current.
 1.12.6.3 28-Feb-2002  nathanw Catch up to -current.
 1.12.6.2 11-Jan-2002  nathanw More catchup.
 1.12.6.1 21-Jun-2001  nathanw Catch up to -current.
 1.15.2.2 06-Sep-2002  jdolecek sync kqueue branch with HEAD
 1.15.2.1 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.16.8.1 20-Jul-2002  gehenna catch up with -current.
 1.19.2.6 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.19.2.5 19-Oct-2004  skrll Sync with HEAD
 1.19.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.19.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.19.2.2 03-Sep-2004  skrll Sync with HEAD
 1.19.2.1 03-Aug-2004  skrll Sync with HEAD
 1.21.2.1 11-Sep-2004  he Pull up revision 1.22 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.24.4.1 29-Apr-2005  kent sync with -current
 1.26.6.2 03-Sep-2007  yamt sync with head.
 1.26.6.1 21-Jun-2006  yamt sync with head.
 1.28.32.1 11-Jul-2007  mjf Sync with head.
 1.28.30.1 01-Jul-2007  ad Adapt to callout API change.
 1.29.32.2 04-May-2009  yamt sync with head.
 1.29.32.1 16-May-2008  yamt sync with head.
 1.29.30.1 18-May-2008  yamt sync with head.
 1.29.28.2 28-Sep-2008  mjf Sync with HEAD.
 1.29.28.1 02-Jun-2008  mjf Sync with HEAD.
 1.30.6.1 19-Oct-2008  haad Sync with HEAD.
 1.30.4.1 18-Jul-2008  simonb Sync with head.
 1.30.2.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.31.8.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.31.2.1 28-Apr-2009  skrll Sync with HEAD.
 1.32.12.1 17-Apr-2012  yamt sync with head
 1.33.2.1 18-Feb-2012  mrg merge to -current.
 1.34.6.1 03-Dec-2017  jdolecek update from HEAD
 1.35.2.3 05-Feb-2017  skrll Sync with HEAD
 1.35.2.2 22-Sep-2015  skrll Sync with HEAD
 1.35.2.1 06-Jun-2015  skrll Sync with HEAD
 1.37.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.38.16.1 10-Jun-2019  christos Sync with HEAD
 1.39.4.1 29-Aug-2022  martin Pull up following revision(s) (requested by mlelstv in ticket #1509):

sys/dev/scsipi/sdvar.h: revision 1.40
sys/dev/scsipi/sd.c: revision 1.335

Don't fetch data beyond end of inquiry buffer, which, here, is not
NUL-terminated.

Reduce target buffer to needed size (product name + NUL terminator).
 1.40.10.1 02-Aug-2025  perseant Sync with HEAD
 1.52 09-Sep-2021  riastradh sys/dev: Memset zero before copyout.

Just in case of uninitialized padding which would lead to kernel
stack disclosure. If the compiler can prove the memset redundant
then it can optimize it away; otherwise better safe than sorry.

I think the iwi(4), mcd(4), and ses(4) changes actually plug leaks;
the raidframe(4) change probably doesn't (but doesn't hurt).
 1.51 08-Mar-2019  msaitoh branches: 1.51.4;
s/ are are / are /
s/ a a / a /
 1.50 20-Nov-2016  mlelstv branches: 1.50.8; 1.50.16;
Make scsipi framework MPSAFE.

Data structures are now protected by a per-adapter mutex at IPL_BIO
that is created by the scsibus or atapibus instance when the adapter
is configured.
The enable reference counter and the channel freeze counter which are
currently used by HBA code before the adapter is configured, are made
atomic.
The target drivers are now all tagged as D_MPSAFE.

Almost all HBA drivers still require the kernel lock to present,
so all callbacks into HBA code are still protected by kernel lock
unless the driver is tagged as SCSIPI_ADAPT_MPSAFE.

TODO: refactor sd and cd to use dksubr.
 1.49 14-Jul-2016  msaitoh branches: 1.49.2;
- Use aprint*() more in xxx_attach().
- Add missing aprint_naive("\n").
- Remove extra spaces and tabs.
- KNF.
 1.48 24-Aug-2015  pooka would you like some freshly ground _KERNEL_OPT with that?
yes? excellent choice, sir/madam.
 1.47 25-Jul-2014  dholland branches: 1.47.4;
Add d_discard to all struct cdevsw instances I could find.

All have been set to "nodiscard"; some should get a real implementation.
 1.46 16-Mar-2014  dholland branches: 1.46.2;
Change (mostly mechanically) every cdevsw/bdevsw I can find to use
designated initializers.

I have not built every extant kernel so I have probably broken at
least one build; however I've also found and fixed some wrong
cdevsw/bdevsw entries so even if so I think we come out ahead.
 1.45 07-Apr-2013  wiz branches: 1.45.4;
Add detach support. From martin@.
"Works for me."

Addresses PR 44283.
 1.44 27-Oct-2012  chs split device_t/softc for all remaining drivers.
replace "struct device *" with "device_t".
use device_xname(), device_unit(), etc.
 1.43 17-Jul-2011  joerg branches: 1.43.2; 1.43.8; 1.43.12; 1.43.14;
Retire varargs.h support. Move machine/stdarg.h logic into MI
sys/stdarg.h and expect compiler to provide proper builtins, defaulting
to the GCC interface. lint still has a special fallback.
Reduce abuse of _BSD_VA_LIST_ by defining __va_list by default and
derive va_list as required by standards.
 1.42 12-May-2009  cegger struct device * -> device_t, no functional changes intended.
 1.41 12-May-2009  cegger struct cfdata * -> cfdata_t, no functional changes intended.
 1.40 08-Jun-2008  tsutsui branches: 1.40.12;
Use device_lookup_private() rather than using cd_devs[] directly to get softc.

XXX maybe we should change a type of cd_devs[] in struct cfdriver
from (void *) to device_t.
 1.39 05-Apr-2008  cegger branches: 1.39.2; 1.39.4; 1.39.6;
use aprint_*_dev and device_xname
 1.38 04-Mar-2007  christos branches: 1.38.36;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.37 16-Nov-2006  christos branches: 1.37.4;
__unused removal on arguments; approved by core.
 1.36 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.35 03-Sep-2006  christos branches: 1.35.2; 1.35.4;
add missing initializer
 1.34 14-Apr-2006  christos Coverity CID 1100: Avoid NULL pointer dereferences.
 1.33 30-Mar-2006  thorpej Use device_private().
 1.32 11-Dec-2005  christos branches: 1.32.4; 1.32.6; 1.32.8; 1.32.10; 1.32.12;
merge ktrace-lwp.
 1.31 30-May-2005  christos branches: 1.31.2;
- remove bogus casts
- add more const
 1.30 27-Feb-2005  perry nuke trailing whitespace
 1.29 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.28 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.27 31-Jan-2005  reinoud As part of cleaning up sys/scsipi, replace all u_char by uint8_t and
replace all `short' with int16_t.
 1.26 18-Sep-2004  mycroft branches: 1.26.4; 1.26.6;
Minor rearrangement. Whitespace and #include cleanup.
 1.25 17-Sep-2004  mycroft Remove the "xfer" argument to scsipi_command().
 1.24 09-Sep-2004  bouyer Make the xxstart() functions reentrant again, as some drivers HBA can call
scsipi_done() from their scsipi_request().
For this, add a struct scsipi_xfer * argument to scsipi_command().
If not NULL scsipi_command() will use this to enqueue this xfer, otherwise
it'll try to allocate a new one. This scsipi_xfer has to be allocated
and initialised by scsipi_make_xs() or equivalent.
In xxstart(), allocate a scsipi_xfer using scsipi_make_xs(), and if not NULL,
dequeue the buffer before calling scsipi_command(). This makes sure that
scsipi_command() will not fail, and also makes sure that xxstart() won't
be called again between the BUFQ_PEEK() and BUFQ_GET().

Fix "dequeued wrong buf" panics reported by Juergen Hannken-Illjes in
private mail and Andreas Wrede on current-users@.
Thanks to Jason Thorpe and Chuck Silver for review, and Andreas Wrede for
testing the patch.
 1.23 21-Aug-2004  thorpej Use ANSI function decls and make use of static.
 1.22 29-Jun-2003  fvdl branches: 1.22.2; 1.22.4;
Back out the lwp/ktrace changes. They contained a lot of colateral damage,
and need to be examined and discussed more.
 1.21 28-Jun-2003  darrenr Pass lwp pointers throughtout the kernel, as required, so that the lwpid can
be inserted into ktrace records. The general change has been to replace
"struct proc *" with "struct lwp *" in various function prototypes, pass
the lwp through and use l_proc to get the process pointer when needed.

Bump the kernel rev up to 1.6V
 1.20 07-Feb-2003  mjacob kern/20231- pass the correct argument (arg_addr) on thru to scsipi_do_ioctl,
not the locally dereferenced fetch.
 1.19 20-Jan-2003  simonb The Double-Semi-Colon Police.
 1.18 23-Oct-2002  jdolecek merge kqueue branch into -current

kqueue provides a stateful and efficient event notification framework
currently supported events include socket, file, directory, fifo,
pipe, tty and device changes, and monitoring of processes and signals

kqueue is supported by all writable filesystems in NetBSD tree
(with exception of Coda) and all device drivers supporting poll(2)

based on work done by Jonathan Lemon for FreeBSD
initial NetBSD port done by Luke Mewburn and Jason Thorpe
 1.17 02-Oct-2002  thorpej Add trailing ; to CFATTACH_DECL.
 1.16 30-Sep-2002  thorpej Use CFATTACH_DECL().
 1.15 27-Sep-2002  thorpej Declare all cfattach structures const.
 1.14 06-Sep-2002  gehenna Merge the gehenna-devsw branch into the trunk.

This merge changes the device switch tables from static array to
dynamically generated by config(8).

- All device switches is defined as a constant structure in device drivers.

- The new grammer ``device-major'' is introduced to ``files''.

device-major <prefix> char <num> [block <num>] [<rules>]

- All device major numbers must be listed up in port dependent majors.<arch>
by using this grammer.

- Added the new naming convention.
The name of the device switch must be <prefix>_[bc]devsw for auto-generation
of device switch tables.

- The backward compatibility of loading block/character device
switch by LKM framework is broken. This is necessary to convert
from block/character device major to device name in runtime and vice versa.

- The restriction to assign device major by LKM is completely removed.
We don't need to reserve LKM entries for dynamic loading of device switch.

- In compile time, device major numbers list is packed into the kernel and
the LKM framework will refer it to assign device major number dynamically.
 1.13 15-Nov-2001  lukem branches: 1.13.8; 1.13.10;
don't need <sys/types.h> when including <sys/param.h>
 1.12 13-Nov-2001  lukem add RCSIDs
 1.11 18-Jul-2001  thorpej branches: 1.11.2;
bcopy -> memcpy
 1.10 18-Jul-2001  thorpej bzero -> memset
 1.9 25-Apr-2001  bouyer branches: 1.9.2;
Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.8 08-Aug-2000  mjacob branches: 1.8.2; 1.8.4;
Don't attempt to actually read SAF-TE temperature objects- nobody seems
to be obeying the original spec as to what the numeric value means.

Temperature flags are unaffected- these are still the 'pseudo-thermometers'
and overtemp/undertemp warnings will be caught and translated to SES objects
here.
 1.7 08-Jul-2000  sommerfeld printf format paranoia
 1.6 22-May-2000  thorpej Remove an used variable and add a missing `}' so that this compiles.
 1.5 14-May-2000  dante Change scsipi_inquiry_data strucure to be ANSI SPC-2 rev16 compliant
 1.4 20-Feb-2000  mjacob Oops...I forgot that the GEM-2 chip is so lame that if you don't tell
it exactly the right amount of data to transfer for WRITE BUFFER/Global
it will stay stuck in command phase (causing a command phase overrun).
 1.3 21-Jan-2000  mjacob slight change for platform independence
 1.2 21-Jan-2000  mjacob cleanup SAF-TE attachment code
 1.1 20-Jan-2000  mjacob add SES driver
 1.8.4.7 11-Nov-2002  nathanw Catch up to -current
 1.8.4.6 18-Oct-2002  nathanw Catch up to -current.
 1.8.4.5 17-Sep-2002  nathanw Catch up to -current.
 1.8.4.4 08-Jan-2002  nathanw Catch up to -current.
 1.8.4.3 14-Nov-2001  nathanw Catch up to -current.
 1.8.4.2 24-Aug-2001  nathanw Catch up with -current.
 1.8.4.1 21-Jun-2001  nathanw Catch up to -current.
 1.8.2.2 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.8.2.1 08-Aug-2000  bouyer file ses.c was added on branch thorpej_scsipi on 2000-11-20 09:59:28 +0000
 1.9.2.3 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.9.2.2 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.9.2.1 03-Aug-2001  lukem update to -current
 1.11.2.2 26-Sep-2001  fvdl * add a VCLONED vnode flag that indicates a vnode representing a cloned
device.
* rename REVOKEALL to REVOKEALIAS, and add a REVOKECLONE flag, to pass
to VOP_REVOKE
* the revoke system call will revoke all aliases, as before, but not the
clones
* vdevgone is called when detaching a device, so make it use REVOKECLONE
to get rid of all clones as well
* clean up all uses of VOP_OPEN wrt. locking.
* add a few VOPS to spec_vnops that need to do something when it's a
clone vnode (access and getattr)
* add a copy of the vnode vattr structure of the original 'master' vnode
to the specinfo of a cloned vnode. could possibly redirect getattr to
the 'master' vnode, but this has issues with revoke
* add a vdev_reassignvp function that disassociates a vnode from its
original device, and reassociates it with the specified dev_t. to be
used by cloning devices only, in case a new minor is allocated.
* change all direct references in drivers to v_devcookie and v_rdev
to vdev_privdata(vp) and vdev_rdev(vp). for diagnostic purposes
when debugging race conditions that still exist wrt. locking and
revoking vnodes.
* make the locking state of a vnode consistent when passed to
d_open and d_close (unlocked). locked would be better, but has
some deadlock issues
 1.11.2.1 07-Sep-2001  thorpej Commit my "devvp" changes to the thorpej-devvp branch. This
replaces the use of dev_t in most places with a struct vnode *.

This will form the basic infrastructure for real cloning device
support (besides being architecurally cleaner -- it'll be good
to get away from using numbers to represent objects).
 1.13.10.2 16-Jun-2003  grant Pull up revision 1.20 (requested by mjacob in ticket #1162):

kern/20231- pass the correct argument (arg_addr) on thru to scsipi_do_ioctl,
not the locally dereferenced fetch.
 1.13.10.1 16-Jun-2003  grant Pull up revision 1.19 (requested by mjacob in ticket #1162):

The Double-Semi-Colon Police.
 1.13.8.1 16-May-2002  gehenna Add the character device switch.
 1.22.4.1 11-Sep-2004  he Pull up revision 1.24 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.22.2.6 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.22.2.5 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.22.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.22.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.22.2.2 25-Aug-2004  skrll Sync with HEAD.
 1.22.2.1 02-Jul-2003  darrenr Apply the aborted ktrace-lwp changes to a specific branch. This is just for
others to review, I'm concerned that patch fuziness may have resulted in some
errant code being generated but I'll look at that later by comparing the diff
from the base to the branch with the file I attempt to apply to it. This will,
at the very least, put the changes in a better context for others to review
them and attempt to tinker with removing passing of 'struct lwp' through
the kernel.
 1.26.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.26.4.1 29-Apr-2005  kent sync with -current
 1.31.2.3 03-Sep-2007  yamt sync with head.
 1.31.2.2 30-Dec-2006  yamt sync with head.
 1.31.2.1 21-Jun-2006  yamt sync with head.
 1.32.12.2 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.32.12.1 31-Mar-2006  tron Merge 2006-03-31 NetBSD-current into the "peter-altq" branch.
 1.32.10.1 19-Apr-2006  elad sync with head.
 1.32.8.3 03-Sep-2006  yamt sync with head.
 1.32.8.2 24-May-2006  yamt sync with head.
 1.32.8.1 01-Apr-2006  yamt sync with head.
 1.32.6.1 22-Apr-2006  simonb Sync with head.
 1.32.4.1 09-Sep-2006  rpaulo sync with head
 1.35.4.2 10-Dec-2006  yamt sync with head.
 1.35.4.1 22-Oct-2006  yamt sync with head
 1.35.2.1 18-Nov-2006  ad Sync with head.
 1.37.4.1 12-Mar-2007  rmind Sync with HEAD.
 1.38.36.3 29-Jun-2008  mjf Sync with HEAD.
 1.38.36.2 02-Jun-2008  mjf Sync with HEAD.
 1.38.36.1 05-Apr-2008  mjf - add "file-system DEVFS" and "pseudo-device devfsctl" to conf/std seeing
as these are always needed.

- convert many, many drivers over to the New Devfs World Order. For a
list of device drivers yet to be converted see,
http://www.netbsd.org/~mjf/devfs-todo.html.

- add a new device_unregister_all(device_t) function to remove all device
names associated with a device_t, which saves us having to construct
device names when the driver is detached.

- add a DEV_AUDIO type for devices.
 1.39.6.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.39.4.2 16-May-2009  yamt sync with head
 1.39.4.1 04-May-2009  yamt sync with head.
 1.39.2.1 17-Jun-2008  yamt sync with head.
 1.40.12.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.43.14.1 29-Apr-2013  riz Pull up following revision(s) (requested by jakllsch in ticket #878):
sys/dev/scsipi/ses.c: revision 1.45
Add detach support. From martin@.
"Works for me."
Addresses PR 44283.
 1.43.12.4 03-Dec-2017  jdolecek update from HEAD
 1.43.12.3 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.43.12.2 23-Jun-2013  tls resync from head
 1.43.12.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.43.8.1 29-Apr-2013  riz Pull up following revision(s) (requested by jakllsch in ticket #878):
sys/dev/scsipi/ses.c: revision 1.45
Add detach support. From martin@.
"Works for me."
Addresses PR 44283.
 1.43.2.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.43.2.1 30-Oct-2012  yamt sync with head
 1.45.4.1 18-May-2014  rmind sync with head
 1.46.2.1 10-Aug-2014  tls Rebase.
 1.47.4.3 05-Dec-2016  skrll Sync with HEAD
 1.47.4.2 05-Oct-2016  skrll Sync with HEAD
 1.47.4.1 22-Sep-2015  skrll Sync with HEAD
 1.49.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.50.16.1 10-Jun-2019  christos Sync with HEAD
 1.50.8.1 21-Jun-2023  martin Pull up following revision(s) (requested by riastradh in ticket #1835):

sys/dev/pci/if_iwi.c: revision 1.117
sys/dev/raidframe/rf_netbsdkintf.c: revision 1.401
sys/dev/scsipi/ses.c: revision 1.52
sys/dev/isa/mcd.c: revision 1.121
(all via patch)

sys/dev: Memset zero before copyout.

Just in case of uninitialized padding which would lead to kernel
stack disclosure. If the compiler can prove the memset redundant
then it can optimize it away; otherwise better safe than sorry.

I think the iwi(4), mcd(4), and ses(4) changes actually plug leaks;
the raidframe(4) change probably doesn't (but doesn't hurt).
 1.51.4.1 03-Aug-2022  martin Pull up following revision(s) (requested by riastradh in ticket #1485):

sys/dev/pci/if_iwi.c: revision 1.117
sys/dev/raidframe/rf_netbsdkintf.c: revision 1.401
sys/dev/scsipi/ses.c: revision 1.52
sys/dev/isa/mcd.c: revision 1.121

sys/dev: Memset zero before copyout.

Just in case of uninitialized padding which would lead to kernel
stack disclosure. If the compiler can prove the memset redundant
then it can optimize it away; otherwise better safe than sorry.

I think the iwi(4), mcd(4), and ses(4) changes actually plug leaks;
the raidframe(4) change probably doesn't (but doesn't hurt).
 1.4 06-Sep-2015  dholland More on PR 41200: headers that declare ioctls should include sys/ioccom.h.
This covers (I think) all the MI headers outside of external/ (and dist/).
 1.3 11-Dec-2005  christos branches: 1.3.120; 1.3.140;
merge ktrace-lwp.
 1.2 27-Feb-2005  perry nuke trailing whitespace
 1.1 20-Jan-2000  mjacob branches: 1.1.6; 1.1.30; 1.1.38; 1.1.40;
add SES driver
 1.1.40.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.38.1 29-Apr-2005  kent sync with -current
 1.1.30.1 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.1.6.2 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.1.6.1 20-Jan-2000  bouyer file ses.h was added on branch thorpej_scsipi on 2000-11-20 09:59:28 +0000
 1.3.140.1 22-Sep-2015  skrll Sync with HEAD
 1.3.120.1 03-Dec-2017  jdolecek update from HEAD
 1.91 23-Feb-2022  andvar fix various typos in comments, mainly immediatly/immediately/,
as well shared and recently fixed typos in OpenBSD code by Jonathan Grey.
 1.90 29-Sep-2020  msaitoh s/occurence/occurrence/
 1.89 17-Jun-2017  mlelstv The atapibus detach path did hold the channel mutex while calling into autoconf,
which would trigger a panic when unplugging a USB ATAPI CDROM.

Align detach code for scsibus and atapibus to fix this.

Also avoid races when detaching devices by replacing callout_stop with
callout_halt.
 1.88 20-Nov-2016  mlelstv branches: 1.88.8;
Make scsipi framework MPSAFE.

Data structures are now protected by a per-adapter mutex at IPL_BIO
that is created by the scsibus or atapibus instance when the adapter
is configured.
The enable reference counter and the channel freeze counter which are
currently used by HBA code before the adapter is configured, are made
atomic.
The target drivers are now all tagged as D_MPSAFE.

Almost all HBA drivers still require the kernel lock to present,
so all callbacks into HBA code are still protected by kernel lock
unless the driver is tagged as SCSIPI_ADAPT_MPSAFE.

TODO: refactor sd and cd to use dksubr.
 1.87 20-Nov-2016  pgoyette Avoid calling bufq_free() from critical code sections.
 1.86 25-Jul-2014  dholland branches: 1.86.4; 1.86.8;
Add d_discard to all struct cdevsw instances I could find.

All have been set to "nodiscard"; some should get a real implementation.
 1.85 16-Mar-2014  dholland branches: 1.85.2;
Change (mostly mechanically) every cdevsw/bdevsw I can find to use
designated initializers.

I have not built every extant kernel so I have probably broken at
least one build; however I've also found and fixed some wrong
cdevsw/bdevsw entries so even if so I think we come out ahead.
 1.84 28-Feb-2012  mbalmer branches: 1.84.2; 1.84.4;
one line comments on one line.
 1.83 28-Feb-2012  mbalmer Unbreak the build.
 1.82 28-Feb-2012  mbalmer Remove a pointless #if 0, compiler will eliminate the code anways.
Noticed by skrll.
 1.81 28-Feb-2012  mbalmer Convert to device_t, aka softc/device_t split, and clean up a bit the code.
 1.80 06-Dec-2009  dyoung branches: 1.80.12; 1.80.16; 1.80.18;
Delete do-nothing device-activation hooks.
 1.79 23-Nov-2009  rmind Remove some unecessary includes sys/user.h header.
 1.78 12-May-2009  cegger struct device * -> device_t, no functional changes intended.
 1.77 12-May-2009  cegger struct cfdata * -> cfdata_t, no functional changes intended.
 1.76 13-Jan-2009  yamt branches: 1.76.2;
g/c BUFQ_FOO() macros and use bufq_foo() directly.
 1.75 11-Jan-2009  cegger make this compile
 1.74 08-Jun-2008  tsutsui branches: 1.74.6;
Use device_lookup_private() rather than using cd_devs[] directly to get softc.

XXX maybe we should change a type of cd_devs[] in struct cfdriver
from (void *) to device_t.
 1.73 05-Apr-2008  cegger branches: 1.73.2; 1.73.4; 1.73.6;
use aprint_*_dev and device_xname
 1.72 29-Jul-2007  ad branches: 1.72.26; 1.72.28;
It's not a good idea for device drivers to modify b_flags, as they don't
need to understand the locking around that field. Instead of setting
B_ERROR, set b_error instead. b_error is 'owned' by whoever completes
the I/O request.
 1.71 09-Jul-2007  ad branches: 1.71.2;
Merge some of the less invasive changes from the vmlocking branch:

- kthread, callout, devsw API changes
- select()/poll() improvements
- miscellaneous MT safety improvements
 1.70 04-Mar-2007  christos branches: 1.70.2; 1.70.4;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.69 16-Nov-2006  christos branches: 1.69.4;
__unused removal on arguments; approved by core.
 1.68 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.67 03-Sep-2006  christos branches: 1.67.2; 1.67.4;
add missing initializer
 1.66 30-Mar-2006  thorpej Use device_private().
 1.65 28-Mar-2006  thorpej Use device_unit().
 1.64 20-Feb-2006  thorpej branches: 1.64.2; 1.64.4; 1.64.6;
Use device_is_active() rather than testing dv_flags for DVF_ACTIVE
directly.
 1.63 11-Dec-2005  christos branches: 1.63.2; 1.63.4; 1.63.6;
merge ktrace-lwp.
 1.62 15-Oct-2005  yamt - change the way to specify a bufq strategy. (by string rather than by number)
- rather than embedding bufq_state in driver softc,
have a pointer to the former.
- move bufq related functions from kern/subr_disk.c to kern/subr_bufq.c.
- rename method to strategy for consistency.
- move some definitions which don't need to be exposed to the rest of kernel
from sys/bufq.h to sys/bufq_impl.h.
(is it better to move it to kern/ or somewhere?)
- fix some obvious breakage in dev/qbus/ts.c. (not tested)
 1.61 30-May-2005  christos branches: 1.61.2;
- remove bogus casts
- add more const
 1.60 31-Mar-2005  yamt introduce a function to drain bufq and use it where appropriate.
 1.59 01-Feb-2005  reinoud branches: 1.59.4;
Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.58 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.57 28-Oct-2004  yamt branches: 1.57.4;
move buffer queue related stuffs from buf.h to their own header, bufq.h.
 1.56 17-Oct-2004  bouyer Adjust some debug printfs after b_bcount was changed from long to int.
From Patrick Welche on current-users@
 1.55 17-Sep-2004  mycroft Oops; fix ssrestart().
 1.54 17-Sep-2004  mycroft Do not manipulate xs->bp in "generic" code -- do it only in the psw_done
routine. As part of this, pass down our pre-parsed error code -- though this
interface will probably change later to accomodate better error handling.
 1.53 27-Aug-2004  bouyer Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
Havard Eidnes's analysis of this problem is that the scsipi_xfer pool is
competing for resources with other pools, including the the inode and vnode
pools which can grow quite large.

*_scsipi_cmd(): don't biodone the buffer if scsipi_make_xs() fails, let the
caller deal with the problem
start function of block devices drivers: dequeue the buffer after the
scsipi_command() call. If scsipi_command() fails with ENOMEM don't dequeue
the buffer, and schedule a callout to call the start function after
some delay.
scsipi_init(): prime the scsipi_xfer_pool with one page. This ensure that
there is always some scsipi_xfer to play with. If scsipi_command() fails
because of pool_get(), we're sure there will be resources available later,
when the pending commands have completed.

Reviewed by Jason Thorpe and Havard Eidnes.
Todo: remove the "unable to allocate scsipi_xfer" and "not queued, error %d"
printfs, but I choose to keep them for now, to help make sure the code does
what it should.
 1.52 21-Aug-2004  thorpej Use ANSI function decls and make use of static.
 1.51 14-Mar-2004  thorpej branches: 1.51.2;
Use scsipi_adapter_minphys().
 1.50 08-Sep-2003  mycroft Use the const shaker.
 1.49 29-Jun-2003  fvdl branches: 1.49.2;
Back out the lwp/ktrace changes. They contained a lot of colateral damage,
and need to be examined and discussed more.
 1.48 28-Jun-2003  darrenr Pass lwp pointers throughtout the kernel, as required, so that the lwpid can
be inserted into ktrace records. The general change has been to replace
"struct proc *" with "struct lwp *" in various function prototypes, pass
the lwp through and use l_proc to get the process pointer when needed.

Bump the kernel rev up to 1.6V
 1.47 20-Mar-2003  dbj use PRId64 to printf bp->b_blkno, which is of type daddr_t
 1.46 21-Jan-2003  simonb Remove a line of unreachable code. It seems to be a remnant of starting
off with a different driver, and the way this code is structured the
offending line isn't needed.
 1.45 23-Oct-2002  jdolecek merge kqueue branch into -current

kqueue provides a stateful and efficient event notification framework
currently supported events include socket, file, directory, fifo,
pipe, tty and device changes, and monitoring of processes and signals

kqueue is supported by all writable filesystems in NetBSD tree
(with exception of Coda) and all device drivers supporting poll(2)

based on work done by Jonathan Lemon for FreeBSD
initial NetBSD port done by Luke Mewburn and Jason Thorpe
 1.44 02-Oct-2002  thorpej Add trailing ; to CFATTACH_DECL.
 1.43 30-Sep-2002  thorpej Use CFATTACH_DECL().
 1.42 27-Sep-2002  thorpej Declare all cfattach structures const.
 1.41 14-Sep-2002  chs recognize HP ScanJet 4c.
add a catch-all case for other HP scanners.
remove a local version of atoi() in favor of strtoul() from libkern.
 1.40 06-Sep-2002  gehenna Merge the gehenna-devsw branch into the trunk.

This merge changes the device switch tables from static array to
dynamically generated by config(8).

- All device switches is defined as a constant structure in device drivers.

- The new grammer ``device-major'' is introduced to ``files''.

device-major <prefix> char <num> [block <num>] [<rules>]

- All device major numbers must be listed up in port dependent majors.<arch>
by using this grammer.

- Added the new naming convention.
The name of the device switch must be <prefix>_[bc]devsw for auto-generation
of device switch tables.

- The backward compatibility of loading block/character device
switch by LKM framework is broken. This is necessary to convert
from block/character device major to device name in runtime and vice versa.

- The restriction to assign device major by LKM is completely removed.
We don't need to reserve LKM entries for dynamic loading of device switch.

- In compile time, device major numbers list is packed into the kernel and
the LKM framework will refer it to assign device major number dynamically.
 1.39 22-Jul-2002  hannken Convert to new device buffer queue interface.
 1.38 15-Nov-2001  lukem branches: 1.38.8;
don't need <sys/types.h> when including <sys/param.h>
 1.37 13-Nov-2001  lukem add RCSIDs
 1.36 18-Jul-2001  thorpej branches: 1.36.2;
bcopy -> memcpy
 1.35 26-Jun-2001  bouyer branches: 1.35.2;
Make it compile with SCSIPI_DEBUG
 1.34 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.33 01-Jan-2001  augustss branches: 1.33.2;
Call vdevgone() with correct minor range.
 1.32 01-Jan-2001  augustss Support device detach.
Don't call scanjet_attach() for all HP scanners.
Also, ANSIfy while I'm here.
 1.31 31-May-2000  phil Add an entry for the HP ScanJet 6300C.
 1.30 30-Mar-2000  augustss branches: 1.30.2;
Get rid of register declarations.
 1.29 21-Jan-2000  thorpej Update for sys/buf.h/disksort_*() changes.
 1.28 19-Jan-2000  abs newline terminate ss: atatch message - from Roger Brooks
 1.27 17-Jan-2000  abs allow ioctl() on all scanner device nodes - now SANE can work with ss* driver
 1.26 30-Sep-1999  thorpej branches: 1.26.2; 1.26.8;
Cleanup the scsipi_xfer flags:
- `flags' is now gone, replaced with `xs_control' and `xs_status'.
- Massive cleanup of the control flags. Now we explicitly say that
a job is to complete asynchronously, rather than relying on side-effects,
and use a new flag to now that device discovery is being performed.
- Do SCSI device discovery interrupt-driven.
 1.25 10-Feb-1999  bouyer branches: 1.25.2;
Abort transfer if b_blkno is negative. Closes PR kern/5553 by Johan Danielsson.
 1.24 08-Dec-1998  thorpej When closing, wait for pending xfers to drain before deleting the reference
to the adapter.
 1.23 20-Nov-1998  thorpej Add adapter reference counting for SCSI and ATAPI devices.
 1.22 31-Aug-1998  cgd kill the last remnants of __BROKEN_INDIRECT_CONFIG. (only the pica port
used it, and it's non-working and apparently slated for replacement.)
 1.21 19-Aug-1998  explorer ss->special could be NULL, so sane would crash the kernel.
 1.20 22-Apr-1998  pk Set a flag during auto-configuration, so SCSI_AUTOCONF can be passed
when doing additional I/O from the scanner attach functions.
 1.19 12-Jan-1998  thorpej Adjust for changes to config.
 1.18 19-Nov-1997  augustss Recognize HP ScanJet 5p.
 1.17 01-Oct-1997  enami branches: 1.17.2;
Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.16 27-Aug-1997  bouyer branches: 1.16.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.15 05-Dec-1996  cgd update these so they compile whether or not __BROKEN_INDIRECT_CONFIG
is defined.
 1.14 29-Nov-1996  thorpej Add an inquiry pattern for the HP ScanJet 4p.
From Valtteri Vuorikoski <vuori@notcom.org>, PR #2957.
 1.13 12-Oct-1996  christos revert previous kprintf change
 1.12 10-Oct-1996  christos printf -> kprintf, sprintf -> ksprintf
 1.11 12-Jul-1996  is Some MUSTEK scanners fill the vendor string in the SCSI identify
string with 0xFF instead of 0x20. Shorten the compare to 6 bytes to
take care of this.

Fixes PR #2617 by myself, which is based on a private problem report
by Neil Brewitt.
 1.10 05-May-1996  christos Cleanup the rest of the SCSIDEBUG printfs. From Bernd Ernesti.
 1.9 30-Mar-1996  christos - Eliminate scsi_conf.h
- Fix prototypes
- Fix PR/2248 [Problems with HP scanjet...]
 1.8 17-Mar-1996  thorpej New device attachment scheme:

- split softc size and match/attach out from cfdriver into
a new struct cfattach.

- new "attach" directive for files.*. May specify the name of
the cfattach structure, so that devices may be easily attached
to parents with different autoconfiguration semantics.
 1.7 05-Mar-1996  thorpej Normalize SCSI autoconfiguration output.
From Chris Demetriou <cgd@NetBSD.ORG>. Fixes PR #1958.
 1.6 19-Feb-1996  mycroft Handle EOF a little differently.
 1.5 18-Feb-1996  mycroft Change variable name to avoid GCC warning.
 1.4 18-Feb-1996  mycroft Minor tweak; make sure window size is 0 on close.
 1.3 18-Feb-1996  mycroft Missed some prototyping foo.
 1.2 18-Feb-1996  mycroft Truncate to the window size in ssminphys(), not ssread().
 1.1 18-Feb-1996  mycroft Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a bit. Needs more work.
 1.16.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.16.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.16.2.1 27-Aug-1997  thorpej file ss.c was added on branch marc-pcmcia on 1997-08-27 23:33:34 +0000
 1.17.2.1 22-Nov-1997  mellon Pull rev 1.18 up from trunk (augustss)
 1.25.2.2 20-Jan-2000  he Pull up revision 1.28 (requested by abs):
Newline terminate 'ss:' attach message.
 1.25.2.1 17-Jan-2000  he Pull up revision 1.27 (requested by abs):
Allow ioctl() on all scanner device nodes - allows SANE to work.
 1.26.8.1 21-Dec-1999  wrstuden Initial commit of recent changes to make DEV_BSIZE go away.

Runs on i386, needs work on other arch's. Main kernel routines should be
fine, but a number of the stand programs need help.

cd, fd, ccd, wd, and sd have been updated. sd has been tested with non-512
byte block devices. vnd, raidframe, and lfs need work.

Non 2**n block support is automatic for LKM's and conditional for kernels
on "options NON_PO2_BLOCKS".
 1.26.2.6 21-Apr-2001  bouyer Get rid of references to sc_link/scsipi_link in debug or comments.
 1.26.2.5 06-Jan-2001  bouyer Make compile.
 1.26.2.4 05-Jan-2001  bouyer Sync with HEAD
 1.26.2.3 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.26.2.2 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.26.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.30.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.33.2.8 11-Nov-2002  nathanw Catch up to -current
 1.33.2.7 18-Oct-2002  nathanw Catch up to -current.
 1.33.2.6 17-Sep-2002  nathanw Catch up to -current.
 1.33.2.5 01-Aug-2002  nathanw Catch up to -current.
 1.33.2.4 08-Jan-2002  nathanw Catch up to -current.
 1.33.2.3 14-Nov-2001  nathanw Catch up to -current.
 1.33.2.2 24-Aug-2001  nathanw Catch up with -current.
 1.33.2.1 21-Jun-2001  nathanw Catch up to -current.
 1.35.2.4 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.35.2.3 06-Sep-2002  jdolecek sync kqueue branch with HEAD
 1.35.2.2 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.35.2.1 03-Aug-2001  lukem update to -current
 1.36.2.2 26-Sep-2001  fvdl * add a VCLONED vnode flag that indicates a vnode representing a cloned
device.
* rename REVOKEALL to REVOKEALIAS, and add a REVOKECLONE flag, to pass
to VOP_REVOKE
* the revoke system call will revoke all aliases, as before, but not the
clones
* vdevgone is called when detaching a device, so make it use REVOKECLONE
to get rid of all clones as well
* clean up all uses of VOP_OPEN wrt. locking.
* add a few VOPS to spec_vnops that need to do something when it's a
clone vnode (access and getattr)
* add a copy of the vnode vattr structure of the original 'master' vnode
to the specinfo of a cloned vnode. could possibly redirect getattr to
the 'master' vnode, but this has issues with revoke
* add a vdev_reassignvp function that disassociates a vnode from its
original device, and reassociates it with the specified dev_t. to be
used by cloning devices only, in case a new minor is allocated.
* change all direct references in drivers to v_devcookie and v_rdev
to vdev_privdata(vp) and vdev_rdev(vp). for diagnostic purposes
when debugging race conditions that still exist wrt. locking and
revoking vnodes.
* make the locking state of a vnode consistent when passed to
d_open and d_close (unlocked). locked would be better, but has
some deadlock issues
 1.36.2.1 07-Sep-2001  thorpej Commit my "devvp" changes to the thorpej-devvp branch. This
replaces the use of dev_t in most places with a struct vnode *.

This will form the basic infrastructure for real cloning device
support (besides being architecurally cleaner -- it'll be good
to get away from using numbers to represent objects).
 1.38.8.2 29-Aug-2002  gehenna catch up with -current.
 1.38.8.1 16-May-2002  gehenna Add the character device switch.
Replace the direct-access to devsw table with calling devsw API.
 1.49.2.10 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.49.2.9 01-Apr-2005  skrll Sync with HEAD.
 1.49.2.8 02-Nov-2004  skrll Sync with HEAD.
 1.49.2.7 19-Oct-2004  skrll Sync with HEAD
 1.49.2.6 21-Sep-2004  skrll Fix the sync with head I botched.
 1.49.2.5 18-Sep-2004  skrll Sync with HEAD.
 1.49.2.4 03-Sep-2004  skrll Sync with HEAD
 1.49.2.3 25-Aug-2004  skrll Sync with HEAD.
 1.49.2.2 03-Aug-2004  skrll Sync with HEAD
 1.49.2.1 02-Jul-2003  darrenr Apply the aborted ktrace-lwp changes to a specific branch. This is just for
others to review, I'm concerned that patch fuziness may have resulted in some
errant code being generated but I'll look at that later by comparing the diff
from the base to the branch with the file I attempt to apply to it. This will,
at the very least, put the changes in a better context for others to review
them and attempt to tinker with removing passing of 'struct lwp' through
the kernel.
 1.51.2.1 11-Sep-2004  he Pull up revision 1.53 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.57.4.1 29-Apr-2005  kent sync with -current
 1.59.4.1 06-Apr-2005  tron Pull up revision 1.60 (requested by yamt in ticket #112):
introduce a function to drain bufq and use it where appropriate.
 1.61.2.3 03-Sep-2007  yamt sync with head.
 1.61.2.2 30-Dec-2006  yamt sync with head.
 1.61.2.1 21-Jun-2006  yamt sync with head.
 1.63.6.1 22-Apr-2006  simonb Sync with head.
 1.63.4.1 09-Sep-2006  rpaulo sync with head
 1.63.2.1 01-Mar-2006  yamt sync with head.
 1.64.6.1 31-Mar-2006  tron Merge 2006-03-31 NetBSD-current into the "peter-altq" branch.
 1.64.4.1 19-Apr-2006  elad sync with head.
 1.64.2.2 03-Sep-2006  yamt sync with head.
 1.64.2.1 01-Apr-2006  yamt sync with head.
 1.67.4.2 10-Dec-2006  yamt sync with head.
 1.67.4.1 22-Oct-2006  yamt sync with head
 1.67.2.1 18-Nov-2006  ad Sync with head.
 1.69.4.1 12-Mar-2007  rmind Sync with HEAD.
 1.70.4.1 11-Jul-2007  mjf Sync with head.
 1.70.2.3 19-Aug-2007  ad - Back out the biodone() changes.
- Eliminate B_ERROR (from HEAD).
 1.70.2.2 01-Jul-2007  ad Adapt to callout API change.
 1.70.2.1 13-May-2007  ad - Pass the error number and residual count to biodone(), and let it handle
setting error indicators. Prepare to eliminate B_ERROR.
- Add a flag argument to brelse() to be set into the buf's flags, instead
of doing it directly. Typically used to set B_INVAL.
- Add a "struct cpu_info *" argument to kthread_create(), to be used to
create bound threads. Change "bool mpsafe" to "int flags".
- Allow exit of LWPs in the IDL state when (l != curlwp).
- More locking fixes & conversion to the new API.
 1.71.2.1 15-Aug-2007  skrll Sync with HEAD.
 1.72.28.2 29-Jul-2007  ad It's not a good idea for device drivers to modify b_flags, as they don't
need to understand the locking around that field. Instead of setting
B_ERROR, set b_error instead. b_error is 'owned' by whoever completes
the I/O request.
 1.72.28.1 29-Jul-2007  ad file ss.c was added on branch matt-mips64 on 2007-07-29 12:50:24 +0000
 1.72.26.5 17-Jan-2009  mjf Sync with HEAD.
 1.72.26.4 29-Jun-2008  mjf Sync with HEAD.
 1.72.26.3 02-Jun-2008  mjf Sync with HEAD.
 1.72.26.2 06-Apr-2008  mjf - after some discussion with agc@ i agreed it would be a good idea to move
device_unregister_* to device_deregister_* to be more like the pmf(9)
functions, especially since a lot of the time the function calls are next
to each other.

- add device_register_name() support for dk(4).
 1.72.26.1 05-Apr-2008  mjf - add "file-system DEVFS" and "pseudo-device devfsctl" to conf/std seeing
as these are always needed.

- convert many, many drivers over to the New Devfs World Order. For a
list of device drivers yet to be converted see,
http://www.netbsd.org/~mjf/devfs-todo.html.

- add a new device_unregister_all(device_t) function to remove all device
names associated with a device_t, which saves us having to construct
device names when the driver is detached.

- add a DEV_AUDIO type for devices.
 1.73.6.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.73.4.3 11-Mar-2010  yamt sync with head
 1.73.4.2 16-May-2009  yamt sync with head
 1.73.4.1 04-May-2009  yamt sync with head.
 1.73.2.1 17-Jun-2008  yamt sync with head.
 1.74.6.1 19-Jan-2009  skrll Sync with HEAD.
 1.76.2.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.80.18.1 02-Mar-2012  riz Pull up following revision(s) (requested by mbalmer in ticket #65):
sys/dev/scsipi/ss_scanjet.c: revision 1.52
sys/dev/scsipi/ss.c: revision 1.81
sys/dev/scsipi/ssvar.h: revision 1.19
sys/dev/scsipi/ss_mustek.c: revision 1.41
Convert to device_t, aka softc/device_t split, and clean up a bit the code.
 1.80.16.3 06-Mar-2012  mrg sync to -current
 1.80.16.2 06-Mar-2012  mrg sync to -current
 1.80.16.1 04-Mar-2012  mrg sync to latest -current.
 1.80.12.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.80.12.1 17-Apr-2012  yamt sync with head
 1.84.4.1 18-May-2014  rmind sync with head
 1.84.2.3 03-Dec-2017  jdolecek update from HEAD
 1.84.2.2 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.84.2.1 12-Sep-2012  tls Initial snapshot of work to eliminate 64K MAXPHYS. Basically works for
physio (I/O to raw devices); needs more doing to get it going with the
filesystems, but it shouldn't damage data.

All work's been done on amd64 so far. Not hard to add support to other
ports. If others want to pitch in, one very helpful thing would be to
sort out when and how IDE disks can do 128K or larger transfers, and
adjust the various PCI IDE (or at least ahcisata) drivers and wd.c
accordingly -- it would make testing much easier. Another very helpful
thing would be to implement a smart minphys() for RAIDframe along the
lines detailed in the MAXPHYS-NOTES file.
 1.85.2.1 10-Aug-2014  tls Rebase.
 1.86.8.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.86.4.2 28-Aug-2017  skrll Sync with HEAD
 1.86.4.1 05-Dec-2016  skrll Sync with HEAD
 1.88.8.1 21-Jun-2017  snj Pull up following revision(s) (requested by mlelstv in ticket #53):
sys/dev/scsipi/atapiconf.c: revision 1.91
sys/dev/scsipi/cd.c: revision 1.341
sys/dev/scsipi/scsi_base.c: revision 1.92
sys/dev/scsipi/scsiconf.c: revision 1.280
sys/dev/scsipi/scsipi_base.c: revisions 1.176, 1.177
sys/dev/scsipi/sd.c: revision 1.325
sys/dev/scsipi/ss.c: revision 1.89
sys/dev/scsipi/st.c: revision 1.231
The atapibus detach path did hold the channel mutex while calling into autoconf,
which would trigger a panic when unplugging a USB ATAPI CDROM.
Align detach code for scsibus and atapibus to fix this.
Also avoid races when detaching devices by replacing callout_stop with
callout_halt.
--
pass config_detach error to caller.
 1.43 20-Nov-2016  mlelstv Make scsipi framework MPSAFE.

Data structures are now protected by a per-adapter mutex at IPL_BIO
that is created by the scsibus or atapibus instance when the adapter
is configured.
The enable reference counter and the channel freeze counter which are
currently used by HBA code before the adapter is configured, are made
atomic.
The target drivers are now all tagged as D_MPSAFE.

Almost all HBA drivers still require the kernel lock to present,
so all callbacks into HBA code are still protected by kernel lock
unless the driver is tagged as SCSIPI_ADAPT_MPSAFE.

TODO: refactor sd and cd to use dksubr.
 1.42 25-Oct-2013  martin branches: 1.42.6; 1.42.10;
Mark a diagnostic-only variable
 1.41 28-Feb-2012  mbalmer branches: 1.41.2; 1.41.4;
Convert to device_t, aka softc/device_t split, and clean up a bit the code.
 1.40 23-Nov-2009  rmind branches: 1.40.12; 1.40.16; 1.40.18;
Remove some unecessary includes sys/user.h header.
 1.39 21-Oct-2009  rmind Remove uarea swap-out functionality:

- Addresses the issue described in PR/38828.
- Some simplification in threading and sleepq subsystems.
- Eliminates pmap_collect() and, as a side note, allows pmap optimisations.
- Eliminates XS_CTL_DATA_ONSTACK in scsipi code.
- Avoids few scans on LWP list and thus potentially long holds of proc_lock.
- Cuts ~1.5k lines of code. Reduces amd64 kernel size by ~4k.
- Removes __SWAP_BROKEN cases.

Tested on x86, mips, acorn32 (thanks <mpumford>) and partly tested on
acorn26 (thanks to <bjh21>).

Discussed on <tech-kern>, reviewed by <ad>.
 1.38 13-Jan-2009  yamt g/c BUFQ_FOO() macros and use bufq_foo() directly.
 1.37 05-Apr-2008  cegger branches: 1.37.4; 1.37.12;
use aprint_*_dev and device_xname
 1.36 04-Mar-2007  christos branches: 1.36.36;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.35 16-Nov-2006  christos branches: 1.35.4;
__unused removal on arguments; approved by core.
 1.34 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.33 03-Sep-2006  christos branches: 1.33.2; 1.33.4;
comment out unreachable code.
 1.32 11-Dec-2005  christos branches: 1.32.4; 1.32.8;
merge ktrace-lwp.
 1.31 15-Oct-2005  yamt - change the way to specify a bufq strategy. (by string rather than by number)
- rather than embedding bufq_state in driver softc,
have a pointer to the former.
- move bufq related functions from kern/subr_disk.c to kern/subr_bufq.c.
- rename method to strategy for consistency.
- move some definitions which don't need to be exposed to the rest of kernel
from sys/bufq.h to sys/bufq_impl.h.
(is it better to move it to kern/ or somewhere?)
- fix some obvious breakage in dev/qbus/ts.c. (not tested)
 1.30 01-Feb-2005  reinoud branches: 1.30.6;
Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.29 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.28 31-Jan-2005  reinoud As part of cleaning up sys/scsipi, replace all u_char by uint8_t and
replace all `short' with int16_t.
 1.27 28-Oct-2004  yamt branches: 1.27.4;
move buffer queue related stuffs from buf.h to their own header, bufq.h.
 1.26 17-Oct-2004  bouyer Adjust some debug printfs after b_bcount was changed from long to int.
From Patrick Welche on current-users@
 1.25 18-Sep-2004  mycroft Minor rearrangement. Whitespace and #include cleanup.
 1.24 18-Sep-2004  mycroft Standardize some variable names and the calling pattern for scsipi_command().
Use void pointer casts.
 1.23 17-Sep-2004  mycroft Remove the "xfer" argument to scsipi_command().
 1.22 17-Sep-2004  mycroft In places where we've already called scsipi_make_xs(), call scsipi_execute_xs()
directly rather than going through scsipi_command().
 1.21 09-Sep-2004  bouyer Make the xxstart() functions reentrant again, as some drivers HBA can call
scsipi_done() from their scsipi_request().
For this, add a struct scsipi_xfer * argument to scsipi_command().
If not NULL scsipi_command() will use this to enqueue this xfer, otherwise
it'll try to allocate a new one. This scsipi_xfer has to be allocated
and initialised by scsipi_make_xs() or equivalent.
In xxstart(), allocate a scsipi_xfer using scsipi_make_xs(), and if not NULL,
dequeue the buffer before calling scsipi_command(). This makes sure that
scsipi_command() will not fail, and also makes sure that xxstart() won't
be called again between the BUFQ_PEEK() and BUFQ_GET().

Fix "dequeued wrong buf" panics reported by Juergen Hannken-Illjes in
private mail and Andreas Wrede on current-users@.
Thanks to Jason Thorpe and Chuck Silver for review, and Andreas Wrede for
testing the patch.
 1.20 27-Aug-2004  bouyer Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
Havard Eidnes's analysis of this problem is that the scsipi_xfer pool is
competing for resources with other pools, including the the inode and vnode
pools which can grow quite large.

*_scsipi_cmd(): don't biodone the buffer if scsipi_make_xs() fails, let the
caller deal with the problem
start function of block devices drivers: dequeue the buffer after the
scsipi_command() call. If scsipi_command() fails with ENOMEM don't dequeue
the buffer, and schedule a callout to call the start function after
some delay.
scsipi_init(): prime the scsipi_xfer_pool with one page. This ensure that
there is always some scsipi_xfer to play with. If scsipi_command() fails
because of pool_get(), we're sure there will be resources available later,
when the pending commands have completed.

Reviewed by Jason Thorpe and Havard Eidnes.
Todo: remove the "unable to allocate scsipi_xfer" and "not queued, error %d"
printfs, but I choose to keep them for now, to help make sure the code does
what it should.
 1.19 21-Aug-2004  thorpej Use ANSI function decls and make use of static.
 1.18 15-Nov-2001  lukem branches: 1.18.16; 1.18.18;
don't need <sys/types.h> when including <sys/param.h>
 1.17 13-Nov-2001  lukem add RCSIDs
 1.16 18-Jul-2001  thorpej bcmp -> memcmp
 1.15 18-Jul-2001  thorpej bcopy -> memcpy
 1.14 18-Jul-2001  thorpej bzero -> memset
 1.13 25-Apr-2001  bouyer branches: 1.13.2;
Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.12 09-Jun-2000  enami branches: 1.12.4;
Prevent a process being swapped out during I/O if the data buffer is
allocated on stack. This potential problem is noticed by Noriyuki Soda
and the idea and sample code to fix is given by Jason R. Thorpe.
 1.11 30-Sep-1999  thorpej branches: 1.11.2; 1.11.10;
Cleanup the scsipi_xfer flags:
- `flags' is now gone, replaced with `xs_control' and `xs_status'.
- Massive cleanup of the control flags. Now we explicitly say that
a job is to complete asynchronously, rather than relying on side-effects,
and use a new flag to now that device discovery is being performed.
- Do SCSI device discovery interrupt-driven.
 1.10 05-Apr-1999  mycroft If scsipi_command() fails, always print out the error code.
 1.9 18-Oct-1997  thorpej branches: 1.9.12;
Implement two macros, scsipi_command() and scsipi_command_direct(), and
use them to hide the structure of the function pointers we jump through
to issue a command.
 1.8 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.7 27-Aug-1997  bouyer branches: 1.7.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.6 12-Oct-1996  christos revert previous kprintf change
 1.5 10-Oct-1996  christos printf -> kprintf, sprintf -> ksprintf
 1.4 05-May-1996  christos Cleanup the rest of the SCSIDEBUG printfs. From Bernd Ernesti.
 1.3 30-Mar-1996  christos - Eliminate scsi_conf.h
- Fix prototypes
- Fix PR/2248 [Problems with HP scanjet...]
 1.2 19-Mar-1996  mycroft Define a full set of [234][bl]tol() and lto[234][bl]() conversion functions,
inlined.
Use sized types in protocol structures.
Make the definition of scsi_sense_data less ugly.
 1.1 18-Feb-1996  mycroft Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a bit. Needs more work.
 1.7.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.7.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.7.2.1 27-Aug-1997  thorpej file ss_mustek.c was added on branch marc-pcmcia on 1997-08-27 23:33:35 +0000
 1.9.12.1 21-Jun-1999  thorpej Sync w/ -current.
 1.11.10.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.11.2.4 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.11.2.3 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.11.2.2 20-Oct-1999  thorpej Remove an unnecessary comment regarding XS_CTL_NOSLEEP.
 1.11.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.12.4.4 08-Jan-2002  nathanw Catch up to -current.
 1.12.4.3 14-Nov-2001  nathanw Catch up to -current.
 1.12.4.2 24-Aug-2001  nathanw Catch up with -current.
 1.12.4.1 21-Jun-2001  nathanw Catch up to -current.
 1.13.2.2 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.13.2.1 03-Aug-2001  lukem update to -current
 1.18.18.1 11-Sep-2004  he Pull up revisions 1.20-1.21 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.18.16.7 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.18.16.6 02-Nov-2004  skrll Sync with HEAD.
 1.18.16.5 19-Oct-2004  skrll Sync with HEAD
 1.18.16.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.18.16.3 18-Sep-2004  skrll Sync with HEAD.
 1.18.16.2 03-Sep-2004  skrll Sync with HEAD
 1.18.16.1 25-Aug-2004  skrll Sync with HEAD.
 1.27.4.1 29-Apr-2005  kent sync with -current
 1.30.6.3 03-Sep-2007  yamt sync with head.
 1.30.6.2 30-Dec-2006  yamt sync with head.
 1.30.6.1 21-Jun-2006  yamt sync with head.
 1.32.8.1 03-Sep-2006  yamt sync with head.
 1.32.4.1 09-Sep-2006  rpaulo sync with head
 1.33.4.2 10-Dec-2006  yamt sync with head.
 1.33.4.1 22-Oct-2006  yamt sync with head
 1.33.2.1 18-Nov-2006  ad Sync with head.
 1.35.4.1 12-Mar-2007  rmind Sync with HEAD.
 1.36.36.2 17-Jan-2009  mjf Sync with HEAD.
 1.36.36.1 02-Jun-2008  mjf Sync with HEAD.
 1.37.12.1 19-Jan-2009  skrll Sync with HEAD.
 1.37.4.2 11-Mar-2010  yamt sync with head
 1.37.4.1 04-May-2009  yamt sync with head.
 1.40.18.1 02-Mar-2012  riz Pull up following revision(s) (requested by mbalmer in ticket #65):
sys/dev/scsipi/ss_scanjet.c: revision 1.52
sys/dev/scsipi/ss.c: revision 1.81
sys/dev/scsipi/ssvar.h: revision 1.19
sys/dev/scsipi/ss_mustek.c: revision 1.41
Convert to device_t, aka softc/device_t split, and clean up a bit the code.
 1.40.16.3 06-Mar-2012  mrg sync to -current
 1.40.16.2 06-Mar-2012  mrg sync to -current
 1.40.16.1 04-Mar-2012  mrg sync to latest -current.
 1.40.12.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.40.12.1 17-Apr-2012  yamt sync with head
 1.41.4.1 18-May-2014  rmind sync with head
 1.41.2.2 03-Dec-2017  jdolecek update from HEAD
 1.41.2.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.42.10.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.42.6.1 05-Dec-2016  skrll Sync with HEAD
 1.9 11-Dec-2005  christos merge ktrace-lwp.
 1.8 27-Feb-2005  perry nuke trailing whitespace
 1.7 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.6 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.5 13-Feb-1998  enami branches: 1.5.48; 1.5.56; 1.5.58;
Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.4 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.3 27-Aug-1997  bouyer branches: 1.3.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.2 19-Mar-1996  mycroft Define a full set of [234][bl]tol() and lto[234][bl]() conversion functions,
inlined.
Use sized types in protocol structures.
Make the definition of scsi_sense_data less ugly.
 1.1 18-Feb-1996  mycroft Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a bit. Needs more work.
 1.3.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.3.2.1 27-Aug-1997  thorpej file ss_mustek.h was added on branch marc-pcmcia on 1997-08-27 23:33:36 +0000
 1.5.58.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.5.56.1 29-Apr-2005  kent sync with -current
 1.5.48.2 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.5.48.1 04-Feb-2005  skrll Sync with HEAD.
 1.54 20-Nov-2016  mlelstv Make scsipi framework MPSAFE.

Data structures are now protected by a per-adapter mutex at IPL_BIO
that is created by the scsibus or atapibus instance when the adapter
is configured.
The enable reference counter and the channel freeze counter which are
currently used by HBA code before the adapter is configured, are made
atomic.
The target drivers are now all tagged as D_MPSAFE.

Almost all HBA drivers still require the kernel lock to present,
so all callbacks into HBA code are still protected by kernel lock
unless the driver is tagged as SCSIPI_ADAPT_MPSAFE.

TODO: refactor sd and cd to use dksubr.
 1.53 25-Oct-2013  martin branches: 1.53.6; 1.53.10;
Mark a diagnostic-only variable
 1.52 28-Feb-2012  mbalmer branches: 1.52.2; 1.52.4;
Convert to device_t, aka softc/device_t split, and clean up a bit the code.
 1.51 23-Nov-2009  rmind branches: 1.51.12; 1.51.16; 1.51.18;
Remove some unecessary includes sys/user.h header.
 1.50 21-Oct-2009  rmind Remove uarea swap-out functionality:

- Addresses the issue described in PR/38828.
- Some simplification in threading and sleepq subsystems.
- Eliminates pmap_collect() and, as a side note, allows pmap optimisations.
- Eliminates XS_CTL_DATA_ONSTACK in scsipi code.
- Avoids few scans on LWP list and thus potentially long holds of proc_lock.
- Cuts ~1.5k lines of code. Reduces amd64 kernel size by ~4k.
- Removes __SWAP_BROKEN cases.

Tested on x86, mips, acorn32 (thanks <mpumford>) and partly tested on
acorn26 (thanks to <bjh21>).

Discussed on <tech-kern>, reviewed by <ad>.
 1.49 13-Jan-2009  yamt g/c BUFQ_FOO() macros and use bufq_foo() directly.
 1.48 05-Apr-2008  cegger branches: 1.48.4; 1.48.12;
use aprint_*_dev and device_xname
 1.47 16-Nov-2006  christos branches: 1.47.48;
__unused removal on arguments; approved by core.
 1.46 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.45 03-Sep-2006  christos branches: 1.45.2; 1.45.4;
comment out unreachable code
 1.44 11-Dec-2005  christos branches: 1.44.4; 1.44.8;
merge ktrace-lwp.
 1.43 15-Oct-2005  yamt - change the way to specify a bufq strategy. (by string rather than by number)
- rather than embedding bufq_state in driver softc,
have a pointer to the former.
- move bufq related functions from kern/subr_disk.c to kern/subr_bufq.c.
- rename method to strategy for consistency.
- move some definitions which don't need to be exposed to the rest of kernel
from sys/bufq.h to sys/bufq_impl.h.
(is it better to move it to kern/ or somewhere?)
- fix some obvious breakage in dev/qbus/ts.c. (not tested)
 1.42 30-May-2005  christos branches: 1.42.2;
- remove bogus casts
- add more const
 1.41 27-Feb-2005  perry nuke trailing whitespace
 1.40 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.39 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.38 31-Jan-2005  reinoud As part of cleaning up sys/scsipi, replace all u_char by uint8_t and
replace all `short' with int16_t.
 1.37 28-Oct-2004  yamt branches: 1.37.4; 1.37.6;
move buffer queue related stuffs from buf.h to their own header, bufq.h.
 1.36 18-Sep-2004  mycroft Minor rearrangement. Whitespace and #include cleanup.
 1.35 18-Sep-2004  mycroft Standardize some variable names and the calling pattern for scsipi_command().
Use void pointer casts.
 1.34 17-Sep-2004  mycroft Remove the "xfer" argument to scsipi_command().
 1.33 17-Sep-2004  mycroft In places where we've already called scsipi_make_xs(), call scsipi_execute_xs()
directly rather than going through scsipi_command().
 1.32 09-Sep-2004  bouyer Make the xxstart() functions reentrant again, as some drivers HBA can call
scsipi_done() from their scsipi_request().
For this, add a struct scsipi_xfer * argument to scsipi_command().
If not NULL scsipi_command() will use this to enqueue this xfer, otherwise
it'll try to allocate a new one. This scsipi_xfer has to be allocated
and initialised by scsipi_make_xs() or equivalent.
In xxstart(), allocate a scsipi_xfer using scsipi_make_xs(), and if not NULL,
dequeue the buffer before calling scsipi_command(). This makes sure that
scsipi_command() will not fail, and also makes sure that xxstart() won't
be called again between the BUFQ_PEEK() and BUFQ_GET().

Fix "dequeued wrong buf" panics reported by Juergen Hannken-Illjes in
private mail and Andreas Wrede on current-users@.
Thanks to Jason Thorpe and Chuck Silver for review, and Andreas Wrede for
testing the patch.
 1.31 27-Aug-2004  bouyer Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
Havard Eidnes's analysis of this problem is that the scsipi_xfer pool is
competing for resources with other pools, including the the inode and vnode
pools which can grow quite large.

*_scsipi_cmd(): don't biodone the buffer if scsipi_make_xs() fails, let the
caller deal with the problem
start function of block devices drivers: dequeue the buffer after the
scsipi_command() call. If scsipi_command() fails with ENOMEM don't dequeue
the buffer, and schedule a callout to call the start function after
some delay.
scsipi_init(): prime the scsipi_xfer_pool with one page. This ensure that
there is always some scsipi_xfer to play with. If scsipi_command() fails
because of pool_get(), we're sure there will be resources available later,
when the pending commands have completed.

Reviewed by Jason Thorpe and Havard Eidnes.
Todo: remove the "unable to allocate scsipi_xfer" and "not queued, error %d"
printfs, but I choose to keep them for now, to help make sure the code does
what it should.
 1.30 21-Aug-2004  thorpej Use ANSI function decls and make use of static.
 1.29 22-Apr-2004  itojun sprintf -> snprintf
 1.28 14-Sep-2002  chs branches: 1.28.6; 1.28.8;
recognize HP ScanJet 4c.
add a catch-all case for other HP scanners.
remove a local version of atoi() in favor of strtoul() from libkern.
 1.27 15-Nov-2001  lukem don't need <sys/types.h> when including <sys/param.h>
 1.26 13-Nov-2001  lukem add RCSIDs
 1.25 18-Jul-2001  thorpej bcmp -> memcmp
 1.24 18-Jul-2001  thorpej bcopy -> memcpy
 1.23 18-Jul-2001  thorpej bzero -> memset
 1.22 25-Apr-2001  bouyer branches: 1.22.2;
Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.21 08-Jul-2000  sommerfeld branches: 1.21.2;
printf format paranoia
 1.20 09-Jun-2000  enami Prevent a process being swapped out during I/O if the data buffer is
allocated on stack. This potential problem is noticed by Noriyuki Soda
and the idea and sample code to fix is given by Jason R. Thorpe.
 1.19 12-Mar-2000  augustss branches: 1.19.2;
Remove extra \n in attach message.
 1.18 30-Sep-1999  thorpej branches: 1.18.2;
Cleanup the scsipi_xfer flags:
- `flags' is now gone, replaced with `xs_control' and `xs_status'.
- Massive cleanup of the control flags. Now we explicitly say that
a job is to complete asynchronously, rather than relying on side-effects,
and use a new flag to now that device discovery is being performed.
- Do SCSI device discovery interrupt-driven.
 1.17 05-Apr-1999  mycroft If scsipi_command() fails, always print out the error code.
 1.16 22-Apr-1998  pk branches: 1.16.10;
Set a flag during auto-configuration, so SCSI_AUTOCONF can be passed
when doing additional I/O from the scanner attach functions.
 1.15 19-Nov-1997  augustss Recognize HP ScanJet 5p.
 1.14 18-Oct-1997  thorpej branches: 1.14.2;
Implement two macros, scsipi_command() and scsipi_command_direct(), and
use them to hide the structure of the function pointers we jump through
to issue a command.
 1.13 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.12 27-Aug-1997  bouyer branches: 1.12.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.11 18-Jan-1997  cgd kill use of strchr(); it's moving to libkern
 1.10 22-Dec-1996  cgd note in a comment (XXX) that strchr() (what's it doing here, anyway?)
causes 'const' to be discarded.
 1.9 03-Dec-1996  thorpej Recognize the HP ScanJet 4p, per PR #2663 (Kenneth Stailey).
 1.8 12-Oct-1996  christos revert previous kprintf change
 1.7 10-Oct-1996  christos - printf -> kprintf, sprintf -> ksprintf
- replace sequences of sprintf(p, ...); p += strlen(p) with p += sprintf(p,
 1.6 18-May-1996  christos Added prototypes for inline functions atoi() and strchr().
 1.5 11-May-1996  mycroft Bug fixes from PR 2329, plus a few from me.
 1.4 05-May-1996  christos Cleanup the rest of the SCSIDEBUG printfs. From Bernd Ernesti.
 1.3 30-Mar-1996  christos - Eliminate scsi_conf.h
- Fix prototypes
- Fix PR/2248 [Problems with HP scanjet...]
 1.2 19-Mar-1996  mycroft Define a full set of [234][bl]tol() and lto[234][bl]() conversion functions,
inlined.
Use sized types in protocol structures.
Make the definition of scsi_sense_data less ugly.
 1.1 18-Feb-1996  mycroft Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a bit. Needs more work.
 1.12.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.12.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.12.2.1 27-Aug-1997  thorpej file ss_scanjet.c was added on branch marc-pcmcia on 1997-08-27 23:33:37 +0000
 1.14.2.1 22-Nov-1997  mellon Pull rev 1.15 up from trunk (augustss)
 1.16.10.1 21-Jun-1999  thorpej Sync w/ -current.
 1.18.2.4 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.18.2.3 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.18.2.2 20-Oct-1999  thorpej Remove an unnecessary comment regarding XS_CTL_NOSLEEP.
 1.18.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.19.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.21.2.5 17-Sep-2002  nathanw Catch up to -current.
 1.21.2.4 08-Jan-2002  nathanw Catch up to -current.
 1.21.2.3 14-Nov-2001  nathanw Catch up to -current.
 1.21.2.2 24-Aug-2001  nathanw Catch up with -current.
 1.21.2.1 21-Jun-2001  nathanw Catch up to -current.
 1.22.2.3 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.22.2.2 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.22.2.1 03-Aug-2001  lukem update to -current
 1.28.8.1 11-Sep-2004  he Pull up revisions 1.31-1.32 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.28.6.8 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.28.6.7 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.28.6.6 02-Nov-2004  skrll Sync with HEAD.
 1.28.6.5 21-Sep-2004  skrll Fix the sync with head I botched.
 1.28.6.4 18-Sep-2004  skrll Sync with HEAD.
 1.28.6.3 03-Sep-2004  skrll Sync with HEAD
 1.28.6.2 25-Aug-2004  skrll Sync with HEAD.
 1.28.6.1 03-Aug-2004  skrll Sync with HEAD
 1.37.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.37.4.1 29-Apr-2005  kent sync with -current
 1.42.2.2 30-Dec-2006  yamt sync with head.
 1.42.2.1 21-Jun-2006  yamt sync with head.
 1.44.8.1 03-Sep-2006  yamt sync with head.
 1.44.4.1 09-Sep-2006  rpaulo sync with head
 1.45.4.2 10-Dec-2006  yamt sync with head.
 1.45.4.1 22-Oct-2006  yamt sync with head
 1.45.2.1 18-Nov-2006  ad Sync with head.
 1.47.48.2 17-Jan-2009  mjf Sync with HEAD.
 1.47.48.1 02-Jun-2008  mjf Sync with HEAD.
 1.48.12.1 19-Jan-2009  skrll Sync with HEAD.
 1.48.4.2 11-Mar-2010  yamt sync with head
 1.48.4.1 04-May-2009  yamt sync with head.
 1.51.18.1 02-Mar-2012  riz Pull up following revision(s) (requested by mbalmer in ticket #65):
sys/dev/scsipi/ss_scanjet.c: revision 1.52
sys/dev/scsipi/ss.c: revision 1.81
sys/dev/scsipi/ssvar.h: revision 1.19
sys/dev/scsipi/ss_mustek.c: revision 1.41
Convert to device_t, aka softc/device_t split, and clean up a bit the code.
 1.51.16.3 06-Mar-2012  mrg sync to -current
 1.51.16.2 06-Mar-2012  mrg sync to -current
 1.51.16.1 04-Mar-2012  mrg sync to latest -current.
 1.51.12.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.51.12.1 17-Apr-2012  yamt sync with head
 1.52.4.1 18-May-2014  rmind sync with head
 1.52.2.2 03-Dec-2017  jdolecek update from HEAD
 1.52.2.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.53.10.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.53.6.1 05-Dec-2016  skrll Sync with HEAD
 1.19 28-Feb-2012  mbalmer Convert to device_t, aka softc/device_t split, and clean up a bit the code.
 1.18 30-Jun-2011  wiz branches: 1.18.2; 1.18.6; 1.18.8;
dependant -> dependent
 1.17 11-Dec-2005  christos merge ktrace-lwp.
 1.16 15-Oct-2005  yamt - change the way to specify a bufq strategy. (by string rather than by number)
- rather than embedding bufq_state in driver softc,
have a pointer to the former.
- move bufq related functions from kern/subr_disk.c to kern/subr_bufq.c.
- rename method to strategy for consistency.
- move some definitions which don't need to be exposed to the rest of kernel
from sys/bufq.h to sys/bufq_impl.h.
(is it better to move it to kern/ or somewhere?)
- fix some obvious breakage in dev/qbus/ts.c. (not tested)
 1.15 27-Feb-2005  perry branches: 1.15.4;
nuke trailing whitespace
 1.14 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.13 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.12 27-Aug-2004  bouyer branches: 1.12.4; 1.12.6;
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
Havard Eidnes's analysis of this problem is that the scsipi_xfer pool is
competing for resources with other pools, including the the inode and vnode
pools which can grow quite large.

*_scsipi_cmd(): don't biodone the buffer if scsipi_make_xs() fails, let the
caller deal with the problem
start function of block devices drivers: dequeue the buffer after the
scsipi_command() call. If scsipi_command() fails with ENOMEM don't dequeue
the buffer, and schedule a callout to call the start function after
some delay.
scsipi_init(): prime the scsipi_xfer_pool with one page. This ensure that
there is always some scsipi_xfer to play with. If scsipi_command() fails
because of pool_get(), we're sure there will be resources available later,
when the pending commands have completed.

Reviewed by Jason Thorpe and Havard Eidnes.
Todo: remove the "unable to allocate scsipi_xfer" and "not queued, error %d"
printfs, but I choose to keep them for now, to help make sure the code does
what it should.
 1.11 21-Aug-2004  thorpej - De-__P.
- Use ANSI function decls.
 1.10 02-Nov-2003  wiz branches: 1.10.2;
boundary, not boundry. Inspired by Tom Cosgrove.
 1.9 22-Jul-2002  hannken branches: 1.9.6;
Convert to new device buffer queue interface.
 1.8 25-Apr-2001  bouyer branches: 1.8.2; 1.8.14;
Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.7 21-Jan-2000  thorpej branches: 1.7.6;
Update for sys/buf.h/disksort_*() changes.
 1.6 22-Apr-1998  pk branches: 1.6.14;
Set a flag during auto-configuration, so SCSI_AUTOCONF can be passed
when doing additional I/O from the scanner attach functions.
 1.5 13-Feb-1998  enami Backout previous change, and rather, remove all protection but the one
actually currently required. Suggested by Charles M. Hannum.
 1.4 13-Feb-1998  enami Fix or add protection for mutiple inclusion.
 1.3 27-Aug-1997  bouyer branches: 1.3.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.2 30-Mar-1996  christos - Eliminate scsi_conf.h
- Fix prototypes
- Fix PR/2248 [Problems with HP scanjet...]
 1.1 18-Feb-1996  mycroft Add SCSI scanner support by Kenneth Stailey and Joachim Koenig-Baltes,
hacked a bit. Needs more work.
 1.3.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.3.2.1 27-Aug-1997  thorpej file ssvar.h was added on branch marc-pcmcia on 1997-08-27 23:33:38 +0000
 1.6.14.2 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.6.14.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.7.6.2 01-Aug-2002  nathanw Catch up to -current.
 1.7.6.1 21-Jun-2001  nathanw Catch up to -current.
 1.8.14.1 29-Aug-2002  gehenna catch up with -current.
 1.8.2.1 06-Sep-2002  jdolecek sync kqueue branch with HEAD
 1.9.6.7 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.9.6.6 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.9.6.5 21-Sep-2004  skrll Fix the sync with head I botched.
 1.9.6.4 18-Sep-2004  skrll Sync with HEAD.
 1.9.6.3 03-Sep-2004  skrll Sync with HEAD
 1.9.6.2 25-Aug-2004  skrll Sync with HEAD.
 1.9.6.1 03-Aug-2004  skrll Sync with HEAD
 1.10.2.1 11-Sep-2004  he Pull up revision 1.12 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.12.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.12.4.1 29-Apr-2005  kent sync with -current
 1.15.4.1 21-Jun-2006  yamt sync with head.
 1.18.8.1 02-Mar-2012  riz Pull up following revision(s) (requested by mbalmer in ticket #65):
sys/dev/scsipi/ss_scanjet.c: revision 1.52
sys/dev/scsipi/ss.c: revision 1.81
sys/dev/scsipi/ssvar.h: revision 1.19
sys/dev/scsipi/ss_mustek.c: revision 1.41
Convert to device_t, aka softc/device_t split, and clean up a bit the code.
 1.18.6.3 06-Mar-2012  mrg sync to -current
 1.18.6.2 06-Mar-2012  mrg sync to -current
 1.18.6.1 04-Mar-2012  mrg sync to latest -current.
 1.18.2.1 17-Apr-2012  yamt sync with head
 1.243 23-Feb-2022  andvar fix various typos in comments, mainly immediatly/immediately/,
as well shared and recently fixed typos in OpenBSD code by Jonathan Grey.
 1.242 05-Dec-2021  msaitoh availabe -> available in comment.
 1.241 13-Jun-2021  mlelstv Restore EOM handling.
 1.240 27-Dec-2019  msaitoh branches: 1.240.12;
s/transfered/transferred/
 1.239 10-Nov-2019  chs in many device attach paths, allocate memory with M_WAITOK instead of M_NOWAIT
and remove code to handle failures that can no longer happen.
 1.238 19-May-2019  kardel Add simple position recovery when positioning to EOM by reading
the position with READ_POSITION.
this allows for
mt eom
mt st
to return the correct file position.
 1.237 23-Feb-2019  kamil Correct printing type of b_blkno (int64_t) in st.c

Fixes build with kUBSan on NetBSD/i386.
 1.236 12-Feb-2019  kardel Fix PR kern/53949:

Fix inconsistent/incomplete file mark handling to conform again
to mtio(4) at close(2) time. This was necessary as the PREVENT/ALLOW
bracket was reduced from a whole mount session to cover only the
open(2)/close(2) time on ~2002-03-22. The rationale was to allow
robots and humans to change the media during a mount session.

Unfortunately this lead to file marks being written to potentially other
media at the beginning on drives that used the two file marks as EOM
pattern. In order for that to happen the media had to be removed after
data and at most one file mark had been written before removal.

The mount error message has been clarified and a warning about
potential data/file mark lossage on UNIT ATTENTION
during an active mount session with unfinished file marks has been
added.

While there, fix, but disable the commented SUN compatibility to write
final file marks by opening and immediately closing the device
in O_WRONLY mode. That code has not been working since around 1998.
It can now be enabled with options ST_SUNCOMPAT.

Additionally debug output coverage has been extended.
 1.235 03-Feb-2019  mrg - add or adjust /* FALLTHROUGH */ where appropriate
- add __unreachable() after functions that can return but won't in
this case, and thus can't be marked __dead easily
 1.234 24-Mar-2018  mlelstv branches: 1.234.2;
Use separate lock to protect internal state and release locks when
calling biodone.
 1.233 24-Mar-2018  kamil Revert previous commit in st.c in order to unbreak build

There are various build errors like:
/usr/src/sys/dev/scsipi/st.c: In function 'stattach':
/usr/src/sys/dev/scsipi/st.c:398:16: error: 'struct st_softc'
has no member named 'buf_defer'; did you mean 'buf_queue'?
bufq_alloc(&st->buf_defer, "fcfs", 0);

Original (reverted) commit:
Use separate lock to protect internal state and release locks when
calling biodone.
 1.232 23-Mar-2018  mlelstv Use separate lock to protect internal state and release locks when
calling biodone.
 1.231 17-Jun-2017  mlelstv branches: 1.231.4;
The atapibus detach path did hold the channel mutex while calling into autoconf,
which would trigger a panic when unplugging a USB ATAPI CDROM.

Align detach code for scsibus and atapibus to fix this.

Also avoid races when detaching devices by replacing callout_stop with
callout_halt.
 1.230 20-Nov-2016  mlelstv branches: 1.230.8;
Make scsipi framework MPSAFE.

Data structures are now protected by a per-adapter mutex at IPL_BIO
that is created by the scsibus or atapibus instance when the adapter
is configured.
The enable reference counter and the channel freeze counter which are
currently used by HBA code before the adapter is configured, are made
atomic.
The target drivers are now all tagged as D_MPSAFE.

Almost all HBA drivers still require the kernel lock to present,
so all callbacks into HBA code are still protected by kernel lock
unless the driver is tagged as SCSIPI_ADAPT_MPSAFE.

TODO: refactor sd and cd to use dksubr.
 1.229 20-Nov-2016  pgoyette Avoid calling bufq_free() from critical code sections.
 1.228 14-Jul-2016  msaitoh branches: 1.228.2;
- Use aprint*() instead of printf() in xxx_attach().
- Add missing aprint_naive("\n");
- KNF
 1.227 24-Aug-2015  pooka would you like some freshly ground _KERNEL_OPT with that?
yes? excellent choice, sir/madam.
 1.226 10-Aug-2014  tls branches: 1.226.2; 1.226.4; 1.226.6; 1.226.10;
Merge tls-earlyentropy branch into HEAD.
 1.225 25-Jul-2014  dholland Add d_discard to all struct cdevsw instances I could find.

All have been set to "nodiscard"; some should get a real implementation.
 1.224 25-Jul-2014  dholland Add d_discard to all struct bdevsw instances I could find.

I've set them all to nodiscard. Some of them (wd, dk, vnd, ld,
raidframe, maybe cgd) should be implemented for real.
 1.223 16-Mar-2014  dholland branches: 1.223.2;
Change (mostly mechanically) every cdevsw/bdevsw I can find to use
designated initializers.

I have not built every extant kernel so I have probably broken at
least one build; however I've also found and fixed some wrong
cdevsw/bdevsw entries so even if so I think we come out ahead.
 1.222 25-Oct-2013  martin Mark a diagnostic-only variable
 1.221 19-Apr-2012  bouyer branches: 1.221.2; 1.221.4;
Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.

This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.

In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.

Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.220 28-Feb-2012  mbalmer Convert st(4) to device_t, while here clean up the code and use uintXX_t
instead of u_intXX_t.
 1.219 25-Feb-2012  shattered Replace magic value 0x7 with constant MT_ISAR.

OK by wiz@
 1.218 02-Feb-2012  tls branches: 1.218.2;
Entropy-pool implementation move and cleanup.

1) Move core entropy-pool code and source/sink/sample management code
to sys/kern from sys/dev.

2) Remove use of NRND as test for presence of entropy-pool code throughout
source tree.

3) Remove use of RND_ENABLED in device drivers as microoptimization to
avoid expensive operations on disabled entropy sources; make the
rnd_add calls do this directly so all callers benefit.

4) Fix bug in recent rnd_add_data()/rnd_add_uint32() changes that might
have lead to slight entropy overestimation for some sources.

5) Add new source types for environmental sensors, power sensors, VM
system events, and skew between clocks, with a sample implementation
for each.

ok releng to go in before the branch due to the difficulty of later
pullup (widespread #ifdef removal and moved files). Tested with release
builds on amd64 and evbarm and live testing on amd64.
 1.217 30-May-2010  pgoyette branches: 1.217.8; 1.217.12;
Extract SCSIVERBOSE into a kernel module. The module can be builtin
by defining 'options SCSIVERBOSE' in the kernel config file (no change
from current behavior), or it can be loaded at boot time on those
architectures that support the boot loader's "load" command.

The module is built for all architectures, whether or not SCSI or
atapi support exists.
 1.216 06-Dec-2009  dyoung branches: 1.216.2; 1.216.4;
Delete do-nothing device-activation hooks.
 1.215 05-Dec-2009  pooka Convert tsleep(&lbolt) to kpause(). Make ltsleep/mtsleep on lbolt
illegal. I examined all places where lbolt is referenced to make
sure there were pointer aliases of it passed to tsleep, but put a
KASSERT in m/ltsleep() just to be sure.
 1.214 23-Nov-2009  rmind Remove some unecessary includes sys/user.h header.
 1.213 21-Oct-2009  rmind Remove uarea swap-out functionality:

- Addresses the issue described in PR/38828.
- Some simplification in threading and sleepq subsystems.
- Eliminates pmap_collect() and, as a side note, allows pmap optimisations.
- Eliminates XS_CTL_DATA_ONSTACK in scsipi code.
- Avoids few scans on LWP list and thus potentially long holds of proc_lock.
- Cuts ~1.5k lines of code. Reduces amd64 kernel size by ~4k.
- Removes __SWAP_BROKEN cases.

Tested on x86, mips, acorn32 (thanks <mpumford>) and partly tested on
acorn26 (thanks to <bjh21>).

Discussed on <tech-kern>, reviewed by <ad>.
 1.212 15-Aug-2009  pgoyette 1. Move the mode_select functionality into common code (in st.c) and
invoke the common routine for both scsi and atapi tapes.

2. Replace a numeric constant with some sizeof's when calculating the
size of the mode_select command buffer, clear the entire buffer, and
KASSERT to ensure the page_0_size loaded from quirk table is valid.

3. Add a quirk for my Seagate Travan-40 tape drive.

As discussed on tech-kern@

Addresses my PR kern/34832
 1.211 12-May-2009  cegger struct device * -> device_t, no functional changes intended.
 1.210 13-Jan-2009  yamt branches: 1.210.2;
g/c BUFQ_FOO() macros and use bufq_foo() directly.
 1.209 11-Jan-2009  cegger make this compile
 1.208 17-Nov-2008  bouyer If we return an error make sure we don't return a b_resid of 0; or this
will trigger a KASSERT in physio_done(). Fix issue reported by Todd Kover
on tech-kern@
 1.207 02-Sep-2008  dholland branches: 1.207.2; 1.207.4;
Reword some misleading code.
ok bouyer@
 1.206 01-Sep-2008  dholland fix typo in comment
 1.205 08-Jun-2008  tsutsui branches: 1.205.4;
Use device_lookup_private() rather than using cd_devs[] directly to get softc.

XXX maybe we should change a type of cd_devs[] in struct cfdriver
from (void *) to device_t.
 1.204 28-Apr-2008  martin branches: 1.204.2;
Remove clause 3 and 4 from TNF licenses
 1.203 05-Apr-2008  cegger branches: 1.203.2; 1.203.4;
use aprint_*_dev and device_xname
 1.202 11-Oct-2007  christos branches: 1.202.18;
PR/37108: Pauly Goyette: Compute the timeout value, before immediate is
changed for atapi tape drives.
 1.201 06-Oct-2007  bouyer Move check for b_resid value to the right place. Should definitively fix
kern/36690.
 1.200 01-Oct-2007  bouyer When checking for invalid b_resid valyes, also check negative ones.
Should fix kern/36690.
 1.199 29-Sep-2007  bouyer Properly set b_resid in an error case.
Clamp b_resid to b_bcount in case the info field is larger. Fix a KASSERT
in physio() when a media error occurs on my SDLT320 drive (maybe this drive
is bogus for not setting the highter bits to 0 in the INFO field in this
case).
 1.198 29-Jul-2007  ad branches: 1.198.4; 1.198.6; 1.198.8; 1.198.10;
It's not a good idea for device drivers to modify b_flags, as they don't
need to understand the locking around that field. Instead of setting
B_ERROR, set b_error instead. b_error is 'owned' by whoever completes
the I/O request.
 1.197 09-Jul-2007  ad branches: 1.197.2;
Merge some of the less invasive changes from the vmlocking branch:

- kthread, callout, devsw API changes
- select()/poll() improvements
- miscellaneous MT safety improvements
 1.196 04-Mar-2007  christos branches: 1.196.2; 1.196.4;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.195 01-Mar-2007  thorpej TRUE -> true, FALSE -> false
 1.194 16-Nov-2006  christos branches: 1.194.2; 1.194.4; 1.194.8;
__unused removal on arguments; approved by core.
 1.193 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.192 23-Aug-2006  christos branches: 1.192.2; 1.192.4;
Change iostat_alloc() to take the parent pointer and the name directly, so
that callers are not responsible for initializing the fields. Store the name
inside the struct instead of maintaining a pointer to external storage, or
leaked memory (nfs case).
 1.191 07-Jun-2006  kardel merge FreeBSD timecounters from branch simonb-timecounters
- struct timeval time is gone
time.tv_sec -> time_second
- struct timeval mono_time is gone
mono_time.tv_sec -> time_uptime
- access to time via
{get,}{micro,nano,bin}time()
get* versions are fast but less precise
- support NTP nanokernel implementation (NTP API 4)
- further reading:
Timecounter Paper: http://phk.freebsd.dk/pubs/timecounter.pdf
NTP Nanokernel: http://www.eecis.udel.edu/~mills/ntp/html/kern.html
 1.190 20-Apr-2006  blymn branches: 1.190.2;
Prefix iostat structure elements with io_
 1.189 14-Apr-2006  blymn Make i/o statistics collection more generic, include tape drives and
nfs mounts in the set of devices that statistics will be reported on.
 1.188 30-Mar-2006  thorpej Use device_private().
 1.187 28-Mar-2006  thorpej Use device_unit().
 1.186 11-Dec-2005  christos branches: 1.186.4; 1.186.6; 1.186.8; 1.186.10; 1.186.12;
merge ktrace-lwp.
 1.185 15-Oct-2005  yamt - change the way to specify a bufq strategy. (by string rather than by number)
- rather than embedding bufq_state in driver softc,
have a pointer to the former.
- move bufq related functions from kern/subr_disk.c to kern/subr_bufq.c.
- rename method to strategy for consistency.
- move some definitions which don't need to be exposed to the rest of kernel
from sys/bufq.h to sys/bufq_impl.h.
(is it better to move it to kern/ or somewhere?)
- fix some obvious breakage in dev/qbus/ts.c. (not tested)
 1.184 13-Aug-2005  blymn Split out stats attach code ready for shifting
Add sysctl hooks here for the time being.
 1.183 07-Aug-2005  blymn Gather statistics for tape drives.
 1.182 16-Jul-2005  rtr change output from "rogue" to "quirks apply" - resolves pr#25387
 1.181 29-May-2005  christos branches: 1.181.2;
- Sprinkle const
- Avoid variable shadowing.
- Eliminate some caddr_t abuse.
 1.180 25-Apr-2005  drochner revert the only part of rev. 1.177 which made a functional
change -- it broke error reporting because st->asc(q) are
not set
(actually, I believe that SSD_RCODE_VALID is misnamed)
 1.179 31-Mar-2005  yamt introduce a function to drain bufq and use it where appropriate.
 1.178 27-Feb-2005  perry branches: 1.178.2;
nuke trailing whitespace
 1.177 21-Feb-2005  thorpej Part 1 of a cleanup pass over the SCSI subsystem. The aim is to name
everything "scsi_*", since we really are talking about the SCSI command
set, ATAPI transport not withstanding. Improve the names of many structures,
and prepend "SCSI_" onto all SCSI command opcodes. Place items described
by the SCSI Primary Commands document into scsi_spc.h.
 1.176 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.175 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.174 31-Jan-2005  reinoud As part of cleaning up sys/scsipi, replace all u_char by uint8_t and
replace all `short' with int16_t.
 1.173 28-Oct-2004  yamt branches: 1.173.4; 1.173.6;
move buffer queue related stuffs from buf.h to their own header, bufq.h.
 1.172 26-Sep-2004  dogcow Fix debug message output args to match yamt's src/sys/sys/buf.h changes.
 1.171 18-Sep-2004  mycroft Minor rearrangement. Whitespace and #include cleanup.
 1.170 18-Sep-2004  mycroft Standardize some variable names and the calling pattern for scsipi_command().
Use void pointer casts.
 1.169 17-Sep-2004  mycroft Remove the "xfer" argument to scsipi_command().
 1.168 17-Sep-2004  mycroft In places where we've already called scsipi_make_xs(), call scsipi_execute_xs()
directly rather than going through scsipi_command().
 1.167 17-Sep-2004  mycroft Do not manipulate xs->bp in "generic" code -- do it only in the psw_done
routine. As part of this, pass down our pre-parsed error code -- though this
interface will probably change later to accomodate better error handling.
 1.166 09-Sep-2004  bouyer Make the xxstart() functions reentrant again, as some drivers HBA can call
scsipi_done() from their scsipi_request().
For this, add a struct scsipi_xfer * argument to scsipi_command().
If not NULL scsipi_command() will use this to enqueue this xfer, otherwise
it'll try to allocate a new one. This scsipi_xfer has to be allocated
and initialised by scsipi_make_xs() or equivalent.
In xxstart(), allocate a scsipi_xfer using scsipi_make_xs(), and if not NULL,
dequeue the buffer before calling scsipi_command(). This makes sure that
scsipi_command() will not fail, and also makes sure that xxstart() won't
be called again between the BUFQ_PEEK() and BUFQ_GET().

Fix "dequeued wrong buf" panics reported by Juergen Hannken-Illjes in
private mail and Andreas Wrede on current-users@.
Thanks to Jason Thorpe and Chuck Silver for review, and Andreas Wrede for
testing the patch.
 1.165 27-Aug-2004  bouyer Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
Havard Eidnes's analysis of this problem is that the scsipi_xfer pool is
competing for resources with other pools, including the the inode and vnode
pools which can grow quite large.

*_scsipi_cmd(): don't biodone the buffer if scsipi_make_xs() fails, let the
caller deal with the problem
start function of block devices drivers: dequeue the buffer after the
scsipi_command() call. If scsipi_command() fails with ENOMEM don't dequeue
the buffer, and schedule a callout to call the start function after
some delay.
scsipi_init(): prime the scsipi_xfer_pool with one page. This ensure that
there is always some scsipi_xfer to play with. If scsipi_command() fails
because of pool_get(), we're sure there will be resources available later,
when the pending commands have completed.

Reviewed by Jason Thorpe and Havard Eidnes.
Todo: remove the "unable to allocate scsipi_xfer" and "not queued, error %d"
printfs, but I choose to keep them for now, to help make sure the code does
what it should.
 1.164 21-Aug-2004  thorpej Use ANSI function decls and make use of static.
 1.163 29-Jun-2003  fvdl branches: 1.163.2; 1.163.4;
Back out the lwp/ktrace changes. They contained a lot of colateral damage,
and need to be examined and discussed more.
 1.162 28-Jun-2003  darrenr Pass lwp pointers throughtout the kernel, as required, so that the lwpid can
be inserted into ktrace records. The general change has been to replace
"struct proc *" with "struct lwp *" in various function prototypes, pass
the lwp through and use l_proc to get the process pointer when needed.

Bump the kernel rev up to 1.6V
 1.161 14-Apr-2003  perry Add a quirk for the Tandberg SLR/5
Fix supplied by Rex McMaster in PR kern/13603
 1.160 20-Mar-2003  dbj use PRId64 to printf bp->b_blkno, which is of type daddr_t
 1.159 03-Feb-2003  thorpej Test callout_pending(), not callout_active(), and eliminate now-unnecessary
callout_deactivate() calls.
 1.158 23-Oct-2002  jdolecek merge kqueue branch into -current

kqueue provides a stateful and efficient event notification framework
currently supported events include socket, file, directory, fifo,
pipe, tty and device changes, and monitoring of processes and signals

kqueue is supported by all writable filesystems in NetBSD tree
(with exception of Coda) and all device drivers supporting poll(2)

based on work done by Jonathan Lemon for FreeBSD
initial NetBSD port done by Luke Mewburn and Jason Thorpe
 1.157 06-Sep-2002  gehenna Merge the gehenna-devsw branch into the trunk.

This merge changes the device switch tables from static array to
dynamically generated by config(8).

- All device switches is defined as a constant structure in device drivers.

- The new grammer ``device-major'' is introduced to ``files''.

device-major <prefix> char <num> [block <num>] [<rules>]

- All device major numbers must be listed up in port dependent majors.<arch>
by using this grammer.

- Added the new naming convention.
The name of the device switch must be <prefix>_[bc]devsw for auto-generation
of device switch tables.

- The backward compatibility of loading block/character device
switch by LKM framework is broken. This is necessary to convert
from block/character device major to device name in runtime and vice versa.

- The restriction to assign device major by LKM is completely removed.
We don't need to reserve LKM entries for dynamic loading of device switch.

- In compile time, device major numbers list is packed into the kernel and
the LKM framework will refer it to assign device major number dynamically.
 1.156 22-Jul-2002  hannken Convert to new device buffer queue interface.
 1.155 05-May-2002  bouyer branches: 1.155.2;
If periph->periph_callout is already active, don't freeze the periph again:
scispi_periph_timed_thaw() will be called only one time anyway.
 1.154 03-May-2002  bouyer Back out previous. It has issues with multiple dumps to one tape using
/dev/nrstx, as pointed out by Matthew Jacob.
My problem is probably related to mt using /dev/nrst0 instead of /dev/enrst0
by default.
 1.153 02-May-2002  bouyer If we get a Unit Attention Not Ready To Ready Transition (medium may have
changed) sense, and the periph was not open, then ignore the error.
 1.152 22-Mar-2002  mjacob kern/16014- if we do an 'mt offline', make sure we've allowed the tape
to be ejected (PR_ALLOW). This got nuked in a previous change.
 1.151 22-Mar-2002  mjacob Forced commit so that some more detail on the previous patches can be
inserted into comments. I really think that more testing was required,
but Christos was eager.

+ Add in tape block/file position reporting support.

+ Change the prevent/allow ejection code so that if the device
is close the tape can be ejected. This allows robots to eject
the tape even if the tape isn't at BOT.

+ Don't do a null filemark write if we're reading block position. This
was such an abysmal performance killer that it had to go. Assume
that the tape will DTRT and tell you what the tape position really is
including any buffered data. You are depending on the tape to do
the right thing to report position at all, so the risk factors don't
increase appreciably here. BTW- we only do this for logical block
position. For 'hardware' block position, we still flush.

+ Some substantial tweaking in st_interpret_sense. This essentially duplicates
the FreeBSD driver's exception handling code. This is to try and correctly
handle EOM cases. I'm not sure that this is now correct with respect to
the ENABLE EARLY WARNING code- I hadn't gotten around to validating that.
Add in full 32 bit residuals- this can then be available to an update
MTIOCGET in the future.

+ Make sure we handle SKEY_VOLUME_OVERFLOW correctly.
 1.150 20-Mar-2002  christos Add the ability to report file number/block number. From Matt Jacob.
 1.149 12-Jan-2002  bouyer For fixed block drive, for invalid sense error code, set info to
xs->datalen / st->blksize instead of xs->datalen; as code assume later
that info is a block number.
Should fix kern/2727.
 1.148 07-Dec-2001  yamt add detach support for st.
 1.147 01-Dec-2001  bouyer Hum, no need to print a message each time we're waiting for the drive.
 1.146 01-Dec-2001  bouyer Various quirks for the ATAPI OnStream DI-30, mostly from the FreeBSD driver.
Many thanks to Chris Pinnock for giving me remote access to his hardware.
 1.145 15-Nov-2001  lukem don't need <sys/types.h> when including <sys/param.h>
 1.144 13-Nov-2001  lukem add RCSIDs
 1.143 18-Jul-2001  thorpej branches: 1.143.2;
bzero -> memset
 1.142 08-Jul-2001  wiz branches: 1.142.2;
Correct various misspellings of 'transfer' and inflected forms.
 1.141 18-Jun-2001  bouyer Snapshot of ATAPI tapes support. Known to be able to read/write to tape with:
st0 at atapibus0 drive 1: <Seagate STT8000A, , 5.51> type 1 sequential
removable
Major changes may still happen in order to properly support other ATAPI
tape drives.
 1.140 30-May-2001  bouyer uninitialised variable (from Krister Walfridsson):
in st_cmprss(), init flags to 0 (never changed, but this should change soon
anyway, for ATAPI tapes support)
 1.139 15-May-2001  lukem delint; use MAX(sizeof(..),sizeof(..)) instead of max(...) for array size
 1.138 14-May-2001  bouyer Use SCSI/ATAPI common definition for MODE_{SELECT,SENSE}{,_BIG}. Define
functions to send theses commands in scsipi_base.c and use them instead
of ad-hoc commands setups.
 1.137 06-May-2001  hannken Fix a typo from thorpej_scsipi merge. No-sense information with retval == 0
should only print if SCSIPI_DEBUG is defined.
 1.136 04-May-2001  bouyer Add an ATAPI front-end to the st driver. Completely untested for now,
but st at scsi should still work :)
 1.135 25-Apr-2001  bouyer Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.134 18-Jan-2001  jdolecek branches: 1.134.2;
constify
 1.133 22-Nov-2000  soren Remove obsolete comment.
 1.132 03-Nov-2000  pk st_touch_tape(): Pass XS_CTL_SILENT to st_mode_select().
 1.131 03-Nov-2000  pk * st_touch_tape(): start probing at the block size from the sense data if
we have it.

* st_unmount(): reset density to the device default. This prevents using
stale density values after changing to a medium with different density.

Section 9.3.3 of the SCSI specs states that a device shall return the
density value specified in the last succesfull MODE SELECT after an
unload operation, in case it is not able to automatically determine
the density of the new medium.
 1.130 03-Nov-2000  pk * Per st(4), retrieving status and setting modes on the control device
should succeed even if no media is loaded.

* In stopen(), check the return value of st_mount_tape().
 1.129 02-Nov-2000  pk Introduce ST_Q_ERASE_NOIMM, a quirk indicating that the device rejects
an ERASE command with the `Immed' bit on.

Use it on the VIPER 21247 & 21531.
 1.128 02-Nov-2000  pk Prevent printing sense information twice.
 1.127 02-Nov-2000  pk Remove useless check for SDEV_MEDIA_LOADED from st_read_block_limits().
This command should work whether or not media is present in the device.
 1.126 02-Nov-2000  pk * In st_touch_tape(), don't bail out if the MODE SELECT fails since we are,
after all, probing the device for acceptable parameters.

* In st_loadquirks(), copy mode-specific quirks from the quirk table
to `st_softc'; otherwise all such quirks save ST_Q_FORCE_BLKSIZE are ignored.
 1.125 16-Aug-2000  matt Add a NOPREVENT quirk which prevents the sending of PREVENT messages (some
manual 9track tapes don't support it). Add a quirk entry for the NCR H621
9track tape drive.
 1.124 09-Jun-2000  enami branches: 1.124.2;
Prevent a process being swapped out during I/O if the data buffer is
allocated on stack. This potential problem is noticed by Noriyuki Soda
and the idea and sample code to fix is given by Jason R. Thorpe.
 1.123 06-Jun-2000  soren Density codes are usually given in decimal, so print them that way.
 1.122 19-May-2000  kleink branches: 1.122.2;
Add a quirk table entry for the OnStream ADR50 Drive;
from S.P.Zeidler <spz@serpens.swb.de> in kern/10118.
 1.121 30-Mar-2000  augustss Get rid of register declarations.
 1.120 21-Feb-2000  mjacob Patches from msouth@scruz.net to handle QIC 3220 (HP T20).
 1.119 20-Feb-2000  mjacob minor spelling change
 1.118 22-Jan-2000  mjacob PR/9271 (from srp@zgi.com)- recognize (indirectly) HP4000s via density
code and do the right QIC type dance (1 FM @EOT, fixed block size).
 1.117 21-Jan-2000  thorpej Update for sys/buf.h/disksort_*() changes.
 1.116 13-Jan-2000  nisimura Add tweaks for TEAC compact cassette tape drive.
 1.115 12-Jan-2000  mjacob Check in the implementation of the ST_MOUNT_DELAY option.

This is an attempt to allow people to change the default configuration
to try harder at 'mounting' a tape. This allows you to specify, in
seconds, the amount of time a non-control unit open will retry
(once per second) the scsipi_test_unit_ready when it tries to mount
the tape. It also turns off the over-verbose error reporting at
this time unless SCSIDEBUG is set.

The reason this is not enabled as a default is that it's a large change
of behaviour. I find it useful to 'try harder' at mounting a tape in
the tape driver, particularly when loaded via a media changer device
rather than specifying the delays in the backup program.
 1.114 30-Sep-1999  thorpej branches: 1.114.2; 1.114.8;
Cleanup the scsipi_xfer flags:
- `flags' is now gone, replaced with `xs_control' and `xs_status'.
- Massive cleanup of the control flags. Now we explicitly say that
a job is to complete asynchronously, rather than relying on side-effects,
and use a new flag to now that device discovery is being performed.
- Do SCSI device discovery interrupt-driven.
 1.113 04-Sep-1999  simonb Fix tyop in a comment.
 1.112 17-Jun-1999  mjacob STK 9490 && SD-3 drives incorrectly quirked as not supporting the LOAD command.
Anyway, just because a drive doesn't support the LOAD (to BOT) command does
not mean that the drive doesn't support the UNLOAD command. Also note and
print errors in rewinds and unloads (and errors in writing closing filemarks
for same).
 1.111 05-Apr-1999  mycroft If scsipi_command() fails, always print out the error code.
 1.110 28-Feb-1999  explorer branches: 1.110.2; 1.110.4;
Update to slightly altered rnd_attach_source() api
 1.109 10-Feb-1999  bouyer Abort transfer if b_blkno is negative. Closes PR kern/5553 by Johan Danielsson.
 1.108 10-Jan-1999  tron Make MTIOCRDSPOS, MTIOCSLOCATE, etc. work with write protected tapes.
Patch supplied by Dave Huang in PR kern/5305.
 1.107 08-Dec-1998  thorpej When closing, wait for pending xfers to drain before deleting the reference
to the adapter.
 1.106 20-Nov-1998  thorpej Add adapter reference counting for SCSI and ATAPI devices.
 1.105 17-Nov-1998  bouyer Rename scsi_interpret_sense() to scsipi_interpret_sense() and move it from
scsi_base.c to scsipi_base.c. Rename the functions from scsi_verbose.c
too, and rename the file itself. Cleaup includes too (scsi_*.h should not
be #included in scsipi_*.h files, which are supposed to be
common to atapi and scsi).
 1.104 11-Sep-1998  mjacob keren/6128: add an entry for the TDC 4200. Full density code set isn't known.
 1.103 02-Sep-1998  mjacob typo for non-SCSIVERBOSE case
 1.102 31-Aug-1998  cgd kill the last remnants of __BROKEN_INDIRECT_CONFIG. (only the pica port
used it, and it's non-working and apparently slated for replacement.)
 1.101 17-Aug-1998  mycroft Assign my copyrights to TNF.
 1.100 15-Aug-1998  mycroft Make copyright notices with my name consistent.
 1.99 10-Aug-1998  mjacob Responding to an issue brought to my attention- when the device is
opened norewind and 2 filemarks are written at the end a phantom file
is left (just what I was afraid of, but I didn't think about it in
the last delta because somehow I had managed to convince myself that
this was a nonissue. It's not.).

So- in stdone clear ST_WRITTEN for regular reads. In st_close, preserve
the state of ST_WRITTEN, and if no error and 2FM@EOD for this device and
this is a no-rewind open, backspace one filemark. This should preserve
(for this mount session) FILE - FMK - FILE - FMK - FILE ... FILE FMK FMK EOD
sequencing.

This doesn't clean up the case of EOM appends- in this case you *will* still
get (after an MTEOM operation and a write of a file) a phantom empty file,
e.g. FILE - FMK FMK - FILE - FMK FMK EOD *unless* you follow the EOM operation
with an explicit backspace. The trouble is that this makes it difficult for
seamless interchange with other systems which don't necessarily follow.

The preferrable alternative would be to eliminate the 2FM@EOD except for
1/2" Reel tapes, but that has been pretty much nixed within developers.
 1.98 06-Aug-1998  drochner fix incorrect length of LOCATE CDB (reported in PR kern/5306 by
Dave Huang <khym@bga.com>)
 1.97 31-Jul-1998  mjacob Suggestion from Matthias Drochner: If at close you decide to write
filemark 'coz you opened write only and didn't do anything else,
call st_check_eod to possibly write TWO furshlugginer filemarks.

Also- return any errors from writing filemarks out of stclose.
 1.96 31-Jul-1998  mjacob Several more changes. First of all, move CTRL_MODE to have I/O behaviour
like a no-rewind device. Secondly figure out whether the initial TUR
for a CTRL_MODE open resulted in a tape being actually found (if so,
then do a mount session).

Move the 'sun compatibility' behaviour into stdone && stclose- don't
mark a tape as having been written in stopenm, fer gosh sakes.
 1.95 30-Jul-1998  mjacob Some minor comment tweaking.

Also- to be fair and on review, kern/391 isn't really addressed by
the previous commits. In reviewing, I'm embarassed to find that this
talks about reading at EOT. I'm actually going to claim that this
is 'not a bug' or 'fixed already' in that at the end of media (at the
edge of recorded media), you may continuously open the tape (should
you choose to) issue a read, and zero bytes will transfer- this is a
sufficient EOF indicator.
 1.94 30-Jul-1998  mjacob Clarify and name some of the 'open' modes. Clarify a few comments. Remove
a now unused variable. Also, remove the restriction against at density
code being greater than the max SCSI 2 density code: 0x80..0xff are the
Vendor Unique codes and most certainly should be allowed. The check for
invalid values should be less than 0 or greater than 255.

Oh- yeah, the previous commit addressed kern/391.
 1.93 30-Jul-1998  mjacob branches: 1.93.2;
Make some changes wrt EOM behaviour. Distinguish EIO_PENDING from
EOM_PENDING. Set up a persistent EARLYWARNING behaviour flag. If
set, EOM behaviour forces a 'short read' to signal logical (as
opposed to physical) end of media. The user application may, of
course, do with this information what it will.

The EARLYWARNING behaviour may be enabled/disabled by a MTIOCTOP
operation. The default action is to not have EARLYWARNING enabled-
but this may be reversed by an option ST_ENABLE_EARLYWARN in
the kernel build.
 1.92 19-Jul-1998  drochner Remove a check which restricted raw SCSI commands to the "minor 3"
("CTLMODE") subdevice. There are legitimate uses for raw commands with
normal tape handles too.
[I'm not sure if this is a final solution. Administrators might want
to set up a more finegrained policy. However, this should not be mixed
with the "set defaults" semantics of the "CTLMODE" subdevice; another
flag should be used instead (eg execute permission or a minor number bit).]
 1.91 15-Jul-1998  mjacob part of fix for kern/3835: use of enumerated returns from target sense handlers
 1.90 04-Jul-1998  mjacob Minor enhancements:

1) Quirk entries for Storage Tek 9490 (Timberline) and D3 (Redwood)
drives.

2) Modification to st_loadtape to do a REWIND to BOT if the
action is a load and the tape doesn't support the LOAD command
(9490, SD3, and IBM 3590).

3) Cleaned up the 'undersized user record' error message to
make a little more sense.

Various bug fixes:

kern/1275: Now returns values in dsreg and erreg and sets resid
(as best as it can for a 16 but integer). See also
a recent change to mtio.h. We are declining to fix
the portion of this bug about naming a more specific
SCSI device. Since there is nothing programmatic
you can do with that information, it is not useful
to pass back at this time.

A side effect of this change is that doing MTIOCGET
also forces a mode sense (to get the current state
of WRITE PROTECT).


kern/5647: Now no longer logs to the console ILI or Filemark or (first)
EOM (on write) errors (unless SCSIDEBUG is set).

kern/5525: Substantially increased timeouts for a variety of
operations, and split them into categories of
I/O, Space, and Control operations (each have
likely different inherent times). I/O is for
reads/writes. Control is for mode sense/select.
Space is for spacing the tape.

Until EOM handling is changed, though kern/391 is still not fixed. A side
effect of EOM handling is that you now always 'lose' (to the writing
application's view) the last write since EIO is what is returned on
EOM detection during writes. Hopefully the reader applications don't
get too bent out of shape by this.
 1.89 13-Feb-1998  enami Don't include <dev/scsipi/scsi_all.h> twice.
 1.88 07-Feb-1998  pk * Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP).
* Double timeout on tape position commands to 30 minutes. Some units
seem capable of taking that much time.
 1.87 12-Jan-1998  thorpej Adjust for changes to config.
 1.86 28-Dec-1997  is Added ST_Q_SENSE_HELP to the Archive Viper 150S quirks so that
density 0 (autosense) actually works.
 1.85 07-Nov-1997  mjacob If we're in the middle of opening, don't let st_interpret_sense get
into the act and return EIO or whatever when we're just eating the
initial TEST UNIT READY induced check condition.
 1.84 18-Oct-1997  thorpej branches: 1.84.2;
Implement two macros, scsipi_command() and scsipi_command_direct(), and
use them to hide the structure of the function pointers we jump through
to issue a command.
 1.83 16-Oct-1997  mycroft Define FALSE and TRUE locally.
 1.82 13-Oct-1997  explorer o Make usage of /dev/random dependant on
pseudo-device rnd # /dev/random and in-kernel generator
in config files.

o Add declaration to all architectures.

o Clean up copyright message in rnd.c, rnd.h, and rndpool.c to include
that this code is derived in part from Ted Tyso's linux code.
 1.81 12-Oct-1997  mjacob Hello? If we're doing block positioning a timeout of greater than 5 seconds
is probably going to be necessary.
 1.80 10-Oct-1997  explorer Add hooks to insert timing info into the random system
 1.79 09-Oct-1997  enami Cosmetic changes;

- dereference a pointer to function explicitly.
- fold long line to fit columns < 80.
- put whitespaces around a binary operator.
- don't put a single statement into a block.
 1.78 09-Oct-1997  mjacob A) At least IBM 3590 tape drives return a NOT READY error if issued a LOAD
to BOT command- even if at BOT. Urk. B) Make READ POSITION SCSI_SILENT.
 1.77 01-Oct-1997  mjacob Many thanks to Wolfgang Rupprecht for spotting this... In order to be sure
about actual position when reading tape position, you should flush any
pending writes. Well, if the tape is write protected, some drives don't
see the zero count WRITE FILE MARK command as a no-op and complain. Dumb!
 1.76 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.75 29-Sep-1997  mjacob Add in mtop operation MTCMPRESS, which can enable or disable tape compression.
The method for doing this is to try and use the DATA COMPRESSION mode page
first, followed by the DEVICE CONFIGURATION page (this is because most newer
tape devices are now using DATA COMPRESSION instead of DEVICE CONFIGURATION
pages).

Add in the logical && hardware read position and set position ioctls. Oddly
enough, because NetBSD is limited in having the driver track file && record
numbers, the usual agony over what to do once you use logical or hardware
block positioning can be avoided. Amusing.

Make a minor change so that for SCSIVERBOSE cases that SCSI_SILENT in
the xs' flags is still observed.
 1.74 13-Sep-1997  enami Declare SCSIVERBOSE by defopt in files.scsipi. Include opt_scsiverbose.h
in scsi_base.c and st.c.
 1.73 27-Aug-1997  bouyer branches: 1.73.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.72 20-Aug-1997  mjacob If SCSIVERBOSE is enabled, call the routine to print out detailed sense
information.
 1.71 21-Feb-1997  thorpej branches: 1.71.4;
If posting an error condition because the media has been unloaded,
make sure to set the residual count to reflect that no data was
transfered.

From Naofumi HONDA / MINOURA Makoto, PR #3007.
 1.70 05-Dec-1996  cgd branches: 1.70.6;
update these so they compile whether or not __BROKEN_INDIRECT_CONFIG
is defined.
 1.69 12-Oct-1996  christos revert previous kprintf change
 1.68 10-Oct-1996  christos printf -> kprintf, sprintf -> ksprintf
 1.67 24-May-1996  thorpej Implement ST_Q_UNIMODAL quirk, which indicates that a drive will reject
all attempts to mode select, even if the attempted mode is supported.
Add the ST_Q_UNIMODAL quirk for the HP Colorado T4000s which exhibits
this behavior. (Someone please lob a tactical nuke in that direction.)

From David Rosenthal <dshr@vitria.com> on netbsd-bugs.
 1.66 05-May-1996  christos Cleanup the rest of the SCSIDEBUG printfs. From Bernd Ernesti.
 1.65 30-Mar-1996  christos Eliminate scsi_conf.h.
 1.64 19-Mar-1996  mycroft Define a full set of [234][bl]tol() and lto[234][bl]() conversion functions,
inlined.
Use sized types in protocol structures.
Make the definition of scsi_sense_data less ugly.
 1.63 17-Mar-1996  thorpej New device attachment scheme:

- split softc size and match/attach out from cfdriver into
a new struct cfattach.

- new "attach" directive for files.*. May specify the name of
the cfattach structure, so that devices may be easily attached
to parents with different autoconfiguration semantics.
 1.62 05-Mar-1996  thorpej Normalize SCSI autoconfiguration output.
From Chris Demetriou <cgd@NetBSD.ORG>. Fixes PR #1958.
 1.61 18-Feb-1996  mycroft Minor change.
 1.60 17-Feb-1996  jtk remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
 1.59 14-Feb-1996  christos scsi prototypes
 1.58 09-Feb-1996  briggs PR#2048. Prototype st_erase().
 1.57 11-Jan-1996  thorpej Honor cache request and implement erase command, and add the SCSI tape
device configuration page. Fixes PRs 807, 1201, and 1705.
From John Kohl <jtk@kolvir.blrc.ma.us>.
 1.56 05-Jan-1996  pk SCSI sense `info' field is a signed value (from John Kohl; PR#1597).
 1.55 30-Nov-1995  pk Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797).
 1.54 13-Oct-1995  gwr Add quirk entries for Wangtek SCSI tapes. Also, add entry for
Tandberg 3800 (fixes PR#1592 - from Jochen Pohl).
 1.53 12-Aug-1995  mycroft opri --> s
 1.52 12-Aug-1995  mycroft Fix oversight in previous.
 1.51 12-Aug-1995  mycroft minphys() functions really should return void.
 1.50 24-Jul-1995  cgd update SCSI minphys routines' definitions to match standard minphys()
definition and usage.
 1.49 04-Jul-1995  mycroft Make each disk and tape driver define its own read and write functions.
Deprecate rawread() and rawwrite() completely. Remove d_strategy from cdevsw to
force the abstraction barrier.
 1.48 26-Jun-1995  cgd make dump stubs consistent
 1.47 25-Mar-1995  mycroft Add a rogue entry for the ANRITSU DMT780.
 1.46 30-Dec-1994  mycroft Update the rogue entry for ancient drivers to match reality.
 1.45 28-Dec-1994  mycroft Increase the st_space() timeout.
 1.44 28-Dec-1994  mycroft Numerous changes. Many bugs fixed, better autoconfig, a few new features.
 1.43 22-Nov-1994  mycroft Complete last change.
 1.42 21-Nov-1994  mycroft Return EIO when the drive is empty.
 1.41 21-Nov-1994  mycroft Clean up open and close routines somewhat.
 1.40 21-Nov-1994  mycroft Replace dev_unit with device_softc in scsi_link. Change argument to foostart()
to void*.
 1.39 30-Oct-1994  cgd be more careful with types, also pull in headers where necessary.
 1.38 20-Oct-1994  mycroft First cut at making user-level SCSI commands work. This is untested.
Partly from John Brezak.
 1.37 12-Aug-1994  deraadt un-aligned access is not cool on some processors
 1.36 09-Aug-1994  mycroft Change ST_Q_NEEDS_PAGE_0 to something more generic.
 1.35 29-Jun-1994  cgd branches: 1.35.2;
New RCS ID's, take two. they're more aesthecially pleasant, and use 'NetBSD'
 1.34 19-Jun-1994  mycroft Fix a comment.
 1.33 16-Jun-1994  chopps resolve confusion over who owns the buf after calling scsi_scsi_cmd()
 1.32 16-Jun-1994  mycroft b_un.b_addr -> b_data
 1.31 11-May-1994  mycroft Add dummy *dump() routines.
 1.30 09-May-1994  chopps remove union's from sense_data struct, conditionaly define RAW_PART
 1.29 24-Apr-1994  mycroft Deal with variable-length drives that don't specify a maximum block size.
c.f. SCSI 2 protocol definition.
 1.28 13-Apr-1994  mycroft Various cleanup, but no functional differences.
 1.27 11-Apr-1994  mycroft Fix various types. Remove some outdated flags.
 1.26 10-Apr-1994  mycroft Add another rogue...
 1.25 05-Apr-1994  mycroft This line is redundant for LD_UNLOAD (only one use, and it does this itself)
but breaks LD_RETEN, forcing a reload of the driver state on the next I/O
operation. Therefore, it goes away.
 1.24 05-Apr-1994  mycroft Reload the tape after retension.
 1.23 05-Apr-1994  mycroft Implement MTRETEN (untested).
 1.22 05-Apr-1994  mycroft Implement MTEOM, currently untested.
 1.21 01-Apr-1994  mycroft Deal with Mark Weaver's rogue tape drive.
 1.20 29-Mar-1994  mycroft New SCSI system, based on Julian's more recent work.
 1.19 06-Feb-1994  mycroft Remove another use of b_actl.
 1.18 11-Jan-1994  mycroft *strategy functions return void.
 1.17 17-Dec-1993  mycroft Canonicalize all #includes.
 1.16 01-Aug-1993  mycroft branches: 1.16.2;
Add RCS identifiers (this time on the correct side of the branch), and
incorporate recent changes in netbsd-0-9 branch.
 1.15 28-Jul-1993  cgd incorporate changes from 0-9-base to 0-9-ALPHA
 1.14 19-Jul-1993  cgd branches: 1.14.2;
patches from allen briggs to fix a minor bug in *attach()
 1.13 09-Jul-1993  cgd fix evil interaction with new physio; don't set error bit for short reads.
 1.12 27-Jun-1993  andrew ANSIfications.
 1.11 16-Jun-1993  andrew woops - another typo.
 1.10 16-Jun-1993  deraadt whoops. typo.
 1.9 16-Jun-1993  deraadt fix to intuit the maximum number of scsi units available on a device
driver. this piece at least, should be safe from changing sizeof(dev_t)
 1.8 20-May-1993  cgd add rcsids and clean up file headers
 1.7 04-May-1993  deraadt support for making dev->id_alive be set, this is for iostat to
find disk devices. wee bit of a kludge. sub-device attach()
routines must now return 1 for successful attach(), 0 otherwise.
Other bsd's do this too..
 1.6 22-Apr-1993  mycroft Fix up error messages and return values.
 1.5 20-Apr-1993  mycroft Display more meaningful message on SCSI `unit attention'.
 1.4 12-Apr-1993  deraadt fixed various bugs like cdattach() returning garbage.
 1.3 12-Apr-1993  deraadt new scsi subsystem.
changes also in config/mkioconf.c
i386/isa/wd.c, fd.c, and all scsi drivers.
 1.2 08-Apr-1993  glass attempting to open an already opened tape drive should get you EBUSY, not
ENXIO.
 1.1 21-Mar-1993  cgd after 0.2.2 "stable" patches applied
 1.14.2.2 31-Jul-1993  cgd give names, err, wmesg's, to my "pain" -- i.e. convert sleep() to tsleep()
 1.14.2.1 26-Jul-1993  briggs When stioctl() got called with an unknown command, it was not
returning an error code consistently. This change forces it
to return EINVAL when the command is unknown.
 1.16.2.12 16-Feb-1994  mycroft More KNF-like.
 1.16.2.11 15-Feb-1994  mycroft Reset SDEV_WAITING so we actually wake up the waiting process...
 1.16.2.10 06-Feb-1994  mycroft Remove another use of b_actl.
 1.16.2.9 01-Feb-1994  mycroft Fix field name clash.
 1.16.2.8 01-Feb-1994  mycroft Change some things into arrays.
 1.16.2.7 22-Jan-1994  briggs NST -> stcd.cd_ndevs in SC_DEBUG statement.
 1.16.2.6 29-Nov-1993  mycroft Return EBUSY, not ENXIO, if alredy open.
 1.16.2.5 25-Nov-1993  mycroft Correct attach message.
 1.16.2.4 25-Nov-1993  mycroft Correct the display of some messages.
 1.16.2.3 25-Nov-1993  mycroft Compiles. Will it work?
 1.16.2.2 24-Nov-1993  mycroft Under construction...
 1.16.2.1 24-Sep-1993  mycroft cd.c, sd.c, st.c: strategy functions return void.
 1.35.2.2 06-Oct-1994  mycroft Update from trunk.
 1.35.2.1 09-Aug-1994  mycroft update from trunk
 1.70.6.1 12-Mar-1997  is Merge in changes from Trunk
 1.71.4.1 23-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.73.2.4 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.73.2.3 16-Sep-1997  thorpej Update marc-pcmcia branch from trunk.
 1.73.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.73.2.1 27-Aug-1997  thorpej file st.c was added on branch marc-pcmcia on 1997-08-27 23:33:41 +0000
 1.84.2.2 30-Sep-1998  cgd pull up rev 1.98 (via patch; doesn't pull up cleanly) from trunk (drochner)
 1.84.2.1 07-Nov-1997  mellon Pull rev 1.85 up from trunk (mjacob)
 1.93.2.2 08-Aug-1998  eeh Revert cdevsw mmap routines to return int.
 1.93.2.1 30-Jul-1998  eeh file st.c was added on branch eeh-paddr_t on 1998-08-08 03:06:52 +0000
 1.110.4.1 21-Jun-1999  thorpej Sync w/ -current.
 1.110.2.1 23-Jan-2000  he Pull up revision 1.118 (requested by mjacob):
Recognize (indirectly) HP4000s via density code and do the right
QIC type dance. Fixes PR#9271.
 1.114.8.1 21-Dec-1999  wrstuden Initial commit of recent changes to make DEV_BSIZE go away.

Runs on i386, needs work on other arch's. Main kernel routines should be
fine, but a number of the stand programs need help.

cd, fd, ccd, wd, and sd have been updated. sd has been tested with non-512
byte block devices. vnd, raidframe, and lfs need work.

Non 2**n block support is automatic for LKM's and conditional for kernels
on "options NON_PO2_BLOCKS".
 1.114.2.8 29-Mar-2001  simonb Make this compile for the non-SCSIVERBOSE case.
 1.114.2.7 11-Feb-2001  bouyer Sync with HEAD.
 1.114.2.6 08-Dec-2000  bouyer Sync with HEAD.
 1.114.2.5 22-Nov-2000  bouyer Sync with HEAD.
 1.114.2.4 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.114.2.3 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.114.2.2 20-Oct-1999  thorpej Remove an unnecessary comment regarding XS_CTL_NOSLEEP.
 1.114.2.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.122.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.124.2.1 03-Nov-2000  tv Pullup 1.127 [pk]:
Remove useless check for SDEV_MEDIA_LOADED from st_read_block_limits().
This command should work whether or not media is present in the device.
 1.134.2.10 11-Nov-2002  nathanw Catch up to -current
 1.134.2.9 17-Sep-2002  nathanw Catch up to -current.
 1.134.2.8 01-Aug-2002  nathanw Catch up to -current.
 1.134.2.7 20-Jun-2002  nathanw Catch up to -current.
 1.134.2.6 01-Apr-2002  nathanw Catch up to -current.
(CVS: It's not just a program. It's an adventure!)
 1.134.2.5 28-Feb-2002  nathanw Catch up to -current.
 1.134.2.4 08-Jan-2002  nathanw Catch up to -current.
 1.134.2.3 14-Nov-2001  nathanw Catch up to -current.
 1.134.2.2 24-Aug-2001  nathanw Catch up with -current.
 1.134.2.1 21-Jun-2001  nathanw Catch up to -current.
 1.142.2.6 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.142.2.5 06-Sep-2002  jdolecek sync kqueue branch with HEAD
 1.142.2.4 23-Jun-2002  jdolecek catch up with -current on kqueue branch
 1.142.2.3 11-Feb-2002  jdolecek Sync w/ -current.
 1.142.2.2 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.142.2.1 03-Aug-2001  lukem update to -current
 1.143.2.2 26-Sep-2001  fvdl * add a VCLONED vnode flag that indicates a vnode representing a cloned
device.
* rename REVOKEALL to REVOKEALIAS, and add a REVOKECLONE flag, to pass
to VOP_REVOKE
* the revoke system call will revoke all aliases, as before, but not the
clones
* vdevgone is called when detaching a device, so make it use REVOKECLONE
to get rid of all clones as well
* clean up all uses of VOP_OPEN wrt. locking.
* add a few VOPS to spec_vnops that need to do something when it's a
clone vnode (access and getattr)
* add a copy of the vnode vattr structure of the original 'master' vnode
to the specinfo of a cloned vnode. could possibly redirect getattr to
the 'master' vnode, but this has issues with revoke
* add a vdev_reassignvp function that disassociates a vnode from its
original device, and reassociates it with the specified dev_t. to be
used by cloning devices only, in case a new minor is allocated.
* change all direct references in drivers to v_devcookie and v_rdev
to vdev_privdata(vp) and vdev_rdev(vp). for diagnostic purposes
when debugging race conditions that still exist wrt. locking and
revoking vnodes.
* make the locking state of a vnode consistent when passed to
d_open and d_close (unlocked). locked would be better, but has
some deadlock issues
 1.143.2.1 07-Sep-2001  thorpej Commit my "devvp" changes to the thorpej-devvp branch. This
replaces the use of dev_t in most places with a struct vnode *.

This will form the basic infrastructure for real cloning device
support (besides being architecurally cleaner -- it'll be good
to get away from using numbers to represent objects).
 1.155.2.2 29-Aug-2002  gehenna catch up with -current.
 1.155.2.1 16-May-2002  gehenna Add the block/character device switches.
Replace the direct-access to devsw table with calling devsw API.
 1.163.4.1 11-Sep-2004  he Pull up revisions 1.165-1.166 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.163.2.10 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.163.2.9 01-Apr-2005  skrll Sync with HEAD.
 1.163.2.8 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.163.2.7 02-Nov-2004  skrll Sync with HEAD.
 1.163.2.6 19-Oct-2004  skrll Sync with HEAD
 1.163.2.5 21-Sep-2004  skrll Fix the sync with head I botched.
 1.163.2.4 18-Sep-2004  skrll Sync with HEAD.
 1.163.2.3 03-Sep-2004  skrll Sync with HEAD
 1.163.2.2 25-Aug-2004  skrll Sync with HEAD.
 1.163.2.1 02-Jul-2003  darrenr Apply the aborted ktrace-lwp changes to a specific branch. This is just for
others to review, I'm concerned that patch fuziness may have resulted in some
errant code being generated but I'll look at that later by comparing the diff
from the base to the branch with the file I attempt to apply to it. This will,
at the very least, put the changes in a better context for others to review
them and attempt to tinker with removing passing of 'struct lwp' through
the kernel.
 1.173.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.173.4.1 29-Apr-2005  kent sync with -current
 1.178.2.3 02-Dec-2007  riz Pull up following revision(s) (requested by bouyer in ticket #1880):
sys/dev/scsipi/st.c: revision 1.199-1.201 via patch
Properly set b_resid in an error case.
Clamp b_resid to b_bcount in case the info field is larger. Fix a KASSERT
in physio() when a media error occurs on my SDLT320 drive (maybe this drive
is bogus for not setting the highter bits to 0 in the INFO field in this
case).
When checking for invalid b_resid valyes, also check negative ones.
Should fix kern/36690.
Move check for b_resid value to the right place. Should definitively fix
kern/36690.
 1.178.2.2 17-Sep-2007  msaitoh Pull up following revision(s) (requested by bouyer in ticket #1837):
sys/dev/scsipi/st.c: revision 1.180
revert the only part of rev. 1.177 which made a functional
change -- it broke error reporting because st->asc(q) are
not set
(actually, I believe that SSD_RCODE_VALID is misnamed)
 1.178.2.1 06-Apr-2005  tron Pull up revision 1.179 (requested by yamt in ticket #112):
introduce a function to drain bufq and use it where appropriate.
 1.181.2.4 27-Oct-2007  yamt sync with head.
 1.181.2.3 03-Sep-2007  yamt sync with head.
 1.181.2.2 30-Dec-2006  yamt sync with head.
 1.181.2.1 21-Jun-2006  yamt sync with head.
 1.186.12.2 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.186.12.1 31-Mar-2006  tron Merge 2006-03-31 NetBSD-current into the "peter-altq" branch.
 1.186.10.2 11-May-2006  elad sync with head
 1.186.10.1 19-Apr-2006  elad sync with head.
 1.186.8.4 03-Sep-2006  yamt sync with head.
 1.186.8.3 26-Jun-2006  yamt sync with head.
 1.186.8.2 24-May-2006  yamt sync with head.
 1.186.8.1 01-Apr-2006  yamt sync with head.
 1.186.6.2 22-Apr-2006  simonb Sync with head.
 1.186.6.1 04-Feb-2006  simonb Adapt for timecounters: mostly use get*time() and use "time_second"
instead of "time.tv_sec".
 1.186.4.1 09-Sep-2006  rpaulo sync with head
 1.190.2.1 19-Jun-2006  chap Sync with head.
 1.192.4.2 10-Dec-2006  yamt sync with head.
 1.192.4.1 22-Oct-2006  yamt sync with head
 1.192.2.1 18-Nov-2006  ad Sync with head.
 1.194.8.1 06-Jan-2008  wrstuden Catch up to netbsd-4.0 release.
 1.194.4.1 12-Mar-2007  rmind Sync with HEAD.
 1.194.2.1 25-Nov-2007  xtraeme Pull up following revision(s) (requested by bouyer in ticket #1002):
sys/dev/scsipi/st.c: revision 1.199 (patch)
sys/dev/scsipi/st.c: revision 1.200 (patch)
sys/dev/scsipi/st.c: revision 1.201 (patch)
Properly set b_resid in an error case.
Clamp b_resid to b_bcount in case the info field is larger. Fix a KASSERT
in physio() when a media error occurs on my SDLT320 drive (maybe this drive
is bogus for not setting the highter bits to 0 in the INFO field in this
case).
When checking for invalid b_resid valyes, also check negative ones.
Should fix kern/36690.
Move check for b_resid value to the right place. Should definitively fix
kern/36690.
 1.196.4.1 11-Jul-2007  mjf Sync with head.
 1.196.2.7 12-Oct-2007  ad Sync with head.
 1.196.2.6 09-Oct-2007  ad Sync with head.
 1.196.2.5 09-Oct-2007  ad Sync with head.
 1.196.2.4 19-Aug-2007  ad - Back out the biodone() changes.
- Eliminate B_ERROR (from HEAD).
 1.196.2.3 15-Jul-2007  ad Sync with head.
 1.196.2.2 01-Jul-2007  ad Adapt to callout API change.
 1.196.2.1 13-May-2007  ad - Pass the error number and residual count to biodone(), and let it handle
setting error indicators. Prepare to eliminate B_ERROR.
- Add a flag argument to brelse() to be set into the buf's flags, instead
of doing it directly. Typically used to set B_INVAL.
- Add a "struct cpu_info *" argument to kthread_create(), to be used to
create bound threads. Change "bool mpsafe" to "int flags".
- Allow exit of LWPs in the IDL state when (l != curlwp).
- More locking fixes & conversion to the new API.
 1.197.2.1 15-Aug-2007  skrll Sync with HEAD.
 1.198.10.2 29-Jul-2007  ad It's not a good idea for device drivers to modify b_flags, as they don't
need to understand the locking around that field. Instead of setting
B_ERROR, set b_error instead. b_error is 'owned' by whoever completes
the I/O request.
 1.198.10.1 29-Jul-2007  ad file st.c was added on branch matt-mips64 on 2007-07-29 12:50:24 +0000
 1.198.8.2 14-Oct-2007  yamt sync with head.
 1.198.8.1 06-Oct-2007  yamt sync with head.
 1.198.6.1 06-Nov-2007  matt sync with HEAD
 1.198.4.3 26-Oct-2007  joerg Sync with HEAD.

Follow the merge of pmap.c on i386 and amd64 and move
pmap_init_tmp_pgtbl into arch/x86/x86/pmap.c. Modify the ACPI wakeup
code to restore CR4 before jumping back into kernel space as the large
page option might cover that.
 1.198.4.2 07-Oct-2007  joerg Sync with HEAD.
 1.198.4.1 02-Oct-2007  joerg Sync with HEAD.
 1.202.18.5 17-Jan-2009  mjf Sync with HEAD.
 1.202.18.4 28-Sep-2008  mjf Sync with HEAD.
 1.202.18.3 29-Jun-2008  mjf Sync with HEAD.
 1.202.18.2 02-Jun-2008  mjf Sync with HEAD.
 1.202.18.1 05-Apr-2008  mjf - add "file-system DEVFS" and "pseudo-device devfsctl" to conf/std seeing
as these are always needed.

- convert many, many drivers over to the New Devfs World Order. For a
list of device drivers yet to be converted see,
http://www.netbsd.org/~mjf/devfs-todo.html.

- add a new device_unregister_all(device_t) function to remove all device
names associated with a device_t, which saves us having to construct
device names when the driver is detached.

- add a DEV_AUDIO type for devices.
 1.203.4.6 11-Aug-2010  yamt sync with head.
 1.203.4.5 11-Mar-2010  yamt sync with head
 1.203.4.4 19-Aug-2009  yamt sync with head.
 1.203.4.3 16-May-2009  yamt sync with head
 1.203.4.2 04-May-2009  yamt sync with head.
 1.203.4.1 16-May-2008  yamt sync with head.
 1.203.2.2 17-Jun-2008  yamt sync with head.
 1.203.2.1 18-May-2008  yamt sync with head.
 1.204.2.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.204.2.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.205.4.2 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.205.4.1 19-Oct-2008  haad Sync with HEAD.
 1.207.4.1 20-Nov-2008  snj Pull up following revision(s) (requested by bouyer in ticket #78):
sys/dev/scsipi/st.c: revision 1.208
If we return an error make sure we don't return a b_resid of 0; or this
will trigger a KASSERT in physio_done(). Fix issue reported by Todd Kover
on tech-kern@
 1.207.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.210.2.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.216.4.1 03-Jul-2010  rmind sync with head
 1.216.2.1 17-Aug-2010  uebayasi Sync with HEAD.
 1.217.12.5 29-Apr-2012  mrg sync to latest -current.
 1.217.12.4 06-Mar-2012  mrg sync to -current
 1.217.12.3 06-Mar-2012  mrg sync to -current
 1.217.12.2 04-Mar-2012  mrg sync to latest -current.
 1.217.12.1 18-Feb-2012  mrg merge to -current.
 1.217.8.3 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.217.8.2 23-May-2012  yamt sync with head.
 1.217.8.1 17-Apr-2012  yamt sync with head
 1.218.2.2 23-Apr-2012  riz Pull up following revision(s) (requested by bouyer in ticket #192):
sys/dev/scsipi/cd.c: revision 1.307
sys/dev/scsipi/scsiconf.c: revision 1.266
sys/dev/scsipi/sd.c: revision 1.298
sys/dev/scsipi/st_scsi.c: revision 1.35
sys/dev/scsipi/atapiconf.c: revision 1.85
sys/dev/scsipi/scsipiconf.h: revision 1.120
sys/dev/usb/umass_scsipi.c: revision 1.44
sys/dev/scsipi/scsiconf.h: revision 1.57
sys/dev/scsipi/st_atapi.c: revision 1.29
sys/dev/scsipi/scsipi_base.c: revision 1.158
sys/dev/scsipi/st.c: revision 1.221
sys/dev/scsipi/scsipi_ioctl.c: revision 1.67
Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.
This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.
In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.
Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.218.2.1 02-Mar-2012  riz Pull up following revision(s) (requested by mbalmer in ticket #64):
sys/dev/scsipi/stvar.h: revision 1.24
sys/dev/scsipi/st_scsi.c: revision 1.34
sys/dev/scsipi/st_atapi.c: revision 1.28
sys/dev/scsipi/st.c: revision 1.220
Convert st(4) to device_t, while here clean up the code and use uintXX_t
instead of u_intXX_t.
 1.221.4.1 18-May-2014  rmind sync with head
 1.221.2.2 03-Dec-2017  jdolecek update from HEAD
 1.221.2.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.223.2.2 10-Aug-2014  tls Rebase.
 1.223.2.1 07-Apr-2014  tls Be a little more clear and consistent about harvesting entropy from devices:

1) deprecate RND_FLAG_NO_ESTIMATE

2) define RND_FLAG_COLLECT_TIME, RND_FLAG_COLLECT_VALUE

3) define RND_FLAG_ESTIMATE_TIME, RND_FLAG_ESTIMATE_VALUE

4) define RND_FLAG_DEFAULT: RND_FLAG_COLLECT_TIME|
RND_FLAG_COLLECT_VALUE|RND_FLAG_ESTIMATE_TIME

5) Make entropy harvesting from environmental sensors a little more generic
and remove it from individual sensor drivers.

6) Remove individual open-coded delta-estimators for values from a few
places in the tree (uvm, environmental drivers).

7) 0 -> RND_FLAG_DEFAULT, actually gather entropy from various drivers
that had stubbed out code, other minor cleanups.
 1.226.10.1 07-Mar-2019  martin Pull up following revision(s) (requested by kardel in ticket #1682):

sys/dev/scsipi/st.c: revision 1.236 (patch)
sys/dev/scsipi/st.c: revision 1.237
sys/dev/scsipi/files.scsipi: revision 1.42

Fix PR kern/53949:
Fix inconsistent/incomplete file mark handling to conform again
to mtio(4) at close(2) time. This was necessary as the PREVENT/ALLOW
bracket was reduced from a whole mount session to cover only the
open(2)/close(2) time on ~2002-03-22. The rationale was to allow
robots and humans to change the media during a mount session.

Unfortunately this lead to file marks being written to potentially other
media at the beginning on drives that used the two file marks as EOM
pattern. In order for that to happen the media had to be removed after
data and at most one file mark had been written before removal.

The mount error message has been clarified and a warning about
potential data/file mark lossage on UNIT ATTENTION
during an active mount session with unfinished file marks has been
added.

While there, fix, but disable the commented SUN compatibility to write
final file marks by opening and immediately closing the device
in O_WRONLY mode. That code has not been working since around 1998.

It can now be enabled with options ST_SUNCOMPAT.

Additionally debug output coverage has been extended.

Correct printing type of b_blkno (int64_t) in st.c

Fixes build with kUBSan on NetBSD/i386.

Fix, but disable the commented SUN compatibility in st.c to write
final file marks by opening and immediately closing the device
in O_WRONLY mode. That code has not been working since around 1998.
It can now be enabled with options ST_SUNCOMPAT.
 1.226.6.1 07-Mar-2019  martin Pull up following revision(s) (requested by kardel in ticket #1682):

sys/dev/scsipi/st.c: revision 1.236 (patch)
sys/dev/scsipi/st.c: revision 1.237
sys/dev/scsipi/files.scsipi: revision 1.42

Fix PR kern/53949:
Fix inconsistent/incomplete file mark handling to conform again
to mtio(4) at close(2) time. This was necessary as the PREVENT/ALLOW
bracket was reduced from a whole mount session to cover only the
open(2)/close(2) time on ~2002-03-22. The rationale was to allow
robots and humans to change the media during a mount session.

Unfortunately this lead to file marks being written to potentially other
media at the beginning on drives that used the two file marks as EOM
pattern. In order for that to happen the media had to be removed after
data and at most one file mark had been written before removal.

The mount error message has been clarified and a warning about
potential data/file mark lossage on UNIT ATTENTION
during an active mount session with unfinished file marks has been
added.

While there, fix, but disable the commented SUN compatibility to write
final file marks by opening and immediately closing the device
in O_WRONLY mode. That code has not been working since around 1998.

It can now be enabled with options ST_SUNCOMPAT.

Additionally debug output coverage has been extended.

Correct printing type of b_blkno (int64_t) in st.c

Fixes build with kUBSan on NetBSD/i386.

Fix, but disable the commented SUN compatibility in st.c to write
final file marks by opening and immediately closing the device
in O_WRONLY mode. That code has not been working since around 1998.
It can now be enabled with options ST_SUNCOMPAT.
 1.226.4.4 28-Aug-2017  skrll Sync with HEAD
 1.226.4.3 05-Dec-2016  skrll Sync with HEAD
 1.226.4.2 05-Oct-2016  skrll Sync with HEAD
 1.226.4.1 22-Sep-2015  skrll Sync with HEAD
 1.226.2.1 07-Mar-2019  martin Pull up following revision(s) (requested by kardel in ticket #1682):

sys/dev/scsipi/st.c: revision 1.236 (patch)
sys/dev/scsipi/st.c: revision 1.237
sys/dev/scsipi/files.scsipi: revision 1.42

Fix PR kern/53949:
Fix inconsistent/incomplete file mark handling to conform again
to mtio(4) at close(2) time. This was necessary as the PREVENT/ALLOW
bracket was reduced from a whole mount session to cover only the
open(2)/close(2) time on ~2002-03-22. The rationale was to allow
robots and humans to change the media during a mount session.

Unfortunately this lead to file marks being written to potentially other
media at the beginning on drives that used the two file marks as EOM
pattern. In order for that to happen the media had to be removed after
data and at most one file mark had been written before removal.

The mount error message has been clarified and a warning about
potential data/file mark lossage on UNIT ATTENTION
during an active mount session with unfinished file marks has been
added.

While there, fix, but disable the commented SUN compatibility to write
final file marks by opening and immediately closing the device
in O_WRONLY mode. That code has not been working since around 1998.

It can now be enabled with options ST_SUNCOMPAT.

Additionally debug output coverage has been extended.

Correct printing type of b_blkno (int64_t) in st.c

Fixes build with kUBSan on NetBSD/i386.

Fix, but disable the commented SUN compatibility in st.c to write
final file marks by opening and immediately closing the device
in O_WRONLY mode. That code has not been working since around 1998.
It can now be enabled with options ST_SUNCOMPAT.
 1.228.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.230.8.4 04-Nov-2019  martin Pull up following revision(s) (requested by jnemeth in ticket #1421):

sys/dev/scsipi/st.c: revision 1.238

Add simple position recovery when positioning to EOM by reading
the position with READ_POSITION.
this allows for

mt eom
mt st

to return the correct file position.
 1.230.8.3 01-Mar-2019  martin Pull up following revision(s) (requested by kardel in ticket #1197):

sys/dev/scsipi/st.c: revision 1.236
sys/dev/scsipi/st.c: revision 1.237
sys/dev/scsipi/files.scsipi: revision 1.42

Fix PR kern/53949:

Fix inconsistent/incomplete file mark handling to conform again
to mtio(4) at close(2) time. This was necessary as the PREVENT/ALLOW
bracket was reduced from a whole mount session to cover only the
open(2)/close(2) time on ~2002-03-22. The rationale was to allow
robots and humans to change the media during a mount session.

Unfortunately this lead to file marks being written to potentially other
media at the beginning on drives that used the two file marks as EOM
pattern. In order for that to happen the media had to be removed after
data and at most one file mark had been written before removal.

The mount error message has been clarified and a warning about
potential data/file mark lossage on UNIT ATTENTION
during an active mount session with unfinished file marks has been
added.

While there, fix, but disable the commented SUN compatibility to write
final file marks by opening and immediately closing the device
in O_WRONLY mode. That code has not been working since around 1998.
It can now be enabled with options ST_SUNCOMPAT.
Additionally debug output coverage has been extended.

-

Correct printing type of b_blkno (int64_t) in st.c

Fixes build with kUBSan on NetBSD/i386.
Fix, but disable the commented SUN compatibility in st.c to write
final file marks by opening and immediately closing the device
in O_WRONLY mode. That code has not been working since around 1998.
It can now be enabled with options ST_SUNCOMPAT.
 1.230.8.2 08-Apr-2018  snj Pull up following revision(s) (requested by mlelstv in ticket #703):
sys/dev/scsipi/st.c: 1.234
sys/dev/scsipi/stvar.h: 1.26
Use separate lock to protect internal state and release locks when
calling biodone.
 1.230.8.1 21-Jun-2017  snj Pull up following revision(s) (requested by mlelstv in ticket #53):
sys/dev/scsipi/atapiconf.c: revision 1.91
sys/dev/scsipi/cd.c: revision 1.341
sys/dev/scsipi/scsi_base.c: revision 1.92
sys/dev/scsipi/scsiconf.c: revision 1.280
sys/dev/scsipi/scsipi_base.c: revisions 1.176, 1.177
sys/dev/scsipi/sd.c: revision 1.325
sys/dev/scsipi/ss.c: revision 1.89
sys/dev/scsipi/st.c: revision 1.231
The atapibus detach path did hold the channel mutex while calling into autoconf,
which would trigger a panic when unplugging a USB ATAPI CDROM.
Align detach code for scsibus and atapibus to fix this.
Also avoid races when detaching devices by replacing callout_stop with
callout_halt.
--
pass config_detach error to caller.
 1.231.4.1 30-Mar-2018  pgoyette Resolve conflicts between branch and HEAD
 1.234.2.3 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.234.2.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.234.2.1 10-Jun-2019  christos Sync with HEAD
 1.240.12.1 17-Jun-2021  thorpej Sync w/ HEAD.
 1.31 14-Jul-2016  msaitoh - Use aprint*() instead of printf() in xxx_attach().
- Add missing aprint_naive("\n");
- KNF
 1.30 24-Aug-2015  pooka would you like some freshly ground _KERNEL_OPT with that?
yes? excellent choice, sir/madam.
 1.29 19-Apr-2012  bouyer branches: 1.29.2; 1.29.16;
Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.

This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.

In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.

Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.28 28-Feb-2012  mbalmer Convert st(4) to device_t, while here clean up the code and use uintXX_t
instead of u_intXX_t.
 1.27 02-Feb-2012  tls branches: 1.27.2;
Entropy-pool implementation move and cleanup.

1) Move core entropy-pool code and source/sink/sample management code
to sys/kern from sys/dev.

2) Remove use of NRND as test for presence of entropy-pool code throughout
source tree.

3) Remove use of RND_ENABLED in device drivers as microoptimization to
avoid expensive operations on disabled entropy sources; make the
rnd_add calls do this directly so all callers benefit.

4) Fix bug in recent rnd_add_data()/rnd_add_uint32() changes that might
have lead to slight entropy overestimation for some sources.

5) Add new source types for environmental sensors, power sensors, VM
system events, and skew between clocks, with a sample implementation
for each.

ok releng to go in before the branch due to the difficulty of later
pullup (widespread #ifdef removal and moved files). Tested with release
builds on amd64 and evbarm and live testing on amd64.
 1.26 06-Dec-2009  dyoung branches: 1.26.12; 1.26.16;
Delete do-nothing device-activation hooks.
 1.25 21-Oct-2009  rmind Remove uarea swap-out functionality:

- Addresses the issue described in PR/38828.
- Some simplification in threading and sleepq subsystems.
- Eliminates pmap_collect() and, as a side note, allows pmap optimisations.
- Eliminates XS_CTL_DATA_ONSTACK in scsipi code.
- Avoids few scans on LWP list and thus potentially long holds of proc_lock.
- Cuts ~1.5k lines of code. Reduces amd64 kernel size by ~4k.
- Removes __SWAP_BROKEN cases.

Tested on x86, mips, acorn32 (thanks <mpumford>) and partly tested on
acorn26 (thanks to <bjh21>).

Discussed on <tech-kern>, reviewed by <ad>.
 1.24 19-Oct-2009  bouyer Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.23 15-Aug-2009  pgoyette 1. Move the mode_select functionality into common code (in st.c) and
invoke the common routine for both scsi and atapi tapes.

2. Replace a numeric constant with some sizeof's when calculating the
size of the mode_select command buffer, clear the entire buffer, and
KASSERT to ensure the page_0_size loaded from quirk table is valid.

3. Add a quirk for my Seagate Travan-40 tape drive.

As discussed on tech-kern@

Addresses my PR kern/34832
 1.22 12-May-2009  cegger struct device * -> device_t, no functional changes intended.
 1.21 12-May-2009  cegger struct cfdata * -> cfdata_t, no functional changes intended.
 1.20 16-Nov-2006  christos branches: 1.20.52; 1.20.68;
__unused removal on arguments; approved by core.
 1.19 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.18 30-Mar-2006  thorpej branches: 1.18.8; 1.18.10;
Use device_private().
 1.17 11-Dec-2005  christos branches: 1.17.4; 1.17.6; 1.17.8; 1.17.10; 1.17.12;
merge ktrace-lwp.
 1.16 29-May-2005  christos branches: 1.16.2;
- Sprinkle const
- Avoid variable shadowing.
- Eliminate some caddr_t abuse.
 1.15 27-Feb-2005  perry nuke trailing whitespace
 1.14 28-Oct-2004  yamt branches: 1.14.4; 1.14.6;
move buffer queue related stuffs from buf.h to their own header, bufq.h.
 1.13 21-Aug-2004  thorpej Use ANSI function decls and make use of static.
 1.12 05-Oct-2003  bouyer Remove references to University of California from my copyright notices.
 1.11 02-Oct-2002  thorpej branches: 1.11.6;
Add trailing ; to CFATTACH_DECL.
 1.10 30-Sep-2002  thorpej Use CFATTACH_DECL().
 1.9 27-Sep-2002  thorpej Declare all cfattach structures const.
 1.8 23-Apr-2002  bouyer More copyright fixes, pointed out by Thomas. Thanks !
 1.7 07-Dec-2001  yamt add detach support for st.
 1.6 01-Dec-2001  bouyer Various quirks for the ATAPI OnStream DI-30, mostly from the FreeBSD driver.
Many thanks to Chris Pinnock for giving me remote access to his hardware.
 1.5 15-Nov-2001  lukem don't need <sys/types.h> when including <sys/param.h>
 1.4 13-Nov-2001  lukem add RCSIDs
 1.3 18-Jun-2001  bouyer branches: 1.3.2; 1.3.4;
Add my copyrigth and remove the older ones. None of the original authors of
the st driver wrote something in this file.
 1.2 18-Jun-2001  bouyer Snapshot of ATAPI tapes support. Known to be able to read/write to tape with:
st0 at atapibus0 drive 1: <Seagate STT8000A, , 5.51> type 1 sequential
removable
Major changes may still happen in order to properly support other ATAPI
tape drives.
 1.1 04-May-2001  bouyer Add an ATAPI front-end to the st driver. Completely untested for now,
but st at scsi should still work :)
 1.3.4.3 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.3.4.2 23-Jun-2002  jdolecek catch up with -current on kqueue branch
 1.3.4.1 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.3.2.6 18-Oct-2002  nathanw Catch up to -current.
 1.3.2.5 20-Jun-2002  nathanw Catch up to -current.
 1.3.2.4 08-Jan-2002  nathanw Catch up to -current.
 1.3.2.3 14-Nov-2001  nathanw Catch up to -current.
 1.3.2.2 21-Jun-2001  nathanw Catch up to -current.
 1.3.2.1 18-Jun-2001  nathanw file st_atapi.c was added on branch nathanw_sa on 2001-06-21 20:06:06 +0000
 1.11.6.7 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.11.6.6 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.11.6.5 02-Nov-2004  skrll Sync with HEAD.
 1.11.6.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.11.6.3 18-Sep-2004  skrll Sync with HEAD.
 1.11.6.2 25-Aug-2004  skrll Sync with HEAD.
 1.11.6.1 03-Aug-2004  skrll Sync with HEAD
 1.14.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.14.4.1 29-Apr-2005  kent sync with -current
 1.16.2.2 30-Dec-2006  yamt sync with head.
 1.16.2.1 21-Jun-2006  yamt sync with head.
 1.17.12.1 31-Mar-2006  tron Merge 2006-03-31 NetBSD-current into the "peter-altq" branch.
 1.17.10.1 19-Apr-2006  elad sync with head.
 1.17.8.1 01-Apr-2006  yamt sync with head.
 1.17.6.1 22-Apr-2006  simonb Sync with head.
 1.17.4.1 09-Sep-2006  rpaulo sync with head
 1.18.10.2 10-Dec-2006  yamt sync with head.
 1.18.10.1 22-Oct-2006  yamt sync with head
 1.18.8.1 18-Nov-2006  ad Sync with head.
 1.20.68.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.20.52.3 11-Mar-2010  yamt sync with head
 1.20.52.2 19-Aug-2009  yamt sync with head.
 1.20.52.1 16-May-2009  yamt sync with head
 1.26.16.5 29-Apr-2012  mrg sync to latest -current.
 1.26.16.4 06-Mar-2012  mrg sync to -current
 1.26.16.3 06-Mar-2012  mrg sync to -current
 1.26.16.2 04-Mar-2012  mrg sync to latest -current.
 1.26.16.1 18-Feb-2012  mrg merge to -current.
 1.26.12.2 23-May-2012  yamt sync with head.
 1.26.12.1 17-Apr-2012  yamt sync with head
 1.27.2.2 23-Apr-2012  riz Pull up following revision(s) (requested by bouyer in ticket #192):
sys/dev/scsipi/cd.c: revision 1.307
sys/dev/scsipi/scsiconf.c: revision 1.266
sys/dev/scsipi/sd.c: revision 1.298
sys/dev/scsipi/st_scsi.c: revision 1.35
sys/dev/scsipi/atapiconf.c: revision 1.85
sys/dev/scsipi/scsipiconf.h: revision 1.120
sys/dev/usb/umass_scsipi.c: revision 1.44
sys/dev/scsipi/scsiconf.h: revision 1.57
sys/dev/scsipi/st_atapi.c: revision 1.29
sys/dev/scsipi/scsipi_base.c: revision 1.158
sys/dev/scsipi/st.c: revision 1.221
sys/dev/scsipi/scsipi_ioctl.c: revision 1.67
Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.
This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.
In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.
Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.27.2.1 02-Mar-2012  riz Pull up following revision(s) (requested by mbalmer in ticket #64):
sys/dev/scsipi/stvar.h: revision 1.24
sys/dev/scsipi/st_scsi.c: revision 1.34
sys/dev/scsipi/st_atapi.c: revision 1.28
sys/dev/scsipi/st.c: revision 1.220
Convert st(4) to device_t, while here clean up the code and use uintXX_t
instead of u_intXX_t.
 1.29.16.2 05-Oct-2016  skrll Sync with HEAD
 1.29.16.1 22-Sep-2015  skrll Sync with HEAD
 1.29.2.1 03-Dec-2017  jdolecek update from HEAD
 1.37 24-Aug-2015  pooka would you like some freshly ground _KERNEL_OPT with that?
yes? excellent choice, sir/madam.
 1.36 18-Oct-2014  snj branches: 1.36.2;
src is too big these days to tolerate superfluous apostrophes. It's
"its", people!
 1.35 19-Apr-2012  bouyer branches: 1.35.2;
Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.

This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.

In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.

Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.34 28-Feb-2012  mbalmer Convert st(4) to device_t, while here clean up the code and use uintXX_t
instead of u_intXX_t.
 1.33 02-Feb-2012  tls branches: 1.33.2;
Entropy-pool implementation move and cleanup.

1) Move core entropy-pool code and source/sink/sample management code
to sys/kern from sys/dev.

2) Remove use of NRND as test for presence of entropy-pool code throughout
source tree.

3) Remove use of RND_ENABLED in device drivers as microoptimization to
avoid expensive operations on disabled entropy sources; make the
rnd_add calls do this directly so all callers benefit.

4) Fix bug in recent rnd_add_data()/rnd_add_uint32() changes that might
have lead to slight entropy overestimation for some sources.

5) Add new source types for environmental sensors, power sensors, VM
system events, and skew between clocks, with a sample implementation
for each.

ok releng to go in before the branch due to the difficulty of later
pullup (widespread #ifdef removal and moved files). Tested with release
builds on amd64 and evbarm and live testing on amd64.
 1.32 06-Dec-2009  dyoung branches: 1.32.12; 1.32.16;
Delete do-nothing device-activation hooks.
 1.31 21-Oct-2009  rmind Remove uarea swap-out functionality:

- Addresses the issue described in PR/38828.
- Some simplification in threading and sleepq subsystems.
- Eliminates pmap_collect() and, as a side note, allows pmap optimisations.
- Eliminates XS_CTL_DATA_ONSTACK in scsipi code.
- Avoids few scans on LWP list and thus potentially long holds of proc_lock.
- Cuts ~1.5k lines of code. Reduces amd64 kernel size by ~4k.
- Removes __SWAP_BROKEN cases.

Tested on x86, mips, acorn32 (thanks <mpumford>) and partly tested on
acorn26 (thanks to <bjh21>).

Discussed on <tech-kern>, reviewed by <ad>.
 1.30 15-Aug-2009  pgoyette 1. Move the mode_select functionality into common code (in st.c) and
invoke the common routine for both scsi and atapi tapes.

2. Replace a numeric constant with some sizeof's when calculating the
size of the mode_select command buffer, clear the entire buffer, and
KASSERT to ensure the page_0_size loaded from quirk table is valid.

3. Add a quirk for my Seagate Travan-40 tape drive.

As discussed on tech-kern@

Addresses my PR kern/34832
 1.29 12-May-2009  cegger struct device * -> device_t, no functional changes intended.
 1.28 12-May-2009  cegger struct cfdata * -> cfdata_t, no functional changes intended.
 1.27 28-Apr-2008  martin branches: 1.27.14;
Remove clause 3 and 4 from TNF licenses
 1.26 16-Nov-2006  christos branches: 1.26.48; 1.26.50; 1.26.52;
__unused removal on arguments; approved by core.
 1.25 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.24 30-Mar-2006  thorpej branches: 1.24.8; 1.24.10;
Use device_private().
 1.23 11-Dec-2005  christos branches: 1.23.4; 1.23.6; 1.23.8; 1.23.10; 1.23.12;
merge ktrace-lwp.
 1.22 30-May-2005  christos branches: 1.22.2;
- remove bogus casts
- add more const
 1.21 27-Feb-2005  perry nuke trailing whitespace
 1.20 21-Feb-2005  thorpej Part 1 of a cleanup pass over the SCSI subsystem. The aim is to name
everything "scsi_*", since we really are talking about the SCSI command
set, ATAPI transport not withstanding. Improve the names of many structures,
and prepend "SCSI_" onto all SCSI command opcodes. Place items described
by the SCSI Primary Commands document into scsi_spc.h.
 1.19 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.18 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.17 31-Jan-2005  reinoud As part of cleaning up sys/scsipi, replace all u_char by uint8_t and
replace all `short' with int16_t.
 1.16 28-Oct-2004  yamt branches: 1.16.4; 1.16.6;
move buffer queue related stuffs from buf.h to their own header, bufq.h.
 1.15 18-Sep-2004  mycroft Minor rearrangement. Whitespace and #include cleanup.
 1.14 18-Sep-2004  mycroft Standardize some variable names and the calling pattern for scsipi_command().
Use void pointer casts.
 1.13 17-Sep-2004  mycroft Remove the "xfer" argument to scsipi_command().
 1.12 09-Sep-2004  bouyer Make the xxstart() functions reentrant again, as some drivers HBA can call
scsipi_done() from their scsipi_request().
For this, add a struct scsipi_xfer * argument to scsipi_command().
If not NULL scsipi_command() will use this to enqueue this xfer, otherwise
it'll try to allocate a new one. This scsipi_xfer has to be allocated
and initialised by scsipi_make_xs() or equivalent.
In xxstart(), allocate a scsipi_xfer using scsipi_make_xs(), and if not NULL,
dequeue the buffer before calling scsipi_command(). This makes sure that
scsipi_command() will not fail, and also makes sure that xxstart() won't
be called again between the BUFQ_PEEK() and BUFQ_GET().

Fix "dequeued wrong buf" panics reported by Juergen Hannken-Illjes in
private mail and Andreas Wrede on current-users@.
Thanks to Jason Thorpe and Chuck Silver for review, and Andreas Wrede for
testing the patch.
 1.11 21-Aug-2004  thorpej Use ANSI function decls and make use of static.
 1.10 02-Oct-2002  thorpej branches: 1.10.6; 1.10.8;
Add trailing ; to CFATTACH_DECL.
 1.9 30-Sep-2002  thorpej Use CFATTACH_DECL().
 1.8 27-Sep-2002  thorpej Declare all cfattach structures const.
 1.7 07-Dec-2001  yamt add detach support for st.
 1.6 15-Nov-2001  lukem don't need <sys/types.h> when including <sys/param.h>
 1.5 13-Nov-2001  lukem add RCSIDs
 1.4 18-Jul-2001  thorpej bcopy -> memcpy
 1.3 18-Jul-2001  thorpej bzero -> memset
 1.2 18-Jun-2001  bouyer branches: 1.2.2; 1.2.4;
Snapshot of ATAPI tapes support. Known to be able to read/write to tape with:
st0 at atapibus0 drive 1: <Seagate STT8000A, , 5.51> type 1 sequential
removable
Major changes may still happen in order to properly support other ATAPI
tape drives.
 1.1 04-May-2001  bouyer Add an ATAPI front-end to the st driver. Completely untested for now,
but st at scsi should still work :)
 1.2.4.3 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.2.4.2 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.2.4.1 03-Aug-2001  lukem update to -current
 1.2.2.6 18-Oct-2002  nathanw Catch up to -current.
 1.2.2.5 08-Jan-2002  nathanw Catch up to -current.
 1.2.2.4 14-Nov-2001  nathanw Catch up to -current.
 1.2.2.3 24-Aug-2001  nathanw Catch up with -current.
 1.2.2.2 21-Jun-2001  nathanw Catch up to -current.
 1.2.2.1 18-Jun-2001  nathanw file st_scsi.c was added on branch nathanw_sa on 2001-06-21 20:06:07 +0000
 1.10.8.1 11-Sep-2004  he Pull up revision 1.12 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.10.6.6 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.10.6.5 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.10.6.4 02-Nov-2004  skrll Sync with HEAD.
 1.10.6.3 21-Sep-2004  skrll Fix the sync with head I botched.
 1.10.6.2 18-Sep-2004  skrll Sync with HEAD.
 1.10.6.1 25-Aug-2004  skrll Sync with HEAD.
 1.16.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.16.4.1 29-Apr-2005  kent sync with -current
 1.22.2.2 30-Dec-2006  yamt sync with head.
 1.22.2.1 21-Jun-2006  yamt sync with head.
 1.23.12.1 31-Mar-2006  tron Merge 2006-03-31 NetBSD-current into the "peter-altq" branch.
 1.23.10.1 19-Apr-2006  elad sync with head.
 1.23.8.1 01-Apr-2006  yamt sync with head.
 1.23.6.1 22-Apr-2006  simonb Sync with head.
 1.23.4.1 09-Sep-2006  rpaulo sync with head
 1.24.10.2 10-Dec-2006  yamt sync with head.
 1.24.10.1 22-Oct-2006  yamt sync with head
 1.24.8.1 18-Nov-2006  ad Sync with head.
 1.26.52.4 11-Mar-2010  yamt sync with head
 1.26.52.3 19-Aug-2009  yamt sync with head.
 1.26.52.2 16-May-2009  yamt sync with head
 1.26.52.1 16-May-2008  yamt sync with head.
 1.26.50.1 18-May-2008  yamt sync with head.
 1.26.48.1 02-Jun-2008  mjf Sync with HEAD.
 1.27.14.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.32.16.5 29-Apr-2012  mrg sync to latest -current.
 1.32.16.4 06-Mar-2012  mrg sync to -current
 1.32.16.3 06-Mar-2012  mrg sync to -current
 1.32.16.2 04-Mar-2012  mrg sync to latest -current.
 1.32.16.1 18-Feb-2012  mrg merge to -current.
 1.32.12.2 23-May-2012  yamt sync with head.
 1.32.12.1 17-Apr-2012  yamt sync with head
 1.33.2.2 23-Apr-2012  riz Pull up following revision(s) (requested by bouyer in ticket #192):
sys/dev/scsipi/cd.c: revision 1.307
sys/dev/scsipi/scsiconf.c: revision 1.266
sys/dev/scsipi/sd.c: revision 1.298
sys/dev/scsipi/st_scsi.c: revision 1.35
sys/dev/scsipi/atapiconf.c: revision 1.85
sys/dev/scsipi/scsipiconf.h: revision 1.120
sys/dev/usb/umass_scsipi.c: revision 1.44
sys/dev/scsipi/scsiconf.h: revision 1.57
sys/dev/scsipi/st_atapi.c: revision 1.29
sys/dev/scsipi/scsipi_base.c: revision 1.158
sys/dev/scsipi/st.c: revision 1.221
sys/dev/scsipi/scsipi_ioctl.c: revision 1.67
Expand struct scsipi_bustype {} in a ABI-backward-compatible way to
pass more informations about the bus:
- bustype_type has 2 different bytes, one holding the existing
SCSIPI_BUSTYPE_* (scsi, atapi, ata), and one for a per-SCSIPI_BUSTYPE_*
subtype. Introduce macros to build or extract bustype_type.
- for SCSIPI_BUSTYPE_SCSI, define subtypes for parallel SCSI, Fibre Channel,
SAS and USB, to specify the transport method. SCSIPI_BUSTYPE_SCSI_PSCSI
is 0 so that bustype_type value doesn't change for existing code
- for non-SCSIPI_BUSTYPE_SCSI busses there's no defined subtype yet,
so the bustype_type value doesn't change.
- provide scsi_fc_bustype, scsi_sas_bustype and scsi_usb_bustype
along with scsi_bustype to be used by bus driver where appropriate
- scsipi_print_xfer_mode(): more existing code under a
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI) case, as
sync/wide parameters only make sense for parallel SCSI.
For (SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC) and
(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS), only print
tagged queing status if enabled. Just be silent for other
bustypes.
This change is prompted by this problem:
right now, FC (e.g. isp(4)) and SAS (e.g. mfi(4)) don't
do anything for ADAPTER_REQ_SET_XFER_MODE, and especially never
call scsipi_async_event(ASYNC_EVENT_XFER_MODE), so sd(4) always
runs untagged. Doing a scsipi_async_event(ASYNC_EVENT_XFER_MODE) with
appropriate parameters is enough to enable tagged queuing,
but then scsipi will print:
sd0: async, 8-bit transfers, tagged queueing
which is harmless (async, 8-bit transfers doens't make sense on SAS anyway)
but will confuse users. With this change scsipi will only print:
sd0: tagged queueing
which is correct.
In the long run, knowning the underlying transport in scsipi will
allow better handling of device which are not parallel SCSI.
Another change adding an extra callback to struct scsipi_bustype {}
will come (so that scsipi_print_xfer_mode(), which is SCSI-specific,
can be moved out of scsipi_base, and split into per-subtype callback),
but this will break kernel ABI and so is not suitable for
netbsd-6, so will be commmited later. The above is enough to get
tagged queuing on FC and SAS in netbsd-6.
 1.33.2.1 02-Mar-2012  riz Pull up following revision(s) (requested by mbalmer in ticket #64):
sys/dev/scsipi/stvar.h: revision 1.24
sys/dev/scsipi/st_scsi.c: revision 1.34
sys/dev/scsipi/st_atapi.c: revision 1.28
sys/dev/scsipi/st.c: revision 1.220
Convert st(4) to device_t, while here clean up the code and use uintXX_t
instead of u_intXX_t.
 1.35.2.1 03-Dec-2017  jdolecek update from HEAD
 1.36.2.1 22-Sep-2015  skrll Sync with HEAD
 1.26 24-Mar-2018  mlelstv Use separate lock to protect internal state and release locks when
calling biodone.
 1.25 13-Apr-2015  riastradh branches: 1.25.10; 1.25.16;
Convert sys/dev to use <sys/rndsource.h>.
 1.24 28-Feb-2012  mbalmer branches: 1.24.2; 1.24.16;
Convert st(4) to device_t, while here clean up the code and use uintXX_t
instead of u_intXX_t.
 1.23 02-Feb-2012  tls branches: 1.23.2;
Entropy-pool implementation move and cleanup.

1) Move core entropy-pool code and source/sink/sample management code
to sys/kern from sys/dev.

2) Remove use of NRND as test for presence of entropy-pool code throughout
source tree.

3) Remove use of RND_ENABLED in device drivers as microoptimization to
avoid expensive operations on disabled entropy sources; make the
rnd_add calls do this directly so all callers benefit.

4) Fix bug in recent rnd_add_data()/rnd_add_uint32() changes that might
have lead to slight entropy overestimation for some sources.

5) Add new source types for environmental sensors, power sensors, VM
system events, and skew between clocks, with a sample implementation
for each.

ok releng to go in before the branch due to the difficulty of later
pullup (widespread #ifdef removal and moved files). Tested with release
builds on amd64 and evbarm and live testing on amd64.
 1.22 19-Nov-2011  tls branches: 1.22.2;
First step of random number subsystem rework described in
<20111022023242.BA26F14A158@mail.netbsd.org>. This change includes
the following:

An initial cleanup and minor reorganization of the entropy pool
code in sys/dev/rnd.c and sys/dev/rndpool.c. Several bugs are
fixed. Some effort is made to accumulate entropy more quickly at
boot time.

A generic interface, "rndsink", is added, for stream generators to
request that they be re-keyed with good quality entropy from the pool
as soon as it is available.

The arc4random()/arc4randbytes() implementation in libkern is
adjusted to use the rndsink interface for rekeying, which helps
address the problem of low-quality keys at boot time.

An implementation of the FIPS 140-2 statistical tests for random
number generator quality is provided (libkern/rngtest.c). This
is based on Greg Rose's implementation from Qualcomm.

A new random stream generator, nist_ctr_drbg, is provided. It is
based on an implementation of the NIST SP800-90 CTR_DRBG by
Henric Jungheim. This generator users AES in a modified counter
mode to generate a backtracking-resistant random stream.

An abstraction layer, "cprng", is provided for in-kernel consumers
of randomness. The arc4random/arc4randbytes API is deprecated for
in-kernel use. It is replaced by "cprng_strong". The current
cprng_fast implementation wraps the existing arc4random
implementation. The current cprng_strong implementation wraps the
new CTR_DRBG implementation. Both interfaces are rekeyed from
the entropy pool automatically at intervals justifiable from best
current cryptographic practice.

In some quick tests, cprng_fast() is about the same speed as
the old arc4randbytes(), and cprng_strong() is about 20% faster
than rnd_extract_data(). Performance is expected to improve.

The AES code in src/crypto/rijndael is no longer an optional
kernel component, as it is required by cprng_strong, which is
not an optional kernel component.

The entropy pool output is subjected to the rngtest tests at
startup time; if it fails, the system will reboot. There is
approximately a 3/10000 chance of a false positive from these
tests. Entropy pool _input_ from hardware random numbers is
subjected to the rngtest tests at attach time, as well as the
FIPS continuous-output test, to detect bad or stuck hardware
RNGs; if any are detected, they are detached, but the system
continues to run.

A problem with rndctl(8) is fixed -- datastructures with
pointers in arrays are no longer passed to userspace (this
was not a security problem, but rather a major issue for
compat32). A new kernel will require a new rndctl.

The sysctl kern.arandom() and kern.urandom() nodes are hooked
up to the new generators, but the /dev/*random pseudodevices
are not, yet.

Manual pages for the new kernel interfaces are forthcoming.
 1.21 06-Dec-2009  dyoung branches: 1.21.12;
Delete do-nothing device-activation hooks.
 1.20 15-Aug-2009  pgoyette 1. Move the mode_select functionality into common code (in st.c) and
invoke the common routine for both scsi and atapi tapes.

2. Replace a numeric constant with some sizeof's when calculating the
size of the mode_select command buffer, clear the entire buffer, and
KASSERT to ensure the page_0_size loaded from quirk table is valid.

3. Add a quirk for my Seagate Travan-40 tape drive.

As discussed on tech-kern@

Addresses my PR kern/34832
 1.19 12-May-2009  cegger struct device * -> device_t, no functional changes intended.
 1.18 28-Apr-2008  martin branches: 1.18.14;
Remove clause 3 and 4 from TNF licenses
 1.17 14-Apr-2006  blymn branches: 1.17.58; 1.17.60; 1.17.62;
Make i/o statistics collection more generic, include tape drives and
nfs mounts in the set of devices that statistics will be reported on.
 1.16 11-Dec-2005  christos branches: 1.16.4; 1.16.6; 1.16.8; 1.16.10; 1.16.12;
merge ktrace-lwp.
 1.15 15-Oct-2005  yamt - change the way to specify a bufq strategy. (by string rather than by number)
- rather than embedding bufq_state in driver softc,
have a pointer to the former.
- move bufq related functions from kern/subr_disk.c to kern/subr_bufq.c.
- rename method to strategy for consistency.
- move some definitions which don't need to be exposed to the rest of kernel
from sys/bufq.h to sys/bufq_impl.h.
(is it better to move it to kern/ or somewhere?)
- fix some obvious breakage in dev/qbus/ts.c. (not tested)
 1.14 07-Aug-2005  blymn Add tape statistics structure pointer.
 1.13 29-May-2005  christos branches: 1.13.2;
- Sprinkle const
- Avoid variable shadowing.
- Eliminate some caddr_t abuse.
 1.12 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.11 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.10 31-Jan-2005  reinoud As part of cleaning up sys/scsipi, replace all u_char by uint8_t and
replace all `short' with int16_t.
 1.9 27-Aug-2004  bouyer branches: 1.9.4;
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
Havard Eidnes's analysis of this problem is that the scsipi_xfer pool is
competing for resources with other pools, including the the inode and vnode
pools which can grow quite large.

*_scsipi_cmd(): don't biodone the buffer if scsipi_make_xs() fails, let the
caller deal with the problem
start function of block devices drivers: dequeue the buffer after the
scsipi_command() call. If scsipi_command() fails with ENOMEM don't dequeue
the buffer, and schedule a callout to call the start function after
some delay.
scsipi_init(): prime the scsipi_xfer_pool with one page. This ensure that
there is always some scsipi_xfer to play with. If scsipi_command() fails
because of pool_get(), we're sure there will be resources available later,
when the pending commands have completed.

Reviewed by Jason Thorpe and Havard Eidnes.
Todo: remove the "unable to allocate scsipi_xfer" and "not queued, error %d"
printfs, but I choose to keep them for now, to help make sure the code does
what it should.
 1.8 21-Aug-2004  thorpej Use ANSI function decls and make use of static.
 1.7 21-Aug-2004  thorpej - De-__P.
- Use ANSI function decls.
 1.6 22-Jul-2002  hannken branches: 1.6.6; 1.6.8;
Convert to new device buffer queue interface.
 1.5 20-Mar-2002  christos branches: 1.5.4;
Add the ability to report file number/block number. From Matt Jacob.
 1.4 07-Dec-2001  yamt add detach support for st.
 1.3 01-Dec-2001  bouyer Various quirks for the ATAPI OnStream DI-30, mostly from the FreeBSD driver.
Many thanks to Chris Pinnock for giving me remote access to his hardware.
 1.2 18-Jun-2001  bouyer branches: 1.2.2; 1.2.4;
Snapshot of ATAPI tapes support. Known to be able to read/write to tape with:
st0 at atapibus0 drive 1: <Seagate STT8000A, , 5.51> type 1 sequential
removable
Major changes may still happen in order to properly support other ATAPI
tape drives.
 1.1 04-May-2001  bouyer Add an ATAPI front-end to the st driver. Completely untested for now,
but st at scsi should still work :)
 1.2.4.3 06-Sep-2002  jdolecek sync kqueue branch with HEAD
 1.2.4.2 23-Jun-2002  jdolecek catch up with -current on kqueue branch
 1.2.4.1 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.2.2.5 01-Aug-2002  nathanw Catch up to -current.
 1.2.2.4 01-Apr-2002  nathanw Catch up to -current.
(CVS: It's not just a program. It's an adventure!)
 1.2.2.3 08-Jan-2002  nathanw Catch up to -current.
 1.2.2.2 21-Jun-2001  nathanw Catch up to -current.
 1.2.2.1 18-Jun-2001  nathanw file stvar.h was added on branch nathanw_sa on 2001-06-21 20:06:07 +0000
 1.5.4.1 29-Aug-2002  gehenna catch up with -current.
 1.6.8.1 11-Sep-2004  he Pull up revision 1.9 (via patch, requested by bouyer in ticket #837):
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
The theory is that other consumers of pool memory is causing
this memory shortage in certain somewhat hard to reproduce
situations.
This is done by giving scsipi_command an extra argument to
optionally pass a preallocated scsipi_xfer, and allocating a
scsipi_xfer before dequeueing a buffer in the various *start()
functions. If the allocation of a scsipi_xfer fails, schedule
a callout for delayed invocation of the start function. Also
reserve one page for scsipi_xfer structs, to ensure that we will
eventually have some available once pending commands complete.
Should fix PR#25670.
 1.6.6.5 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.6.6.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.6.6.3 18-Sep-2004  skrll Sync with HEAD.
 1.6.6.2 03-Sep-2004  skrll Sync with HEAD
 1.6.6.1 25-Aug-2004  skrll Sync with HEAD.
 1.9.4.1 29-Apr-2005  kent sync with -current
 1.13.2.1 21-Jun-2006  yamt sync with head.
 1.16.12.1 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.16.10.1 19-Apr-2006  elad sync with head.
 1.16.8.1 24-May-2006  yamt sync with head.
 1.16.6.1 22-Apr-2006  simonb Sync with head.
 1.16.4.1 09-Sep-2006  rpaulo sync with head
 1.17.62.4 11-Mar-2010  yamt sync with head
 1.17.62.3 19-Aug-2009  yamt sync with head.
 1.17.62.2 16-May-2009  yamt sync with head
 1.17.62.1 16-May-2008  yamt sync with head.
 1.17.60.1 18-May-2008  yamt sync with head.
 1.17.58.1 02-Jun-2008  mjf Sync with HEAD.
 1.18.14.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.21.12.1 17-Apr-2012  yamt sync with head
 1.22.2.4 06-Mar-2012  mrg sync to -current
 1.22.2.3 06-Mar-2012  mrg sync to -current
 1.22.2.2 04-Mar-2012  mrg sync to latest -current.
 1.22.2.1 18-Feb-2012  mrg merge to -current.
 1.23.2.1 02-Mar-2012  riz Pull up following revision(s) (requested by mbalmer in ticket #64):
sys/dev/scsipi/stvar.h: revision 1.24
sys/dev/scsipi/st_scsi.c: revision 1.34
sys/dev/scsipi/st_atapi.c: revision 1.28
sys/dev/scsipi/st.c: revision 1.220
Convert st(4) to device_t, while here clean up the code and use uintXX_t
instead of u_intXX_t.
 1.24.16.1 06-Jun-2015  skrll Sync with HEAD
 1.24.2.1 03-Dec-2017  jdolecek update from HEAD
 1.25.16.1 30-Mar-2018  pgoyette Resolve conflicts between branch and HEAD
 1.25.10.1 08-Apr-2018  snj Pull up following revision(s) (requested by mlelstv in ticket #703):
sys/dev/scsipi/st.c: 1.234
sys/dev/scsipi/stvar.h: 1.26
Use separate lock to protect internal state and release locks when
calling biodone.
 1.4 16-May-2002  thorpej Garbage-collect the empty-for-years "su" device.
 1.3 27-Aug-1997  bouyer branches: 1.3.2; 1.3.32; 1.3.34; 1.3.46;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.2 29-Jun-1994  cgd New RCS ID's, take two. they're more aesthecially pleasant, and use 'NetBSD'
 1.1 24-Nov-1993  mycroft Under construction...
 1.3.46.1 30-May-2002  gehenna Catch up with -current.
 1.3.34.1 23-Jun-2002  jdolecek catch up with -current on kqueue branch
 1.3.32.1 20-Jun-2002  nathanw Catch up to -current.
 1.3.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.3.2.1 27-Aug-1997  thorpej file su.c was added on branch marc-pcmcia on 1997-08-27 23:33:42 +0000
 1.65 20-Nov-2016  mlelstv Make scsipi framework MPSAFE.

Data structures are now protected by a per-adapter mutex at IPL_BIO
that is created by the scsibus or atapibus instance when the adapter
is configured.
The enable reference counter and the channel freeze counter which are
currently used by HBA code before the adapter is configured, are made
atomic.
The target drivers are now all tagged as D_MPSAFE.

Almost all HBA drivers still require the kernel lock to present,
so all callbacks into HBA code are still protected by kernel lock
unless the driver is tagged as SCSIPI_ADAPT_MPSAFE.

TODO: refactor sd and cd to use dksubr.
 1.64 05-Aug-2016  maya uk(4) is safe to suspend, register it as such
 1.63 14-Jul-2016  msaitoh branches: 1.63.2;
- Use aprint*() instead of printf() in xxx_attach().
- Add missing aprint_naive("\n");
- KNF
 1.62 25-Jul-2014  dholland branches: 1.62.4;
Add d_discard to all struct cdevsw instances I could find.

All have been set to "nodiscard"; some should get a real implementation.
 1.61 16-Mar-2014  dholland branches: 1.61.2;
Change (mostly mechanically) every cdevsw/bdevsw I can find to use
designated initializers.

I have not built every extant kernel so I have probably broken at
least one build; however I've also found and fixed some wrong
cdevsw/bdevsw entries so even if so I think we come out ahead.
 1.60 08-Feb-2012  mbalmer branches: 1.60.6; 1.60.10;
Convert uk(4) to device_t. While here, clean up the code a bit.
releng ok (ack 3, nak 0, wait no more)
 1.59 06-Dec-2009  dyoung branches: 1.59.12; 1.59.16;
Delete do-nothing device-activation hooks.
 1.58 12-May-2009  cegger struct device * -> device_t, no functional changes intended.
 1.57 12-May-2009  cegger struct cfdata * -> cfdata_t, no functional changes intended.
 1.56 11-Jan-2009  cegger branches: 1.56.2;
make this compile
 1.55 08-Jun-2008  tsutsui branches: 1.55.6;
Use device_lookup_private() rather than using cd_devs[] directly to get softc.

XXX maybe we should change a type of cd_devs[] in struct cfdriver
from (void *) to device_t.
 1.54 28-Apr-2008  martin branches: 1.54.2;
Remove clause 3 and 4 from TNF licenses
 1.53 05-Apr-2008  cegger branches: 1.53.2; 1.53.4;
use aprint_*_dev and device_xname
 1.52 04-Mar-2007  christos branches: 1.52.36;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.51 16-Nov-2006  christos branches: 1.51.4;
__unused removal on arguments; approved by core.
 1.50 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.49 03-Sep-2006  christos branches: 1.49.2; 1.49.4;
add missing initializer
 1.48 30-Mar-2006  thorpej Use device_private().
 1.47 28-Mar-2006  thorpej Use device_unit().
 1.46 11-Dec-2005  christos branches: 1.46.4; 1.46.6; 1.46.8; 1.46.10; 1.46.12;
merge ktrace-lwp.
 1.45 27-Feb-2005  perry branches: 1.45.4;
nuke trailing whitespace
 1.44 01-Feb-2005  reinoud Backing out changes to clean up scsipi. I was pointed out there were
problems i hadn't seen. To prevent lossage i'd decided to back off all
changes and let them be reviewed on tech-kern.
 1.43 31-Jan-2005  reinoud Part of the cleanup of sys/scsipi's use of types; rename all u_int* to
uint* and change the u_long's to uint32_t's where possible. Note that the
iocl definitions/hooks have to be ulong (or u_long) or they'll bomb out.
 1.42 21-Aug-2004  thorpej branches: 1.42.4; 1.42.6;
Use ANSI function decls and make use of static.
 1.41 08-Sep-2003  mycroft We only need a single attachment for uk at scsibus and atapibus.
 1.40 29-Jun-2003  fvdl branches: 1.40.2;
Back out the lwp/ktrace changes. They contained a lot of colateral damage,
and need to be examined and discussed more.
 1.39 28-Jun-2003  darrenr Pass lwp pointers throughtout the kernel, as required, so that the lwpid can
be inserted into ktrace records. The general change has been to replace
"struct proc *" with "struct lwp *" in various function prototypes, pass
the lwp through and use l_proc to get the process pointer when needed.

Bump the kernel rev up to 1.6V
 1.38 23-Oct-2002  jdolecek merge kqueue branch into -current

kqueue provides a stateful and efficient event notification framework
currently supported events include socket, file, directory, fifo,
pipe, tty and device changes, and monitoring of processes and signals

kqueue is supported by all writable filesystems in NetBSD tree
(with exception of Coda) and all device drivers supporting poll(2)

based on work done by Jonathan Lemon for FreeBSD
initial NetBSD port done by Luke Mewburn and Jason Thorpe
 1.37 02-Oct-2002  thorpej Add trailing ; to CFATTACH_DECL.
 1.36 30-Sep-2002  thorpej Use CFATTACH_DECL().
 1.35 27-Sep-2002  thorpej Declare all cfattach structures const.
 1.34 06-Sep-2002  gehenna Merge the gehenna-devsw branch into the trunk.

This merge changes the device switch tables from static array to
dynamically generated by config(8).

- All device switches is defined as a constant structure in device drivers.

- The new grammer ``device-major'' is introduced to ``files''.

device-major <prefix> char <num> [block <num>] [<rules>]

- All device major numbers must be listed up in port dependent majors.<arch>
by using this grammer.

- Added the new naming convention.
The name of the device switch must be <prefix>_[bc]devsw for auto-generation
of device switch tables.

- The backward compatibility of loading block/character device
switch by LKM framework is broken. This is necessary to convert
from block/character device major to device name in runtime and vice versa.

- The restriction to assign device major by LKM is completely removed.
We don't need to reserve LKM entries for dynamic loading of device switch.

- In compile time, device major numbers list is packed into the kernel and
the LKM framework will refer it to assign device major number dynamically.
 1.33 10-Feb-2002  thorpej branches: 1.33.8;
Remove a pretty much useless autoconfiguration message.
 1.32 15-Nov-2001  lukem don't need <sys/types.h> when including <sys/param.h>
 1.31 13-Nov-2001  lukem add RCSIDs
 1.30 25-Apr-2001  bouyer branches: 1.30.2; 1.30.4;
Pull up the thorpej_scsipi branch to main branch.
This is a completely rewritten scsipi_xfer execution engine, and the
associated changes to HBA drivers. Overview of changes & features:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).
- support for QUEUE FULL and CHECK CONDITION status in mid-layer; the command
will be requeued, or a REQUEST SENSE will be sent as appropriate.

Just before the merge syssrc has been tagged with thorpej_scsipi_beforemerge
 1.29 30-Mar-2000  augustss branches: 1.29.6;
Get rid of register declarations.
 1.28 29-Mar-2000  augustss Add ukactivate&ukdetach so uk devices can be detached.
 1.27 08-Dec-1998  thorpej branches: 1.27.10;
When closing, wait for pending xfers to drain before deleting the reference
to the adapter.
 1.26 20-Nov-1998  thorpej Add adapter reference counting for SCSI and ATAPI devices.
 1.25 19-Oct-1998  bouyer Allow uk to attach at atapibus.
 1.24 31-Aug-1998  cgd kill the last remnants of __BROKEN_INDIRECT_CONFIG. (only the pica port
used it, and it's non-working and apparently slated for replacement.)
 1.23 17-Aug-1998  mycroft Assign my copyrights to TNF.
 1.22 15-Aug-1998  mycroft Make copyright notices with my name consistent.
 1.21 12-Jan-1998  thorpej Adjust for changes to config.
 1.20 01-Oct-1997  enami Cosmetic changes to keep coding style consistency in this directory;

- Indent with tab of width 8.
- Use four column to indent continuation line.
- Fold long line if possible.
- Use return (xx) instead of return xx.
- Compare pointer against NULL instead of testing like boolean.
- Delete whitespace at the end of line.
- Delete whitespace in front of function call operator.
- Delete whitespace after cast.
- Dereference a pointer to function explicitly.
- Add an empty line after local variable declaration.
- Use NULL instead of (char *)0.
- Dont use block for single statement.
 1.19 27-Aug-1997  bouyer branches: 1.19.2;
Merge scsipi branch in the mainline. This add support for ATAPI devices
(currently only CD-ROM drives on i386). The sys/dev/scsipi system provides 2
busses to which devices can attach (scsibus and atapibus). This needed to
change some include files and structure names in the low level scsi drivers.
 1.18 05-Dec-1996  cgd update these so they compile whether or not __BROKEN_INDIRECT_CONFIG
is defined.
 1.17 12-Oct-1996  christos revert previous kprintf change
 1.16 10-Oct-1996  christos - printf -> kprintf, sprintf -> ksprintf
- make this compile cleanly.
 1.15 17-Mar-1996  thorpej New device attachment scheme:

- split softc size and match/attach out from cfdriver into
a new struct cfattach.

- new "attach" directive for files.*. May specify the name of
the cfattach structure, so that devices may be easily attached
to parents with different autoconfiguration semantics.
 1.14 05-Mar-1996  thorpej Normalize SCSI autoconfiguration output.
From Chris Demetriou <cgd@NetBSD.ORG>. Fixes PR #1958.
 1.13 24-Mar-1995  glass fix syntax error. Andreas Gustafsson <gson@clinet.fi>
 1.12 28-Dec-1994  mycroft Numerous changes. Many bugs fixed, better autoconfig, a few new features.
 1.11 21-Nov-1994  mycroft Clean up open and close routines somewhat.
 1.10 21-Nov-1994  mycroft Replace dev_unit with device_softc in scsi_link. Change argument to foostart()
to void*.
 1.9 30-Oct-1994  cgd be more careful with types, also pull in headers where necessary.
 1.8 20-Oct-1994  mycroft First cut at making user-level SCSI commands work. This is untested.
Partly from John Brezak.
 1.7 29-Jun-1994  cgd New RCS ID's, take two. they're more aesthecially pleasant, and use 'NetBSD'
 1.6 11-Apr-1994  mycroft Fix various types. Remove some outdated flags.
 1.5 29-Mar-1994  mycroft New SCSI system, based on Julian's more recent work.
 1.4 16-Feb-1994  mycroft More KNF-like.
 1.3 01-Feb-1994  mycroft Add scsi_stop_unit(), from Mac code. Other minor nits.
 1.2 17-Dec-1993  mycroft Canonicalize all #includes.
 1.1 24-Nov-1993  mycroft Under construction...
 1.19.2.3 14-Oct-1997  thorpej Update marc-pcmcia branch from trunk.
 1.19.2.2 27-Aug-1997  thorpej Update marc-pcmcia branch from trunk.
 1.19.2.1 27-Aug-1997  thorpej file uk.c was added on branch marc-pcmcia on 1997-08-27 23:33:43 +0000
 1.27.10.3 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.

Main changes to the scsipi code itself:
- add a scsipi_channel->type to allow umass to attach to both atapibus and
scsibus. Will die when IDE is converted from ata_atapi_attach to
scsipi_channel/scsipi_adapter
- Add a chan_defquirks to scsipi_channel so that adapters can pass a default
set of quirks to be set for each device attached
- add adapt_getgeom and adapt_accesschk callbacks
 1.27.10.2 01-Nov-1999  thorpej Fixup the SC_DEBUG() stuff for the new world order.
 1.27.10.1 19-Oct-1999  thorpej Completely rewritten scsipi_xfer execution engine:
- All xfers are queued in the mid-layer, rather than doing so in an
ad-hoc fashion in individual adapter drivers.
- Adapter/channel resource management in the mid-layer, avoids even trying
to start running an xfer if the adapter/channel doesn't have the resources.
- Better communication between the mid-layer and the adapters.
- Asynchronous event notification mechanism from adapter to mid-layer and
peripherals.
- Better peripheral queue management: freeze/thaw, sorted requeueing during
recovery, etc.
- Clean separation of peripherals, adapters, and adapter channels (no more
scsipi_link).
- Kernel thread for each scsipi_channel makes error recovery much easier
(no more dealing with interrupt context when recovering from an error).
- Mid-layer support for tagged queueing: commands can have the tag type
set explicitly, tag IDs are allocated in the mid-layer (thus eliminating
the need to use buggy tag ID allocation schemes in many adapter drivers).

There is a lot more work to do, but this correctly functions for the most
part on several file servers I run.
 1.29.6.7 11-Nov-2002  nathanw Catch up to -current
 1.29.6.6 18-Oct-2002  nathanw Catch up to -current.
 1.29.6.5 17-Sep-2002  nathanw Catch up to -current.
 1.29.6.4 28-Feb-2002  nathanw Catch up to -current.
 1.29.6.3 08-Jan-2002  nathanw Catch up to -current.
 1.29.6.2 14-Nov-2001  nathanw Catch up to -current.
 1.29.6.1 21-Jun-2001  nathanw Catch up to -current.
 1.30.4.2 26-Sep-2001  fvdl * add a VCLONED vnode flag that indicates a vnode representing a cloned
device.
* rename REVOKEALL to REVOKEALIAS, and add a REVOKECLONE flag, to pass
to VOP_REVOKE
* the revoke system call will revoke all aliases, as before, but not the
clones
* vdevgone is called when detaching a device, so make it use REVOKECLONE
to get rid of all clones as well
* clean up all uses of VOP_OPEN wrt. locking.
* add a few VOPS to spec_vnops that need to do something when it's a
clone vnode (access and getattr)
* add a copy of the vnode vattr structure of the original 'master' vnode
to the specinfo of a cloned vnode. could possibly redirect getattr to
the 'master' vnode, but this has issues with revoke
* add a vdev_reassignvp function that disassociates a vnode from its
original device, and reassociates it with the specified dev_t. to be
used by cloning devices only, in case a new minor is allocated.
* change all direct references in drivers to v_devcookie and v_rdev
to vdev_privdata(vp) and vdev_rdev(vp). for diagnostic purposes
when debugging race conditions that still exist wrt. locking and
revoking vnodes.
* make the locking state of a vnode consistent when passed to
d_open and d_close (unlocked). locked would be better, but has
some deadlock issues
 1.30.4.1 07-Sep-2001  thorpej Commit my "devvp" changes to the thorpej-devvp branch. This
replaces the use of dev_t in most places with a struct vnode *.

This will form the basic infrastructure for real cloning device
support (besides being architecurally cleaner -- it'll be good
to get away from using numbers to represent objects).
 1.30.2.3 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.30.2.2 16-Mar-2002  jdolecek Catch up with -current.
 1.30.2.1 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.33.8.1 16-May-2002  gehenna Add the character device switch.
Replace the direct-access to devsw table with calling devsw API.
 1.40.2.6 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.40.2.5 21-Sep-2004  skrll Fix the sync with head I botched.
 1.40.2.4 18-Sep-2004  skrll Sync with HEAD.
 1.40.2.3 25-Aug-2004  skrll Sync with HEAD.
 1.40.2.2 03-Aug-2004  skrll Sync with HEAD
 1.40.2.1 02-Jul-2003  darrenr Apply the aborted ktrace-lwp changes to a specific branch. This is just for
others to review, I'm concerned that patch fuziness may have resulted in some
errant code being generated but I'll look at that later by comparing the diff
from the base to the branch with the file I attempt to apply to it. This will,
at the very least, put the changes in a better context for others to review
them and attempt to tinker with removing passing of 'struct lwp' through
the kernel.
 1.42.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.42.4.1 29-Apr-2005  kent sync with -current
 1.45.4.3 03-Sep-2007  yamt sync with head.
 1.45.4.2 30-Dec-2006  yamt sync with head.
 1.45.4.1 21-Jun-2006  yamt sync with head.
 1.46.12.1 31-Mar-2006  tron Merge 2006-03-31 NetBSD-current into the "peter-altq" branch.
 1.46.10.1 19-Apr-2006  elad sync with head.
 1.46.8.2 14-Sep-2006  yamt sync with head.
 1.46.8.1 01-Apr-2006  yamt sync with head.
 1.46.6.1 22-Apr-2006  simonb Sync with head.
 1.46.4.1 09-Sep-2006  rpaulo sync with head
 1.49.4.2 10-Dec-2006  yamt sync with head.
 1.49.4.1 22-Oct-2006  yamt sync with head
 1.49.2.1 18-Nov-2006  ad Sync with head.
 1.51.4.1 12-Mar-2007  rmind Sync with HEAD.
 1.52.36.5 17-Jan-2009  mjf Sync with HEAD.
 1.52.36.4 29-Jun-2008  mjf Sync with HEAD.
 1.52.36.3 02-Jun-2008  mjf Sync with HEAD.
 1.52.36.2 06-Apr-2008  mjf - after some discussion with agc@ i agreed it would be a good idea to move
device_unregister_* to device_deregister_* to be more like the pmf(9)
functions, especially since a lot of the time the function calls are next
to each other.

- add device_register_name() support for dk(4).
 1.52.36.1 05-Apr-2008  mjf - add "file-system DEVFS" and "pseudo-device devfsctl" to conf/std seeing
as these are always needed.

- convert many, many drivers over to the New Devfs World Order. For a
list of device drivers yet to be converted see,
http://www.netbsd.org/~mjf/devfs-todo.html.

- add a new device_unregister_all(device_t) function to remove all device
names associated with a device_t, which saves us having to construct
device names when the driver is detached.

- add a DEV_AUDIO type for devices.
 1.53.4.4 11-Mar-2010  yamt sync with head
 1.53.4.3 16-May-2009  yamt sync with head
 1.53.4.2 04-May-2009  yamt sync with head.
 1.53.4.1 16-May-2008  yamt sync with head.
 1.53.2.2 17-Jun-2008  yamt sync with head.
 1.53.2.1 18-May-2008  yamt sync with head.
 1.54.2.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.55.6.1 19-Jan-2009  skrll Sync with HEAD.
 1.56.2.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.59.16.1 18-Feb-2012  mrg merge to -current.
 1.59.12.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.59.12.1 17-Apr-2012  yamt sync with head
 1.60.10.1 18-May-2014  rmind sync with head
 1.60.6.2 03-Dec-2017  jdolecek update from HEAD
 1.60.6.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.61.2.1 10-Aug-2014  tls Rebase.
 1.62.4.2 05-Dec-2016  skrll Sync with HEAD
 1.62.4.1 05-Oct-2016  skrll Sync with HEAD
 1.63.2.2 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.63.2.1 06-Aug-2016  pgoyette Sync with HEAD

RSS XML Feed