History log of /openbsd-current/sys/scsi/scsi_ioctl.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.67 22-Sep-2020 krw

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

No intentional functional change.

luna88k test by aoyama@, sparc64 test by jmatthew@

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


# 1.66 19-Aug-2020 krw

Replace last 2 'blah & (flag1 | flag2) == 0' with !ISSET(blah, (flag1 | flag2)).


# 1.65 11-Aug-2020 krw

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

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


# 1.64 16-Jul-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.63 07-Dec-2019 krw

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


# 1.62 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.61 23-Nov-2019 krw

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


# 1.60 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.59 22-Nov-2019 krw

Be consistent and always use CLR() to clear flags.


# 1.58 21-Nov-2019 krw

Be consistent and always use SET() to set flags in xs->flags.


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

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


# 1.56 29-Sep-2019 krw

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


# 1.55 27-Sep-2019 krw

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


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.54 27-Apr-2018 guenther

Delete superfluous includes of <sys/file.>

ok krw@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.53 08-Sep-2017 deraadt

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


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

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

No functional change.

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


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

More damned eye searing whitespace.


# 1.50 14-Mar-2015 jsg

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

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.49 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.48 21-Jun-2011 matthew

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

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


# 1.47 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.46 22-Jul-2010 matthew

Mark two more DMA accessible malloc(9)s.


# 1.45 10-Jul-2010 matthew

Limit SCIOCCOMMAND and ATAIOCCOMMAND requests to transferring MAXPHYS
bytes worth of data. Also, pass M_CANFAIL to malloc(9) and handle
allocation failures by returning ENOMEM.

Prevents user space from causing a kernel panic by passing
ridiculously large datalen values.

ok dlg@, krw@


# 1.44 01-Jul-2010 krw

ioctl XS_SENSE/XS_SHORTSENSE should print the sense info like
everyone else when SCSIDEBUG is set.


# 1.43 01-Jul-2010 matthew

Call malloc(9) with M_ZERO to prevent code paths from exposing
uninitialized memory to user space or devices.

ok deraadt@, dlg@


# 1.42 15-Jun-2010 dlg

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

ok krw@ marco@


# 1.41 23-Apr-2010 dlg

the SAT spec from t10.org defines messages for tunnelling ATA commands over
SCSI when you're talking to a scsi to ata translation layer (satl). this
implements the ata command ioctl handler in scsi_ioctl.c that wraps the
ata command requests up in these scsi commands and then issues them against
the hardware. this cuts atascsi over to interpreting these scsi commands
rather than the ioctls directly.

this should allow mpi users (with a relatively recent firmware) to use
atactl against their SATA disks. i cant test cos my mpi(4) parts are too
old to either support these scsi commands or too old to take recent
firmwares.

ok marco@ @krw tested by krw@ and me on ahci/atascsi


Revision tags: OPENBSD_4_7_BASE
# 1.40 27-Feb-2010 krw

Don't print sense info for ioctl's. Let the userland program decide
what to tell the user. Silences cdio and cdrecord while probing
media.

ok dlg@ marco@ deraadt@


# 1.39 04-Jan-2010 dlg

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

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

ok krw@


# 1.38 01-Dec-2009 dlg

put the midlayer changes back in.

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

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.37 12-Nov-2009 dlg

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

sigh, this makes me sad.


# 1.36 10-Nov-2009 dlg

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

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


# 1.35 05-Nov-2009 marco

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

ok deraadt krw miod


# 1.34 27-Oct-2009 krw

Check return value of scsi_xs_get() in scsi_ioc_cmd() for NULL. It
may or may not be possible to get NULL these days, but paranoia and
consistancy of usage are nice. Prompted by looking at PR#6247 from
Steven Mestdagh.

ok dlg@


# 1.33 12-Oct-2009 dlg

avoid smashing the stack when we have sense data to return back to userland


# 1.32 14-Sep-2009 dlg

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

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

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

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

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

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

thanks to dave del debbio for testing.

ok deraadt@


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

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


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.30 16-Sep-2007 krw

MALLOC/FREE -> malloc/free, M_ZERO, extraneous casts,
extraneous #include <malloc.h>


# 1.29 07-Sep-2007 krw

The obvious bzero/memset -> M_ZERO changes.


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.28 16-Jan-2007 krw

Report SDEV_UMASS devices as non-'SCSI', i.e. 'ATAPI', devices when
asked via SCIOCIDENTIFY. More closely emulates the Linux
SCIOCIDENTIFY/SG_EMULATED_HOST combination used by cdparanoia (and
god knows what other ports). Nothing in the tree itself uses
SCIOCIDENTIFY.

Fixes cdparanoia's handling of USB CD drives that can't handle the
MODE SENSE command. cdparanoia sends 'ATAPI' devices only MODE SENSE
BIG (0x5a) commands. Finding actual SCSI devices that might have
trouble with MODE SENSE BIG (i.e. very old devices) behind USB is
deemed highly unlikely.

Problem reported, debugged and fix tested by bernd@.

ok marco@ millert@ miod@


# 1.27 21-Dec-2006 krw

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


Revision tags: OPENBSD_4_0_BASE
# 1.26 23-Jul-2006 dlg

remove some scsi ioctls, most importantly the ones that can reprobe the
bus. supporting these complicates the midlayer unnecessarily.

ok krw@


# 1.25 11-Jul-2006 dlg

a few formatting tweaks. this was mostly knf/ansi already. no binary change


# 1.24 11-May-2006 krw

Zap trailing whitespace.


# 1.23 20-Apr-2006 miod

Fix various printf() issues: too many arguments, not enough arguments, argument
order reversed, wrong modifiers. ok deraadt@ marco@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.22 08-Dec-2005 krw

Don't panic for pathological i/o sizes unless minphys() really is
broken. Eliminate an unneeded variable and potential conversion issues
in SCIOCCOMMAND code before calling physio.

Similar to what NetBSD does.

Fixes cdda2wav vs "Billie Holiday - Songs for Distingue Lovers"
problem noted by Alexandre Ratchov. Tested by Alexandre.

ok marco@ pedro@ deraadt@ mickey@


# 1.21 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.20 28-May-2005 krw

Handle XS_SHORTSENSE error codes in ioctl situations. From NetBSD.

Fixes spurious error messages from cdparanoia when using ATAPI CDROM
in USB enclosure.

ok pascoe@.


Revision tags: OPENBSD_3_7_BASE
# 1.19 26-Dec-2004 miod

Use list and queue macros where applicable to make the code easier to read;
no change in compiler assembly output.


# 1.18 30-Nov-2004 krw

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

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

Tested by Nikolay, ok deraadt@.


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE SMP_SYNC_A SMP_SYNC_B
# 1.17 03-Jan-2004 krw

Make 'unknown error category' message more informative by including error
number. Same change as was made to scsi_base.c a few days ago.

ok deraadt@.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE OPENBSD_3_4_BASE UBC_SYNC_A UBC_SYNC_B
# 1.16 09-Jun-2002 art

some splassert and protection of biodone.


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

First round of __P removal in sys


# 1.14 07-Jan-2002 mickey

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


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

branches: 1.13.4;
KNF


# 1.12 30-Apr-2001 csapuntz

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


Revision tags: OPENBSD_2_9_BASE
# 1.11 25-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE kame_19991208
# 1.10 24-Aug-1999 csapuntz

branches: 1.10.4;


Added type field to scsi_addr to reflect bus/device type (ATAPI or SCSI).
Updated ioctls to match

Minor patches in anticipation of wd->sd translation layer


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.9 01-Sep-1997 niklas

Correct a debug printout


# 1.8 31-Aug-1997 downsj

* Make scsi_do_ioctl() sane again.
* Use scsi_do_ioctl() in cd.c and uk.c again.


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

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

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

Everybody needs to test this.


Revision tags: OPENBSD_2_0_BASE
# 1.6 13-Aug-1996 niklas

Remove unused variable


# 1.5 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.4 17-Jun-1996 downsj

SCIOCCOMMAND now requires that the device be open for writing.


# 1.3 16-Jun-1996 downsj

netbsd pr#2220: add SCIOCRESET. I can't think of any reason not to.


# 1.2 20-Feb-1996 briggs

Sync. w/ NetBSD: scsi prototypes.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.66 19-Aug-2020 krw

Replace last 2 'blah & (flag1 | flag2) == 0' with !ISSET(blah, (flag1 | flag2)).


# 1.65 11-Aug-2020 krw

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

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


# 1.64 16-Jul-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.63 07-Dec-2019 krw

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


# 1.62 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.61 23-Nov-2019 krw

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


# 1.60 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.59 22-Nov-2019 krw

Be consistent and always use CLR() to clear flags.


# 1.58 21-Nov-2019 krw

Be consistent and always use SET() to set flags in xs->flags.


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

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


# 1.56 29-Sep-2019 krw

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


# 1.55 27-Sep-2019 krw

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


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.54 27-Apr-2018 guenther

Delete superfluous includes of <sys/file.>

ok krw@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.53 08-Sep-2017 deraadt

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


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

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

No functional change.

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


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

More damned eye searing whitespace.


# 1.50 14-Mar-2015 jsg

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

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.49 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.48 21-Jun-2011 matthew

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

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


# 1.47 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.46 22-Jul-2010 matthew

Mark two more DMA accessible malloc(9)s.


# 1.45 10-Jul-2010 matthew

Limit SCIOCCOMMAND and ATAIOCCOMMAND requests to transferring MAXPHYS
bytes worth of data. Also, pass M_CANFAIL to malloc(9) and handle
allocation failures by returning ENOMEM.

Prevents user space from causing a kernel panic by passing
ridiculously large datalen values.

ok dlg@, krw@


# 1.44 01-Jul-2010 krw

ioctl XS_SENSE/XS_SHORTSENSE should print the sense info like
everyone else when SCSIDEBUG is set.


# 1.43 01-Jul-2010 matthew

Call malloc(9) with M_ZERO to prevent code paths from exposing
uninitialized memory to user space or devices.

ok deraadt@, dlg@


# 1.42 15-Jun-2010 dlg

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

ok krw@ marco@


# 1.41 23-Apr-2010 dlg

the SAT spec from t10.org defines messages for tunnelling ATA commands over
SCSI when you're talking to a scsi to ata translation layer (satl). this
implements the ata command ioctl handler in scsi_ioctl.c that wraps the
ata command requests up in these scsi commands and then issues them against
the hardware. this cuts atascsi over to interpreting these scsi commands
rather than the ioctls directly.

this should allow mpi users (with a relatively recent firmware) to use
atactl against their SATA disks. i cant test cos my mpi(4) parts are too
old to either support these scsi commands or too old to take recent
firmwares.

