1 .. SPDX-License-Identifier: GPL-2.0
3 =============================================
4 SCSI mid_level - lower_level driver interface
5 =============================================
9 This document outlines the interface between the Linux SCSI mid level and
10 SCSI lower level drivers. Lower level drivers (LLDs) are variously called
11 host bus adapter (HBA) drivers and host drivers (HD). A "host" in this
12 context is a bridge between a computer IO bus (e.g. PCI or ISA) and a
13 single SCSI initiator port on a SCSI transport. An "initiator" port
14 (SCSI terminology, see SAM-3 at http://www.t10.org) sends SCSI commands
15 to "target" SCSI ports (e.g. disks). There can be many LLDs in a running
16 system, but only one per hardware type. Most LLDs can control one or more
17 SCSI HBAs. Some HBAs contain multiple hosts.
19 In some cases the SCSI transport is an external bus that already has
20 its own subsystem in Linux (e.g. USB and ieee1394). In such cases the
21 SCSI subsystem LLD is a software bridge to the other driver subsystem.
22 Examples are the usb-storage driver (found in the drivers/usb/storage
23 directory) and the ieee1394/sbp2 driver (found in the drivers/ieee1394
26 For example, the aic7xxx LLD controls Adaptec SCSI parallel interface
27 (SPI) controllers based on that company's 7xxx chip series. The aic7xxx
28 LLD can be built into the kernel or loaded as a module. There can only be
29 one aic7xxx LLD running in a Linux system but it may be controlling many
30 HBAs. These HBAs might be either on PCI daughter-boards or built into
31 the motherboard (or both). Some aic7xxx based HBAs are dual controllers
32 and thus represent two hosts. Like most modern HBAs, each aic7xxx host
33 has its own PCI device address. [The one-to-one correspondence between
34 a SCSI host and a PCI device is common but not required (e.g. with
37 The SCSI mid level isolates an LLD from other layers such as the SCSI
38 upper layer drivers and the block layer.
40 This version of the document roughly matches linux kernel version 2.6.8 .
44 There is a SCSI documentation directory within the kernel source tree,
45 typically Documentation/scsi . Most documents are in plain
46 (i.e. ASCII) text. This file is named scsi_mid_low_api.txt and can be
47 found in that directory. A more recent copy of this document may be found
48 at http://web.archive.org/web/20070107183357rn_1/sg.torque.net/scsi/.
49 Many LLDs are documented there (e.g. aic7xxx.txt). The SCSI mid-level is
50 briefly described in scsi.txt which contains a url to a document
51 describing the SCSI subsystem in the lk 2.4 series. Two upper level
52 drivers have documents in that directory: st.txt (SCSI tape driver) and
53 scsi-generic.txt (for the sg driver).
55 Some documentation (or urls) for LLDs may be found in the C source code
56 or in the same directory as the C source code. For example to find a url
57 about the USB mass storage driver see the
58 /usr/src/linux/drivers/usb/storage directory.
62 Traditionally an LLD for the SCSI subsystem has been at least two files in
63 the drivers/scsi directory. For example, a driver called "xyz" has a header
64 file "xyz.h" and a source file "xyz.c". [Actually there is no good reason
65 why this couldn't all be in one file; the header file is superfluous.] Some
66 drivers that have been ported to several operating systems have more than
67 two files. For example the aic7xxx driver has separate files for generic
68 and OS-specific code (e.g. FreeBSD and Linux). Such drivers tend to have
69 their own directory under the drivers/scsi directory.
71 When a new LLD is being added to Linux, the following files (found in the
72 drivers/scsi directory) will need some attention: Makefile and Kconfig .
73 It is probably best to study how existing LLDs are organized.
75 As the 2.5 series development kernels evolve into the 2.6 series
76 production series, changes are being introduced into this interface. An
77 example of this is driver initialization code where there are now 2 models
78 available. The older one, similar to what was found in the lk 2.4 series,
79 is based on hosts that are detected at HBA driver load time. This will be
80 referred to the "passive" initialization model. The newer model allows HBAs
81 to be hot plugged (and unplugged) during the lifetime of the LLD and will
82 be referred to as the "hotplug" initialization model. The newer model is
83 preferred as it can handle both traditional SCSI equipment that is
84 permanently connected as well as modern "SCSI" devices (e.g. USB or
85 IEEE 1394 connected digital cameras) that are hotplugged. Both
86 initialization models are discussed in the following sections.
88 An LLD interfaces to the SCSI subsystem several ways:
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
98 Those functions in group a) are listed in a section entitled "Mid level
99 supplied functions" below.
101 Those functions in group b) are listed in a section entitled "Interface
102 functions" below. Their function pointers are placed in the members of
103 "struct scsi_host_template", an instance of which is passed to
104 scsi_host_alloc() [#]_. Those interface functions that the LLD does not
105 wish to supply should have NULL placed in the corresponding member of
106 struct scsi_host_template. Defining an instance of struct
107 scsi_host_template at file scope will cause NULL to be placed in function
108 pointer members not explicitly initialized.
110 Those usages in group c) should be handled with care, especially in a
111 "hotplug" environment. LLDs should be aware of the lifetime of instances
112 that are shared with the mid level and other layers.
114 All functions defined within an LLD and all data defined at file scope
115 should be static. For example the slave_alloc() function in an LLD
116 called "xxx" could be defined as
117 ``static int xxx_slave_alloc(struct scsi_device * sdev) { /* code */ }``
119 .. [#] the scsi_host_alloc() function is a replacement for the rather vaguely
120 named scsi_register() function in most situations.
123 Hotplug initialization model
124 ============================
125 In this model an LLD controls when SCSI hosts are introduced and removed
126 from the SCSI subsystem. Hosts can be introduced as early as driver
127 initialization and removed as late as driver shutdown. Typically a driver
128 will respond to a sysfs probe() callback that indicates an HBA has been
129 detected. After confirming that the new device is one that the LLD wants
130 to control, the LLD will initialize the HBA and then register a new host
131 with the SCSI mid level.
133 During LLD initialization the driver should register itself with the
134 appropriate IO bus on which it expects to find HBA(s) (e.g. the PCI bus).
135 This can probably be done via sysfs. Any driver parameters (especially
136 those that are writable after the driver is loaded) could also be
137 registered with sysfs at this point. The SCSI mid level first becomes
138 aware of an LLD when that LLD registers its first HBA.
140 At some later time, the LLD becomes aware of an HBA and what follows
141 is a typical sequence of calls between the LLD and the mid level.
142 This example shows the mid level scanning the newly introduced HBA for 3
143 scsi devices of which only the first 2 respond::
145 HBA PROBE: assume 2 SCSI devices found in scan
147 ===-------------------=========--------------------===------
148 scsi_host_alloc() -->
149 scsi_add_host() ---->
150 scsi_scan_host() -------+
153 slave_configure() --> scsi_change_queue_depth()
162 *** For scsi devices that the mid level tries to scan but do not
163 respond, a slave_alloc(), slave_destroy() pair is called.
165 If the LLD wants to adjust the default queue settings, it can invoke
166 scsi_change_queue_depth() in its slave_configure() routine.
168 When an HBA is being removed it could be as part of an orderly shutdown
169 associated with the LLD module being unloaded (e.g. with the "rmmod"
170 command) or in response to a "hot unplug" indicated by sysfs()'s
171 remove() callback being invoked. In either case, the sequence is the
174 HBA REMOVE: assume 2 SCSI devices attached
176 ===----------------------=========-----------------===------
177 scsi_remove_host() ---------+
183 It may be useful for a LLD to keep track of struct Scsi_Host instances
184 (a pointer is returned by scsi_host_alloc()). Such instances are "owned"
185 by the mid-level. struct Scsi_Host instances are freed from
186 scsi_host_put() when the reference count hits zero.
188 Hot unplugging an HBA that controls a disk which is processing SCSI
189 commands on a mounted file system is an interesting situation. Reference
190 counting logic is being introduced into the mid level to cope with many
191 of the issues involved. See the section on reference counting below.
194 The hotplug concept may be extended to SCSI devices. Currently, when an
195 HBA is added, the scsi_scan_host() function causes a scan for SCSI devices
196 attached to the HBA's SCSI transport. On newer SCSI transports the HBA
197 may become aware of a new SCSI device _after_ the scan has completed.
198 An LLD can use this sequence to make the mid level aware of a SCSI device::
202 ===-------------------=========--------------------===------
203 scsi_add_device() ------+
206 slave_configure() [--> scsi_change_queue_depth()]
208 In a similar fashion, an LLD may become aware that a SCSI device has been
209 removed (unplugged) or the connection to it has been interrupted. Some
210 existing SCSI transports (e.g. SPI) may not become aware that a SCSI
211 device has been removed until a subsequent SCSI command fails which will
212 probably cause that device to be set offline by the mid level. An LLD that
213 detects the removal of a SCSI device can instigate its removal from
214 upper layers with this sequence::
216 SCSI DEVICE hot unplug
218 ===----------------------=========-----------------===------
219 scsi_remove_device() -------+
223 It may be useful for an LLD to keep track of struct scsi_device instances
224 (a pointer is passed as the parameter to slave_alloc() and
225 slave_configure() callbacks). Such instances are "owned" by the mid-level.
226 struct scsi_device instances are freed after slave_destroy().
231 The Scsi_Host structure has had reference counting infrastructure added.
232 This effectively spreads the ownership of struct Scsi_Host instances
233 across the various SCSI layers which use them. Previously such instances
234 were exclusively owned by the mid level. LLDs would not usually need to
235 directly manipulate these reference counts but there may be some cases
238 There are 3 reference counting functions of interest associated with
242 returns a pointer to new instance of struct
243 Scsi_Host which has its reference count ^^ set to 1
246 adds 1 to the reference count of the given instance
249 decrements 1 from the reference count of the given
250 instance. If the reference count reaches 0 then the given instance
253 The scsi_device structure has had reference counting infrastructure added.
254 This effectively spreads the ownership of struct scsi_device instances
255 across the various SCSI layers which use them. Previously such instances
256 were exclusively owned by the mid level. See the access functions declared
257 towards the end of include/scsi/scsi_device.h . If an LLD wants to keep
258 a copy of a pointer to a scsi_device instance it should use scsi_device_get()
259 to bump its reference count. When it is finished with the pointer it can
260 use scsi_device_put() to decrement its reference count (and potentially
265 struct Scsi_Host actually has 2 reference counts which are manipulated
266 in parallel by these functions.
271 First, Linus Torvalds's thoughts on C coding style can be found in the
272 Documentation/process/coding-style.rst file.
274 Next, there is a movement to "outlaw" typedefs introducing synonyms for
275 struct tags. Both can be still found in the SCSI subsystem, but
276 the typedefs have been moved to a single file, scsi_typedefs.h to
277 make their future removal easier, for example:
278 "typedef struct scsi_cmnd Scsi_Cmnd;"
280 Also, most C99 enhancements are encouraged to the extent they are supported
281 by the relevant gcc compilers. So C99 style structure and array
282 initializers are encouraged where appropriate. Don't go too far,
283 VLAs are not properly supported yet. An exception to this is the use of
284 ``//`` style comments; ``/*...*/`` comments are still preferred in Linux.
286 Well written, tested and documented code, need not be re-formatted to
287 comply with the above conventions. For example, the aic7xxx driver
288 comes to Linux from FreeBSD and Adaptec's own labs. No doubt FreeBSD
289 and Adaptec have their own coding conventions.
292 Mid level supplied functions
293 ============================
294 These functions are supplied by the SCSI mid level for use by LLDs.
295 The names (i.e. entry points) of these functions are exported
296 so an LLD that is a module can access them. The kernel will
297 arrange for the SCSI mid level to be loaded and initialized before any LLD
298 is initialized. The functions below are listed alphabetically and their
299 names all start with ``scsi_``.
303 - scsi_add_device - creates new scsi device (lu) instance
304 - scsi_add_host - perform sysfs registration and set up transport class
305 - scsi_change_queue_depth - change the queue depth on a SCSI device
306 - scsi_bios_ptable - return copy of block device's partition table
307 - scsi_block_requests - prevent further commands being queued to given host
308 - scsi_host_alloc - return a new scsi_host instance whose refcount==1
309 - scsi_host_get - increments Scsi_Host instance's refcount
310 - scsi_host_put - decrements Scsi_Host instance's refcount (free if 0)
311 - scsi_register - create and register a scsi host adapter instance.
312 - scsi_remove_device - detach and remove a SCSI device
313 - scsi_remove_host - detach and remove all SCSI devices owned by host
314 - scsi_report_bus_reset - report scsi _bus_ reset observed
315 - scsi_scan_host - scan SCSI bus
316 - scsi_track_queue_full - track successive QUEUE_FULL events
317 - scsi_unblock_requests - allow further commands to be queued to given host
318 - scsi_unregister - [calls scsi_host_put()]
324 * scsi_add_device - creates new scsi device (lu) instance
325 * @shost: pointer to scsi host instance
326 * @channel: channel number (rarely other than 0)
327 * @id: target id number
328 * @lun: logical unit number
330 * Returns pointer to new struct scsi_device instance or
331 * ERR_PTR(-ENODEV) (or some other bent pointer) if something is
332 * wrong (e.g. no lu responds at given address)
336 * Notes: This call is usually performed internally during a scsi
337 * bus scan when an HBA is added (i.e. scsi_scan_host()). So it
338 * should only be called if the HBA becomes aware of a new scsi
339 * device (lu) after scsi_scan_host() has completed. If successful
340 * this call can lead to slave_alloc() and slave_configure() callbacks
343 * Defined in: drivers/scsi/scsi_scan.c
345 struct scsi_device * scsi_add_device(struct Scsi_Host *shost,
346 unsigned int channel,
347 unsigned int id, unsigned int lun)
351 * scsi_add_host - perform sysfs registration and set up transport class
352 * @shost: pointer to scsi host instance
353 * @dev: pointer to struct device of type scsi class
355 * Returns 0 on success, negative errno of failure (e.g. -ENOMEM)
359 * Notes: Only required in "hotplug initialization model" after a
360 * successful call to scsi_host_alloc(). This function does not
361 * scan the bus; this can be done by calling scsi_scan_host() or
362 * in some other transport-specific way. The LLD must set up
363 * the transport template before calling this function and may only
364 * access the transport class data after this function has been called.
366 * Defined in: drivers/scsi/hosts.c
368 int scsi_add_host(struct Scsi_Host *shost, struct device * dev)
372 * scsi_change_queue_depth - allow LLD to change queue depth on a SCSI device
373 * @sdev: pointer to SCSI device to change queue depth on
374 * @tags Number of tags allowed if tagged queuing enabled,
375 * or number of commands the LLD can queue up
376 * in non-tagged mode (as per cmd_per_lun).
382 * Notes: Can be invoked any time on a SCSI device controlled by this
383 * LLD. [Specifically during and after slave_configure() and prior to
384 * slave_destroy().] Can safely be invoked from interrupt code.
386 * Defined in: drivers/scsi/scsi.c [see source code for more notes]
389 int scsi_change_queue_depth(struct scsi_device *sdev, int tags)
393 * scsi_bios_ptable - return copy of block device's partition table
394 * @dev: pointer to block device
396 * Returns pointer to partition table, or NULL for failure
400 * Notes: Caller owns memory returned (free with kfree() )
402 * Defined in: drivers/scsi/scsicam.c
404 unsigned char *scsi_bios_ptable(struct block_device *dev)
408 * scsi_block_requests - prevent further commands being queued to given host
410 * @shost: pointer to host to block commands on
416 * Notes: There is no timer nor any other means by which the requests
417 * get unblocked other than the LLD calling scsi_unblock_requests().
419 * Defined in: drivers/scsi/scsi_lib.c
421 void scsi_block_requests(struct Scsi_Host * shost)
425 * scsi_host_alloc - create a scsi host adapter instance and perform basic
427 * @sht: pointer to scsi host template
428 * @privsize: extra bytes to allocate in hostdata array (which is the
429 * last member of the returned Scsi_Host instance)
431 * Returns pointer to new Scsi_Host instance or NULL on failure
435 * Notes: When this call returns to the LLD, the SCSI bus scan on
436 * this host has _not_ yet been done.
437 * The hostdata array (by default zero length) is a per host scratch
438 * area for the LLD's exclusive use.
439 * Both associated refcounting objects have their refcount set to 1.
440 * Full registration (in sysfs) and a bus scan are performed later when
441 * scsi_add_host() and scsi_scan_host() are called.
443 * Defined in: drivers/scsi/hosts.c .
445 struct Scsi_Host * scsi_host_alloc(struct scsi_host_template * sht,
450 * scsi_host_get - increment Scsi_Host instance refcount
451 * @shost: pointer to struct Scsi_Host instance
455 * Might block: currently may block but may be changed to not block
457 * Notes: Actually increments the counts in two sub-objects
459 * Defined in: drivers/scsi/hosts.c
461 void scsi_host_get(struct Scsi_Host *shost)
465 * scsi_host_put - decrement Scsi_Host instance refcount, free if 0
466 * @shost: pointer to struct Scsi_Host instance
470 * Might block: currently may block but may be changed to not block
472 * Notes: Actually decrements the counts in two sub-objects. If the
473 * latter refcount reaches 0, the Scsi_Host instance is freed.
474 * The LLD need not worry exactly when the Scsi_Host instance is
475 * freed, it just shouldn't access the instance after it has balanced
476 * out its refcount usage.
478 * Defined in: drivers/scsi/hosts.c
480 void scsi_host_put(struct Scsi_Host *shost)
484 * scsi_register - create and register a scsi host adapter instance.
485 * @sht: pointer to scsi host template
486 * @privsize: extra bytes to allocate in hostdata array (which is the
487 * last member of the returned Scsi_Host instance)
489 * Returns pointer to new Scsi_Host instance or NULL on failure
493 * Notes: When this call returns to the LLD, the SCSI bus scan on
494 * this host has _not_ yet been done.
495 * The hostdata array (by default zero length) is a per host scratch
498 * Defined in: drivers/scsi/hosts.c .
500 struct Scsi_Host * scsi_register(struct scsi_host_template * sht,
505 * scsi_remove_device - detach and remove a SCSI device
506 * @sdev: a pointer to a scsi device instance
508 * Returns value: 0 on success, -EINVAL if device not attached
512 * Notes: If an LLD becomes aware that a scsi device (lu) has
513 * been removed but its host is still present then it can request
514 * the removal of that scsi device. If successful this call will
515 * lead to the slave_destroy() callback being invoked. sdev is an
516 * invalid pointer after this call.
518 * Defined in: drivers/scsi/scsi_sysfs.c .
520 int scsi_remove_device(struct scsi_device *sdev)
524 * scsi_remove_host - detach and remove all SCSI devices owned by host
525 * @shost: a pointer to a scsi host instance
527 * Returns value: 0 on success, 1 on failure (e.g. LLD busy ??)
531 * Notes: Should only be invoked if the "hotplug initialization
532 * model" is being used. It should be called _prior_ to
535 * Defined in: drivers/scsi/hosts.c .
537 int scsi_remove_host(struct Scsi_Host *shost)
541 * scsi_report_bus_reset - report scsi _bus_ reset observed
542 * @shost: a pointer to a scsi host involved
543 * @channel: channel (within) host on which scsi bus reset occurred
549 * Notes: This only needs to be called if the reset is one which
550 * originates from an unknown location. Resets originated by the
551 * mid level itself don't need to call this, but there should be
552 * no harm. The main purpose of this is to make sure that a
553 * CHECK_CONDITION is properly treated.
555 * Defined in: drivers/scsi/scsi_error.c .
557 void scsi_report_bus_reset(struct Scsi_Host * shost, int channel)
561 * scsi_scan_host - scan SCSI bus
562 * @shost: a pointer to a scsi host instance
566 * Notes: Should be called after scsi_add_host()
568 * Defined in: drivers/scsi/scsi_scan.c
570 void scsi_scan_host(struct Scsi_Host *shost)
574 * scsi_track_queue_full - track successive QUEUE_FULL events on given
575 * device to determine if and when there is a need
576 * to adjust the queue depth on the device.
577 * @sdev: pointer to SCSI device instance
578 * @depth: Current number of outstanding SCSI commands on this device,
579 * not counting the one returned as QUEUE_FULL.
581 * Returns 0 - no change needed
582 * >0 - adjust queue depth to this new depth
583 * -1 - drop back to untagged operation using host->cmd_per_lun
584 * as the untagged command depth
588 * Notes: LLDs may call this at any time and we will do "The Right
589 * Thing"; interrupt context safe.
591 * Defined in: drivers/scsi/scsi.c .
593 int scsi_track_queue_full(struct scsi_device *sdev, int depth)
597 * scsi_unblock_requests - allow further commands to be queued to given host
599 * @shost: pointer to host to unblock commands on
605 * Defined in: drivers/scsi/scsi_lib.c .
607 void scsi_unblock_requests(struct Scsi_Host * shost)
611 * scsi_unregister - unregister and free memory used by host instance
612 * @shp: pointer to scsi host instance to unregister.
618 * Notes: Should not be invoked if the "hotplug initialization
619 * model" is being used. Called internally by exit_this_scsi_driver()
620 * in the "passive initialization model". Hence a LLD has no need to
621 * call this function directly.
623 * Defined in: drivers/scsi/hosts.c .
625 void scsi_unregister(struct Scsi_Host * shp)
632 Interface functions are supplied (defined) by LLDs and their function
633 pointers are placed in an instance of struct scsi_host_template which
634 is passed to scsi_host_alloc() [or scsi_register() / init_this_scsi_driver()].
635 Some are mandatory. Interface functions should be declared static. The
636 accepted convention is that driver "xyz" will declare its slave_configure()
639 static int xyz_slave_configure(struct scsi_device * sdev);
641 and so forth for all interface functions listed below.
643 A pointer to this function should be placed in the 'slave_configure' member
644 of a "struct scsi_host_template" instance. A pointer to such an instance
645 should be passed to the mid level's scsi_host_alloc() [or scsi_register() /
646 init_this_scsi_driver()].
648 The interface functions are also described in the include/scsi/scsi_host.h
649 file immediately above their definition point in "struct scsi_host_template".
650 In some cases more detail is given in scsi_host.h than below.
652 The interface functions are listed below in alphabetical order.
656 - bios_param - fetch head, sector, cylinder info for a disk
657 - eh_timed_out - notify the host that a command timer expired
658 - eh_abort_handler - abort given command
659 - eh_bus_reset_handler - issue SCSI bus reset
660 - eh_device_reset_handler - issue SCSI device reset
661 - eh_host_reset_handler - reset host (host bus adapter)
662 - info - supply information about given host
663 - ioctl - driver can respond to ioctls
664 - proc_info - supports /proc/scsi/{driver_name}/{host_no}
665 - queuecommand - queue scsi command, invoke 'done' on completion
666 - slave_alloc - prior to any commands being sent to a new device
667 - slave_configure - driver fine tuning for given device after attach
668 - slave_destroy - given device is about to be shut down
674 * bios_param - fetch head, sector, cylinder info for a disk
675 * @sdev: pointer to scsi device context (defined in
676 * include/scsi/scsi_device.h)
677 * @bdev: pointer to block device context (defined in fs.h)
678 * @capacity: device size (in 512 byte sectors)
679 * @params: three element array to place output:
680 * params[0] number of heads (max 255)
681 * params[1] number of sectors (max 63)
682 * params[2] number of cylinders
684 * Return value is ignored
688 * Calling context: process (sd)
690 * Notes: an arbitrary geometry (based on READ CAPACITY) is used
691 * if this function is not provided. The params array is
692 * pre-initialized with made up values just in case this function
693 * doesn't output anything.
695 * Optionally defined in: LLD
697 int bios_param(struct scsi_device * sdev, struct block_device *bdev,
698 sector_t capacity, int params[3])
702 * eh_timed_out - The timer for the command has just fired
703 * @scp: identifies command timing out
707 * EH_HANDLED: I fixed the error, please complete the command
708 * EH_RESET_TIMER: I need more time, reset the timer and
709 * begin counting again
710 * EH_NOT_HANDLED Begin normal error recovery
715 * Calling context: interrupt
717 * Notes: This is to give the LLD an opportunity to do local recovery.
718 * This recovery is limited to determining if the outstanding command
719 * will ever complete. You may not abort and restart the command from
722 * Optionally defined in: LLD
724 int eh_timed_out(struct scsi_cmnd * scp)
728 * eh_abort_handler - abort command associated with scp
729 * @scp: identifies command to be aborted
731 * Returns SUCCESS if command aborted else FAILED
735 * Calling context: kernel thread
737 * Notes: If 'no_async_abort' is defined this callback
738 * will be invoked from scsi_eh thread. No other commands
739 * will then be queued on current host during eh.
740 * Otherwise it will be called whenever scsi_times_out()
741 * is called due to a command timeout.
743 * Optionally defined in: LLD
745 int eh_abort_handler(struct scsi_cmnd * scp)
749 * eh_bus_reset_handler - issue SCSI bus reset
750 * @scp: SCSI bus that contains this device should be reset
752 * Returns SUCCESS if command aborted else FAILED
756 * Calling context: kernel thread
758 * Notes: Invoked from scsi_eh thread. No other commands will be
759 * queued on current host during eh.
761 * Optionally defined in: LLD
763 int eh_bus_reset_handler(struct scsi_cmnd * scp)
767 * eh_device_reset_handler - issue SCSI device reset
768 * @scp: identifies SCSI device to be reset
770 * Returns SUCCESS if command aborted else FAILED
774 * Calling context: kernel thread
776 * Notes: Invoked from scsi_eh thread. No other commands will be
777 * queued on current host during eh.
779 * Optionally defined in: LLD
781 int eh_device_reset_handler(struct scsi_cmnd * scp)
785 * eh_host_reset_handler - reset host (host bus adapter)
786 * @scp: SCSI host that contains this device should be reset
788 * Returns SUCCESS if command aborted else FAILED
792 * Calling context: kernel thread
794 * Notes: Invoked from scsi_eh thread. No other commands will be
795 * queued on current host during eh.
796 * With the default eh_strategy in place, if none of the _abort_,
797 * _device_reset_, _bus_reset_ or this eh handler function are
798 * defined (or they all return FAILED) then the device in question
799 * will be set offline whenever eh is invoked.
801 * Optionally defined in: LLD
803 int eh_host_reset_handler(struct scsi_cmnd * scp)
807 * info - supply information about given host: driver name plus data
808 * to distinguish given host
809 * @shp: host to supply information about
811 * Return ASCII null terminated string. [This driver is assumed to
812 * manage the memory pointed to and maintain it, typically for the
813 * lifetime of this host.]
817 * Calling context: process
819 * Notes: Often supplies PCI or ISA information such as IO addresses
820 * and interrupt numbers. If not supplied struct Scsi_Host::name used
821 * instead. It is assumed the returned information fits on one line
822 * (i.e. does not included embedded newlines).
823 * The SCSI_IOCTL_PROBE_HOST ioctl yields the string returned by this
824 * function (or struct Scsi_Host::name if this function is not
826 * In a similar manner, init_this_scsi_driver() outputs to the console
827 * each host's "info" (or name) for the driver it is registering.
828 * Also if proc_info() is not supplied, the output of this function
831 * Optionally defined in: LLD
833 const char * info(struct Scsi_Host * shp)
837 * ioctl - driver can respond to ioctls
838 * @sdp: device that ioctl was issued for
840 * @arg: pointer to read or write data from. Since it points to
841 * user space, should use appropriate kernel functions
842 * (e.g. copy_from_user() ). In the Unix style this argument
843 * can also be viewed as an unsigned long.
845 * Returns negative "errno" value when there is a problem. 0 or a
846 * positive value indicates success and is returned to the user space.
850 * Calling context: process
852 * Notes: The SCSI subsystem uses a "trickle down" ioctl model.
853 * The user issues an ioctl() against an upper level driver
854 * (e.g. /dev/sdc) and if the upper level driver doesn't recognize
855 * the 'cmd' then it is passed to the SCSI mid level. If the SCSI
856 * mid level does not recognize it, then the LLD that controls
857 * the device receives the ioctl. According to recent Unix standards
858 * unsupported ioctl() 'cmd' numbers should return -ENOTTY.
860 * Optionally defined in: LLD
862 int ioctl(struct scsi_device *sdp, int cmd, void *arg)
866 * proc_info - supports /proc/scsi/{driver_name}/{host_no}
867 * @buffer: anchor point to output to (0==writeto1_read0) or fetch from
868 * (1==writeto1_read0).
869 * @start: where "interesting" data is written to. Ignored when
871 * @offset: offset within buffer 0==writeto1_read0 is actually
872 * interested in. Ignored when 1==writeto1_read0 .
873 * @length: maximum (or actual) extent of buffer
874 * @host_no: host number of interest (struct Scsi_Host::host_no)
875 * @writeto1_read0: 1 -> data coming from user space towards driver
876 * (e.g. "echo some_string > /proc/scsi/xyz/2")
877 * 0 -> user what data from this driver
878 * (e.g. "cat /proc/scsi/xyz/2")
880 * Returns length when 1==writeto1_read0. Otherwise number of chars
881 * output to buffer past offset.
885 * Calling context: process
887 * Notes: Driven from scsi_proc.c which interfaces to proc_fs. proc_fs
888 * support can now be configured out of the scsi subsystem.
890 * Optionally defined in: LLD
892 int proc_info(char * buffer, char ** start, off_t offset,
893 int length, int host_no, int writeto1_read0)
897 * queuecommand - queue scsi command, invoke scp->scsi_done on completion
898 * @shost: pointer to the scsi host object
899 * @scp: pointer to scsi command object
901 * Returns 0 on success.
903 * If there's a failure, return either:
905 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
906 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
908 * On both of these returns, the mid-layer will requeue the I/O
910 * - if the return is SCSI_MLQUEUE_DEVICE_BUSY, only that particular
911 * device will be paused, and it will be unpaused when a command to
912 * the device returns (or after a brief delay if there are no more
913 * outstanding commands to it). Commands to other devices continue
914 * to be processed normally.
916 * - if the return is SCSI_MLQUEUE_HOST_BUSY, all I/O to the host
917 * is paused and will be unpaused when any command returns from
918 * the host (or after a brief delay if there are no outstanding
919 * commands to the host).
921 * For compatibility with earlier versions of queuecommand, any
922 * other return value is treated the same as
923 * SCSI_MLQUEUE_HOST_BUSY.
925 * Other types of errors that are detected immediately may be
926 * flagged by setting scp->result to an appropriate value,
927 * invoking the scp->scsi_done callback, and then returning 0
928 * from this function. If the command is not performed
929 * immediately (and the LLD is starting (or will start) the given
930 * command) then this function should place 0 in scp->result and
933 * Command ownership. If the driver returns zero, it owns the
934 * command and must take responsibility for ensuring the
935 * scp->scsi_done callback is executed. Note: the driver may
936 * call scp->scsi_done before returning zero, but after it has
937 * called scp->scsi_done, it may not return any value other than
938 * zero. If the driver makes a non-zero return, it must not
939 * execute the command's scsi_done callback at any time.
941 * Locks: up to and including 2.6.36, struct Scsi_Host::host_lock
942 * held on entry (with "irqsave") and is expected to be
943 * held on return. From 2.6.37 onwards, queuecommand is
944 * called without any locks held.
946 * Calling context: in interrupt (soft irq) or process context
948 * Notes: This function should be relatively fast. Normally it
949 * will not wait for IO to complete. Hence the scp->scsi_done
950 * callback is invoked (often directly from an interrupt service
951 * routine) some time after this function has returned. In some
952 * cases (e.g. pseudo adapter drivers that manufacture the
953 * response to a SCSI INQUIRY) the scp->scsi_done callback may be
954 * invoked before this function returns. If the scp->scsi_done
955 * callback is not invoked within a certain period the SCSI mid
956 * level will commence error processing. If a status of CHECK
957 * CONDITION is placed in "result" when the scp->scsi_done
958 * callback is invoked, then the LLD driver should perform
959 * autosense and fill in the struct scsi_cmnd::sense_buffer
960 * array. The scsi_cmnd::sense_buffer array is zeroed prior to
961 * the mid level queuing a command to an LLD.
965 int queuecommand(struct Scsi_Host *shost, struct scsi_cmnd * scp)
969 * slave_alloc - prior to any commands being sent to a new device
970 * (i.e. just prior to scan) this call is made
971 * @sdp: pointer to new device (about to be scanned)
973 * Returns 0 if ok. Any other return is assumed to be an error and
974 * the device is ignored.
978 * Calling context: process
980 * Notes: Allows the driver to allocate any resources for a device
981 * prior to its initial scan. The corresponding scsi device may not
982 * exist but the mid level is just about to scan for it (i.e. send
983 * and INQUIRY command plus ...). If a device is found then
984 * slave_configure() will be called while if a device is not found
985 * slave_destroy() is called.
986 * For more details see the include/scsi/scsi_host.h file.
988 * Optionally defined in: LLD
990 int slave_alloc(struct scsi_device *sdp)
994 * slave_configure - driver fine tuning for given device just after it
995 * has been first scanned (i.e. it responded to an
997 * @sdp: device that has just been attached
999 * Returns 0 if ok. Any other return is assumed to be an error and
1000 * the device is taken offline. [offline devices will _not_ have
1001 * slave_destroy() called on them so clean up resources.]
1005 * Calling context: process
1007 * Notes: Allows the driver to inspect the response to the initial
1008 * INQUIRY done by the scanning code and take appropriate action.
1009 * For more details see the include/scsi/scsi_host.h file.
1011 * Optionally defined in: LLD
1013 int slave_configure(struct scsi_device *sdp)
1017 * slave_destroy - given device is about to be shut down. All
1018 * activity has ceased on this device.
1019 * @sdp: device that is about to be shut down
1025 * Calling context: process
1027 * Notes: Mid level structures for given device are still in place
1028 * but are about to be torn down. Any per device resources allocated
1029 * by this driver for given device should be freed now. No further
1030 * commands will be sent for this sdp instance. [However the device
1031 * could be re-attached in the future in which case a new instance
1032 * of struct scsi_device would be supplied by future slave_alloc()
1033 * and slave_configure() calls.]
1035 * Optionally defined in: LLD
1037 void slave_destroy(struct scsi_device *sdp)
1043 struct scsi_host_template
1044 -------------------------
1045 There is one "struct scsi_host_template" instance per LLD [#]_. It is
1046 typically initialized as a file scope static in a driver's header file. That
1047 way members that are not explicitly initialized will be set to 0 or NULL.
1051 - name of driver (may contain spaces, please limit to
1052 less than 80 characters)
1055 - name used in "/proc/scsi/<proc_name>/<host_no>" and
1056 by sysfs in one of its "drivers" directories. Hence
1057 "proc_name" should only contain characters acceptable
1058 to a Unix file name.
1060 ``(*queuecommand)()``
1061 - primary callback that the mid level uses to inject
1062 SCSI commands into an LLD.
1064 The structure is defined and commented in include/scsi/scsi_host.h
1066 .. [#] In extreme situations a single driver may have several instances
1067 if it controls several different classes of hardware (e.g. an LLD
1068 that handles both ISA and PCI cards and has a separate instance of
1069 struct scsi_host_template for each class).
1073 There is one struct Scsi_Host instance per host (HBA) that an LLD
1074 controls. The struct Scsi_Host structure has many members in common
1075 with "struct scsi_host_template". When a new struct Scsi_Host instance
1076 is created (in scsi_host_alloc() in hosts.c) those common members are
1077 initialized from the driver's struct scsi_host_template instance. Members
1081 - system wide unique number that is used for identifying
1082 this host. Issued in ascending order from 0.
1084 - must be greater than 0; do not send more than can_queue
1085 commands to the adapter.
1087 - scsi id of host (scsi initiator) or -1 if not known
1089 - maximum scatter gather elements allowed by host.
1090 Set this to SG_ALL or less to avoid chained SG lists.
1093 - maximum number of sectors (usually 512 bytes) allowed
1094 in a single SCSI command. The default value of 0 leads
1095 to a setting of SCSI_DEFAULT_MAX_SECTORS (defined in
1096 scsi_host.h) which is currently set to 1024. So for a
1097 disk the maximum transfer size is 512 KB when max_sectors
1098 is not defined. Note that this size may not be sufficient
1099 for disk firmware uploads.
1101 - maximum number of commands that can be queued on devices
1102 controlled by the host. Overridden by LLD calls to
1103 scsi_change_queue_depth().
1105 - 1=>only use bottom 16 MB of ram (ISA DMA addressing
1106 restriction), 0=>can use full 32 bit (or better) DMA
1109 - 1=>Asynchronous aborts are not supported
1110 - 0=>Timed-out commands will be aborted asynchronously
1112 - pointer to driver's struct scsi_host_template from which
1113 this struct Scsi_Host instance was spawned
1115 - name of LLD. This is the driver name that sysfs uses
1117 - pointer to driver's struct scsi_transport_template instance
1118 (if any). FC and SPI transports currently supported.
1120 - a double linked list of pointers to all struct Scsi_Host
1121 instances (currently ordered by ascending host_no)
1123 - a double linked list of pointers to struct scsi_device
1124 instances that belong to this host.
1126 - area reserved for LLD at end of struct Scsi_Host. Size
1127 is set by the second argument (named 'xtr_bytes') to
1128 scsi_host_alloc() or scsi_register().
1130 - a unique value that identifies the vendor supplying
1131 the LLD for the Scsi_Host. Used most often in validating
1132 vendor-specific message requests. Value consists of an
1133 identifier type and a vendor-specific value.
1134 See scsi_netlink.h for a description of valid formats.
1136 The scsi_host structure is defined in include/scsi/scsi_host.h
1140 Generally, there is one instance of this structure for each SCSI logical unit
1141 on a host. Scsi devices connected to a host are uniquely identified by a
1142 channel number, target id and logical unit number (lun).
1143 The structure is defined in include/scsi/scsi_device.h
1147 Instances of this structure convey SCSI commands to the LLD and responses
1148 back to the mid level. The SCSI mid level will ensure that no more SCSI
1149 commands become queued against the LLD than are indicated by
1150 scsi_change_queue_depth() (or struct Scsi_Host::cmd_per_lun). There will
1151 be at least one instance of struct scsi_cmnd available for each SCSI device.
1152 Members of interest:
1155 - array containing SCSI command
1157 - length (in bytes) of SCSI command
1159 - direction of data transfer in data phase. See
1160 "enum dma_data_direction" in include/linux/dma-mapping.h
1162 - number of data bytes to transfer (0 if no data phase)
1164 - ==0 -> no scatter gather list, hence transfer data
1165 to/from request_buffer
1166 - >0 -> scatter gather list (actually an array) in
1167 request_buffer with use_sg elements
1169 - either contains data buffer or scatter gather list
1170 depending on the setting of use_sg. Scatter gather
1171 elements are defined by 'struct scatterlist' found
1172 in include/linux/scatterlist.h .
1174 - function pointer that should be invoked by LLD when the
1175 SCSI command is completed (successfully or otherwise).
1176 Should only be called by an LLD if the LLD has accepted
1177 the command (i.e. queuecommand() returned or will return
1178 0). The LLD may invoke 'done' prior to queuecommand()
1181 - should be set by LLD prior to calling 'done'. A value
1182 of 0 implies a successfully completed command (and all
1183 data (if any) has been transferred to or from the SCSI
1184 target device). 'result' is a 32 bit unsigned integer that
1185 can be viewed as 4 related bytes. The SCSI status value is
1186 in the LSB. See include/scsi/scsi.h status_byte(),
1187 msg_byte(), host_byte() and driver_byte() macros and
1190 - an array (maximum size: SCSI_SENSE_BUFFERSIZE bytes) that
1191 should be written when the SCSI status (LSB of 'result')
1192 is set to CHECK_CONDITION (2). When CHECK_CONDITION is
1193 set, if the top nibble of sense_buffer[0] has the value 7
1194 then the mid level will assume the sense_buffer array
1195 contains a valid SCSI sense buffer; otherwise the mid
1196 level will issue a REQUEST_SENSE SCSI command to
1197 retrieve the sense buffer. The latter strategy is error
1198 prone in the presence of command queuing so the LLD should
1199 always "auto-sense".
1201 - pointer to scsi_device object that this command is
1204 - an LLD should set this signed integer to the requested
1205 transfer length (i.e. 'request_bufflen') less the number
1206 of bytes that are actually transferred. 'resid' is
1207 preset to 0 so an LLD can ignore it if it cannot detect
1208 underruns (overruns should be rare). If possible an LLD
1209 should set 'resid' prior to invoking 'done'. The most
1210 interesting case is data transfers from a SCSI target
1211 device (e.g. READs) that underrun.
1213 - LLD should place (DID_ERROR << 16) in 'result' if
1214 actual number of bytes transferred is less than this
1215 figure. Not many LLDs implement this check and some that
1216 do just output an error message to the log rather than
1217 report a DID_ERROR. Better for an LLD to implement
1220 It is recommended that a LLD set 'resid' on data transfers from a SCSI
1221 target device (e.g. READs). It is especially important that 'resid' is set
1222 when such data transfers have sense keys of MEDIUM ERROR and HARDWARE ERROR
1223 (and possibly RECOVERED ERROR). In these cases if a LLD is in doubt how much
1224 data has been received then the safest approach is to indicate no bytes have
1225 been received. For example: to indicate that no valid data has been received
1226 a LLD might use these helpers::
1228 scsi_set_resid(SCpnt, scsi_bufflen(SCpnt));
1230 where 'SCpnt' is a pointer to a scsi_cmnd object. To indicate only three 512
1231 bytes blocks has been received 'resid' could be set like this::
1233 scsi_set_resid(SCpnt, scsi_bufflen(SCpnt) - (3 * 512));
1235 The scsi_cmnd structure is defined in include/scsi/scsi_cmnd.h
1240 Each struct Scsi_Host instance has a spin_lock called struct
1241 Scsi_Host::default_lock which is initialized in scsi_host_alloc() [found in
1242 hosts.c]. Within the same function the struct Scsi_Host::host_lock pointer
1243 is initialized to point at default_lock. Thereafter lock and unlock
1244 operations performed by the mid level use the struct Scsi_Host::host_lock
1245 pointer. Previously drivers could override the host_lock pointer but
1246 this is not allowed anymore.
1251 Autosense (or auto-sense) is defined in the SAM-2 document as "the
1252 automatic return of sense data to the application client coincident
1253 with the completion of a SCSI command" when a status of CHECK CONDITION
1254 occurs. LLDs should perform autosense. This should be done when the LLD
1255 detects a CHECK CONDITION status by either:
1257 a) instructing the SCSI protocol (e.g. SCSI Parallel Interface (SPI))
1258 to perform an extra data in phase on such responses
1259 b) or, the LLD issuing a REQUEST SENSE command itself
1261 Either way, when a status of CHECK CONDITION is detected, the mid level
1262 decides whether the LLD has performed autosense by checking struct
1263 scsi_cmnd::sense_buffer[0] . If this byte has an upper nibble of 7 (or 0xf)
1264 then autosense is assumed to have taken place. If it has another value (and
1265 this byte is initialized to 0 before each command) then the mid level will
1266 issue a REQUEST SENSE command.
1268 In the presence of queued commands the "nexus" that maintains sense
1269 buffer data from the command that failed until a following REQUEST SENSE
1270 may get out of synchronization. This is why it is best for the LLD
1271 to perform autosense.
1274 Changes since lk 2.4 series
1275 ===========================
1276 io_request_lock has been replaced by several finer grained locks. The lock
1277 relevant to LLDs is struct Scsi_Host::host_lock and there is
1280 The older error handling mechanism has been removed. This means the
1281 LLD interface functions abort() and reset() have been removed.
1282 The struct scsi_host_template::use_new_eh_code flag has been removed.
1284 In the 2.4 series the SCSI subsystem configuration descriptions were
1285 aggregated with the configuration descriptions from all other Linux
1286 subsystems in the Documentation/Configure.help file. In the 2.6 series,
1287 the SCSI subsystem now has its own (much smaller) drivers/scsi/Kconfig
1288 file that contains both configuration and help information.
1290 struct SHT has been renamed to struct scsi_host_template.
1292 Addition of the "hotplug initialization model" and many extra functions
1298 The following people have contributed to this document:
1300 - Mike Anderson <andmike at us dot ibm dot com>
1301 - James Bottomley <James dot Bottomley at hansenpartnership dot com>
1302 - Patrick Mansfield <patmans at us dot ibm dot com>
1303 - Christoph Hellwig <hch at infradead dot org>
1304 - Doug Ledford <dledford at redhat dot com>
1305 - Andries Brouwer <Andries dot Brouwer at cwi dot nl>
1306 - Randy Dunlap <rdunlap at xenotime dot net>
1307 - Alan Stern <stern at rowland dot harvard dot edu>
1311 dgilbert at interlog dot com