xref: /linux/Documentation/scsi/scsi_mid_low_api.rst (revision 88e45067a30918ebb4942120892963e2311330af)
1.. SPDX-License-Identifier: GPL-2.0
2
3=============================================
4SCSI mid_level - lower_level driver interface
5=============================================
6
7Introduction
8============
9This document outlines the interface between the Linux SCSI mid level and
10SCSI lower level drivers. Lower level drivers (LLDs) are variously called
11host bus adapter (HBA) drivers and host drivers (HD). A "host" in this
12context is a bridge between a computer IO bus (e.g. PCI or ISA) and a
13single SCSI initiator port on a SCSI transport. An "initiator" port
14(SCSI terminology, see SAM-3 at http://www.t10.org) sends SCSI commands
15to "target" SCSI ports (e.g. disks). There can be many LLDs in a running
16system, but only one per hardware type. Most LLDs can control one or more
17SCSI HBAs. Some HBAs contain multiple hosts.
18
19In some cases the SCSI transport is an external bus that already has
20its own subsystem in Linux (e.g. USB and ieee1394). In such cases the
21SCSI subsystem LLD is a software bridge to the other driver subsystem.
22Examples are the usb-storage driver (found in the drivers/usb/storage
23directory) and the ieee1394/sbp2 driver (found in the drivers/ieee1394
24directory).
25
26For example, the aic7xxx LLD controls Adaptec SCSI parallel interface
27(SPI) controllers based on that company's 7xxx chip series. The aic7xxx
28LLD can be built into the kernel or loaded as a module. There can only be
29one aic7xxx LLD running in a Linux system but it may be controlling many
30HBAs. These HBAs might be either on PCI daughter-boards or built into
31the motherboard (or both). Some aic7xxx based HBAs are dual controllers
32and thus represent two hosts. Like most modern HBAs, each aic7xxx host
33has its own PCI device address. [The one-to-one correspondence between
34a SCSI host and a PCI device is common but not required (e.g. with
35ISA adapters).]
36
37The SCSI mid level isolates an LLD from other layers such as the SCSI
38upper layer drivers and the block layer.
39
40This version of the document roughly matches linux kernel version 2.6.8 .
41
42Documentation
43=============
44There is a SCSI documentation directory within the kernel source tree,
45typically Documentation/scsi . Most documents are in reStructuredText
46format. This file is named scsi_mid_low_api.rst and can be
47found in that directory. A more recent copy of this document may be found
48at https://docs.kernel.org/scsi/scsi_mid_low_api.html. Many LLDs are
49documented in Documentation/scsi (e.g. aic7xxx.rst). The SCSI mid-level is
50briefly described in scsi.rst which contains a URL to a document describing
51the SCSI subsystem in the Linux Kernel 2.4 series. Two upper level
52drivers have documents in that directory: st.rst (SCSI tape driver) and
53scsi-generic.rst (for the sg driver).
54
55Some documentation (or URLs) for LLDs may be found in the C source code
56or in the same directory as the C source code. For example to find a URL
57about the USB mass storage driver see the
58/usr/src/linux/drivers/usb/storage directory.
59
60Driver structure
61================
62Traditionally an LLD for the SCSI subsystem has been at least two files in
63the drivers/scsi directory. For example, a driver called "xyz" has a header
64file "xyz.h" and a source file "xyz.c". [Actually there is no good reason
65why this couldn't all be in one file; the header file is superfluous.] Some
66drivers that have been ported to several operating systems have more than
67two files. For example the aic7xxx driver has separate files for generic
68and OS-specific code (e.g. FreeBSD and Linux). Such drivers tend to have
69their own directory under the drivers/scsi directory.
70
71When a new LLD is being added to Linux, the following files (found in the
72drivers/scsi directory) will need some attention: Makefile and Kconfig .
73It is probably best to study how existing LLDs are organized.
74
75As the 2.5 series development kernels evolve into the 2.6 series
76production series, changes are being introduced into this interface. An
77example of this is driver initialization code where there are now 2 models
78available. The older one, similar to what was found in the lk 2.4 series,
79is based on hosts that are detected at HBA driver load time. This will be
80referred to the "passive" initialization model. The newer model allows HBAs
81to be hot plugged (and unplugged) during the lifetime of the LLD and will
82be referred to as the "hotplug" initialization model. The newer model is
83preferred as it can handle both traditional SCSI equipment that is
84permanently connected as well as modern "SCSI" devices (e.g. USB or
85IEEE 1394 connected digital cameras) that are hotplugged. Both
86initialization models are discussed in the following sections.
87
88An LLD interfaces to the SCSI subsystem several ways:
89
90  a) directly invoking functions supplied by the mid level
91  b) passing a set of function pointers to a registration function
92     supplied by the mid level. The mid level will then invoke these
93     functions at some point in the future. The LLD will supply
94     implementations of these functions.
95  c) direct access to instances of well known data structures maintained
96     by the mid level
97
98Those functions in group a) are listed in a section entitled "Mid level
99supplied functions" below.
100
101Those functions in group b) are listed in a section entitled "Interface
102functions" below. Their function pointers are placed in the members of
103"struct scsi_host_template", an instance of which is passed to
104scsi_host_alloc().  Those interface functions that the LLD does not
105wish to supply should have NULL placed in the corresponding member of
106struct scsi_host_template.  Defining an instance of struct
107scsi_host_template at file scope will cause NULL to be  placed in function
108pointer members not explicitly initialized.
109
110Those usages in group c) should be handled with care, especially in a
111"hotplug" environment. LLDs should be aware of the lifetime of instances
112that are shared with the mid level and other layers.
113
114All functions defined within an LLD and all data defined at file scope
115should be static. For example the sdev_init() function in an LLD
116called "xxx" could be defined as
117``static int xxx_sdev_init(struct scsi_device * sdev) { /* code */ }``
118
119
120Hotplug initialization model
121============================
122In this model an LLD controls when SCSI hosts are introduced and removed
123from the SCSI subsystem. Hosts can be introduced as early as driver
124initialization and removed as late as driver shutdown. Typically a driver
125will respond to a sysfs probe() callback that indicates an HBA has been
126detected. After confirming that the new device is one that the LLD wants
127to control, the LLD will initialize the HBA and then register a new host
128with the SCSI mid level.
129
130During LLD initialization the driver should register itself with the
131appropriate IO bus on which it expects to find HBA(s) (e.g. the PCI bus).
132This can probably be done via sysfs. Any driver parameters (especially
133those that are writable after the driver is loaded) could also be
134registered with sysfs at this point. The SCSI mid level first becomes
135aware of an LLD when that LLD registers its first HBA.
136
137At some later time, the LLD becomes aware of an HBA and what follows
138is a typical sequence of calls between the LLD and the mid level.
139This example shows the mid level scanning the newly introduced HBA for 3
140scsi devices of which only the first 2 respond::
141
142	HBA PROBE: assume 2 SCSI devices found in scan
143    LLD                   mid level                    LLD
144    ===-------------------=========--------------------===------
145    scsi_host_alloc()  -->
146    scsi_add_host()  ---->
147    scsi_scan_host()  -------+
148			    |
149			sdev_init()
150			sdev_configure() -->  scsi_change_queue_depth()
151			    |
152			sdev_init()
153			sdev_configure()
154			    |
155			sdev_init()   ***
156			sdev_destroy() ***
157
158
159    *** For scsi devices that the mid level tries to scan but do not
160	respond, a sdev_init(), sdev_destroy() pair is called.
161
162If the LLD wants to adjust the default queue settings, it can invoke
163scsi_change_queue_depth() in its sdev_configure() routine.
164
165When an HBA is being removed it could be as part of an orderly shutdown
166associated with the LLD module being unloaded (e.g. with the "rmmod"
167command) or in response to a "hot unplug" indicated by sysfs()'s
168remove() callback being invoked. In either case, the sequence is the
169same::
170
171	    HBA REMOVE: assume 2 SCSI devices attached
172    LLD                      mid level                 LLD
173    ===----------------------=========-----------------===------
174    scsi_remove_host() ---------+
175				|
176			sdev_destroy()
177			sdev_destroy()
178    scsi_host_put()
179
180It may be useful for a LLD to keep track of struct Scsi_Host instances
181(a pointer is returned by scsi_host_alloc()). Such instances are "owned"
182by the mid-level.  struct Scsi_Host instances are freed from
183scsi_host_put() when the reference count hits zero.
184
185Hot unplugging an HBA that controls a disk which is processing SCSI
186commands on a mounted file system is an interesting situation. Reference
187counting logic is being introduced into the mid level to cope with many
188of the issues involved. See the section on reference counting below.
189
190
191The hotplug concept may be extended to SCSI devices. Currently, when an
192HBA is added, the scsi_scan_host() function causes a scan for SCSI devices
193attached to the HBA's SCSI transport. On newer SCSI transports the HBA
194may become aware of a new SCSI device _after_ the scan has completed.
195An LLD can use this sequence to make the mid level aware of a SCSI device::
196
197		    SCSI DEVICE hotplug
198    LLD                   mid level                    LLD
199    ===-------------------=========--------------------===------
200    scsi_add_device()  ------+
201			    |
202			sdev_init()
203			sdev_configure()   [--> scsi_change_queue_depth()]
204
205In a similar fashion, an LLD may become aware that a SCSI device has been
206removed (unplugged) or the connection to it has been interrupted. Some
207existing SCSI transports (e.g. SPI) may not become aware that a SCSI
208device has been removed until a subsequent SCSI command fails which will
209probably cause that device to be set offline by the mid level. An LLD that
210detects the removal of a SCSI device can instigate its removal from
211upper layers with this sequence::
212
213		    SCSI DEVICE hot unplug
214    LLD                      mid level                 LLD
215    ===----------------------=========-----------------===------
216    scsi_remove_device() -------+
217				|
218			sdev_destroy()
219
220It may be useful for an LLD to keep track of struct scsi_device instances
221(a pointer is passed as the parameter to sdev_init() and
222sdev_configure() callbacks). Such instances are "owned" by the mid-level.
223struct scsi_device instances are freed after sdev_destroy().
224
225
226Reference Counting
227==================
228The Scsi_Host structure has had reference counting infrastructure added.
229This effectively spreads the ownership of struct Scsi_Host instances
230across the various SCSI layers which use them. Previously such instances
231were exclusively owned by the mid level. LLDs would not usually need to
232directly manipulate these reference counts but there may be some cases
233where they do.
234
235There are 3 reference counting functions of interest associated with
236struct Scsi_Host:
237
238  - scsi_host_alloc():
239	returns a pointer to new instance of struct
240        Scsi_Host which has its reference count ^^ set to 1
241
242  - scsi_host_get():
243	adds 1 to the reference count of the given instance
244
245  - scsi_host_put():
246	decrements 1 from the reference count of the given
247        instance. If the reference count reaches 0 then the given instance
248        is freed
249
250The scsi_device structure has had reference counting infrastructure added.
251This effectively spreads the ownership of struct scsi_device instances
252across the various SCSI layers which use them. Previously such instances
253were exclusively owned by the mid level. See the access functions declared
254towards the end of include/scsi/scsi_device.h . If an LLD wants to keep
255a copy of a pointer to a scsi_device instance it should use scsi_device_get()
256to bump its reference count. When it is finished with the pointer it can
257use scsi_device_put() to decrement its reference count (and potentially
258delete it).
259
260.. Note::
261
262   struct Scsi_Host actually has 2 reference counts which are manipulated
263   in parallel by these functions.
264
265
266Conventions
267===========
268First, Linus Torvalds's thoughts on C coding style can be found in the
269Documentation/process/coding-style.rst file.
270
271Also, most C99 enhancements are encouraged to the extent they are supported
272by the relevant gcc compilers. So C99 style structure and array
273initializers are encouraged where appropriate. Don't go too far,
274VLAs are not properly supported yet.  An exception to this is the use of
275``//`` style comments; ``/*...*/`` comments are still preferred in Linux.
276
277Well written, tested and documented code, need not be re-formatted to
278comply with the above conventions. For example, the aic7xxx driver
279comes to Linux from FreeBSD and Adaptec's own labs. No doubt FreeBSD
280and Adaptec have their own coding conventions.
281
282
283Mid level supplied functions
284============================
285These functions are supplied by the SCSI mid level for use by LLDs.
286The names (i.e. entry points) of these functions are exported
287so an LLD that is a module can access them. The kernel will
288arrange for the SCSI mid level to be loaded and initialized before any LLD
289is initialized. The functions below are listed alphabetically and their
290names all start with ``scsi_``.
291
292Summary:
293
294  - scsi_add_device - creates new scsi device (lu) instance
295  - scsi_add_host - perform sysfs registration and set up transport class
296  - scsi_change_queue_depth - change the queue depth on a SCSI device
297  - scsi_bios_ptable - return copy of block device's partition table
298  - scsi_block_requests - prevent further commands being queued to given host
299  - scsi_host_alloc - return a new scsi_host instance whose refcount==1
300  - scsi_host_get - increments Scsi_Host instance's refcount
301  - scsi_host_put - decrements Scsi_Host instance's refcount (free if 0)
302  - scsi_remove_device - detach and remove a SCSI device
303  - scsi_remove_host - detach and remove all SCSI devices owned by host
304  - scsi_report_bus_reset - report scsi _bus_ reset observed
305  - scsi_scan_host - scan SCSI bus
306  - scsi_track_queue_full - track successive QUEUE_FULL events
307  - scsi_unblock_requests - allow further commands to be queued to given host
308
309
310Details::
311
312    /**
313    * scsi_add_device - creates new scsi device (lu) instance
314    * @shost:   pointer to scsi host instance
315    * @channel: channel number (rarely other than 0)
316    * @id:      target id number
317    * @lun:     logical unit number
318    *
319    *      Returns pointer to new struct scsi_device instance or
320    *      ERR_PTR(-ENODEV) (or some other bent pointer) if something is
321    *      wrong (e.g. no lu responds at given address)
322    *
323    *      Might block: yes
324    *
325    *      Notes: This call is usually performed internally during a scsi
326    *      bus scan when an HBA is added (i.e. scsi_scan_host()). So it
327    *      should only be called if the HBA becomes aware of a new scsi
328    *      device (lu) after scsi_scan_host() has completed. If successful
329    *      this call can lead to sdev_init() and sdev_configure() callbacks
330    *      into the LLD.
331    *
332    *      Defined in: drivers/scsi/scsi_scan.c
333    **/
334    struct scsi_device * scsi_add_device(struct Scsi_Host *shost,
335					unsigned int channel,
336					unsigned int id, unsigned int lun)
337
338
339    /**
340    * scsi_add_host - perform sysfs registration and set up transport class
341    * @shost:   pointer to scsi host instance
342    * @dev:     pointer to struct device of type scsi class
343    *
344    *      Returns 0 on success, negative errno of failure (e.g. -ENOMEM)
345    *
346    *      Might block: no
347    *
348    *      Notes: Only required in "hotplug initialization model" after a
349    *      successful call to scsi_host_alloc().  This function does not
350    *	scan the bus; this can be done by calling scsi_scan_host() or
351    *	in some other transport-specific way.  The LLD must set up
352    *	the transport template before calling this function and may only
353    *	access the transport class data after this function has been called.
354    *
355    *      Defined in: drivers/scsi/hosts.c
356    **/
357    int scsi_add_host(struct Scsi_Host *shost, struct device * dev)
358
359
360    /**
361    * scsi_change_queue_depth - allow LLD to change queue depth on a SCSI device
362    * @sdev:       pointer to SCSI device to change queue depth on
363    * @tags        Number of tags allowed if tagged queuing enabled,
364    *              or number of commands the LLD can queue up
365    *              in non-tagged mode (as per cmd_per_lun).
366    *
367    *      Returns nothing
368    *
369    *      Might block: no
370    *
371    *      Notes: Can be invoked any time on a SCSI device controlled by this
372    *      LLD. [Specifically during and after sdev_configure() and prior to
373    *      sdev_destroy().] Can safely be invoked from interrupt code.
374    *
375    *      Defined in: drivers/scsi/scsi.c [see source code for more notes]
376    *
377    **/
378    int scsi_change_queue_depth(struct scsi_device *sdev, int tags)
379
380
381    /**
382    * scsi_bios_ptable - return copy of block device's partition table
383    * @dev:        pointer to block device
384    *
385    *      Returns pointer to partition table, or NULL for failure
386    *
387    *      Might block: yes
388    *
389    *      Notes: Caller owns memory returned (free with kfree() )
390    *
391    *      Defined in: drivers/scsi/scsicam.c
392    **/
393    unsigned char *scsi_bios_ptable(struct block_device *dev)
394
395
396    /**
397    * scsi_block_requests - prevent further commands being queued to given host
398    *
399    * @shost: pointer to host to block commands on
400    *
401    *      Returns nothing
402    *
403    *      Might block: no
404    *
405    *      Notes: There is no timer nor any other means by which the requests
406    *      get unblocked other than the LLD calling scsi_unblock_requests().
407    *
408    *      Defined in: drivers/scsi/scsi_lib.c
409    **/
410    void scsi_block_requests(struct Scsi_Host * shost)
411
412
413    /**
414    * scsi_host_alloc - create a scsi host adapter instance and perform basic
415    *                   initialization.
416    * @sht:        pointer to scsi host template
417    * @privsize:   extra bytes to allocate in hostdata array (which is the
418    *              last member of the returned Scsi_Host instance)
419    *
420    *      Returns pointer to new Scsi_Host instance or NULL on failure
421    *
422    *      Might block: yes
423    *
424    *      Notes: When this call returns to the LLD, the SCSI bus scan on
425    *      this host has _not_ yet been done.
426    *      The hostdata array (by default zero length) is a per host scratch
427    *      area for the LLD's exclusive use.
428    *      Both associated refcounting objects have their refcount set to 1.
429    *      Full registration (in sysfs) and a bus scan are performed later when
430    *      scsi_add_host() and scsi_scan_host() are called.
431    *
432    *      Defined in: drivers/scsi/hosts.c .
433    **/
434    struct Scsi_Host * scsi_host_alloc(const struct scsi_host_template * sht,
435				    int privsize)
436
437
438    /**
439    * scsi_host_get - increment Scsi_Host instance refcount
440    * @shost:   pointer to struct Scsi_Host instance
441    *
442    *      Returns nothing
443    *
444    *      Might block: currently may block but may be changed to not block
445    *
446    *      Notes: Actually increments the counts in two sub-objects
447    *
448    *      Defined in: drivers/scsi/hosts.c
449    **/
450    void scsi_host_get(struct Scsi_Host *shost)
451
452
453    /**
454    * scsi_host_put - decrement Scsi_Host instance refcount, free if 0
455    * @shost:   pointer to struct Scsi_Host instance
456    *
457    *      Returns nothing
458    *
459    *      Might block: currently may block but may be changed to not block
460    *
461    *      Notes: Actually decrements the counts in two sub-objects. If the
462    *      latter refcount reaches 0, the Scsi_Host instance is freed.
463    *      The LLD need not worry exactly when the Scsi_Host instance is
464    *      freed, it just shouldn't access the instance after it has balanced
465    *      out its refcount usage.
466    *
467    *      Defined in: drivers/scsi/hosts.c
468    **/
469    void scsi_host_put(struct Scsi_Host *shost)
470
471
472    /**
473    * scsi_remove_device - detach and remove a SCSI device
474    * @sdev:      a pointer to a scsi device instance
475    *
476    *      Returns value: 0 on success, -EINVAL if device not attached
477    *
478    *      Might block: yes
479    *
480    *      Notes: If an LLD becomes aware that a scsi device (lu) has
481    *      been removed but its host is still present then it can request
482    *      the removal of that scsi device. If successful this call will
483    *      lead to the sdev_destroy() callback being invoked. sdev is an
484    *      invalid pointer after this call.
485    *
486    *      Defined in: drivers/scsi/scsi_sysfs.c .
487    **/
488    int scsi_remove_device(struct scsi_device *sdev)
489
490
491    /**
492    * scsi_remove_host - detach and remove all SCSI devices owned by host
493    * @shost:      a pointer to a scsi host instance
494    *
495    *      Returns value: 0 on success, 1 on failure (e.g. LLD busy ??)
496    *
497    *      Might block: yes
498    *
499    *      Notes: Should only be invoked if the "hotplug initialization
500    *      model" is being used. It should be called _prior_ to
501    *      calling scsi_host_put().
502    *
503    *      Defined in: drivers/scsi/hosts.c .
504    **/
505    int scsi_remove_host(struct Scsi_Host *shost)
506
507
508    /**
509    * scsi_report_bus_reset - report scsi _bus_ reset observed
510    * @shost: a pointer to a scsi host involved
511    * @channel: channel (within) host on which scsi bus reset occurred
512    *
513    *      Returns nothing
514    *
515    *      Might block: no
516    *
517    *      Notes: This only needs to be called if the reset is one which
518    *      originates from an unknown location.  Resets originated by the
519    *      mid level itself don't need to call this, but there should be
520    *      no harm.  The main purpose of this is to make sure that a
521    *      CHECK_CONDITION is properly treated.
522    *
523    *      Defined in: drivers/scsi/scsi_error.c .
524    **/
525    void scsi_report_bus_reset(struct Scsi_Host * shost, int channel)
526
527
528    /**
529    * scsi_scan_host - scan SCSI bus
530    * @shost: a pointer to a scsi host instance
531    *
532    *	Might block: yes
533    *
534    *	Notes: Should be called after scsi_add_host()
535    *
536    *	Defined in: drivers/scsi/scsi_scan.c
537    **/
538    void scsi_scan_host(struct Scsi_Host *shost)
539
540
541    /**
542    * scsi_track_queue_full - track successive QUEUE_FULL events on given
543    *                      device to determine if and when there is a need
544    *                      to adjust the queue depth on the device.
545    * @sdev:  pointer to SCSI device instance
546    * @depth: Current number of outstanding SCSI commands on this device,
547    *         not counting the one returned as QUEUE_FULL.
548    *
549    *      Returns 0  - no change needed
550    *              >0 - adjust queue depth to this new depth
551    *              -1 - drop back to untagged operation using host->cmd_per_lun
552    *                   as the untagged command depth
553    *
554    *      Might block: no
555    *
556    *      Notes: LLDs may call this at any time and we will do "The Right
557    *              Thing"; interrupt context safe.
558    *
559    *      Defined in: drivers/scsi/scsi.c .
560    **/
561    int scsi_track_queue_full(struct scsi_device *sdev, int depth)
562
563
564    /**
565    * scsi_unblock_requests - allow further commands to be queued to given host
566    *
567    * @shost: pointer to host to unblock commands on
568    *
569    *      Returns nothing
570    *
571    *      Might block: no
572    *
573    *      Defined in: drivers/scsi/scsi_lib.c .
574    **/
575    void scsi_unblock_requests(struct Scsi_Host * shost)
576
577
578
579Interface Functions
580===================
581Interface functions are supplied (defined) by LLDs and their function
582pointers are placed in an instance of struct scsi_host_template which
583is passed to scsi_host_alloc().
584Some are mandatory. Interface functions should be declared static. The
585accepted convention is that driver "xyz" will declare its sdev_configure()
586function as::
587
588    static int xyz_sdev_configure(struct scsi_device * sdev);
589
590and so forth for all interface functions listed below.
591
592A pointer to this function should be placed in the 'sdev_configure' member
593of a "struct scsi_host_template" instance. A pointer to such an instance
594should be passed to the mid level's scsi_host_alloc().
595.
596
597The interface functions are also described in the include/scsi/scsi_host.h
598file immediately above their definition point in "struct scsi_host_template".
599In some cases more detail is given in scsi_host.h than below.
600
601The interface functions are listed below in alphabetical order.
602
603Summary:
604
605  - bios_param - fetch head, sector, cylinder info for a disk
606  - eh_timed_out - notify the host that a command timer expired
607  - eh_abort_handler - abort given command
608  - eh_bus_reset_handler - issue SCSI bus reset
609  - eh_device_reset_handler - issue SCSI device reset
610  - eh_host_reset_handler - reset host (host bus adapter)
611  - info - supply information about given host
612  - ioctl - driver can respond to ioctls
613  - proc_info - supports /proc/scsi/{driver_name}/{host_no}
614  - queuecommand - queue scsi command, invoke 'done' on completion
615  - sdev_init - prior to any commands being sent to a new device
616  - sdev_configure - driver fine tuning for given device after attach
617  - sdev_destroy - given device is about to be shut down
618
619
620Details::
621
622    /**
623    *      bios_param - fetch head, sector, cylinder info for a disk
624    *      @sdev: pointer to scsi device context (defined in
625    *             include/scsi/scsi_device.h)
626    *      @bdev: pointer to block device context (defined in fs.h)
627    *      @capacity:  device size (in 512 byte sectors)
628    *      @params: three element array to place output:
629    *              params[0] number of heads (max 255)
630    *              params[1] number of sectors (max 63)
631    *              params[2] number of cylinders
632    *
633    *      Return value is ignored
634    *
635    *      Locks: none
636    *
637    *      Calling context: process (sd)
638    *
639    *      Notes: an arbitrary geometry (based on READ CAPACITY) is used
640    *      if this function is not provided. The params array is
641    *      pre-initialized with made up values just in case this function
642    *      doesn't output anything.
643    *
644    *      Optionally defined in: LLD
645    **/
646	int bios_param(struct scsi_device * sdev, struct block_device *bdev,
647		    sector_t capacity, int params[3])
648
649
650    /**
651    *      eh_timed_out - The timer for the command has just fired
652    *      @scp: identifies command timing out
653    *
654    *      Returns:
655    *
656    *      EH_HANDLED:             I fixed the error, please complete the command
657    *      EH_RESET_TIMER:         I need more time, reset the timer and
658    *                              begin counting again
659    *      EH_NOT_HANDLED          Begin normal error recovery
660    *
661    *
662    *      Locks: None held
663    *
664    *      Calling context: interrupt
665    *
666    *      Notes: This is to give the LLD an opportunity to do local recovery.
667    *      This recovery is limited to determining if the outstanding command
668    *      will ever complete.  You may not abort and restart the command from
669    *      this callback.
670    *
671    *      Optionally defined in: LLD
672    **/
673	int eh_timed_out(struct scsi_cmnd * scp)
674
675
676    /**
677    *      eh_abort_handler - abort command associated with scp
678    *      @scp: identifies command to be aborted
679    *
680    *      Returns SUCCESS if command aborted else FAILED
681    *
682    *      Locks: None held
683    *
684    *      Calling context: kernel thread
685    *
686    *      Notes: This is called only for a command that has timed out.
687    *
688    *      Optionally defined in: LLD
689    **/
690	int eh_abort_handler(struct scsi_cmnd * scp)
691
692
693    /**
694    *      eh_bus_reset_handler - issue SCSI bus reset
695    *      @scp: SCSI bus that contains this device should be reset
696    *
697    *      Returns SUCCESS if command aborted else FAILED
698    *
699    *      Locks: None held
700    *
701    *      Calling context: kernel thread
702    *
703    *      Notes: Invoked from scsi_eh thread. No other commands will be
704    *      queued on current host during eh.
705    *
706    *      Optionally defined in: LLD
707    **/
708	int eh_bus_reset_handler(struct scsi_cmnd * scp)
709
710
711    /**
712    *      eh_device_reset_handler - issue SCSI device reset
713    *      @scp: identifies SCSI device to be reset
714    *
715    *      Returns SUCCESS if command aborted else FAILED
716    *
717    *      Locks: None held
718    *
719    *      Calling context: kernel thread
720    *
721    *      Notes: Invoked from scsi_eh thread. No other commands will be
722    *      queued on current host during eh.
723    *
724    *      Optionally defined in: LLD
725    **/
726	int eh_device_reset_handler(struct scsi_cmnd * scp)
727
728
729    /**
730    *      eh_host_reset_handler - reset host (host bus adapter)
731    *      @scp: SCSI host that contains this device should be reset
732    *
733    *      Returns SUCCESS if command aborted else FAILED
734    *
735    *      Locks: None held
736    *
737    *      Calling context: kernel thread
738    *
739    *      Notes: Invoked from scsi_eh thread. No other commands will be
740    *      queued on current host during eh.
741    *      With the default eh_strategy in place, if none of the _abort_,
742    *      _device_reset_, _bus_reset_ or this eh handler function are
743    *      defined (or they all return FAILED) then the device in question
744    *      will be set offline whenever eh is invoked.
745    *
746    *      Optionally defined in: LLD
747    **/
748	int eh_host_reset_handler(struct scsi_cmnd * scp)
749
750
751    /**
752    *      info - supply information about given host: driver name plus data
753    *             to distinguish given host
754    *      @shp: host to supply information about
755    *
756    *      Return ASCII null terminated string. [This driver is assumed to
757    *      manage the memory pointed to and maintain it, typically for the
758    *      lifetime of this host.]
759    *
760    *      Locks: none
761    *
762    *      Calling context: process
763    *
764    *      Notes: Often supplies PCI or ISA information such as IO addresses
765    *      and interrupt numbers. If not supplied struct Scsi_Host::name used
766    *      instead. It is assumed the returned information fits on one line
767    *      (i.e. does not included embedded newlines).
768    *      The SCSI_IOCTL_PROBE_HOST ioctl yields the string returned by this
769    *      function (or struct Scsi_Host::name if this function is not
770    *      available).
771    *
772    *      Optionally defined in: LLD
773    **/
774	const char * info(struct Scsi_Host * shp)
775
776
777    /**
778    *      ioctl - driver can respond to ioctls
779    *      @sdp: device that ioctl was issued for
780    *      @cmd: ioctl number
781    *      @arg: pointer to read or write data from. Since it points to
782    *            user space, should use appropriate kernel functions
783    *            (e.g. copy_from_user() ). In the Unix style this argument
784    *            can also be viewed as an unsigned long.
785    *
786    *      Returns negative "errno" value when there is a problem. 0 or a
787    *      positive value indicates success and is returned to the user space.
788    *
789    *      Locks: none
790    *
791    *      Calling context: process
792    *
793    *      Notes: The SCSI subsystem uses a "trickle down" ioctl model.
794    *      The user issues an ioctl() against an upper level driver
795    *      (e.g. /dev/sdc) and if the upper level driver doesn't recognize
796    *      the 'cmd' then it is passed to the SCSI mid level. If the SCSI
797    *      mid level does not recognize it, then the LLD that controls
798    *      the device receives the ioctl. According to recent Unix standards
799    *      unsupported ioctl() 'cmd' numbers should return -ENOTTY.
800    *
801    *      Optionally defined in: LLD
802    **/
803	int ioctl(struct scsi_device *sdp, int cmd, void *arg)
804
805
806    /**
807    *      proc_info - supports /proc/scsi/{driver_name}/{host_no}
808    *      @buffer: anchor point to output to (0==writeto1_read0) or fetch from
809    *               (1==writeto1_read0).
810    *      @start: where "interesting" data is written to. Ignored when
811    *              1==writeto1_read0.
812    *      @offset: offset within buffer 0==writeto1_read0 is actually
813    *               interested in. Ignored when 1==writeto1_read0 .
814    *      @length: maximum (or actual) extent of buffer
815    *      @host_no: host number of interest (struct Scsi_Host::host_no)
816    *      @writeto1_read0: 1 -> data coming from user space towards driver
817    *                            (e.g. "echo some_string > /proc/scsi/xyz/2")
818    *                       0 -> user what data from this driver
819    *                            (e.g. "cat /proc/scsi/xyz/2")
820    *
821    *      Returns length when 1==writeto1_read0. Otherwise number of chars
822    *      output to buffer past offset.
823    *
824    *      Locks: none held
825    *
826    *      Calling context: process
827    *
828    *      Notes: Driven from scsi_proc.c which interfaces to proc_fs. proc_fs
829    *      support can now be configured out of the scsi subsystem.
830    *
831    *      Optionally defined in: LLD
832    **/
833	int proc_info(char * buffer, char ** start, off_t offset,
834		    int length, int host_no, int writeto1_read0)
835
836
837    /**
838    *      queuecommand - queue scsi command, invoke scp->scsi_done on completion
839    *      @shost: pointer to the scsi host object
840    *      @scp: pointer to scsi command object
841    *
842    *      Returns 0 on success.
843    *
844    *      If there's a failure, return either:
845    *
846    *      SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
847    *      SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
848    *
849    *      On both of these returns, the mid-layer will requeue the I/O
850    *
851    *      - if the return is SCSI_MLQUEUE_DEVICE_BUSY, only that particular
852    *      device will be paused, and it will be unpaused when a command to
853    *      the device returns (or after a brief delay if there are no more
854    *      outstanding commands to it).  Commands to other devices continue
855    *      to be processed normally.
856    *
857    *      - if the return is SCSI_MLQUEUE_HOST_BUSY, all I/O to the host
858    *      is paused and will be unpaused when any command returns from
859    *      the host (or after a brief delay if there are no outstanding
860    *      commands to the host).
861    *
862    *      For compatibility with earlier versions of queuecommand, any
863    *      other return value is treated the same as
864    *      SCSI_MLQUEUE_HOST_BUSY.
865    *
866    *      Other types of errors that are detected immediately may be
867    *      flagged by setting scp->result to an appropriate value,
868    *      invoking the scp->scsi_done callback, and then returning 0
869    *      from this function. If the command is not performed
870    *      immediately (and the LLD is starting (or will start) the given
871    *      command) then this function should place 0 in scp->result and
872    *      return 0.
873    *
874    *      Command ownership.  If the driver returns zero, it owns the
875    *      command and must take responsibility for ensuring the
876    *      scp->scsi_done callback is executed.  Note: the driver may
877    *      call scp->scsi_done before returning zero, but after it has
878    *      called scp->scsi_done, it may not return any value other than
879    *      zero.  If the driver makes a non-zero return, it must not
880    *      execute the command's scsi_done callback at any time.
881    *
882    *      Locks: up to and including 2.6.36, struct Scsi_Host::host_lock
883    *             held on entry (with "irqsave") and is expected to be
884    *             held on return. From 2.6.37 onwards, queuecommand is
885    *             called without any locks held.
886    *
887    *      Calling context: in interrupt (soft irq) or process context
888    *
889    *      Notes: This function should be relatively fast. Normally it
890    *      will not wait for IO to complete. Hence the scp->scsi_done
891    *      callback is invoked (often directly from an interrupt service
892    *      routine) some time after this function has returned. In some
893    *      cases (e.g. pseudo adapter drivers that manufacture the
894    *      response to a SCSI INQUIRY) the scp->scsi_done callback may be
895    *      invoked before this function returns.  If the scp->scsi_done
896    *      callback is not invoked within a certain period the SCSI mid
897    *      level will commence error processing.  If a status of CHECK
898    *      CONDITION is placed in "result" when the scp->scsi_done
899    *      callback is invoked, then the LLD driver should perform
900    *      autosense and fill in the struct scsi_cmnd::sense_buffer
901    *      array. The scsi_cmnd::sense_buffer array is zeroed prior to
902    *      the mid level queuing a command to an LLD.
903    *
904    *      Defined in: LLD
905    **/
906	int queuecommand(struct Scsi_Host *shost, struct scsi_cmnd * scp)
907
908
909    /**
910    *      sdev_init -   prior to any commands being sent to a new device
911    *                      (i.e. just prior to scan) this call is made
912    *      @sdp: pointer to new device (about to be scanned)
913    *
914    *      Returns 0 if ok. Any other return is assumed to be an error and
915    *      the device is ignored.
916    *
917    *      Locks: none
918    *
919    *      Calling context: process
920    *
921    *      Notes: Allows the driver to allocate any resources for a device
922    *      prior to its initial scan. The corresponding scsi device may not
923    *      exist but the mid level is just about to scan for it (i.e. send
924    *      and INQUIRY command plus ...). If a device is found then
925    *      sdev_configure() will be called while if a device is not found
926    *      sdev_destroy() is called.
927    *      For more details see the include/scsi/scsi_host.h file.
928    *
929    *      Optionally defined in: LLD
930    **/
931	int sdev_init(struct scsi_device *sdp)
932
933
934    /**
935    *      sdev_configure - driver fine tuning for given device just after it
936    *                     has been first scanned (i.e. it responded to an
937    *                     INQUIRY)
938    *      @sdp: device that has just been attached
939    *
940    *      Returns 0 if ok. Any other return is assumed to be an error and
941    *      the device is taken offline. [offline devices will _not_ have
942    *      sdev_destroy() called on them so clean up resources.]
943    *
944    *      Locks: none
945    *
946    *      Calling context: process
947    *
948    *      Notes: Allows the driver to inspect the response to the initial
949    *      INQUIRY done by the scanning code and take appropriate action.
950    *      For more details see the include/scsi/scsi_host.h file.
951    *
952    *      Optionally defined in: LLD
953    **/
954	int sdev_configure(struct scsi_device *sdp)
955
956
957    /**
958    *      sdev_destroy - given device is about to be shut down. All
959    *                      activity has ceased on this device.
960    *      @sdp: device that is about to be shut down
961    *
962    *      Returns nothing
963    *
964    *      Locks: none
965    *
966    *      Calling context: process
967    *
968    *      Notes: Mid level structures for given device are still in place
969    *      but are about to be torn down. Any per device resources allocated
970    *      by this driver for given device should be freed now. No further
971    *      commands will be sent for this sdp instance. [However the device
972    *      could be re-attached in the future in which case a new instance
973    *      of struct scsi_device would be supplied by future sdev_init()
974    *      and sdev_configure() calls.]
975    *
976    *      Optionally defined in: LLD
977    **/
978	void sdev_destroy(struct scsi_device *sdp)
979
980
981
982Data Structures
983===============
984struct scsi_host_template
985-------------------------
986There is one "struct scsi_host_template" instance per LLD [#]_. It is
987typically initialized as a file scope static in a driver's header file. That
988way members that are not explicitly initialized will be set to 0 or NULL.
989Members of interest:
990
991    name
992		 - name of driver (may contain spaces, please limit to
993                   less than 80 characters)
994
995    proc_name
996		 - name used in "/proc/scsi/<proc_name>/<host_no>" and
997                   by sysfs in one of its "drivers" directories. Hence
998                   "proc_name" should only contain characters acceptable
999                   to a Unix file name.
1000
1001   ``(*queuecommand)()``
1002		 - primary callback that the mid level uses to inject
1003                   SCSI commands into an LLD.
1004
1005    vendor_id
1006		 - a unique value that identifies the vendor supplying
1007                   the LLD for the Scsi_Host.  Used most often in validating
1008                   vendor-specific message requests.  Value consists of an
1009                   identifier type and a vendor-specific value.
1010                   See scsi_netlink.h for a description of valid formats.
1011
1012The structure is defined and commented in include/scsi/scsi_host.h
1013
1014.. [#] In extreme situations a single driver may have several instances
1015       if it controls several different classes of hardware (e.g. an LLD
1016       that handles both ISA and PCI cards and has a separate instance of
1017       struct scsi_host_template for each class).
1018
1019struct Scsi_Host
1020----------------
1021There is one struct Scsi_Host instance per host (HBA) that an LLD
1022controls. The struct Scsi_Host structure has many members in common
1023with "struct scsi_host_template". When a new struct Scsi_Host instance
1024is created (in scsi_host_alloc() in hosts.c) those common members are
1025initialized from the driver's struct scsi_host_template instance. Members
1026of interest:
1027
1028    host_no
1029		 - system wide unique number that is used for identifying
1030                   this host. Issued in ascending order from 0.
1031    can_queue
1032		 - must be greater than 0; do not send more than can_queue
1033                   commands to the adapter.
1034    this_id
1035		 - scsi id of host (scsi initiator) or -1 if not known
1036    sg_tablesize
1037		 - maximum scatter gather elements allowed by host.
1038                   Set this to SG_ALL or less to avoid chained SG lists.
1039                   Must be at least 1.
1040    max_sectors
1041		 - maximum number of sectors (usually 512 bytes) allowed
1042                   in a single SCSI command. The default value of 0 leads
1043                   to a setting of SCSI_DEFAULT_MAX_SECTORS (defined in
1044                   scsi_host.h) which is currently set to 1024. So for a
1045                   disk the maximum transfer size is 512 KB when max_sectors
1046                   is not defined. Note that this size may not be sufficient
1047                   for disk firmware uploads.
1048    cmd_per_lun
1049		 - maximum number of commands that can be queued on devices
1050                   controlled by the host. Overridden by LLD calls to
1051                   scsi_change_queue_depth().
1052    hostt
1053		 - pointer to driver's struct scsi_host_template from which
1054                   this struct Scsi_Host instance was spawned
1055    hostt->proc_name
1056		 - name of LLD. This is the driver name that sysfs uses
1057    transportt
1058		 - pointer to driver's struct scsi_transport_template instance
1059                   (if any). FC and SPI transports currently supported.
1060    hostdata[0]
1061		 - area reserved for LLD at end of struct Scsi_Host. Size
1062                   is set by the second argument (named 'privsize') to
1063                   scsi_host_alloc().
1064
1065The scsi_host structure is defined in include/scsi/scsi_host.h
1066
1067struct scsi_device
1068------------------
1069Generally, there is one instance of this structure for each SCSI logical unit
1070on a host. Scsi devices connected to a host are uniquely identified by a
1071channel number, target id and logical unit number (lun).
1072The structure is defined in include/scsi/scsi_device.h
1073
1074struct scsi_cmnd
1075----------------
1076Instances of this structure convey SCSI commands to the LLD and responses
1077back to the mid level. The SCSI mid level will ensure that no more SCSI
1078commands become queued against the LLD than are indicated by
1079scsi_change_queue_depth() (or struct Scsi_Host::cmd_per_lun). There will
1080be at least one instance of struct scsi_cmnd available for each SCSI device.
1081Members of interest:
1082
1083    cmnd
1084		 - array containing SCSI command
1085    cmd_len
1086		 - length (in bytes) of SCSI command
1087    sc_data_direction
1088		 - direction of data transfer in data phase. See
1089                   "enum dma_data_direction" in include/linux/dma-mapping.h
1090    result
1091		 - should be set by LLD prior to calling 'done'. A value
1092                   of 0 implies a successfully completed command (and all
1093                   data (if any) has been transferred to or from the SCSI
1094                   target device). 'result' is a 32 bit unsigned integer that
1095                   can be viewed as 2 related bytes. The SCSI status value is
1096                   in the LSB. See include/scsi/scsi.h status_byte() and
1097                   host_byte() macros and related constants.
1098    sense_buffer
1099		 - an array (maximum size: SCSI_SENSE_BUFFERSIZE bytes) that
1100                   should be written when the SCSI status (LSB of 'result')
1101                   is set to CHECK_CONDITION (2). When CHECK_CONDITION is
1102                   set, if the top nibble of sense_buffer[0] has the value 7
1103                   then the mid level will assume the sense_buffer array
1104                   contains a valid SCSI sense buffer; otherwise the mid
1105                   level will issue a REQUEST_SENSE SCSI command to
1106                   retrieve the sense buffer. The latter strategy is error
1107                   prone in the presence of command queuing so the LLD should
1108                   always "auto-sense".
1109    device
1110		 - pointer to scsi_device object that this command is
1111                   associated with.
1112    resid_len   (access by calling scsi_set_resid() / scsi_get_resid())
1113		 - an LLD should set this unsigned integer to the requested
1114                   transfer length (i.e. 'request_bufflen') less the number
1115                   of bytes that are actually transferred. 'resid_len' is
1116                   preset to 0 so an LLD can ignore it if it cannot detect
1117                   underruns (overruns should not be reported). An LLD
1118                   should set 'resid_len' prior to invoking 'done'. The most
1119                   interesting case is data transfers from a SCSI target
1120                   device (e.g. READs) that underrun.
1121    underflow
1122		 - LLD should place (DID_ERROR << 16) in 'result' if
1123                   actual number of bytes transferred is less than this
1124                   figure. Not many LLDs implement this check and some that
1125                   do just output an error message to the log rather than
1126                   report a DID_ERROR. Better for an LLD to implement
1127                   'resid_len'.
1128
1129It is recommended that a LLD set 'resid_len' on data transfers from a SCSI
1130target device (e.g. READs). It is especially important that 'resid_len' is set
1131when such data transfers have sense keys of MEDIUM ERROR and HARDWARE ERROR
1132(and possibly RECOVERED ERROR). In these cases if a LLD is in doubt how much
1133data has been received then the safest approach is to indicate no bytes have
1134been received. For example: to indicate that no valid data has been received
1135a LLD might use these helpers::
1136
1137    scsi_set_resid(SCpnt, scsi_bufflen(SCpnt));
1138
1139where 'SCpnt' is a pointer to a scsi_cmnd object. To indicate only three 512
1140bytes blocks have been received 'resid_len' could be set like this::
1141
1142    scsi_set_resid(SCpnt, scsi_bufflen(SCpnt) - (3 * 512));
1143
1144The scsi_cmnd structure is defined in include/scsi/scsi_cmnd.h
1145
1146
1147Locks
1148=====
1149Each struct Scsi_Host instance has a spin_lock called struct
1150Scsi_Host::default_lock which is initialized in scsi_host_alloc() [found in
1151hosts.c]. Within the same function the struct Scsi_Host::host_lock pointer
1152is initialized to point at default_lock.  Thereafter lock and unlock
1153operations performed by the mid level use the struct Scsi_Host::host_lock
1154pointer.  Previously drivers could override the host_lock pointer but
1155this is not allowed anymore.
1156
1157
1158Autosense
1159=========
1160Autosense (or auto-sense) is defined in the SAM-2 document as "the
1161automatic return of sense data to the application client coincident
1162with the completion of a SCSI command" when a status of CHECK CONDITION
1163occurs. LLDs should perform autosense. This should be done when the LLD
1164detects a CHECK CONDITION status by either:
1165
1166    a) instructing the SCSI protocol (e.g. SCSI Parallel Interface (SPI))
1167       to perform an extra data in phase on such responses
1168    b) or, the LLD issuing a REQUEST SENSE command itself
1169
1170Either way, when a status of CHECK CONDITION is detected, the mid level
1171decides whether the LLD has performed autosense by checking struct
1172scsi_cmnd::sense_buffer[0] . If this byte has an upper nibble of 7 (or 0xf)
1173then autosense is assumed to have taken place. If it has another value (and
1174this byte is initialized to 0 before each command) then the mid level will
1175issue a REQUEST SENSE command.
1176
1177In the presence of queued commands the "nexus" that maintains sense
1178buffer data from the command that failed until a following REQUEST SENSE
1179may get out of synchronization. This is why it is best for the LLD
1180to perform autosense.
1181
1182
1183Changes since lk 2.4 series
1184===========================
1185io_request_lock has been replaced by several finer grained locks. The lock
1186relevant to LLDs is struct Scsi_Host::host_lock and there is
1187one per SCSI host.
1188
1189The older error handling mechanism has been removed. This means the
1190LLD interface functions abort() and reset() have been removed.
1191The struct scsi_host_template::use_new_eh_code flag has been removed.
1192
1193In the 2.4 series the SCSI subsystem configuration descriptions were
1194aggregated with the configuration descriptions from all other Linux
1195subsystems in the Documentation/Configure.help file. In the 2.6 series,
1196the SCSI subsystem now has its own (much smaller) drivers/scsi/Kconfig
1197file that contains both configuration and help information.
1198
1199struct SHT has been renamed to struct scsi_host_template.
1200
1201Addition of the "hotplug initialization model" and many extra functions
1202to support it.
1203
1204
1205Credits
1206=======
1207The following people have contributed to this document:
1208
1209	- Mike Anderson <andmike at us dot ibm dot com>
1210	- James Bottomley <James dot Bottomley at hansenpartnership dot com>
1211	- Patrick Mansfield <patmans at us dot ibm dot com>
1212	- Christoph Hellwig <hch at infradead dot org>
1213	- Doug Ledford <dledford at redhat dot com>
1214	- Andries Brouwer <Andries dot Brouwer at cwi dot nl>
1215	- Randy Dunlap <rdunlap at xenotime dot net>
1216	- Alan Stern <stern at rowland dot harvard dot edu>
1217
1218
1219Douglas Gilbert
1220dgilbert at interlog dot com
1221
122221st September 2004
1223