ok marco@ @krw tested by krw@ and me on ahci/atascsi


Revision tags: OPENBSD_4_7_BASE
# 1.40 27-Feb-2010 krw

Don't print sense info for ioctl's. Let the userland program decide
what to tell the user. Silences cdio and cdrecord while probing
media.

ok dlg@ marco@ deraadt@


# 1.39 04-Jan-2010 dlg

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

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

ok krw@


# 1.38 01-Dec-2009 dlg

put the midlayer changes back in.

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

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.37 12-Nov-2009 dlg

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

sigh, this makes me sad.


# 1.36 10-Nov-2009 dlg

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

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


# 1.35 05-Nov-2009 marco

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

ok deraadt krw miod


# 1.34 27-Oct-2009 krw

Check return value of scsi_xs_get() in scsi_ioc_cmd() for NULL. It
may or may not be possible to get NULL these days, but paranoia and
consistancy of usage are nice. Prompted by looking at PR#6247 from
Steven Mestdagh.

ok dlg@


# 1.33 12-Oct-2009 dlg

avoid smashing the stack when we have sense data to return back to userland


# 1.32 14-Sep-2009 dlg

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

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

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

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

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

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

thanks to dave del debbio for testing.

ok deraadt@


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

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


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.30 16-Sep-2007 krw

MALLOC/FREE -> malloc/free, M_ZERO, extraneous casts,
extraneous #include <malloc.h>


# 1.29 07-Sep-2007 krw

The obvious bzero/memset -> M_ZERO changes.


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.28 16-Jan-2007 krw

Report SDEV_UMASS devices as non-'SCSI', i.e. 'ATAPI', devices when
asked via SCIOCIDENTIFY. More closely emulates the Linux
SCIOCIDENTIFY/SG_EMULATED_HOST combination used by cdparanoia (and
god knows what other ports). Nothing in the tree itself uses
SCIOCIDENTIFY.

Fixes cdparanoia's handling of USB CD drives that can't handle the
MODE SENSE command. cdparanoia sends 'ATAPI' devices only MODE SENSE
BIG (0x5a) commands. Finding actual SCSI devices that might have
trouble with MODE SENSE BIG (i.e. very old devices) behind USB is
deemed highly unlikely.

Problem reported, debugged and fix tested by bernd@.

ok marco@ millert@ miod@


# 1.27 21-Dec-2006 krw

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


Revision tags: OPENBSD_4_0_BASE
# 1.26 23-Jul-2006 dlg

remove some scsi ioctls, most importantly the ones that can reprobe the
bus. supporting these complicates the midlayer unnecessarily.

ok krw@


# 1.25 11-Jul-2006 dlg

a few formatting tweaks. this was mostly knf/ansi already. no binary change


# 1.24 11-May-2006 krw

Zap trailing whitespace.


# 1.23 20-Apr-2006 miod

Fix various printf() issues: too many arguments, not enough arguments, argument
order reversed, wrong modifiers. ok deraadt@ marco@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.22 08-Dec-2005 krw

Don't panic for pathological i/o sizes unless minphys() really is
broken. Eliminate an unneeded variable and potential conversion issues
in SCIOCCOMMAND code before calling physio.

Similar to what NetBSD does.

Fixes cdda2wav vs "Billie Holiday - Songs for Distingue Lovers"
problem noted by Alexandre Ratchov. Tested by Alexandre.

ok marco@ pedro@ deraadt@ mickey@


# 1.21 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.20 28-May-2005 krw

Handle XS_SHORTSENSE error codes in ioctl situations. From NetBSD.

Fixes spurious error messages from cdparanoia when using ATAPI CDROM
in USB enclosure.

ok pascoe@.


Revision tags: OPENBSD_3_7_BASE
# 1.19 26-Dec-2004 miod

Use list and queue macros where applicable to make the code easier to read;
no change in compiler assembly output.


# 1.18 30-Nov-2004 krw

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

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

Tested by Nikolay, ok deraadt@.


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE SMP_SYNC_A SMP_SYNC_B
# 1.17 03-Jan-2004 krw

Make 'unknown error category' message more informative by including error
number. Same change as was made to scsi_base.c a few days ago.

ok deraadt@.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE OPENBSD_3_4_BASE UBC_SYNC_A UBC_SYNC_B
# 1.16 09-Jun-2002 art

some splassert and protection of biodone.


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

First round of __P removal in sys


# 1.14 07-Jan-2002 mickey

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


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

branches: 1.13.4;
KNF


# 1.12 30-Apr-2001 csapuntz

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


Revision tags: OPENBSD_2_9_BASE
# 1.11 25-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE kame_19991208
# 1.10 24-Aug-1999 csapuntz

branches: 1.10.4;


Added type field to scsi_addr to reflect bus/device type (ATAPI or SCSI).
Updated ioctls to match

Minor patches in anticipation of wd->sd translation layer


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.9 01-Sep-1997 niklas

Correct a debug printout


# 1.8 31-Aug-1997 downsj

* Make scsi_do_ioctl() sane again.
* Use scsi_do_ioctl() in cd.c and uk.c again.


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

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

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

Everybody needs to test this.


Revision tags: OPENBSD_2_0_BASE
# 1.6 13-Aug-1996 niklas

Remove unused variable


# 1.5 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.4 17-Jun-1996 downsj

SCIOCCOMMAND now requires that the device be open for writing.


# 1.3 16-Jun-1996 downsj

netbsd pr#2220: add SCIOCRESET. I can't think of any reason not to.


# 1.2 20-Feb-1996 briggs

Sync. w/ NetBSD: scsi prototypes.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.65 11-Aug-2020 krw

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

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


# 1.64 16-Jul-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.63 07-Dec-2019 krw

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


# 1.62 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.61 23-Nov-2019 krw

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


# 1.60 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.59 22-Nov-2019 krw

Be consistent and always use CLR() to clear flags.


# 1.58 21-Nov-2019 krw

Be consistent and always use SET() to set flags in xs->flags.


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

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


# 1.56 29-Sep-2019 krw

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


# 1.55 27-Sep-2019 krw

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


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.54 27-Apr-2018 guenther

Delete superfluous includes of <sys/file.>

ok krw@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.53 08-Sep-2017 deraadt

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


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

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

No functional change.

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


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

More damned eye searing whitespace.


# 1.50 14-Mar-2015 jsg

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

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.49 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.48 21-Jun-2011 matthew

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

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


# 1.47 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.46 22-Jul-2010 matthew

Mark two more DMA accessible malloc(9)s.


# 1.45 10-Jul-2010 matthew

Limit SCIOCCOMMAND and ATAIOCCOMMAND requests to transferring MAXPHYS
bytes worth of data. Also, pass M_CANFAIL to malloc(9) and handle
allocation failures by returning ENOMEM.

Prevents user space from causing a kernel panic by passing
ridiculously large datalen values.

ok dlg@, krw@


# 1.44 01-Jul-2010 krw

ioctl XS_SENSE/XS_SHORTSENSE should print the sense info like
everyone else when SCSIDEBUG is set.


# 1.43 01-Jul-2010 matthew

Call malloc(9) with M_ZERO to prevent code paths from exposing
uninitialized memory to user space or devices.

ok deraadt@, dlg@


# 1.42 15-Jun-2010 dlg

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

ok krw@ marco@


# 1.41 23-Apr-2010 dlg

the SAT spec from t10.org defines messages for tunnelling ATA commands over
SCSI when you're talking to a scsi to ata translation layer (satl). this
implements the ata command ioctl handler in scsi_ioctl.c that wraps the
ata command requests up in these scsi commands and then issues them against
the hardware. this cuts atascsi over to interpreting these scsi commands
rather than the ioctls directly.

this should allow mpi users (with a relatively recent firmware) to use
atactl against their SATA disks. i cant test cos my mpi(4) parts are too
old to either support these scsi commands or too old to take recent
firmwares.

ok marco@ @krw tested by krw@ and me on ahci/atascsi


Revision tags: OPENBSD_4_7_BASE
# 1.40 27-Feb-2010 krw

Don't print sense info for ioctl's. Let the userland program decide
what to tell the user. Silences cdio and cdrecord while probing
media.

ok dlg@ marco@ deraadt@


# 1.39 04-Jan-2010 dlg

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

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

ok krw@


# 1.38 01-Dec-2009 dlg

put the midlayer changes back in.

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

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.37 12-Nov-2009 dlg

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

sigh, this makes me sad.


# 1.36 10-Nov-2009 dlg

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

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


# 1.35 05-Nov-2009 marco

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

ok deraadt krw miod


# 1.34 27-Oct-2009 krw

Check return value of scsi_xs_get() in scsi_ioc_cmd() for NULL. It
may or may not be possible to get NULL these days, but paranoia and
consistancy of usage are nice. Prompted by looking at PR#6247 from
Steven Mestdagh.

ok dlg@


# 1.33 12-Oct-2009 dlg

avoid smashing the stack when we have sense data to return back to userland


# 1.32 14-Sep-2009 dlg

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

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

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

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

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

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

thanks to dave del debbio for testing.

ok deraadt@


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

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


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.30 16-Sep-2007 krw

MALLOC/FREE -> malloc/free, M_ZERO, extraneous casts,
extraneous #include <malloc.h>


# 1.29 07-Sep-2007 krw

The obvious bzero/memset -> M_ZERO changes.


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.28 16-Jan-2007 krw

Report SDEV_UMASS devices as non-'SCSI', i.e. 'ATAPI', devices when
asked via SCIOCIDENTIFY. More closely emulates the Linux
SCIOCIDENTIFY/SG_EMULATED_HOST combination used by cdparanoia (and
god knows what other ports). Nothing in the tree itself uses
SCIOCIDENTIFY.

Fixes cdparanoia's handling of USB CD drives that can't handle the
MODE SENSE command. cdparanoia sends 'ATAPI' devices only MODE SENSE
BIG (0x5a) commands. Finding actual SCSI devices that might have
trouble with MODE SENSE BIG (i.e. very old devices) behind USB is
deemed highly unlikely.

Problem reported, debugged and fix tested by bernd@.

ok marco@ millert@ miod@


# 1.27 21-Dec-2006 krw

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


Revision tags: OPENBSD_4_0_BASE
# 1.26 23-Jul-2006 dlg

remove some scsi ioctls, most importantly the ones that can reprobe the
bus. supporting these complicates the midlayer unnecessarily.

ok krw@


# 1.25 11-Jul-2006 dlg

a few formatting tweaks. this was mostly knf/ansi already. no binary change


# 1.24 11-May-2006 krw

Zap trailing whitespace.


# 1.23 20-Apr-2006 miod

