Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 
0003 =============================================
0004 SCSI mid_level - lower_level driver interface
0005 =============================================
0006 
0007 Introduction
0008 ============
0009 This document outlines the interface between the Linux SCSI mid level and
0010 SCSI lower level drivers. Lower level drivers (LLDs) are variously called
0011 host bus adapter (HBA) drivers and host drivers (HD). A "host" in this
0012 context is a bridge between a computer IO bus (e.g. PCI or ISA) and a
0013 single SCSI initiator port on a SCSI transport. An "initiator" port
0014 (SCSI terminology, see SAM-3 at http://www.t10.org) sends SCSI commands
0015 to "target" SCSI ports (e.g. disks). There can be many LLDs in a running
0016 system, but only one per hardware type. Most LLDs can control one or more
0017 SCSI HBAs. Some HBAs contain multiple hosts.
0018 
0019 In some cases the SCSI transport is an external bus that already has
0020 its own subsystem in Linux (e.g. USB and ieee1394). In such cases the
0021 SCSI subsystem LLD is a software bridge to the other driver subsystem.
0022 Examples are the usb-storage driver (found in the drivers/usb/storage
0023 directory) and the ieee1394/sbp2 driver (found in the drivers/ieee1394
0024 directory).
0025 
0026 For example, the aic7xxx LLD controls Adaptec SCSI parallel interface
0027 (SPI) controllers based on that company's 7xxx chip series. The aic7xxx
0028 LLD can be built into the kernel or loaded as a module. There can only be
0029 one aic7xxx LLD running in a Linux system but it may be controlling many
0030 HBAs. These HBAs might be either on PCI daughter-boards or built into
0031 the motherboard (or both). Some aic7xxx based HBAs are dual controllers
0032 and thus represent two hosts. Like most modern HBAs, each aic7xxx host
0033 has its own PCI device address. [The one-to-one correspondence between
0034 a SCSI host and a PCI device is common but not required (e.g. with
0035 ISA adapters).]
0036 
0037 The SCSI mid level isolates an LLD from other layers such as the SCSI
0038 upper layer drivers and the block layer.
0039 
0040 This version of the document roughly matches linux kernel version 2.6.8 .
0041 
0042 Documentation
0043 =============
0044 There is a SCSI documentation directory within the kernel source tree,
0045 typically Documentation/scsi . Most documents are in plain
0046 (i.e. ASCII) text. This file is named scsi_mid_low_api.txt and can be
0047 found in that directory. A more recent copy of this document may be found
0048 at http://web.archive.org/web/20070107183357rn_1/sg.torque.net/scsi/.
0049 Many LLDs are documented there (e.g. aic7xxx.txt). The SCSI mid-level is
0050 briefly described in scsi.txt which contains a url to a document
0051 describing the SCSI subsystem in the lk 2.4 series. Two upper level
0052 drivers have documents in that directory: st.txt (SCSI tape driver) and
0053 scsi-generic.txt (for the sg driver).
0054 
0055 Some documentation (or urls) for LLDs may be found in the C source code
0056 or in the same directory as the C source code. For example to find a url
0057 about the USB mass storage driver see the
0058 /usr/src/linux/drivers/usb/storage directory.
0059 
0060 Driver structure
0061 ================
0062 Traditionally an LLD for the SCSI subsystem has been at least two files in
0063 the drivers/scsi directory. For example, a driver called "xyz" has a header
0064 file "xyz.h" and a source file "xyz.c". [Actually there is no good reason
0065 why this couldn't all be in one file; the header file is superfluous.] Some
0066 drivers that have been ported to several operating systems have more than
0067 two files. For example the aic7xxx driver has separate files for generic
0068 and OS-specific code (e.g. FreeBSD and Linux). Such drivers tend to have
0069 their own directory under the drivers/scsi directory.
0070 
0071 When a new LLD is being added to Linux, the following files (found in the
0072 drivers/scsi directory) will need some attention: Makefile and Kconfig .
0073 It is probably best to study how existing LLDs are organized.
0074 
0075 As the 2.5 series development kernels evolve into the 2.6 series
0076 production series, changes are being introduced into this interface. An
0077 example of this is driver initialization code where there are now 2 models
0078 available. The older one, similar to what was found in the lk 2.4 series,
0079 is based on hosts that are detected at HBA driver load time. This will be
0080 referred to the "passive" initialization model. The newer model allows HBAs
0081 to be hot plugged (and unplugged) during the lifetime of the LLD and will
0082 be referred to as the "hotplug" initialization model. The newer model is
0083 preferred as it can handle both traditional SCSI equipment that is
0084 permanently connected as well as modern "SCSI" devices (e.g. USB or
0085 IEEE 1394 connected digital cameras) that are hotplugged. Both
0086 initialization models are discussed in the following sections.
0087 
0088 An LLD interfaces to the SCSI subsystem several ways:
0089 
0090   a) directly invoking functions supplied by the mid level
0091   b) passing a set of function pointers to a registration function
0092      supplied by the mid level. The mid level will then invoke these
0093      functions at some point in the future. The LLD will supply
0094      implementations of these functions.
0095   c) direct access to instances of well known data structures maintained
0096      by the mid level
0097 
0098 Those functions in group a) are listed in a section entitled "Mid level
0099 supplied functions" below.
0100 
0101 Those functions in group b) are listed in a section entitled "Interface
0102 functions" below. Their function pointers are placed in the members of
0103 "struct scsi_host_template", an instance of which is passed to
0104 scsi_host_alloc() [#]_.  Those interface functions that the LLD does not
0105 wish to supply should have NULL placed in the corresponding member of
0106 struct scsi_host_template.  Defining an instance of struct
0107 scsi_host_template at file scope will cause NULL to be  placed in function
0108 pointer members not explicitly initialized.
0109 
0110 Those usages in group c) should be handled with care, especially in a
0111 "hotplug" environment. LLDs should be aware of the lifetime of instances
0112 that are shared with the mid level and other layers.
0113 
0114 All functions defined within an LLD and all data defined at file scope
0115 should be static. For example the slave_alloc() function in an LLD
0116 called "xxx" could be defined as
0117 ``static int xxx_slave_alloc(struct scsi_device * sdev) { /* code */ }``
0118 
0119 .. [#] the scsi_host_alloc() function is a replacement for the rather vaguely
0120        named scsi_register() function in most situations.
0121 
0122 
0123 Hotplug initialization model
0124 ============================
0125 In this model an LLD controls when SCSI hosts are introduced and removed
0126 from the SCSI subsystem. Hosts can be introduced as early as driver
0127 initialization and removed as late as driver shutdown. Typically a driver
0128 will respond to a sysfs probe() callback that indicates an HBA has been
0129 detected. After confirming that the new device is one that the LLD wants
0130 to control, the LLD will initialize the HBA and then register a new host
0131 with the SCSI mid level.
0132 
0133 During LLD initialization the driver should register itself with the
0134 appropriate IO bus on which it expects to find HBA(s) (e.g. the PCI bus).
0135 This can probably be done via sysfs. Any driver parameters (especially
0136 those that are writable after the driver is loaded) could also be
0137 registered with sysfs at this point. The SCSI mid level first becomes
0138 aware of an LLD when that LLD registers its first HBA.
0139 
0140 At some later time, the LLD becomes aware of an HBA and what follows
0141 is a typical sequence of calls between the LLD and the mid level.
0142 This example shows the mid level scanning the newly introduced HBA for 3
0143 scsi devices of which only the first 2 respond::
0144 
0145         HBA PROBE: assume 2 SCSI devices found in scan
0146     LLD                   mid level                    LLD
0147     ===-------------------=========--------------------===------
0148     scsi_host_alloc()  -->
0149     scsi_add_host()  ---->
0150     scsi_scan_host()  -------+
0151                             |
0152                         slave_alloc()
0153                         slave_configure() -->  scsi_change_queue_depth()
0154                             |
0155                         slave_alloc()
0156                         slave_configure()
0157                             |
0158                         slave_alloc()   ***
0159                         slave_destroy() ***
0160 
0161 
0162     *** For scsi devices that the mid level tries to scan but do not
0163         respond, a slave_alloc(), slave_destroy() pair is called.
0164 
0165 If the LLD wants to adjust the default queue settings, it can invoke
0166 scsi_change_queue_depth() in its slave_configure() routine.
0167 
0168 When an HBA is being removed it could be as part of an orderly shutdown
0169 associated with the LLD module being unloaded (e.g. with the "rmmod"
0170 command) or in response to a "hot unplug" indicated by sysfs()'s
0171 remove() callback being invoked. In either case, the sequence is the
0172 same::
0173 
0174             HBA REMOVE: assume 2 SCSI devices attached
0175     LLD                      mid level                 LLD
0176     ===----------------------=========-----------------===------
0177     scsi_remove_host() ---------+
0178                                 |
0179                         slave_destroy()
0180                         slave_destroy()
0181     scsi_host_put()
0182 
0183 It may be useful for a LLD to keep track of struct Scsi_Host instances
0184 (a pointer is returned by scsi_host_alloc()). Such instances are "owned"
0185 by the mid-level.  struct Scsi_Host instances are freed from
0186 scsi_host_put() when the reference count hits zero.
0187 
0188 Hot unplugging an HBA that controls a disk which is processing SCSI
0189 commands on a mounted file system is an interesting situation. Reference
0190 counting logic is being introduced into the mid level to cope with many
0191 of the issues involved. See the section on reference counting below.
0192 
0193 
0194 The hotplug concept may be extended to SCSI devices. Currently, when an
0195 HBA is added, the scsi_scan_host() function causes a scan for SCSI devices
0196 attached to the HBA's SCSI transport. On newer SCSI transports the HBA
0197 may become aware of a new SCSI device _after_ the scan has completed.
0198 An LLD can use this sequence to make the mid level aware of a SCSI device::
0199 
0200                     SCSI DEVICE hotplug
0201     LLD                   mid level                    LLD
0202     ===-------------------=========--------------------===------
0203     scsi_add_device()  ------+
0204                             |
0205                         slave_alloc()
0206                         slave_configure()   [--> scsi_change_queue_depth()]
0207 
0208 In a similar fashion, an LLD may become aware that a SCSI device has been
0209 removed (unplugged) or the connection to it has been interrupted. Some
0210 existing SCSI transports (e.g. SPI) may not become aware that a SCSI
0211 device has been removed until a subsequent SCSI command fails which will
0212 probably cause that device to be set offline by the mid level. An LLD that
0213 detects the removal of a SCSI device can instigate its removal from
0214 upper layers with this sequence::
0215 
0216                     SCSI DEVICE hot unplug
0217     LLD                      mid level                 LLD
0218     ===----------------------=========-----------------===------
0219     scsi_remove_device() -------+
0220                                 |
0221                         slave_destroy()
0222 
0223 It may be useful for an LLD to keep track of struct scsi_device instances
0224 (a pointer is passed as the parameter to slave_alloc() and
0225 slave_configure() callbacks). Such instances are "owned" by the mid-level.
0226 struct scsi_device instances are freed after slave_destroy().
0227 
0228 
0229 Reference Counting
0230 ==================
0231 The Scsi_Host structure has had reference counting infrastructure added.
0232 This effectively spreads the ownership of struct Scsi_Host instances
0233 across the various SCSI layers which use them. Previously such instances
0234 were exclusively owned by the mid level. LLDs would not usually need to
0235 directly manipulate these reference counts but there may be some cases
0236 where they do.
0237 
0238 There are 3 reference counting functions of interest associated with
0239 struct Scsi_Host:
0240 
0241   - scsi_host_alloc():
0242         returns a pointer to new instance of struct
0243         Scsi_Host which has its reference count ^^ set to 1
0244 
0245   - scsi_host_get():
0246         adds 1 to the reference count of the given instance
0247 
0248   - scsi_host_put():
0249         decrements 1 from the reference count of the given
0250         instance. If the reference count reaches 0 then the given instance
0251         is freed
0252 
0253 The scsi_device structure has had reference counting infrastructure added.
0254 This effectively spreads the ownership of struct scsi_device instances
0255 across the various SCSI layers which use them. Previously such instances
0256 were exclusively owned by the mid level. See the access functions declared
0257 towards the end of include/scsi/scsi_device.h . If an LLD wants to keep
0258 a copy of a pointer to a scsi_device instance it should use scsi_device_get()
0259 to bump its reference count. When it is finished with the pointer it can
0260 use scsi_device_put() to decrement its reference count (and potentially
0261 delete it).
0262 
0263 .. Note::
0264 
0265    struct Scsi_Host actually has 2 reference counts which are manipulated
0266    in parallel by these functions.
0267 
0268 
0269 Conventions
0270 ===========
0271 First, Linus Torvalds's thoughts on C coding style can be found in the
0272 Documentation/process/coding-style.rst file.
0273 
0274 Also, most C99 enhancements are encouraged to the extent they are supported
0275 by the relevant gcc compilers. So C99 style structure and array
0276 initializers are encouraged where appropriate. Don't go too far,
0277 VLAs are not properly supported yet.  An exception to this is the use of
0278 ``//`` style comments; ``/*...*/`` comments are still preferred in Linux.
0279 
0280 Well written, tested and documented code, need not be re-formatted to
0281 comply with the above conventions. For example, the aic7xxx driver
0282 comes to Linux from FreeBSD and Adaptec's own labs. No doubt FreeBSD
0283 and Adaptec have their own coding conventions.
0284 
0285 
0286 Mid level supplied functions
0287 ============================
0288 These functions are supplied by the SCSI mid level for use by LLDs.
0289 The names (i.e. entry points) of these functions are exported
0290 so an LLD that is a module can access them. The kernel will
0291 arrange for the SCSI mid level to be loaded and initialized before any LLD
0292 is initialized. The functions below are listed alphabetically and their
0293 names all start with ``scsi_``.
0294 
0295 Summary:
0296 
0297   - scsi_add_device - creates new scsi device (lu) instance
0298   - scsi_add_host - perform sysfs registration and set up transport class
0299   - scsi_change_queue_depth - change the queue depth on a SCSI device
0300   - scsi_bios_ptable - return copy of block device's partition table
0301   - scsi_block_requests - prevent further commands being queued to given host
0302   - scsi_host_alloc - return a new scsi_host instance whose refcount==1
0303   - scsi_host_get - increments Scsi_Host instance's refcount
0304   - scsi_host_put - decrements Scsi_Host instance's refcount (free if 0)
0305   - scsi_register - create and register a scsi host adapter instance.
0306   - scsi_remove_device - detach and remove a SCSI device
0307   - scsi_remove_host - detach and remove all SCSI devices owned by host
0308   - scsi_report_bus_reset - report scsi _bus_ reset observed
0309   - scsi_scan_host - scan SCSI bus
0310   - scsi_track_queue_full - track successive QUEUE_FULL events
0311   - scsi_unblock_requests - allow further commands to be queued to given host
0312   - scsi_unregister - [calls scsi_host_put()]
0313 
0314 
0315 Details::
0316 
0317     /**
0318     * scsi_add_device - creates new scsi device (lu) instance
0319     * @shost:   pointer to scsi host instance
0320     * @channel: channel number (rarely other than 0)
0321     * @id:      target id number
0322     * @lun:     logical unit number
0323     *
0324     *      Returns pointer to new struct scsi_device instance or
0325     *      ERR_PTR(-ENODEV) (or some other bent pointer) if something is
0326     *      wrong (e.g. no lu responds at given address)
0327     *
0328     *      Might block: yes
0329     *
0330     *      Notes: This call is usually performed internally during a scsi
0331     *      bus scan when an HBA is added (i.e. scsi_scan_host()). So it
0332     *      should only be called if the HBA becomes aware of a new scsi
0333     *      device (lu) after scsi_scan_host() has completed. If successful
0334     *      this call can lead to slave_alloc() and slave_configure() callbacks
0335     *      into the LLD.
0336     *
0337     *      Defined in: drivers/scsi/scsi_scan.c
0338     **/
0339     struct scsi_device * scsi_add_device(struct Scsi_Host *shost,
0340                                         unsigned int channel,
0341                                         unsigned int id, unsigned int lun)
0342 
0343 
0344     /**
0345     * scsi_add_host - perform sysfs registration and set up transport class
0346     * @shost:   pointer to scsi host instance
0347     * @dev:     pointer to struct device of type scsi class
0348     *
0349     *      Returns 0 on success, negative errno of failure (e.g. -ENOMEM)
0350     *
0351     *      Might block: no
0352     *
0353     *      Notes: Only required in "hotplug initialization model" after a
0354     *      successful call to scsi_host_alloc().  This function does not
0355     *   scan the bus; this can be done by calling scsi_scan_host() or
0356     *   in some other transport-specific way.  The LLD must set up
0357     *   the transport template before calling this function and may only
0358     *   access the transport class data after this function has been called.
0359     *
0360     *      Defined in: drivers/scsi/hosts.c
0361     **/
0362     int scsi_add_host(struct Scsi_Host *shost, struct device * dev)
0363 
0364 
0365     /**
0366     * scsi_change_queue_depth - allow LLD to change queue depth on a SCSI device
0367     * @sdev:       pointer to SCSI device to change queue depth on
0368     * @tags        Number of tags allowed if tagged queuing enabled,
0369     *              or number of commands the LLD can queue up
0370     *              in non-tagged mode (as per cmd_per_lun).
0371     *
0372     *      Returns nothing
0373     *
0374     *      Might block: no
0375     *
0376     *      Notes: Can be invoked any time on a SCSI device controlled by this
0377     *      LLD. [Specifically during and after slave_configure() and prior to
0378     *      slave_destroy().] Can safely be invoked from interrupt code.
0379     *
0380     *      Defined in: drivers/scsi/scsi.c [see source code for more notes]
0381     *
0382     **/
0383     int scsi_change_queue_depth(struct scsi_device *sdev, int tags)
0384 
0385 
0386     /**
0387     * scsi_bios_ptable - return copy of block device's partition table
0388     * @dev:        pointer to block device
0389     *
0390     *      Returns pointer to partition table, or NULL for failure
0391     *
0392     *      Might block: yes
0393     *
0394     *      Notes: Caller owns memory returned (free with kfree() )
0395     *
0396     *      Defined in: drivers/scsi/scsicam.c
0397     **/
0398     unsigned char *scsi_bios_ptable(struct block_device *dev)
0399 
0400 
0401     /**
0402     * scsi_block_requests - prevent further commands being queued to given host
0403     *
0404     * @shost: pointer to host to block commands on
0405     *
0406     *      Returns nothing
0407     *
0408     *      Might block: no
0409     *
0410     *      Notes: There is no timer nor any other means by which the requests
0411     *      get unblocked other than the LLD calling scsi_unblock_requests().
0412     *
0413     *      Defined in: drivers/scsi/scsi_lib.c
0414     **/
0415     void scsi_block_requests(struct Scsi_Host * shost)
0416 
0417 
0418     /**
0419     * scsi_host_alloc - create a scsi host adapter instance and perform basic
0420     *                   initialization.
0421     * @sht:        pointer to scsi host template
0422     * @privsize:   extra bytes to allocate in hostdata array (which is the
0423     *              last member of the returned Scsi_Host instance)
0424     *
0425     *      Returns pointer to new Scsi_Host instance or NULL on failure
0426     *
0427     *      Might block: yes
0428     *
0429     *      Notes: When this call returns to the LLD, the SCSI bus scan on
0430     *      this host has _not_ yet been done.
0431     *      The hostdata array (by default zero length) is a per host scratch
0432     *      area for the LLD's exclusive use.
0433     *      Both associated refcounting objects have their refcount set to 1.
0434     *      Full registration (in sysfs) and a bus scan are performed later when
0435     *      scsi_add_host() and scsi_scan_host() are called.
0436     *
0437     *      Defined in: drivers/scsi/hosts.c .
0438     **/
0439     struct Scsi_Host * scsi_host_alloc(struct scsi_host_template * sht,
0440                                     int privsize)
0441 
0442 
0443     /**
0444     * scsi_host_get - increment Scsi_Host instance refcount
0445     * @shost:   pointer to struct Scsi_Host instance
0446     *
0447     *      Returns nothing
0448     *
0449     *      Might block: currently may block but may be changed to not block
0450     *
0451     *      Notes: Actually increments the counts in two sub-objects
0452     *
0453     *      Defined in: drivers/scsi/hosts.c
0454     **/
0455     void scsi_host_get(struct Scsi_Host *shost)
0456 
0457 
0458     /**
0459     * scsi_host_put - decrement Scsi_Host instance refcount, free if 0
0460     * @shost:   pointer to struct Scsi_Host instance
0461     *
0462     *      Returns nothing
0463     *
0464     *      Might block: currently may block but may be changed to not block
0465     *
0466     *      Notes: Actually decrements the counts in two sub-objects. If the
0467     *      latter refcount reaches 0, the Scsi_Host instance is freed.
0468     *      The LLD need not worry exactly when the Scsi_Host instance is
0469     *      freed, it just shouldn't access the instance after it has balanced
0470     *      out its refcount usage.
0471     *
0472     *      Defined in: drivers/scsi/hosts.c
0473     **/
0474     void scsi_host_put(struct Scsi_Host *shost)
0475 
0476 
0477     /**
0478     * scsi_register - create and register a scsi host adapter instance.
0479     * @sht:        pointer to scsi host template
0480     * @privsize:   extra bytes to allocate in hostdata array (which is the
0481     *              last member of the returned Scsi_Host instance)
0482     *
0483     *      Returns pointer to new Scsi_Host instance or NULL on failure
0484     *
0485     *      Might block: yes
0486     *
0487     *      Notes: When this call returns to the LLD, the SCSI bus scan on
0488     *      this host has _not_ yet been done.
0489     *      The hostdata array (by default zero length) is a per host scratch
0490     *      area for the LLD.
0491     *
0492     *      Defined in: drivers/scsi/hosts.c .
0493     **/
0494     struct Scsi_Host * scsi_register(struct scsi_host_template * sht,
0495                                     int privsize)
0496 
0497 
0498     /**
0499     * scsi_remove_device - detach and remove a SCSI device
0500     * @sdev:      a pointer to a scsi device instance
0501     *
0502     *      Returns value: 0 on success, -EINVAL if device not attached
0503     *
0504     *      Might block: yes
0505     *
0506     *      Notes: If an LLD becomes aware that a scsi device (lu) has
0507     *      been removed but its host is still present then it can request
0508     *      the removal of that scsi device. If successful this call will
0509     *      lead to the slave_destroy() callback being invoked. sdev is an
0510     *      invalid pointer after this call.
0511     *
0512     *      Defined in: drivers/scsi/scsi_sysfs.c .
0513     **/
0514     int scsi_remove_device(struct scsi_device *sdev)
0515 
0516 
0517     /**
0518     * scsi_remove_host - detach and remove all SCSI devices owned by host
0519     * @shost:      a pointer to a scsi host instance
0520     *
0521     *      Returns value: 0 on success, 1 on failure (e.g. LLD busy ??)
0522     *
0523     *      Might block: yes
0524     *
0525     *      Notes: Should only be invoked if the "hotplug initialization
0526     *      model" is being used. It should be called _prior_ to
0527     *      scsi_unregister().
0528     *
0529     *      Defined in: drivers/scsi/hosts.c .
0530     **/
0531     int scsi_remove_host(struct Scsi_Host *shost)
0532 
0533 
0534     /**
0535     * scsi_report_bus_reset - report scsi _bus_ reset observed
0536     * @shost: a pointer to a scsi host involved
0537     * @channel: channel (within) host on which scsi bus reset occurred
0538     *
0539     *      Returns nothing
0540     *
0541     *      Might block: no
0542     *
0543     *      Notes: This only needs to be called if the reset is one which
0544     *      originates from an unknown location.  Resets originated by the
0545     *      mid level itself don't need to call this, but there should be
0546     *      no harm.  The main purpose of this is to make sure that a
0547     *      CHECK_CONDITION is properly treated.
0548     *
0549     *      Defined in: drivers/scsi/scsi_error.c .
0550     **/
0551     void scsi_report_bus_reset(struct Scsi_Host * shost, int channel)
0552 
0553 
0554     /**
0555     * scsi_scan_host - scan SCSI bus
0556     * @shost: a pointer to a scsi host instance
0557     *
0558     *   Might block: yes
0559     *
0560     *   Notes: Should be called after scsi_add_host()
0561     *
0562     *   Defined in: drivers/scsi/scsi_scan.c
0563     **/
0564     void scsi_scan_host(struct Scsi_Host *shost)
0565 
0566 
0567     /**
0568     * scsi_track_queue_full - track successive QUEUE_FULL events on given
0569     *                      device to determine if and when there is a need
0570     *                      to adjust the queue depth on the device.
0571     * @sdev:  pointer to SCSI device instance
0572     * @depth: Current number of outstanding SCSI commands on this device,
0573     *         not counting the one returned as QUEUE_FULL.
0574     *
0575     *      Returns 0  - no change needed
0576     *              >0 - adjust queue depth to this new depth
0577     *              -1 - drop back to untagged operation using host->cmd_per_lun
0578     *                   as the untagged command depth
0579     *
0580     *      Might block: no
0581     *
0582     *      Notes: LLDs may call this at any time and we will do "The Right
0583     *              Thing"; interrupt context safe.
0584     *
0585     *      Defined in: drivers/scsi/scsi.c .
0586     **/
0587     int scsi_track_queue_full(struct scsi_device *sdev, int depth)
0588 
0589 
0590     /**
0591     * scsi_unblock_requests - allow further commands to be queued to given host
0592     *
0593     * @shost: pointer to host to unblock commands on
0594     *
0595     *      Returns nothing
0596     *
0597     *      Might block: no
0598     *
0599     *      Defined in: drivers/scsi/scsi_lib.c .
0600     **/
0601     void scsi_unblock_requests(struct Scsi_Host * shost)
0602 
0603 
0604     /**
0605     * scsi_unregister - unregister and free memory used by host instance
0606     * @shp:        pointer to scsi host instance to unregister.
0607     *
0608     *      Returns nothing
0609     *
0610     *      Might block: no
0611     *
0612     *      Notes: Should not be invoked if the "hotplug initialization
0613     *      model" is being used. Called internally by exit_this_scsi_driver()
0614     *      in the "passive initialization model". Hence a LLD has no need to
0615     *      call this function directly.
0616     *
0617     *      Defined in: drivers/scsi/hosts.c .
0618     **/
0619     void scsi_unregister(struct Scsi_Host * shp)
0620 
0621 
0622 
0623 
0624 Interface Functions
0625 ===================
0626 Interface functions are supplied (defined) by LLDs and their function
0627 pointers are placed in an instance of struct scsi_host_template which
0628 is passed to scsi_host_alloc() [or scsi_register() / init_this_scsi_driver()].
0629 Some are mandatory. Interface functions should be declared static. The
0630 accepted convention is that driver "xyz" will declare its slave_configure()
0631 function as::
0632 
0633     static int xyz_slave_configure(struct scsi_device * sdev);
0634 
0635 and so forth for all interface functions listed below.
0636 
0637 A pointer to this function should be placed in the 'slave_configure' member
0638 of a "struct scsi_host_template" instance. A pointer to such an instance
0639 should be passed to the mid level's scsi_host_alloc() [or scsi_register() /
0640 init_this_scsi_driver()].
0641 
0642 The interface functions are also described in the include/scsi/scsi_host.h
0643 file immediately above their definition point in "struct scsi_host_template".
0644 In some cases more detail is given in scsi_host.h than below.
0645 
0646 The interface functions are listed below in alphabetical order.
0647 
0648 Summary:
0649 
0650   - bios_param - fetch head, sector, cylinder info for a disk
0651   - eh_timed_out - notify the host that a command timer expired
0652   - eh_abort_handler - abort given command
0653   - eh_bus_reset_handler - issue SCSI bus reset
0654   - eh_device_reset_handler - issue SCSI device reset
0655   - eh_host_reset_handler - reset host (host bus adapter)
0656   - info - supply information about given host
0657   - ioctl - driver can respond to ioctls
0658   - proc_info - supports /proc/scsi/{driver_name}/{host_no}
0659   - queuecommand - queue scsi command, invoke 'done' on completion
0660   - slave_alloc - prior to any commands being sent to a new device
0661   - slave_configure - driver fine tuning for given device after attach
0662   - slave_destroy - given device is about to be shut down
0663 
0664 
0665 Details::
0666 
0667     /**
0668     *      bios_param - fetch head, sector, cylinder info for a disk
0669     *      @sdev: pointer to scsi device context (defined in
0670     *             include/scsi/scsi_device.h)
0671     *      @bdev: pointer to block device context (defined in fs.h)
0672     *      @capacity:  device size (in 512 byte sectors)
0673     *      @params: three element array to place output:
0674     *              params[0] number of heads (max 255)
0675     *              params[1] number of sectors (max 63)
0676     *              params[2] number of cylinders
0677     *
0678     *      Return value is ignored
0679     *
0680     *      Locks: none
0681     *
0682     *      Calling context: process (sd)
0683     *
0684     *      Notes: an arbitrary geometry (based on READ CAPACITY) is used
0685     *      if this function is not provided. The params array is
0686     *      pre-initialized with made up values just in case this function
0687     *      doesn't output anything.
0688     *
0689     *      Optionally defined in: LLD
0690     **/
0691         int bios_param(struct scsi_device * sdev, struct block_device *bdev,
0692                     sector_t capacity, int params[3])
0693 
0694 
0695     /**
0696     *      eh_timed_out - The timer for the command has just fired
0697     *      @scp: identifies command timing out
0698     *
0699     *      Returns:
0700     *
0701     *      EH_HANDLED:             I fixed the error, please complete the command
0702     *      EH_RESET_TIMER:         I need more time, reset the timer and
0703     *                              begin counting again
0704     *      EH_NOT_HANDLED          Begin normal error recovery
0705     *
0706     *
0707     *      Locks: None held
0708     *
0709     *      Calling context: interrupt
0710     *
0711     *      Notes: This is to give the LLD an opportunity to do local recovery.
0712     *      This recovery is limited to determining if the outstanding command
0713     *      will ever complete.  You may not abort and restart the command from
0714     *      this callback.
0715     *
0716     *      Optionally defined in: LLD
0717     **/
0718         int eh_timed_out(struct scsi_cmnd * scp)
0719 
0720 
0721     /**
0722     *      eh_abort_handler - abort command associated with scp
0723     *      @scp: identifies command to be aborted
0724     *
0725     *      Returns SUCCESS if command aborted else FAILED
0726     *
0727     *      Locks: None held
0728     *
0729     *      Calling context: kernel thread
0730     *
0731     *      Notes: If 'no_async_abort' is defined this callback
0732     *   will be invoked from scsi_eh thread. No other commands
0733     *   will then be queued on current host during eh.
0734     *   Otherwise it will be called whenever scsi_timeout()
0735     *      is called due to a command timeout.
0736     *
0737     *      Optionally defined in: LLD
0738     **/
0739         int eh_abort_handler(struct scsi_cmnd * scp)
0740 
0741 
0742     /**
0743     *      eh_bus_reset_handler - issue SCSI bus reset
0744     *      @scp: SCSI bus that contains this device should be reset
0745     *
0746     *      Returns SUCCESS if command aborted else FAILED
0747     *
0748     *      Locks: None held
0749     *
0750     *      Calling context: kernel thread
0751     *
0752     *      Notes: Invoked from scsi_eh thread. No other commands will be
0753     *      queued on current host during eh.
0754     *
0755     *      Optionally defined in: LLD
0756     **/
0757         int eh_bus_reset_handler(struct scsi_cmnd * scp)
0758 
0759 
0760     /**
0761     *      eh_device_reset_handler - issue SCSI device reset
0762     *      @scp: identifies SCSI device to be reset
0763     *
0764     *      Returns SUCCESS if command aborted else FAILED
0765     *
0766     *      Locks: None held
0767     *
0768     *      Calling context: kernel thread
0769     *
0770     *      Notes: Invoked from scsi_eh thread. No other commands will be
0771     *      queued on current host during eh.
0772     *
0773     *      Optionally defined in: LLD
0774     **/
0775         int eh_device_reset_handler(struct scsi_cmnd * scp)
0776 
0777 
0778     /**
0779     *      eh_host_reset_handler - reset host (host bus adapter)
0780     *      @scp: SCSI host that contains this device should be reset
0781     *
0782     *      Returns SUCCESS if command aborted else FAILED
0783     *
0784     *      Locks: None held
0785     *
0786     *      Calling context: kernel thread
0787     *
0788     *      Notes: Invoked from scsi_eh thread. No other commands will be
0789     *      queued on current host during eh.
0790     *      With the default eh_strategy in place, if none of the _abort_,
0791     *      _device_reset_, _bus_reset_ or this eh handler function are
0792     *      defined (or they all return FAILED) then the device in question
0793     *      will be set offline whenever eh is invoked.
0794     *
0795     *      Optionally defined in: LLD
0796     **/
0797         int eh_host_reset_handler(struct scsi_cmnd * scp)
0798 
0799 
0800     /**
0801     *      info - supply information about given host: driver name plus data
0802     *             to distinguish given host
0803     *      @shp: host to supply information about
0804     *
0805     *      Return ASCII null terminated string. [This driver is assumed to
0806     *      manage the memory pointed to and maintain it, typically for the
0807     *      lifetime of this host.]
0808     *
0809     *      Locks: none
0810     *
0811     *      Calling context: process
0812     *
0813     *      Notes: Often supplies PCI or ISA information such as IO addresses
0814     *      and interrupt numbers. If not supplied struct Scsi_Host::name used
0815     *      instead. It is assumed the returned information fits on one line
0816     *      (i.e. does not included embedded newlines).
0817     *      The SCSI_IOCTL_PROBE_HOST ioctl yields the string returned by this
0818     *      function (or struct Scsi_Host::name if this function is not
0819     *      available).
0820     *      In a similar manner, init_this_scsi_driver() outputs to the console
0821     *      each host's "info" (or name) for the driver it is registering.
0822     *      Also if proc_info() is not supplied, the output of this function
0823     *      is used instead.
0824     *
0825     *      Optionally defined in: LLD
0826     **/
0827         const char * info(struct Scsi_Host * shp)
0828 
0829 
0830     /**
0831     *      ioctl - driver can respond to ioctls
0832     *      @sdp: device that ioctl was issued for
0833     *      @cmd: ioctl number
0834     *      @arg: pointer to read or write data from. Since it points to
0835     *            user space, should use appropriate kernel functions
0836     *            (e.g. copy_from_user() ). In the Unix style this argument
0837     *            can also be viewed as an unsigned long.
0838     *
0839     *      Returns negative "errno" value when there is a problem. 0 or a
0840     *      positive value indicates success and is returned to the user space.
0841     *
0842     *      Locks: none
0843     *
0844     *      Calling context: process
0845     *
0846     *      Notes: The SCSI subsystem uses a "trickle down" ioctl model.
0847     *      The user issues an ioctl() against an upper level driver
0848     *      (e.g. /dev/sdc) and if the upper level driver doesn't recognize
0849     *      the 'cmd' then it is passed to the SCSI mid level. If the SCSI
0850     *      mid level does not recognize it, then the LLD that controls
0851     *      the device receives the ioctl. According to recent Unix standards
0852     *      unsupported ioctl() 'cmd' numbers should return -ENOTTY.
0853     *
0854     *      Optionally defined in: LLD
0855     **/
0856         int ioctl(struct scsi_device *sdp, int cmd, void *arg)
0857 
0858 
0859     /**
0860     *      proc_info - supports /proc/scsi/{driver_name}/{host_no}
0861     *      @buffer: anchor point to output to (0==writeto1_read0) or fetch from
0862     *               (1==writeto1_read0).
0863     *      @start: where "interesting" data is written to. Ignored when
0864     *              1==writeto1_read0.
0865     *      @offset: offset within buffer 0==writeto1_read0 is actually
0866     *               interested in. Ignored when 1==writeto1_read0 .
0867     *      @length: maximum (or actual) extent of buffer
0868     *      @host_no: host number of interest (struct Scsi_Host::host_no)
0869     *      @writeto1_read0: 1 -> data coming from user space towards driver
0870     *                            (e.g. "echo some_string > /proc/scsi/xyz/2")
0871     *                       0 -> user what data from this driver
0872     *                            (e.g. "cat /proc/scsi/xyz/2")
0873     *
0874     *      Returns length when 1==writeto1_read0. Otherwise number of chars
0875     *      output to buffer past offset.
0876     *
0877     *      Locks: none held
0878     *
0879     *      Calling context: process
0880     *
0881     *      Notes: Driven from scsi_proc.c which interfaces to proc_fs. proc_fs
0882     *      support can now be configured out of the scsi subsystem.
0883     *
0884     *      Optionally defined in: LLD
0885     **/
0886         int proc_info(char * buffer, char ** start, off_t offset,
0887                     int length, int host_no, int writeto1_read0)
0888 
0889 
0890     /**
0891     *      queuecommand - queue scsi command, invoke scp->scsi_done on completion
0892     *      @shost: pointer to the scsi host object
0893     *      @scp: pointer to scsi command object
0894     *
0895     *      Returns 0 on success.
0896     *
0897     *      If there's a failure, return either:
0898     *
0899     *      SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
0900     *      SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
0901     *
0902     *      On both of these returns, the mid-layer will requeue the I/O
0903     *
0904     *      - if the return is SCSI_MLQUEUE_DEVICE_BUSY, only that particular
0905     *      device will be paused, and it will be unpaused when a command to
0906     *      the device returns (or after a brief delay if there are no more
0907     *      outstanding commands to it).  Commands to other devices continue
0908     *      to be processed normally.
0909     *
0910     *      - if the return is SCSI_MLQUEUE_HOST_BUSY, all I/O to the host
0911     *      is paused and will be unpaused when any command returns from
0912     *      the host (or after a brief delay if there are no outstanding
0913     *      commands to the host).
0914     *
0915     *      For compatibility with earlier versions of queuecommand, any
0916     *      other return value is treated the same as
0917     *      SCSI_MLQUEUE_HOST_BUSY.
0918     *
0919     *      Other types of errors that are detected immediately may be
0920     *      flagged by setting scp->result to an appropriate value,
0921     *      invoking the scp->scsi_done callback, and then returning 0
0922     *      from this function. If the command is not performed
0923     *      immediately (and the LLD is starting (or will start) the given
0924     *      command) then this function should place 0 in scp->result and
0925     *      return 0.
0926     *
0927     *      Command ownership.  If the driver returns zero, it owns the
0928     *      command and must take responsibility for ensuring the
0929     *      scp->scsi_done callback is executed.  Note: the driver may
0930     *      call scp->scsi_done before returning zero, but after it has
0931     *      called scp->scsi_done, it may not return any value other than
0932     *      zero.  If the driver makes a non-zero return, it must not
0933     *      execute the command's scsi_done callback at any time.
0934     *
0935     *      Locks: up to and including 2.6.36, struct Scsi_Host::host_lock
0936     *             held on entry (with "irqsave") and is expected to be
0937     *             held on return. From 2.6.37 onwards, queuecommand is
0938     *             called without any locks held.
0939     *
0940     *      Calling context: in interrupt (soft irq) or process context
0941     *
0942     *      Notes: This function should be relatively fast. Normally it
0943     *      will not wait for IO to complete. Hence the scp->scsi_done
0944     *      callback is invoked (often directly from an interrupt service
0945     *      routine) some time after this function has returned. In some
0946     *      cases (e.g. pseudo adapter drivers that manufacture the
0947     *      response to a SCSI INQUIRY) the scp->scsi_done callback may be
0948     *      invoked before this function returns.  If the scp->scsi_done
0949     *      callback is not invoked within a certain period the SCSI mid
0950     *      level will commence error processing.  If a status of CHECK
0951     *      CONDITION is placed in "result" when the scp->scsi_done
0952     *      callback is invoked, then the LLD driver should perform
0953     *      autosense and fill in the struct scsi_cmnd::sense_buffer
0954     *      array. The scsi_cmnd::sense_buffer array is zeroed prior to
0955     *      the mid level queuing a command to an LLD.
0956     *
0957     *      Defined in: LLD
0958     **/
0959         int queuecommand(struct Scsi_Host *shost, struct scsi_cmnd * scp)
0960 
0961 
0962     /**
0963     *      slave_alloc -   prior to any commands being sent to a new device
0964     *                      (i.e. just prior to scan) this call is made
0965     *      @sdp: pointer to new device (about to be scanned)
0966     *
0967     *      Returns 0 if ok. Any other return is assumed to be an error and
0968     *      the device is ignored.
0969     *
0970     *      Locks: none
0971     *
0972     *      Calling context: process
0973     *
0974     *      Notes: Allows the driver to allocate any resources for a device
0975     *      prior to its initial scan. The corresponding scsi device may not
0976     *      exist but the mid level is just about to scan for it (i.e. send
0977     *      and INQUIRY command plus ...). If a device is found then
0978     *      slave_configure() will be called while if a device is not found
0979     *      slave_destroy() is called.
0980     *      For more details see the include/scsi/scsi_host.h file.
0981     *
0982     *      Optionally defined in: LLD
0983     **/
0984         int slave_alloc(struct scsi_device *sdp)
0985 
0986 
0987     /**
0988     *      slave_configure - driver fine tuning for given device just after it
0989     *                     has been first scanned (i.e. it responded to an
0990     *                     INQUIRY)
0991     *      @sdp: device that has just been attached
0992     *
0993     *      Returns 0 if ok. Any other return is assumed to be an error and
0994     *      the device is taken offline. [offline devices will _not_ have
0995     *      slave_destroy() called on them so clean up resources.]
0996     *
0997     *      Locks: none
0998     *
0999     *      Calling context: process
1000     *
1001     *      Notes: Allows the driver to inspect the response to the initial
1002     *      INQUIRY done by the scanning code and take appropriate action.
1003     *      For more details see the include/scsi/scsi_host.h file.
1004     *
1005     *      Optionally defined in: LLD
1006     **/
1007         int slave_configure(struct scsi_device *sdp)
1008 
1009 
1010     /**
1011     *      slave_destroy - given device is about to be shut down. All
1012     *                      activity has ceased on this device.
1013     *      @sdp: device that is about to be shut down
1014     *
1015     *      Returns nothing
1016     *
1017     *      Locks: none
1018     *
1019     *      Calling context: process
1020     *
1021     *      Notes: Mid level structures for given device are still in place
1022     *      but are about to be torn down. Any per device resources allocated
1023     *      by this driver for given device should be freed now. No further
1024     *      commands will be sent for this sdp instance. [However the device
1025     *      could be re-attached in the future in which case a new instance
1026     *      of struct scsi_device would be supplied by future slave_alloc()
1027     *      and slave_configure() calls.]
1028     *
1029     *      Optionally defined in: LLD
1030     **/
1031         void slave_destroy(struct scsi_device *sdp)
1032 
1033 
1034 
1035 Data Structures
1036 ===============
1037 struct scsi_host_template
1038 -------------------------
1039 There is one "struct scsi_host_template" instance per LLD [#]_. It is
1040 typically initialized as a file scope static in a driver's header file. That
1041 way members that are not explicitly initialized will be set to 0 or NULL.
1042 Member of interest:
1043 
1044     name
1045                  - name of driver (may contain spaces, please limit to
1046                    less than 80 characters)
1047 
1048     proc_name
1049                  - name used in "/proc/scsi/<proc_name>/<host_no>" and
1050                    by sysfs in one of its "drivers" directories. Hence
1051                    "proc_name" should only contain characters acceptable
1052                    to a Unix file name.
1053 
1054    ``(*queuecommand)()``
1055                  - primary callback that the mid level uses to inject
1056                    SCSI commands into an LLD.
1057 
1058 The structure is defined and commented in include/scsi/scsi_host.h
1059 
1060 .. [#] In extreme situations a single driver may have several instances
1061        if it controls several different classes of hardware (e.g. an LLD
1062        that handles both ISA and PCI cards and has a separate instance of
1063        struct scsi_host_template for each class).
1064 
1065 struct Scsi_Host
1066 ----------------
1067 There is one struct Scsi_Host instance per host (HBA) that an LLD
1068 controls. The struct Scsi_Host structure has many members in common
1069 with "struct scsi_host_template". When a new struct Scsi_Host instance
1070 is created (in scsi_host_alloc() in hosts.c) those common members are
1071 initialized from the driver's struct scsi_host_template instance. Members
1072 of interest:
1073 
1074     host_no
1075                  - system wide unique number that is used for identifying
1076                    this host. Issued in ascending order from 0.
1077     can_queue
1078                  - must be greater than 0; do not send more than can_queue
1079                    commands to the adapter.
1080     this_id
1081                  - scsi id of host (scsi initiator) or -1 if not known
1082     sg_tablesize
1083                  - maximum scatter gather elements allowed by host.
1084                    Set this to SG_ALL or less to avoid chained SG lists.
1085                    Must be at least 1.
1086     max_sectors
1087                  - maximum number of sectors (usually 512 bytes) allowed
1088                    in a single SCSI command. The default value of 0 leads
1089                    to a setting of SCSI_DEFAULT_MAX_SECTORS (defined in
1090                    scsi_host.h) which is currently set to 1024. So for a
1091                    disk the maximum transfer size is 512 KB when max_sectors
1092                    is not defined. Note that this size may not be sufficient
1093                    for disk firmware uploads.
1094     cmd_per_lun
1095                  - maximum number of commands that can be queued on devices
1096                    controlled by the host. Overridden by LLD calls to
1097                    scsi_change_queue_depth().
1098     no_async_abort
1099                  - 1=>Asynchronous aborts are not supported
1100                  - 0=>Timed-out commands will be aborted asynchronously
1101     hostt
1102                  - pointer to driver's struct scsi_host_template from which
1103                    this struct Scsi_Host instance was spawned
1104     hostt->proc_name
1105                  - name of LLD. This is the driver name that sysfs uses
1106     transportt
1107                  - pointer to driver's struct scsi_transport_template instance
1108                    (if any). FC and SPI transports currently supported.
1109     sh_list
1110                  - a double linked list of pointers to all struct Scsi_Host
1111                    instances (currently ordered by ascending host_no)
1112     my_devices
1113                  - a double linked list of pointers to struct scsi_device
1114                    instances that belong to this host.
1115     hostdata[0]
1116                  - area reserved for LLD at end of struct Scsi_Host. Size
1117                    is set by the second argument (named 'xtr_bytes') to
1118                    scsi_host_alloc() or scsi_register().
1119     vendor_id
1120                  - a unique value that identifies the vendor supplying
1121                    the LLD for the Scsi_Host.  Used most often in validating
1122                    vendor-specific message requests.  Value consists of an
1123                    identifier type and a vendor-specific value.
1124                    See scsi_netlink.h for a description of valid formats.
1125 
1126 The scsi_host structure is defined in include/scsi/scsi_host.h
1127 
1128 struct scsi_device
1129 ------------------
1130 Generally, there is one instance of this structure for each SCSI logical unit
1131 on a host. Scsi devices connected to a host are uniquely identified by a
1132 channel number, target id and logical unit number (lun).
1133 The structure is defined in include/scsi/scsi_device.h
1134 
1135 struct scsi_cmnd
1136 ----------------
1137 Instances of this structure convey SCSI commands to the LLD and responses
1138 back to the mid level. The SCSI mid level will ensure that no more SCSI
1139 commands become queued against the LLD than are indicated by
1140 scsi_change_queue_depth() (or struct Scsi_Host::cmd_per_lun). There will
1141 be at least one instance of struct scsi_cmnd available for each SCSI device.
1142 Members of interest:
1143 
1144     cmnd
1145                  - array containing SCSI command
1146     cmnd_len
1147                  - length (in bytes) of SCSI command
1148     sc_data_direction
1149                  - direction of data transfer in data phase. See
1150                    "enum dma_data_direction" in include/linux/dma-mapping.h
1151     request_bufflen
1152                  - number of data bytes to transfer (0 if no data phase)
1153     use_sg
1154                  - ==0 -> no scatter gather list, hence transfer data
1155                           to/from request_buffer
1156                  - >0 ->  scatter gather list (actually an array) in
1157                           request_buffer with use_sg elements
1158     request_buffer
1159                    - either contains data buffer or scatter gather list
1160                      depending on the setting of use_sg. Scatter gather
1161                      elements are defined by 'struct scatterlist' found
1162                      in include/linux/scatterlist.h .
1163     done
1164                  - function pointer that should be invoked by LLD when the
1165                    SCSI command is completed (successfully or otherwise).
1166                    Should only be called by an LLD if the LLD has accepted
1167                    the command (i.e. queuecommand() returned or will return
1168                    0). The LLD may invoke 'done'  prior to queuecommand()
1169                    finishing.
1170     result
1171                  - should be set by LLD prior to calling 'done'. A value
1172                    of 0 implies a successfully completed command (and all
1173                    data (if any) has been transferred to or from the SCSI
1174                    target device). 'result' is a 32 bit unsigned integer that
1175                    can be viewed as 2 related bytes. The SCSI status value is
1176                    in the LSB. See include/scsi/scsi.h status_byte() and
1177                    host_byte() macros and related constants.
1178     sense_buffer
1179                  - an array (maximum size: SCSI_SENSE_BUFFERSIZE bytes) that
1180                    should be written when the SCSI status (LSB of 'result')
1181                    is set to CHECK_CONDITION (2). When CHECK_CONDITION is
1182                    set, if the top nibble of sense_buffer[0] has the value 7
1183                    then the mid level will assume the sense_buffer array
1184                    contains a valid SCSI sense buffer; otherwise the mid
1185                    level will issue a REQUEST_SENSE SCSI command to
1186                    retrieve the sense buffer. The latter strategy is error
1187                    prone in the presence of command queuing so the LLD should
1188                    always "auto-sense".
1189     device
1190                  - pointer to scsi_device object that this command is
1191                    associated with.
1192     resid
1193                  - an LLD should set this signed integer to the requested
1194                    transfer length (i.e. 'request_bufflen') less the number
1195                    of bytes that are actually transferred. 'resid' is
1196                    preset to 0 so an LLD can ignore it if it cannot detect
1197                    underruns (overruns should be rare). If possible an LLD
1198                    should set 'resid' prior to invoking 'done'. The most
1199                    interesting case is data transfers from a SCSI target
1200                    device (e.g. READs) that underrun.
1201     underflow
1202                  - LLD should place (DID_ERROR << 16) in 'result' if
1203                    actual number of bytes transferred is less than this
1204                    figure. Not many LLDs implement this check and some that
1205                    do just output an error message to the log rather than
1206                    report a DID_ERROR. Better for an LLD to implement
1207                    'resid'.
1208 
1209 It is recommended that a LLD set 'resid' on data transfers from a SCSI
1210 target device (e.g. READs). It is especially important that 'resid' is set
1211 when such data transfers have sense keys of MEDIUM ERROR and HARDWARE ERROR
1212 (and possibly RECOVERED ERROR). In these cases if a LLD is in doubt how much
1213 data has been received then the safest approach is to indicate no bytes have
1214 been received. For example: to indicate that no valid data has been received
1215 a LLD might use these helpers::
1216 
1217     scsi_set_resid(SCpnt, scsi_bufflen(SCpnt));
1218 
1219 where 'SCpnt' is a pointer to a scsi_cmnd object. To indicate only three 512
1220 bytes blocks has been received 'resid' could be set like this::
1221 
1222     scsi_set_resid(SCpnt, scsi_bufflen(SCpnt) - (3 * 512));
1223 
1224 The scsi_cmnd structure is defined in include/scsi/scsi_cmnd.h
1225 
1226 
1227 Locks
1228 =====
1229 Each struct Scsi_Host instance has a spin_lock called struct
1230 Scsi_Host::default_lock which is initialized in scsi_host_alloc() [found in
1231 hosts.c]. Within the same function the struct Scsi_Host::host_lock pointer
1232 is initialized to point at default_lock.  Thereafter lock and unlock
1233 operations performed by the mid level use the struct Scsi_Host::host_lock
1234 pointer.  Previously drivers could override the host_lock pointer but
1235 this is not allowed anymore.
1236 
1237 
1238 Autosense
1239 =========
1240 Autosense (or auto-sense) is defined in the SAM-2 document as "the
1241 automatic return of sense data to the application client coincident
1242 with the completion of a SCSI command" when a status of CHECK CONDITION
1243 occurs. LLDs should perform autosense. This should be done when the LLD
1244 detects a CHECK CONDITION status by either:
1245 
1246     a) instructing the SCSI protocol (e.g. SCSI Parallel Interface (SPI))
1247        to perform an extra data in phase on such responses
1248     b) or, the LLD issuing a REQUEST SENSE command itself
1249 
1250 Either way, when a status of CHECK CONDITION is detected, the mid level
1251 decides whether the LLD has performed autosense by checking struct
1252 scsi_cmnd::sense_buffer[0] . If this byte has an upper nibble of 7 (or 0xf)
1253 then autosense is assumed to have taken place. If it has another value (and
1254 this byte is initialized to 0 before each command) then the mid level will
1255 issue a REQUEST SENSE command.
1256 
1257 In the presence of queued commands the "nexus" that maintains sense
1258 buffer data from the command that failed until a following REQUEST SENSE
1259 may get out of synchronization. This is why it is best for the LLD
1260 to perform autosense.
1261 
1262 
1263 Changes since lk 2.4 series
1264 ===========================
1265 io_request_lock has been replaced by several finer grained locks. The lock
1266 relevant to LLDs is struct Scsi_Host::host_lock and there is
1267 one per SCSI host.
1268 
1269 The older error handling mechanism has been removed. This means the
1270 LLD interface functions abort() and reset() have been removed.
1271 The struct scsi_host_template::use_new_eh_code flag has been removed.
1272 
1273 In the 2.4 series the SCSI subsystem configuration descriptions were
1274 aggregated with the configuration descriptions from all other Linux
1275 subsystems in the Documentation/Configure.help file. In the 2.6 series,
1276 the SCSI subsystem now has its own (much smaller) drivers/scsi/Kconfig
1277 file that contains both configuration and help information.
1278 
1279 struct SHT has been renamed to struct scsi_host_template.
1280 
1281 Addition of the "hotplug initialization model" and many extra functions
1282 to support it.
1283 
1284 
1285 Credits
1286 =======
1287 The following people have contributed to this document:
1288 
1289         - Mike Anderson <andmike at us dot ibm dot com>
1290         - James Bottomley <James dot Bottomley at hansenpartnership dot com>
1291         - Patrick Mansfield <patmans at us dot ibm dot com>
1292         - Christoph Hellwig <hch at infradead dot org>
1293         - Doug Ledford <dledford at redhat dot com>
1294         - Andries Brouwer <Andries dot Brouwer at cwi dot nl>
1295         - Randy Dunlap <rdunlap at xenotime dot net>
1296         - Alan Stern <stern at rowland dot harvard dot edu>
1297 
1298 
1299 Douglas Gilbert
1300 dgilbert at interlog dot com
1301 
1302 21st September 2004