Fix various printf() issues: too many arguments, not enough arguments, argument
order reversed, wrong modifiers. ok deraadt@ marco@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.22 08-Dec-2005 krw

Don't panic for pathological i/o sizes unless minphys() really is
broken. Eliminate an unneeded variable and potential conversion issues
in SCIOCCOMMAND code before calling physio.

Similar to what NetBSD does.

Fixes cdda2wav vs "Billie Holiday - Songs for Distingue Lovers"
problem noted by Alexandre Ratchov. Tested by Alexandre.

ok marco@ pedro@ deraadt@ mickey@


# 1.21 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.20 28-May-2005 krw

Handle XS_SHORTSENSE error codes in ioctl situations. From NetBSD.

Fixes spurious error messages from cdparanoia when using ATAPI CDROM
in USB enclosure.

ok pascoe@.


Revision tags: OPENBSD_3_7_BASE
# 1.19 26-Dec-2004 miod

Use list and queue macros where applicable to make the code easier to read;
no change in compiler assembly output.


# 1.18 30-Nov-2004 krw

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

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

Tested by Nikolay, ok deraadt@.


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE SMP_SYNC_A SMP_SYNC_B
# 1.17 03-Jan-2004 krw

Make 'unknown error category' message more informative by including error
number. Same change as was made to scsi_base.c a few days ago.

ok deraadt@.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE OPENBSD_3_4_BASE UBC_SYNC_A UBC_SYNC_B
# 1.16 09-Jun-2002 art

some splassert and protection of biodone.


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

First round of __P removal in sys


# 1.14 07-Jan-2002 mickey

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


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

branches: 1.13.4;
KNF


# 1.12 30-Apr-2001 csapuntz

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


Revision tags: OPENBSD_2_9_BASE
# 1.11 25-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE kame_19991208
# 1.10 24-Aug-1999 csapuntz

branches: 1.10.4;


Added type field to scsi_addr to reflect bus/device type (ATAPI or SCSI).
Updated ioctls to match

Minor patches in anticipation of wd->sd translation layer


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.9 01-Sep-1997 niklas

Correct a debug printout


# 1.8 31-Aug-1997 downsj

* Make scsi_do_ioctl() sane again.
* Use scsi_do_ioctl() in cd.c and uk.c again.


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

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

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

Everybody needs to test this.


Revision tags: OPENBSD_2_0_BASE
# 1.6 13-Aug-1996 niklas

Remove unused variable


# 1.5 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.4 17-Jun-1996 downsj

SCIOCCOMMAND now requires that the device be open for writing.


# 1.3 16-Jun-1996 downsj

netbsd pr#2220: add SCIOCRESET. I can't think of any reason not to.


# 1.2 20-Feb-1996 briggs

Sync. w/ NetBSD: scsi prototypes.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.64 16-Jul-2020 krw

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


Revision tags: OPENBSD_6_7_BASE
# 1.63 07-Dec-2019 krw

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


# 1.62 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.61 23-Nov-2019 krw

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


# 1.60 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.59 22-Nov-2019 krw

Be consistent and always use CLR() to clear flags.


# 1.58 21-Nov-2019 krw

Be consistent and always use SET() to set flags in xs->flags.


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

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


# 1.56 29-Sep-2019 krw

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


# 1.55 27-Sep-2019 krw

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


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.54 27-Apr-2018 guenther

Delete superfluous includes of <sys/file.>

ok krw@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.53 08-Sep-2017 deraadt

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


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

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

No functional change.

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


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

More damned eye searing whitespace.


# 1.50 14-Mar-2015 jsg

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

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.49 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.48 21-Jun-2011 matthew

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

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


# 1.47 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.46 22-Jul-2010 matthew

Mark two more DMA accessible malloc(9)s.


# 1.45 10-Jul-2010 matthew

Limit SCIOCCOMMAND and ATAIOCCOMMAND requests to transferring MAXPHYS
bytes worth of data. Also, pass M_CANFAIL to malloc(9) and handle
allocation failures by returning ENOMEM.

Prevents user space from causing a kernel panic by passing
ridiculously large datalen values.

ok dlg@, krw@


# 1.44 01-Jul-2010 krw

ioctl XS_SENSE/XS_SHORTSENSE should print the sense info like
everyone else when SCSIDEBUG is set.


# 1.43 01-Jul-2010 matthew

Call malloc(9) with M_ZERO to prevent code paths from exposing
uninitialized memory to user space or devices.

ok deraadt@, dlg@


# 1.42 15-Jun-2010 dlg

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

ok krw@ marco@


# 1.41 23-Apr-2010 dlg

the SAT spec from t10.org defines messages for tunnelling ATA commands over
SCSI when you're talking to a scsi to ata translation layer (satl). this
implements the ata command ioctl handler in scsi_ioctl.c that wraps the
ata command requests up in these scsi commands and then issues them against
the hardware. this cuts atascsi over to interpreting these scsi commands
rather than the ioctls directly.

this should allow mpi users (with a relatively recent firmware) to use
atactl against their SATA disks. i cant test cos my mpi(4) parts are too
old to either support these scsi commands or too old to take recent
firmwares.

ok marco@ @krw tested by krw@ and me on ahci/atascsi


Revision tags: OPENBSD_4_7_BASE
# 1.40 27-Feb-2010 krw

Don't print sense info for ioctl's. Let the userland program decide
what to tell the user. Silences cdio and cdrecord while probing
media.

ok dlg@ marco@ deraadt@


# 1.39 04-Jan-2010 dlg

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

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

ok krw@


# 1.38 01-Dec-2009 dlg

put the midlayer changes back in.

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

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.37 12-Nov-2009 dlg

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

sigh, this makes me sad.


# 1.36 10-Nov-2009 dlg

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

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


# 1.35 05-Nov-2009 marco

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

ok deraadt krw miod


# 1.34 27-Oct-2009 krw

Check return value of scsi_xs_get() in scsi_ioc_cmd() for NULL. It
may or may not be possible to get NULL these days, but paranoia and
consistancy of usage are nice. Prompted by looking at PR#6247 from
Steven Mestdagh.

ok dlg@


# 1.33 12-Oct-2009 dlg

avoid smashing the stack when we have sense data to return back to userland


# 1.32 14-Sep-2009 dlg

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

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

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

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

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

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

thanks to dave del debbio for testing.

ok deraadt@


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

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


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.30 16-Sep-2007 krw

MALLOC/FREE -> malloc/free, M_ZERO, extraneous casts,
extraneous #include <malloc.h>


# 1.29 07-Sep-2007 krw

The obvious bzero/memset -> M_ZERO changes.


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.28 16-Jan-2007 krw

Report SDEV_UMASS devices as non-'SCSI', i.e. 'ATAPI', devices when
asked via SCIOCIDENTIFY. More closely emulates the Linux
SCIOCIDENTIFY/SG_EMULATED_HOST combination used by cdparanoia (and
god knows what other ports). Nothing in the tree itself uses
SCIOCIDENTIFY.

Fixes cdparanoia's handling of USB CD drives that can't handle the
MODE SENSE command. cdparanoia sends 'ATAPI' devices only MODE SENSE
BIG (0x5a) commands. Finding actual SCSI devices that might have
trouble with MODE SENSE BIG (i.e. very old devices) behind USB is
deemed highly unlikely.

Problem reported, debugged and fix tested by bernd@.

ok marco@ millert@ miod@


# 1.27 21-Dec-2006 krw

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


Revision tags: OPENBSD_4_0_BASE
# 1.26 23-Jul-2006 dlg

remove some scsi ioctls, most importantly the ones that can reprobe the
bus. supporting these complicates the midlayer unnecessarily.

ok krw@


# 1.25 11-Jul-2006 dlg

a few formatting tweaks. this was mostly knf/ansi already. no binary change


# 1.24 11-May-2006 krw

Zap trailing whitespace.


# 1.23 20-Apr-2006 miod

Fix various printf() issues: too many arguments, not enough arguments, argument
order reversed, wrong modifiers. ok deraadt@ marco@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.22 08-Dec-2005 krw

Don't panic for pathological i/o sizes unless minphys() really is
broken. Eliminate an unneeded variable and potential conversion issues
in SCIOCCOMMAND code before calling physio.

Similar to what NetBSD does.

Fixes cdda2wav vs "Billie Holiday - Songs for Distingue Lovers"
problem noted by Alexandre Ratchov. Tested by Alexandre.

ok marco@ pedro@ deraadt@ mickey@


# 1.21 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.20 28-May-2005 krw

Handle XS_SHORTSENSE error codes in ioctl situations. From NetBSD.

Fixes spurious error messages from cdparanoia when using ATAPI CDROM
in USB enclosure.

ok pascoe@.


Revision tags: OPENBSD_3_7_BASE
# 1.19 26-Dec-2004 miod

Use list and queue macros where applicable to make the code easier to read;
no change in compiler assembly output.


# 1.18 30-Nov-2004 krw

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

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

Tested by Nikolay, ok deraadt@.


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE SMP_SYNC_A SMP_SYNC_B
# 1.17 03-Jan-2004 krw

Make 'unknown error category' message more informative by including error
number. Same change as was made to scsi_base.c a few days ago.

ok deraadt@.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE OPENBSD_3_4_BASE UBC_SYNC_A UBC_SYNC_B
# 1.16 09-Jun-2002 art

some splassert and protection of biodone.


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

First round of __P removal in sys


# 1.14 07-Jan-2002 mickey

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


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

branches: 1.13.4;
KNF


# 1.12 30-Apr-2001 csapuntz

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


Revision tags: OPENBSD_2_9_BASE
# 1.11 25-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE kame_19991208
# 1.10 24-Aug-1999 csapuntz

branches: 1.10.4;


Added type field to scsi_addr to reflect bus/device type (ATAPI or SCSI).
Updated ioctls to match

Minor patches in anticipation of wd->sd translation layer


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.9 01-Sep-1997 niklas

Correct a debug printout


# 1.8 31-Aug-1997 downsj

* Make scsi_do_ioctl() sane again.
* Use scsi_do_ioctl() in cd.c and uk.c again.


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

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

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

Everybody needs to test this.


Revision tags: OPENBSD_2_0_BASE
# 1.6 13-Aug-1996 niklas

Remove unused variable


# 1.5 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.4 17-Jun-1996 downsj

SCIOCCOMMAND now requires that the device be open for writing.


# 1.3 16-Jun-1996 downsj

netbsd pr#2220: add SCIOCRESET. I can't think of any reason not to.


# 1.2 20-Feb-1996 briggs

Sync. w/ NetBSD: scsi prototypes.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.63 07-Dec-2019 krw

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


# 1.62 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.61 23-Nov-2019 krw

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


# 1.60 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.59 22-Nov-2019 krw

Be consistent and always use CLR() to clear flags.


# 1.58 21-Nov-2019 krw

Be consistent and always use SET() to set flags in xs->flags.


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

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


# 1.56 29-Sep-2019 krw

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


# 1.55 27-Sep-2019 krw

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


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.54 27-Apr-2018 guenther

Delete superfluous includes of <sys/file.>

ok krw@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.53 08-Sep-2017 deraadt

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


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

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

No functional change.

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


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

More damned eye searing whitespace.


# 1.50 14-Mar-2015 jsg

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

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.49 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.48 21-Jun-2011 matthew

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

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


# 1.47 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.46 22-Jul-2010 matthew

Mark two more DMA accessible malloc(9)s.


# 1.45 10-Jul-2010 matthew

Limit SCIOCCOMMAND and ATAIOCCOMMAND requests to transferring MAXPHYS
bytes worth of data. Also, pass M_CANFAIL to malloc(9) and handle
allocation failures by returning ENOMEM.

Prevents user space from causing a kernel panic by passing
ridiculously large datalen values.

ok dlg@, krw@


# 1.44 01-Jul-2010 krw

ioctl XS_SENSE/XS_SHORTSENSE should print the sense info like
everyone else when SCSIDEBUG is set.


# 1.43 01-Jul-2010 matthew

Call malloc(9) with M_ZERO to prevent code paths from exposing
uninitialized memory to user space or devices.

ok deraadt@, dlg@


# 1.42 15-Jun-2010 dlg

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

ok krw@ marco@


# 1.41 23-Apr-2010 dlg

the SAT spec from t10.org defines messages for tunnelling ATA commands over
SCSI when you're talking to a scsi to ata translation layer (satl). this
implements the ata command ioctl handler in scsi_ioctl.c that wraps the
ata command requests up in these scsi commands and then issues them against
the hardware. this cuts atascsi over to interpreting these scsi commands
rather than the ioctls directly.

this should allow mpi users (with a relatively recent firmware) to use
atactl against their SATA disks. i cant test cos my mpi(4) parts are too
old to either support these scsi commands or too old to take recent
firmwares.

ok marco@ @krw tested by krw@ and me on ahci/atascsi


Revision tags: OPENBSD_4_7_BASE
# 1.40 27-Feb-2010 krw

Don't print sense info for ioctl's. Let the userland program decide
what to tell the user. Silences cdio and cdrecord while probing
media.

ok dlg@ marco@ deraadt@


# 1.39 04-Jan-2010 dlg

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

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

ok krw@


# 1.38 01-Dec-2009 dlg

put the midlayer changes back in.

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

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.37 12-Nov-2009 dlg

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

sigh, this makes me sad.


# 1.36 10-Nov-2009 dlg

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

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


# 1.35 05-Nov-2009 marco

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

ok deraadt krw miod


# 1.34 27-Oct-2009 krw

Check return value of scsi_xs_get() in scsi_ioc_cmd() for NULL. It
may or may not be possible to get NULL these days, but paranoia and
consistancy of usage are nice. Prompted by looking at PR#6247 from
Steven Mestdagh.

ok dlg@


# 1.33 12-Oct-2009 dlg

avoid smashing the stack when we have sense data to return back to userland


# 1.32 14-Sep-2009 dlg

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

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

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

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

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

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

thanks to dave del debbio for testing.

ok deraadt@


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

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


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.30 16-Sep-2007 krw

MALLOC/FREE -> malloc/free, M_ZERO, extraneous casts,
extraneous #include <malloc.h>


# 1.29 07-Sep-2007 krw

The obvious bzero/memset -> M_ZERO changes.


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.28 16-Jan-2007 krw

Report SDEV_UMASS devices as non-'SCSI', i.e. 'ATAPI', devices when
asked via SCIOCIDENTIFY. More closely emulates the Linux
SCIOCIDENTIFY/SG_EMULATED_HOST combination used by cdparanoia (and
god knows what other ports). Nothing in the tree itself uses
SCIOCIDENTIFY.

Fixes cdparanoia's handling of USB CD drives that can't handle the
MODE SENSE command. cdparanoia sends 'ATAPI' devices only MODE SENSE
BIG (0x5a) commands. Finding actual SCSI devices that might have
trouble with MODE SENSE BIG (i.e. very old devices) behind USB is
deemed highly unlikely.

Problem reported, debugged and fix tested by bernd@.

ok marco@ millert@ miod@


# 1.27 21-Dec-2006 krw

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


Revision tags: OPENBSD_4_0_BASE
# 1.26 23-Jul-2006 dlg

remove some scsi ioctls, most importantly the ones that can reprobe the
bus. supporting these complicates the midlayer unnecessarily.

ok krw@


# 1.25 11-Jul-2006 dlg

a few formatting tweaks. this was mostly knf/ansi already. no binary change


# 1.24 11-May-2006 krw

Zap trailing whitespace.


# 1.23 20-Apr-2006 miod

Fix various printf() issues: too many arguments, not enough arguments, argument
order reversed, wrong modifiers. ok deraadt@ marco@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.22 08-Dec-2005 krw

Don't panic for pathological i/o sizes unless minphys() really is
broken. Eliminate an unneeded variable and potential conversion issues
in SCIOCCOMMAND code before calling physio.

Similar to what NetBSD does.

Fixes cdda2wav vs "Billie Holiday - Songs for Distingue Lovers"
problem noted by Alexandre Ratchov. Tested by Alexandre.

ok marco@ pedro@ deraadt@ mickey@


# 1.21 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.20 28-May-2005 krw

Handle XS_SHORTSENSE error codes in ioctl situations. From NetBSD.

Fixes spurious error messages from cdparanoia when using ATAPI CDROM
in USB enclosure.

ok pascoe@.


Revision tags: OPENBSD_3_7_BASE
# 1.19 26-Dec-2004 miod

Use list and queue macros where applicable to make the code easier to read;
no change in compiler assembly output.


# 1.18 30-Nov-2004 krw

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

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

Tested by Nikolay, ok deraadt@.


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE SMP_SYNC_A SMP_SYNC_B
# 1.17 03-Jan-2004 krw

Make 'unknown error category' message more informative by including error
number. Same change as was made to scsi_base.c a few days ago.

ok deraadt@.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE OPENBSD_3_4_BASE UBC_SYNC_A UBC_SYNC_B
# 1.16 09-Jun-2002 art

some splassert and protection of biodone.


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

First round of __P removal in sys


# 1.14 07-Jan-2002 mickey

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


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

branches: 1.13.4;
KNF


# 1.12 30-Apr-2001 csapuntz

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


Revision tags: OPENBSD_2_9_BASE
# 1.11 25-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE kame_19991208
# 1.10 24-Aug-1999 csapuntz

branches: 1.10.4;


Added type field to scsi_addr to reflect bus/device type (ATAPI or SCSI).
Updated ioctls to match

Minor patches in anticipation of wd->sd translation layer


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.9 01-Sep-1997 niklas

Correct a debug printout


# 1.8 31-Aug-1997 downsj

* Make scsi_do_ioctl() sane again.
* Use scsi_do_ioctl() in cd.c and uk.c again.


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

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

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

Everybody needs to test this.


Revision tags: OPENBSD_2_0_BASE
# 1.6 13-Aug-1996 niklas

Remove unused variable


# 1.5 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.4 17-Jun-1996 downsj

SCIOCCOMMAND now requires that the device be open for writing.


# 1.3 16-Jun-1996 downsj

netbsd pr#2220: add SCIOCRESET. I can't think of any reason not to.


# 1.2 20-Feb-1996 briggs

Sync. w/ NetBSD: scsi prototypes.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.62 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


# 1.61 23-Nov-2019 krw

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


# 1.60 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.59 22-Nov-2019 krw

Be consistent and always use CLR() to clear flags.


# 1.58 21-Nov-2019 krw

Be consistent and always use SET() to set flags in xs->flags.


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

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


# 1.56 29-Sep-2019 krw

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


# 1.55 27-Sep-2019 krw

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


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.54 27-Apr-2018 guenther

Delete superfluous includes of <sys/file.>

ok krw@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.53 08-Sep-2017 deraadt

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


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

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

No functional change.

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


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

More damned eye searing whitespace.


# 1.50 14-Mar-2015 jsg

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

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.49 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.48 21-Jun-2011 matthew

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

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


# 1.47 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.46 22-Jul-2010 matthew

Mark two more DMA accessible malloc(9)s.


# 1.45 10-Jul-2010 matthew

Limit SCIOCCOMMAND and ATAIOCCOMMAND requests to transferring MAXPHYS
bytes worth of data. Also, pass M_CANFAIL to malloc(9) and handle
allocation failures by returning ENOMEM.

Prevents user space from causing a kernel panic by passing
ridiculously large datalen values.

ok dlg@, krw@


# 1.44 01-Jul-2010 krw

ioctl XS_SENSE/XS_SHORTSENSE should print the sense info like
everyone else when SCSIDEBUG is set.


# 1.43 01-Jul-2010 matthew

Call malloc(9) with M_ZERO to prevent code paths from exposing
uninitialized memory to user space or devices.

ok deraadt@, dlg@


# 1.42 15-Jun-2010 dlg

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

ok krw@ marco@


# 1.41 23-Apr-2010 dlg

the SAT spec from t10.org defines messages for tunnelling ATA commands over
SCSI when you're talking to a scsi to ata translation layer (satl). this
implements the ata command ioctl handler in scsi_ioctl.c that wraps the
ata command requests up in these scsi commands and then issues them against
the hardware. this cuts atascsi over to interpreting these scsi commands
rather than the ioctls directly.

this should allow mpi users (with a relatively recent firmware) to use
atactl against their SATA disks. i cant test cos my mpi(4) parts are too
old to either support these scsi commands or too old to take recent
firmwares.

ok marco@ @krw tested by krw@ and me on ahci/atascsi


Revision tags: OPENBSD_4_7_BASE
# 1.40 27-Feb-2010 krw

Don't print sense info for ioctl's. Let the userland program decide
what to tell the user. Silences cdio and cdrecord while probing
media.

ok dlg@ marco@ deraadt@


# 1.39 04-Jan-2010 dlg

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

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

ok krw@


# 1.38 01-Dec-2009 dlg

put the midlayer changes back in.

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

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.37 12-Nov-2009 dlg

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

sigh, this makes me sad.


# 1.36 10-Nov-2009 dlg

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

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


# 1.35 05-Nov-2009 marco

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

ok deraadt krw miod


# 1.34 27-Oct-2009 krw

Check return value of scsi_xs_get() in scsi_ioc_cmd() for NULL. It
may or may not be possible to get NULL these days, but paranoia and
consistancy of usage are nice. Prompted by looking at PR#6247 from
Steven Mestdagh.

ok dlg@


# 1.33 12-Oct-2009 dlg

avoid smashing the stack when we have sense data to return back to userland


# 1.32 14-Sep-2009 dlg

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

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

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

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

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

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

thanks to dave del debbio for testing.

ok deraadt@


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

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


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.30 16-Sep-2007 krw

MALLOC/FREE -> malloc/free, M_ZERO, extraneous casts,
extraneous #include <malloc.h>


# 1.29 07-Sep-2007 krw

The obvious bzero/memset -> M_ZERO changes.


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.28 16-Jan-2007 krw

Report SDEV_UMASS devices as non-'SCSI', i.e. 'ATAPI', devices when
asked via SCIOCIDENTIFY. More closely emulates the Linux
SCIOCIDENTIFY/SG_EMULATED_HOST combination used by cdparanoia (and
god knows what other ports). Nothing in the tree itself uses
SCIOCIDENTIFY.

Fixes cdparanoia's handling of USB CD drives that can't handle the
MODE SENSE command. cdparanoia sends 'ATAPI' devices only MODE SENSE
BIG (0x5a) commands. Finding actual SCSI devices that might have
trouble with MODE SENSE BIG (i.e. very old devices) behind USB is
deemed highly unlikely.

Problem reported, debugged and fix tested by bernd@.

ok marco@ millert@ miod@


# 1.27 21-Dec-2006 krw

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


Revision tags: OPENBSD_4_0_BASE
# 1.26 23-Jul-2006 dlg

remove some scsi ioctls, most importantly the ones that can reprobe the
bus. supporting these complicates the midlayer unnecessarily.

ok krw@


# 1.25 11-Jul-2006 dlg

a few formatting tweaks. this was mostly knf/ansi already. no binary change


# 1.24 11-May-2006 krw

Zap trailing whitespace.


# 1.23 20-Apr-2006 miod

Fix various printf() issues: too many arguments, not enough arguments, argument
order reversed, wrong modifiers. ok deraadt@ marco@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.22 08-Dec-2005 krw

Don't panic for pathological i/o sizes unless minphys() really is
broken. Eliminate an unneeded variable and potential conversion issues
in SCIOCCOMMAND code before calling physio.

Similar to what NetBSD does.

Fixes cdda2wav vs "Billie Holiday - Songs for Distingue Lovers"
problem noted by Alexandre Ratchov. Tested by Alexandre.

ok marco@ pedro@ deraadt@ mickey@


# 1.21 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.20 28-May-2005 krw

Handle XS_SHORTSENSE error codes in ioctl situations. From NetBSD.

Fixes spurious error messages from cdparanoia when using ATAPI CDROM
in USB enclosure.

ok pascoe@.


Revision tags: OPENBSD_3_7_BASE
# 1.19 26-Dec-2004 miod

Use list and queue macros where applicable to make the code easier to read;
no change in compiler assembly output.


# 1.18 30-Nov-2004 krw

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

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

Tested by Nikolay, ok deraadt@.


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE SMP_SYNC_A SMP_SYNC_B
# 1.17 03-Jan-2004 krw

Make 'unknown error category' message more informative by including error
number. Same change as was made to scsi_base.c a few days ago.

ok deraadt@.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE OPENBSD_3_4_BASE UBC_SYNC_A UBC_SYNC_B
# 1.16 09-Jun-2002 art

some splassert and protection of biodone.


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

First round of __P removal in sys


# 1.14 07-Jan-2002 mickey

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


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

branches: 1.13.4;
KNF


# 1.12 30-Apr-2001 csapuntz

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


Revision tags: OPENBSD_2_9_BASE
# 1.11 25-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE kame_19991208
# 1.10 24-Aug-1999 csapuntz

branches: 1.10.4;


Added type field to scsi_addr to reflect bus/device type (ATAPI or SCSI).
Updated ioctls to match

Minor patches in anticipation of wd->sd translation layer


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.9 01-Sep-1997 niklas

Correct a debug printout


# 1.8 31-Aug-1997 downsj

* Make scsi_do_ioctl() sane again.
* Use scsi_do_ioctl() in cd.c and uk.c again.


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

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

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

Everybody needs to test this.


Revision tags: OPENBSD_2_0_BASE
# 1.6 13-Aug-1996 niklas

Remove unused variable


# 1.5 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.4 17-Jun-1996 downsj

SCIOCCOMMAND now requires that the device be open for writing.


# 1.3 16-Jun-1996 downsj

netbsd pr#2220: add SCIOCRESET. I can't think of any reason not to.


# 1.2 20-Feb-1996 briggs

Sync. w/ NetBSD: scsi prototypes.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.61 23-Nov-2019 krw

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


# 1.60 23-Nov-2019 krw

Consistently use SET() to set bits.


# 1.59 22-Nov-2019 krw

Be consistent and always use CLR() to clear flags.


# 1.58 21-Nov-2019 krw

Be consistent and always use SET() to set flags in xs->flags.


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

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


# 1.56 29-Sep-2019 krw

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


# 1.55 27-Sep-2019 krw

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


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.54 27-Apr-2018 guenther

Delete superfluous includes of <sys/file.>

ok krw@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.53 08-Sep-2017 deraadt

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


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

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

No functional change.

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


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

More damned eye searing whitespace.


# 1.50 14-Mar-2015 jsg

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

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.49 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.48 21-Jun-2011 matthew

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

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


# 1.47 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.46 22-Jul-2010 matthew

Mark two more DMA accessible malloc(9)s.


# 1.45 10-Jul-2010 matthew

Limit SCIOCCOMMAND and ATAIOCCOMMAND requests to transferring MAXPHYS
bytes worth of data. Also, pass M_CANFAIL to malloc(9) and handle
allocation failures by returning ENOMEM.

Prevents user space from causing a kernel panic by passing
ridiculously large datalen values.

ok dlg@, krw@


# 1.44 01-Jul-2010 krw

ioctl XS_SENSE/XS_SHORTSENSE should print the sense info like
everyone else when SCSIDEBUG is set.


# 1.43 01-Jul-2010 matthew

Call malloc(9) with M_ZERO to prevent code paths from exposing
uninitialized memory to user space or devices.

ok deraadt@, dlg@


# 1.42 15-Jun-2010 dlg

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

ok krw@ marco@


# 1.41 23-Apr-2010 dlg

the SAT spec from t10.org defines messages for tunnelling ATA commands over
SCSI when you're talking to a scsi to ata translation layer (satl). this
implements the ata command ioctl handler in scsi_ioctl.c that wraps the
ata command requests up in these scsi commands and then issues them against
the hardware. this cuts atascsi over to interpreting these scsi commands
rather than the ioctls directly.

this should allow mpi users (with a relatively recent firmware) to use
atactl against their SATA disks. i cant test cos my mpi(4) parts are too
old to either support these scsi commands or too old to take recent
firmwares.

ok marco@ @krw tested by krw@ and me on ahci/atascsi


Revision tags: OPENBSD_4_7_BASE
# 1.40 27-Feb-2010 krw

Don't print sense info for ioctl's. Let the userland program decide
what to tell the user. Silences cdio and cdrecord while probing
media.

ok dlg@ marco@ deraadt@


# 1.39 04-Jan-2010 dlg

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

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

ok krw@


# 1.38 01-Dec-2009 dlg

put the midlayer changes back in.

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

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.37 12-Nov-2009 dlg

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

sigh, this makes me sad.


# 1.36 10-Nov-2009 dlg

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

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


# 1.35 05-Nov-2009 marco

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

ok deraadt krw miod


# 1.34 27-Oct-2009 krw

Check return value of scsi_xs_get() in scsi_ioc_cmd() for NULL. It
may or may not be possible to get NULL these days, but paranoia and
consistancy of usage are nice. Prompted by looking at PR#6247 from
Steven Mestdagh.

ok dlg@


# 1.33 12-Oct-2009 dlg

avoid smashing the stack when we have sense data to return back to userland


# 1.32 14-Sep-2009 dlg

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

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

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

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

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

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

thanks to dave del debbio for testing.

ok deraadt@


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

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


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.30 16-Sep-2007 krw

MALLOC/FREE -> malloc/free, M_ZERO, extraneous casts,
extraneous #include <malloc.h>


# 1.29 07-Sep-2007 krw

The obvious bzero/memset -> M_ZERO changes.


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.28 16-Jan-2007 krw

Report SDEV_UMASS devices as non-'SCSI', i.e. 'ATAPI', devices when
asked via SCIOCIDENTIFY. More closely emulates the Linux
SCIOCIDENTIFY/SG_EMULATED_HOST combination used by cdparanoia (and
god knows what other ports). Nothing in the tree itself uses
SCIOCIDENTIFY.

Fixes cdparanoia's handling of USB CD drives that can't handle the
MODE SENSE command. cdparanoia sends 'ATAPI' devices only MODE SENSE
BIG (0x5a) commands. Finding actual SCSI devices that might have
trouble with MODE SENSE BIG (i.e. very old devices) behind USB is
deemed highly unlikely.

Problem reported, debugged and fix tested by bernd@.

ok marco@ millert@ miod@


# 1.27 21-Dec-2006 krw

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


Revision tags: OPENBSD_4_0_BASE
# 1.26 23-Jul-2006 dlg

remove some scsi ioctls, most importantly the ones that can reprobe the
bus. supporting these complicates the midlayer unnecessarily.

ok krw@


# 1.25 11-Jul-2006 dlg

a few formatting tweaks. this was mostly knf/ansi already. no binary change


# 1.24 11-May-2006 krw

Zap trailing whitespace.


# 1.23 20-Apr-2006 miod

Fix various printf() issues: too many arguments, not enough arguments, argument
order reversed, wrong modifiers. ok deraadt@ marco@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.22 08-Dec-2005 krw

Don't panic for pathological i/o sizes unless minphys() really is
broken. Eliminate an unneeded variable and potential conversion issues
in SCIOCCOMMAND code before calling physio.

Similar to what NetBSD does.

Fixes cdda2wav vs "Billie Holiday - Songs for Distingue Lovers"
problem noted by Alexandre Ratchov. Tested by Alexandre.

ok marco@ pedro@ deraadt@ mickey@


# 1.21 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.20 28-May-2005 krw

Handle XS_SHORTSENSE error codes in ioctl situations. From NetBSD.

Fixes spurious error messages from cdparanoia when using ATAPI CDROM
in USB enclosure.

ok pascoe@.


Revision tags: OPENBSD_3_7_BASE
# 1.19 26-Dec-2004 miod

Use list and queue macros where applicable to make the code easier to read;
no change in compiler assembly output.


# 1.18 30-Nov-2004 krw

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

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

Tested by Nikolay, ok deraadt@.


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE SMP_SYNC_A SMP_SYNC_B
# 1.17 03-Jan-2004 krw

Make 'unknown error category' message more informative by including error
number. Same change as was made to scsi_base.c a few days ago.

ok deraadt@.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE OPENBSD_3_4_BASE UBC_SYNC_A UBC_SYNC_B
# 1.16 09-Jun-2002 art

some splassert and protection of biodone.


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

First round of __P removal in sys


# 1.14 07-Jan-2002 mickey

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


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

branches: 1.13.4;
KNF


# 1.12 30-Apr-2001 csapuntz

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


Revision tags: OPENBSD_2_9_BASE
# 1.11 25-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE kame_19991208
# 1.10 24-Aug-1999 csapuntz

branches: 1.10.4;


Added type field to scsi_addr to reflect bus/device type (ATAPI or SCSI).
Updated ioctls to match

Minor patches in anticipation of wd->sd translation layer


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.9 01-Sep-1997 niklas

Correct a debug printout


# 1.8 31-Aug-1997 downsj

* Make scsi_do_ioctl() sane again.
* Use scsi_do_ioctl() in cd.c and uk.c again.


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

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

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

Everybody needs to test this.


Revision tags: OPENBSD_2_0_BASE
# 1.6 13-Aug-1996 niklas

Remove unused variable


# 1.5 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.4 17-Jun-1996 downsj

SCIOCCOMMAND now requires that the device be open for writing.


# 1.3 16-Jun-1996 downsj

netbsd pr#2220: add SCIOCRESET. I can't think of any reason not to.


# 1.2 20-Feb-1996 briggs

Sync. w/ NetBSD: scsi prototypes.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.58 21-Nov-2019 krw

Be consistent and always use SET() to set flags in xs->flags.


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

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


# 1.56 29-Sep-2019 krw

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


# 1.55 27-Sep-2019 krw

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


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.54 27-Apr-2018 guenther

Delete superfluous includes of <sys/file.>

ok krw@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.53 08-Sep-2017 deraadt

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


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

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

No functional change.

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


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

More damned eye searing whitespace.


# 1.50 14-Mar-2015 jsg

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

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.49 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.48 21-Jun-2011 matthew

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

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


# 1.47 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.46 22-Jul-2010 matthew

Mark two more DMA accessible malloc(9)s.


# 1.45 10-Jul-2010 matthew

Limit SCIOCCOMMAND and ATAIOCCOMMAND requests to transferring MAXPHYS
bytes worth of data. Also, pass M_CANFAIL to malloc(9) and handle
allocation failures by returning ENOMEM.

Prevents user space from causing a kernel panic by passing
ridiculously large datalen values.

ok dlg@, krw@


# 1.44 01-Jul-2010 krw

ioctl XS_SENSE/XS_SHORTSENSE should print the sense info like
everyone else when SCSIDEBUG is set.


# 1.43 01-Jul-2010 matthew

Call malloc(9) with M_ZERO to prevent code paths from exposing
uninitialized memory to user space or devices.

ok deraadt@, dlg@


# 1.42 15-Jun-2010 dlg

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

ok krw@ marco@


# 1.41 23-Apr-2010 dlg

the SAT spec from t10.org defines messages for tunnelling ATA commands over
SCSI when you're talking to a scsi to ata translation layer (satl). this
implements the ata command ioctl handler in scsi_ioctl.c that wraps the
ata command requests up in these scsi commands and then issues them against
the hardware. this cuts atascsi over to interpreting these scsi commands
rather than the ioctls directly.

this should allow mpi users (with a relatively recent firmware) to use
atactl against their SATA disks. i cant test cos my mpi(4) parts are too
old to either support these scsi commands or too old to take recent
firmwares.

ok marco@ @krw tested by krw@ and me on ahci/atascsi


Revision tags: OPENBSD_4_7_BASE
# 1.40 27-Feb-2010 krw

Don't print sense info for ioctl's. Let the userland program decide
what to tell the user. Silences cdio and cdrecord while probing
media.

ok dlg@ marco@ deraadt@


# 1.39 04-Jan-2010 dlg

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

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

ok krw@


# 1.38 01-Dec-2009 dlg

put the midlayer changes back in.

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

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.37 12-Nov-2009 dlg

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

sigh, this makes me sad.


# 1.36 10-Nov-2009 dlg

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

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


# 1.35 05-Nov-2009 marco

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

ok deraadt krw miod


# 1.34 27-Oct-2009 krw

Check return value of scsi_xs_get() in scsi_ioc_cmd() for NULL. It
may or may not be possible to get NULL these days, but paranoia and
consistancy of usage are nice. Prompted by looking at PR#6247 from
Steven Mestdagh.

ok dlg@


# 1.33 12-Oct-2009 dlg

avoid smashing the stack when we have sense data to return back to userland


# 1.32 14-Sep-2009 dlg

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

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

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

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

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

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

thanks to dave del debbio for testing.

ok deraadt@


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

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


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.30 16-Sep-2007 krw

MALLOC/FREE -> malloc/free, M_ZERO, extraneous casts,
extraneous #include <malloc.h>


# 1.29 07-Sep-2007 krw

The obvious bzero/memset -> M_ZERO changes.


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.28 16-Jan-2007 krw

Report SDEV_UMASS devices as non-'SCSI', i.e. 'ATAPI', devices when
asked via SCIOCIDENTIFY. More closely emulates the Linux
SCIOCIDENTIFY/SG_EMULATED_HOST combination used by cdparanoia (and
god knows what other ports). Nothing in the tree itself uses
SCIOCIDENTIFY.

Fixes cdparanoia's handling of USB CD drives that can't handle the
MODE SENSE command. cdparanoia sends 'ATAPI' devices only MODE SENSE
BIG (0x5a) commands. Finding actual SCSI devices that might have
trouble with MODE SENSE BIG (i.e. very old devices) behind USB is
deemed highly unlikely.

Problem reported, debugged and fix tested by bernd@.

ok marco@ millert@ miod@


# 1.27 21-Dec-2006 krw

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


Revision tags: OPENBSD_4_0_BASE
# 1.26 23-Jul-2006 dlg

remove some scsi ioctls, most importantly the ones that can reprobe the
bus. supporting these complicates the midlayer unnecessarily.

ok krw@


# 1.25 11-Jul-2006 dlg

a few formatting tweaks. this was mostly knf/ansi already. no binary change


# 1.24 11-May-2006 krw

Zap trailing whitespace.


# 1.23 20-Apr-2006 miod

Fix various printf() issues: too many arguments, not enough arguments, argument
order reversed, wrong modifiers. ok deraadt@ marco@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.22 08-Dec-2005 krw

Don't panic for pathological i/o sizes unless minphys() really is
broken. Eliminate an unneeded variable and potential conversion issues
in SCIOCCOMMAND code before calling physio.

Similar to what NetBSD does.

Fixes cdda2wav vs "Billie Holiday - Songs for Distingue Lovers"
problem noted by Alexandre Ratchov. Tested by Alexandre.

ok marco@ pedro@ deraadt@ mickey@


# 1.21 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.20 28-May-2005 krw

Handle XS_SHORTSENSE error codes in ioctl situations. From NetBSD.

Fixes spurious error messages from cdparanoia when using ATAPI CDROM
in USB enclosure.

ok pascoe@.


Revision tags: OPENBSD_3_7_BASE
# 1.19 26-Dec-2004 miod

Use list and queue macros where applicable to make the code easier to read;
no change in compiler assembly output.


# 1.18 30-Nov-2004 krw

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

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

Tested by Nikolay, ok deraadt@.


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE SMP_SYNC_A SMP_SYNC_B
# 1.17 03-Jan-2004 krw

Make 'unknown error category' message more informative by including error
number. Same change as was made to scsi_base.c a few days ago.

ok deraadt@.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE OPENBSD_3_4_BASE UBC_SYNC_A UBC_SYNC_B
# 1.16 09-Jun-2002 art

some splassert and protection of biodone.


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

First round of __P removal in sys


# 1.14 07-Jan-2002 mickey

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


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

branches: 1.13.4;
KNF


# 1.12 30-Apr-2001 csapuntz

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


Revision tags: OPENBSD_2_9_BASE
# 1.11 25-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE kame_19991208
# 1.10 24-Aug-1999 csapuntz

branches: 1.10.4;


Added type field to scsi_addr to reflect bus/device type (ATAPI or SCSI).
Updated ioctls to match

Minor patches in anticipation of wd->sd translation layer


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.9 01-Sep-1997 niklas

Correct a debug printout


# 1.8 31-Aug-1997 downsj

* Make scsi_do_ioctl() sane again.
* Use scsi_do_ioctl() in cd.c and uk.c again.


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

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

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

Everybody needs to test this.


Revision tags: OPENBSD_2_0_BASE
# 1.6 13-Aug-1996 niklas

Remove unused variable


# 1.5 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.4 17-Jun-1996 downsj

SCIOCCOMMAND now requires that the device be open for writing.


# 1.3 16-Jun-1996 downsj

netbsd pr#2220: add SCIOCRESET. I can't think of any reason not to.


# 1.2 20-Feb-1996 briggs

Sync. w/ NetBSD: scsi prototypes.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.57 29-Sep-2019 krw

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


# 1.56 29-Sep-2019 krw

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


# 1.55 27-Sep-2019 krw

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


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.54 27-Apr-2018 guenther

Delete superfluous includes of <sys/file.>

ok krw@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.53 08-Sep-2017 deraadt

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


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

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

No functional change.

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


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

More damned eye searing whitespace.


# 1.50 14-Mar-2015 jsg

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

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.49 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.48 21-Jun-2011 matthew

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

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


# 1.47 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.46 22-Jul-2010 matthew

Mark two more DMA accessible malloc(9)s.


# 1.45 10-Jul-2010 matthew

Limit SCIOCCOMMAND and ATAIOCCOMMAND requests to transferring MAXPHYS
bytes worth of data. Also, pass M_CANFAIL to malloc(9) and handle
allocation failures by returning ENOMEM.

Prevents user space from causing a kernel panic by passing
ridiculously large datalen values.

ok dlg@, krw@


# 1.44 01-Jul-2010 krw

ioctl XS_SENSE/XS_SHORTSENSE should print the sense info like
everyone else when SCSIDEBUG is set.


# 1.43 01-Jul-2010 matthew

Call malloc(9) with M_ZERO to prevent code paths from exposing
uninitialized memory to user space or devices.

ok deraadt@, dlg@


# 1.42 15-Jun-2010 dlg

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

ok krw@ marco@


# 1.41 23-Apr-2010 dlg

the SAT spec from t10.org defines messages for tunnelling ATA commands over
SCSI when you're talking to a scsi to ata translation layer (satl). this
implements the ata command ioctl handler in scsi_ioctl.c that wraps the
ata command requests up in these scsi commands and then issues them against
the hardware. this cuts atascsi over to interpreting these scsi commands
rather than the ioctls directly.

this should allow mpi users (with a relatively recent firmware) to use
atactl against their SATA disks. i cant test cos my mpi(4) parts are too
old to either support these scsi commands or too old to take recent
firmwares.

ok marco@ @krw tested by krw@ and me on ahci/atascsi


Revision tags: OPENBSD_4_7_BASE
# 1.40 27-Feb-2010 krw

Don't print sense info for ioctl's. Let the userland program decide
what to tell the user. Silences cdio and cdrecord while probing
media.

ok dlg@ marco@ deraadt@


# 1.39 04-Jan-2010 dlg

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

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

ok krw@


# 1.38 01-Dec-2009 dlg

put the midlayer changes back in.

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

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.37 12-Nov-2009 dlg

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

sigh, this makes me sad.


# 1.36 10-Nov-2009 dlg

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

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


# 1.35 05-Nov-2009 marco

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

ok deraadt krw miod


# 1.34 27-Oct-2009 krw

Check return value of scsi_xs_get() in scsi_ioc_cmd() for NULL. It
may or may not be possible to get NULL these days, but paranoia and
consistancy of usage are nice. Prompted by looking at PR#6247 from
Steven Mestdagh.

ok dlg@


# 1.33 12-Oct-2009 dlg

avoid smashing the stack when we have sense data to return back to userland


# 1.32 14-Sep-2009 dlg

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

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

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

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

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

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

thanks to dave del debbio for testing.

ok deraadt@


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

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


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.30 16-Sep-2007 krw

MALLOC/FREE -> malloc/free, M_ZERO, extraneous casts,
extraneous #include <malloc.h>


# 1.29 07-Sep-2007 krw

The obvious bzero/memset -> M_ZERO changes.


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.28 16-Jan-2007 krw

Report SDEV_UMASS devices as non-'SCSI', i.e. 'ATAPI', devices when
asked via SCIOCIDENTIFY. More closely emulates the Linux
SCIOCIDENTIFY/SG_EMULATED_HOST combination used by cdparanoia (and
god knows what other ports). Nothing in the tree itself uses
SCIOCIDENTIFY.

Fixes cdparanoia's handling of USB CD drives that can't handle the
MODE SENSE command. cdparanoia sends 'ATAPI' devices only MODE SENSE
BIG (0x5a) commands. Finding actual SCSI devices that might have
trouble with MODE SENSE BIG (i.e. very old devices) behind USB is
deemed highly unlikely.

Problem reported, debugged and fix tested by bernd@.

ok marco@ millert@ miod@


# 1.27 21-Dec-2006 krw

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


Revision tags: OPENBSD_4_0_BASE
# 1.26 23-Jul-2006 dlg

remove some scsi ioctls, most importantly the ones that can reprobe the
bus. supporting these complicates the midlayer unnecessarily.

ok krw@


# 1.25 11-Jul-2006 dlg

a few formatting tweaks. this was mostly knf/ansi already. no binary change


# 1.24 11-May-2006 krw

Zap trailing whitespace.


# 1.23 20-Apr-2006 miod

Fix various printf() issues: too many arguments, not enough arguments, argument
order reversed, wrong modifiers. ok deraadt@ marco@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.22 08-Dec-2005 krw

Don't panic for pathological i/o sizes unless minphys() really is
broken. Eliminate an unneeded variable and potential conversion issues
in SCIOCCOMMAND code before calling physio.

Similar to what NetBSD does.

Fixes cdda2wav vs "Billie Holiday - Songs for Distingue Lovers"
problem noted by Alexandre Ratchov. Tested by Alexandre.

ok marco@ pedro@ deraadt@ mickey@


# 1.21 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.20 28-May-2005 krw

Handle XS_SHORTSENSE error codes in ioctl situations. From NetBSD.

Fixes spurious error messages from cdparanoia when using ATAPI CDROM
in USB enclosure.

ok pascoe@.


Revision tags: OPENBSD_3_7_BASE
# 1.19 26-Dec-2004 miod

Use list and queue macros where applicable to make the code easier to read;
no change in compiler assembly output.


# 1.18 30-Nov-2004 krw

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

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

Tested by Nikolay, ok deraadt@.


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE SMP_SYNC_A SMP_SYNC_B
# 1.17 03-Jan-2004 krw

Make 'unknown error category' message more informative by including error
number. Same change as was made to scsi_base.c a few days ago.

ok deraadt@.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE OPENBSD_3_4_BASE UBC_SYNC_A UBC_SYNC_B
# 1.16 09-Jun-2002 art

some splassert and protection of biodone.


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

First round of __P removal in sys


# 1.14 07-Jan-2002 mickey

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


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

branches: 1.13.4;
KNF


# 1.12 30-Apr-2001 csapuntz

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


Revision tags: OPENBSD_2_9_BASE
# 1.11 25-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE kame_19991208
# 1.10 24-Aug-1999 csapuntz

branches: 1.10.4;


Added type field to scsi_addr to reflect bus/device type (ATAPI or SCSI).
Updated ioctls to match

Minor patches in anticipation of wd->sd translation layer


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.9 01-Sep-1997 niklas

Correct a debug printout


# 1.8 31-Aug-1997 downsj

* Make scsi_do_ioctl() sane again.
* Use scsi_do_ioctl() in cd.c and uk.c again.


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

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

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

Everybody needs to test this.


Revision tags: OPENBSD_2_0_BASE
# 1.6 13-Aug-1996 niklas

Remove unused variable


# 1.5 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.4 17-Jun-1996 downsj

SCIOCCOMMAND now requires that the device be open for writing.


# 1.3 16-Jun-1996 downsj

netbsd pr#2220: add SCIOCRESET. I can't think of any reason not to.


# 1.2 20-Feb-1996 briggs

Sync. w/ NetBSD: scsi prototypes.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.54 27-Apr-2018 guenther

Delete superfluous includes of <sys/file.>

ok krw@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.53 08-Sep-2017 deraadt

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


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

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

No functional change.

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


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

More damned eye searing whitespace.


# 1.50 14-Mar-2015 jsg

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

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.49 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.48 21-Jun-2011 matthew

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

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


# 1.47 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.46 22-Jul-2010 matthew

Mark two more DMA accessible malloc(9)s.


# 1.45 10-Jul-2010 matthew

Limit SCIOCCOMMAND and ATAIOCCOMMAND requests to transferring MAXPHYS
bytes worth of data. Also, pass M_CANFAIL to malloc(9) and handle
allocation failures by returning ENOMEM.

Prevents user space from causing a kernel panic by passing
ridiculously large datalen values.

ok dlg@, krw@


# 1.44 01-Jul-2010 krw

ioctl XS_SENSE/XS_SHORTSENSE should print the sense info like
everyone else when SCSIDEBUG is set.


# 1.43 01-Jul-2010 matthew

Call malloc(9) with M_ZERO to prevent code paths from exposing
uninitialized memory to user space or devices.

ok deraadt@, dlg@


# 1.42 15-Jun-2010 dlg

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

ok krw@ marco@


# 1.41 23-Apr-2010 dlg

the SAT spec from t10.org defines messages for tunnelling ATA commands over
SCSI when you're talking to a scsi to ata translation layer (satl). this
implements the ata command ioctl handler in scsi_ioctl.c that wraps the
ata command requests up in these scsi commands and then issues them against
the hardware. this cuts atascsi over to interpreting these scsi commands
rather than the ioctls directly.

this should allow mpi users (with a relatively recent firmware) to use
atactl against their SATA disks. i cant test cos my mpi(4) parts are too
old to either support these scsi commands or too old to take recent
firmwares.

ok marco@ @krw tested by krw@ and me on ahci/atascsi


Revision tags: OPENBSD_4_7_BASE
# 1.40 27-Feb-2010 krw

Don't print sense info for ioctl's. Let the userland program decide
what to tell the user. Silences cdio and cdrecord while probing
media.

ok dlg@ marco@ deraadt@


# 1.39 04-Jan-2010 dlg

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

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

ok krw@


# 1.38 01-Dec-2009 dlg

put the midlayer changes back in.

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

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.37 12-Nov-2009 dlg

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

sigh, this makes me sad.


# 1.36 10-Nov-2009 dlg

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

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


# 1.35 05-Nov-2009 marco

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

ok deraadt krw miod


# 1.34 27-Oct-2009 krw

Check return value of scsi_xs_get() in scsi_ioc_cmd() for NULL. It
may or may not be possible to get NULL these days, but paranoia and
consistancy of usage are nice. Prompted by looking at PR#6247 from
Steven Mestdagh.

ok dlg@


# 1.33 12-Oct-2009 dlg

avoid smashing the stack when we have sense data to return back to userland


# 1.32 14-Sep-2009 dlg

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

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

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

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

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

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

thanks to dave del debbio for testing.

ok deraadt@


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

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


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.30 16-Sep-2007 krw

MALLOC/FREE -> malloc/free, M_ZERO, extraneous casts,
extraneous #include <malloc.h>


# 1.29 07-Sep-2007 krw

The obvious bzero/memset -> M_ZERO changes.


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.28 16-Jan-2007 krw

Report SDEV_UMASS devices as non-'SCSI', i.e. 'ATAPI', devices when
asked via SCIOCIDENTIFY. More closely emulates the Linux
SCIOCIDENTIFY/SG_EMULATED_HOST combination used by cdparanoia (and
god knows what other ports). Nothing in the tree itself uses
SCIOCIDENTIFY.

Fixes cdparanoia's handling of USB CD drives that can't handle the
MODE SENSE command. cdparanoia sends 'ATAPI' devices only MODE SENSE
BIG (0x5a) commands. Finding actual SCSI devices that might have
trouble with MODE SENSE BIG (i.e. very old devices) behind USB is
deemed highly unlikely.

Problem reported, debugged and fix tested by bernd@.

ok marco@ millert@ miod@


# 1.27 21-Dec-2006 krw

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


Revision tags: OPENBSD_4_0_BASE
# 1.26 23-Jul-2006 dlg

remove some scsi ioctls, most importantly the ones that can reprobe the
bus. supporting these complicates the midlayer unnecessarily.

ok krw@


# 1.25 11-Jul-2006 dlg

a few formatting tweaks. this was mostly knf/ansi already. no binary change


# 1.24 11-May-2006 krw

Zap trailing whitespace.


# 1.23 20-Apr-2006 miod

Fix various printf() issues: too many arguments, not enough arguments, argument
order reversed, wrong modifiers. ok deraadt@ marco@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.22 08-Dec-2005 krw

Don't panic for pathological i/o sizes unless minphys() really is
broken. Eliminate an unneeded variable and potential conversion issues
in SCIOCCOMMAND code before calling physio.

Similar to what NetBSD does.

Fixes cdda2wav vs "Billie Holiday - Songs for Distingue Lovers"
problem noted by Alexandre Ratchov. Tested by Alexandre.

ok marco@ pedro@ deraadt@ mickey@


# 1.21 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.20 28-May-2005 krw

Handle XS_SHORTSENSE error codes in ioctl situations. From NetBSD.

Fixes spurious error messages from cdparanoia when using ATAPI CDROM
in USB enclosure.

ok pascoe@.


Revision tags: OPENBSD_3_7_BASE
# 1.19 26-Dec-2004 miod

Use list and queue macros where applicable to make the code easier to read;
no change in compiler assembly output.


# 1.18 30-Nov-2004 krw

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

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

Tested by Nikolay, ok deraadt@.


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE SMP_SYNC_A SMP_SYNC_B
# 1.17 03-Jan-2004 krw

Make 'unknown error category' message more informative by including error
number. Same change as was made to scsi_base.c a few days ago.

ok deraadt@.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE OPENBSD_3_4_BASE UBC_SYNC_A UBC_SYNC_B
# 1.16 09-Jun-2002 art

some splassert and protection of biodone.


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

First round of __P removal in sys


# 1.14 07-Jan-2002 mickey

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


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

branches: 1.13.4;
KNF


# 1.12 30-Apr-2001 csapuntz

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


Revision tags: OPENBSD_2_9_BASE
# 1.11 25-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE kame_19991208
# 1.10 24-Aug-1999 csapuntz

branches: 1.10.4;


Added type field to scsi_addr to reflect bus/device type (ATAPI or SCSI).
Updated ioctls to match

Minor patches in anticipation of wd->sd translation layer


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.9 01-Sep-1997 niklas

Correct a debug printout


# 1.8 31-Aug-1997 downsj

* Make scsi_do_ioctl() sane again.
* Use scsi_do_ioctl() in cd.c and uk.c again.


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

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

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

Everybody needs to test this.


Revision tags: OPENBSD_2_0_BASE
# 1.6 13-Aug-1996 niklas

Remove unused variable


# 1.5 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.4 17-Jun-1996 downsj

SCIOCCOMMAND now requires that the device be open for writing.


# 1.3 16-Jun-1996 downsj

netbsd pr#2220: add SCIOCRESET. I can't think of any reason not to.


# 1.2 20-Feb-1996 briggs

Sync. w/ NetBSD: scsi prototypes.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


Revision tags: OPENBSD_6_2_BASE
# 1.53 08-Sep-2017 deraadt

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


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

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

No functional change.

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


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

More damned eye searing whitespace.


# 1.50 14-Mar-2015 jsg

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

ok tedu@ deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.49 15-Dec-2014 tedu

convert bcopy to memcpy. ok dlg krw


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.48 21-Jun-2011 matthew

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

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


# 1.47 17-Mar-2011 deraadt

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


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.46 22-Jul-2010 matthew

Mark two more DMA accessible malloc(9)s.


# 1.45 10-Jul-2010 matthew

Limit SCIOCCOMMAND and ATAIOCCOMMAND requests to transferring MAXPHYS
bytes worth of data. Also, pass M_CANFAIL to malloc(9) and handle
allocation failures by returning ENOMEM.

Prevents user space from causing a kernel panic by passing
ridiculously large datalen values.

ok dlg@, krw@


# 1.44 01-Jul-2010 krw

ioctl XS_SENSE/XS_SHORTSENSE should print the sense info like
everyone else when SCSIDEBUG is set.


# 1.43 01-Jul-2010 matthew

Call malloc(9) with M_ZERO to prevent code paths from exposing
uninitialized memory to user space or devices.

ok deraadt@, dlg@


# 1.42 15-Jun-2010 dlg

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

ok krw@ marco@


# 1.41 23-Apr-2010 dlg

the SAT spec from t10.org defines messages for tunnelling ATA commands over
SCSI when you're talking to a scsi to ata translation layer (satl). this
implements the ata command ioctl handler in scsi_ioctl.c that wraps the
ata command requests up in these scsi commands and then issues them against
the hardware. this cuts atascsi over to interpreting these scsi commands
rather than the ioctls directly.

this should allow mpi users (with a relatively recent firmware) to use
atactl against their SATA disks. i cant test cos my mpi(4) parts are too
old to either support these scsi commands or too old to take recent
firmwares.

ok marco@ @krw tested by krw@ and me on ahci/atascsi


Revision tags: OPENBSD_4_7_BASE
# 1.40 27-Feb-2010 krw

Don't print sense info for ioctl's. Let the userland program decide
what to tell the user. Silences cdio and cdrecord while probing
media.

ok dlg@ marco@ deraadt@


# 1.39 04-Jan-2010 dlg

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

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

ok krw@


# 1.38 01-Dec-2009 dlg

put the midlayer changes back in.

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

putting this in so we can move forward.

ok krw@ "commit please" marco@


# 1.37 12-Nov-2009 dlg

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

sigh, this makes me sad.


# 1.36 10-Nov-2009 dlg

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

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


# 1.35 05-Nov-2009 marco

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

ok deraadt krw miod


# 1.34 27-Oct-2009 krw

Check return value of scsi_xs_get() in scsi_ioc_cmd() for NULL. It
may or may not be possible to get NULL these days, but paranoia and
consistancy of usage are nice. Prompted by looking at PR#6247 from
Steven Mestdagh.

ok dlg@


# 1.33 12-Oct-2009 dlg

avoid smashing the stack when we have sense data to return back to userland


# 1.32 14-Sep-2009 dlg

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

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

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

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

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

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

thanks to dave del debbio for testing.

ok deraadt@


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

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


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.30 16-Sep-2007 krw

MALLOC/FREE -> malloc/free, M_ZERO, extraneous casts,
extraneous #include <malloc.h>


# 1.29 07-Sep-2007 krw

The obvious bzero/memset -> M_ZERO changes.


Revision tags: OPENBSD_4_1_BASE OPENBSD_4_2_BASE
# 1.28 16-Jan-2007 krw

Report SDEV_UMASS devices as non-'SCSI', i.e. 'ATAPI', devices when
asked via SCIOCIDENTIFY. More closely emulates the Linux
SCIOCIDENTIFY/SG_EMULATED_HOST combination used by cdparanoia (and
god knows what other ports). Nothing in the tree itself uses
SCIOCIDENTIFY.

Fixes cdparanoia's handling of USB CD drives that can't handle the
MODE SENSE command. cdparanoia sends 'ATAPI' devices only MODE SENSE
BIG (0x5a) commands. Finding actual SCSI devices that might have
trouble with MODE SENSE BIG (i.e. very old devices) behind USB is
deemed highly unlikely.

Problem reported, debugged and fix tested by bernd@.

ok marco@ millert@ miod@


# 1.27 21-Dec-2006 krw

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


Revision tags: OPENBSD_4_0_BASE
# 1.26 23-Jul-2006 dlg

remove some scsi ioctls, most importantly the ones that can reprobe the
bus. supporting these complicates the midlayer unnecessarily.

ok krw@


# 1.25 11-Jul-2006 dlg

a few formatting tweaks. this was mostly knf/ansi already. no binary change


# 1.24 11-May-2006 krw

Zap trailing whitespace.


# 1.23 20-Apr-2006 miod

Fix various printf() issues: too many arguments, not enough arguments, argument
order reversed, wrong modifiers. ok deraadt@ marco@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.22 08-Dec-2005 krw

Don't panic for pathological i/o sizes unless minphys() really is
broken. Eliminate an unneeded variable and potential conversion issues
in SCIOCCOMMAND code before calling physio.

Similar to what NetBSD does.

Fixes cdda2wav vs "Billie Holiday - Songs for Distingue Lovers"
problem noted by Alexandre Ratchov. Tested by Alexandre.

ok marco@ pedro@ deraadt@ mickey@


# 1.21 10-Oct-2005 krw

Make some panic messages more useful.


Revision tags: OPENBSD_3_8_BASE
# 1.20 28-May-2005 krw

Handle XS_SHORTSENSE error codes in ioctl situations. From NetBSD.

Fixes spurious error messages from cdparanoia when using ATAPI CDROM
in USB enclosure.

ok pascoe@.


Revision tags: OPENBSD_3_7_BASE
# 1.19 26-Dec-2004 miod

Use list and queue macros where applicable to make the code easier to read;
no change in compiler assembly output.


# 1.18 30-Nov-2004 krw

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

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

Tested by Nikolay, ok deraadt@.


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE SMP_SYNC_A SMP_SYNC_B
# 1.17 03-Jan-2004 krw

Make 'unknown error category' message more informative by including error
number. Same change as was made to scsi_base.c a few days ago.

ok deraadt@.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE OPENBSD_3_4_BASE UBC_SYNC_A UBC_SYNC_B
# 1.16 09-Jun-2002 art

some splassert and protection of biodone.


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

First round of __P removal in sys


# 1.14 07-Jan-2002 mickey

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


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

branches: 1.13.4;
KNF


# 1.12 30-Apr-2001 csapuntz

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


Revision tags: OPENBSD_2_9_BASE
# 1.11 25-Jan-2001 todd

spelling


Revision tags: OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE kame_19991208
# 1.10 24-Aug-1999 csapuntz

branches: 1.10.4;


Added type field to scsi_addr to reflect bus/device type (ATAPI or SCSI).
Updated ioctls to match

Minor patches in anticipation of wd->sd translation layer


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.9 01-Sep-1997 niklas

Correct a debug printout


# 1.8 31-Aug-1997 downsj

* Make scsi_do_ioctl() sane again.
* Use scsi_do_ioctl() in cd.c and uk.c again.


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

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

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

Everybody needs to test this.


Revision tags: OPENBSD_2_0_BASE
# 1.6 13-Aug-1996 niklas

Remove unused variable


# 1.5 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.4 17-Jun-1996 downsj

SCIOCCOMMAND now requires that the device be open for writing.


# 1.3 16-Jun-1996 downsj

netbsd pr#2220: add SCIOCRESET. I can't think of any reason not to.


# 1.2 20-Feb-1996 briggs

Sync. w/ NetBSD: scsi prototypes.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision