Lines Matching +full:early +full:- +full:to +full:- +full:mid

1 .. SPDX-License-Identifier: GPL-2.0
4 SCSI mid_level - lower_level driver interface
9 This document outlines the interface between the Linux SCSI mid level and
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
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
30 HBAs. These HBAs might be either on PCI daughter-boards or built into
33 has its own PCI device address. [The one-to-one correspondence between
37 The SCSI mid level isolates an LLD from other layers such as the SCSI
49 documented in Documentation/scsi (e.g. aic7xxx.rst). The SCSI mid-level is
50 briefly described in scsi.rst which contains a URL to a document describing
53 scsi-generic.rst (for the sg driver).
56 or in the same directory as the C source code. For example to find a URL
66 drivers that have been ported to several operating systems have more than
68 and OS-specific code (e.g. FreeBSD and Linux). Such drivers tend to have
71 When a new LLD is being added to Linux, the following files (found in the
73 It is probably best to study how existing LLDs are organized.
78 available. The older one, similar to what was found in the lk 2.4 series,
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
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
95 c) direct access to instances of well known data structures maintained
96 by the mid level
98 Those functions in group a) are listed in a section entitled "Mid level
103 "struct scsi_host_template", an instance of which is passed to
105 wish to supply should have NULL placed in the corresponding member of
107 scsi_host_template at file scope will cause NULL to be placed in function
112 that are shared with the mid level and other layers.
123 from the SCSI subsystem. Hosts can be introduced as early as driver
125 will respond to a sysfs probe() callback that indicates an HBA has been
127 to control, the LLD will initialize the HBA and then register a new host
128 with the SCSI mid level.
131 appropriate IO bus on which it expects to find HBA(s) (e.g. the PCI bus).
134 registered with sysfs at this point. The SCSI mid level first becomes
138 is a typical sequence of calls between the LLD and the mid level.
139 This example shows the mid level scanning the newly introduced HBA for 3
143 LLD mid level LLD
144 ===-------------------=========--------------------===------
145 scsi_host_alloc() -->
146 scsi_add_host() ---->
147 scsi_scan_host() -------+
150 sdev_configure() --> scsi_change_queue_depth()
159 *** For scsi devices that the mid level tries to scan but do not
162 If the LLD wants to adjust the default queue settings, it can invoke
167 command) or in response to a "hot unplug" indicated by sysfs()'s
172 LLD mid level LLD
173 ===----------------------=========-----------------===------
174 scsi_remove_host() ---------+
180 It may be useful for a LLD to keep track of struct Scsi_Host instances
182 by the mid-level. struct Scsi_Host instances are freed from
187 counting logic is being introduced into the mid level to cope with many
191 The hotplug concept may be extended to SCSI devices. Currently, when an
193 attached to the HBA's SCSI transport. On newer SCSI transports the HBA
195 An LLD can use this sequence to make the mid level aware of a SCSI device::
198 LLD mid level LLD
199 ===-------------------=========--------------------===------
200 scsi_add_device() ------+
203 sdev_configure() [--> scsi_change_queue_depth()]
206 removed (unplugged) or the connection to it has been interrupted. Some
209 probably cause that device to be set offline by the mid level. An LLD that
214 LLD mid level LLD
215 ===----------------------=========-----------------===------
216 scsi_remove_device() -------+
220 It 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
222 sdev_configure() callbacks). Such instances are "owned" by the mid-level.
231 were exclusively owned by the mid level. LLDs would not usually need to
238 - scsi_host_alloc():
239 returns a pointer to new instance of struct
240 Scsi_Host which has its reference count ^^ set to 1
242 - scsi_host_get():
243 adds 1 to the reference count of the given instance
245 - scsi_host_put():
253 were exclusively owned by the mid level. See the access functions declared
254 towards the end of include/scsi/scsi_device.h . If an LLD wants to keep
255 a copy of a pointer to a scsi_device instance it should use scsi_device_get()
256 to bump its reference count. When it is finished with the pointer it can
257 use scsi_device_put() to decrement its reference count (and potentially
269 Documentation/process/coding-style.rst file.
271 Also, most C99 enhancements are encouraged to the extent they are supported
274 VLAs are not properly supported yet. An exception to this is the use of
277 Well written, tested and documented code, need not be re-formatted to
279 comes to Linux from FreeBSD and Adaptec's own labs. No doubt FreeBSD
283 Mid level supplied functions
285 These functions are supplied by the SCSI mid level for use by LLDs.
288 arrange for the SCSI mid level to be loaded and initialized before any LLD
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
313 * scsi_add_device - creates new scsi device (lu) instance
314 * @shost: pointer to scsi host instance
319 * Returns pointer to new struct scsi_device instance or
320 * ERR_PTR(-ENODEV) (or some other bent pointer) if something is
329 * this call can lead to sdev_init() and sdev_configure() callbacks
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
344 * Returns 0 on success, negative errno of failure (e.g. -ENOMEM)
349 * successful call to scsi_host_alloc(). This function does not
351 * in some other transport-specific way. The LLD must set up
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
365 * in non-tagged mode (as per cmd_per_lun).
372 * LLD. [Specifically during and after sdev_configure() and prior to
382 * scsi_bios_ptable - return copy of block device's partition table
383 * @dev: pointer to block device
385 * Returns pointer to partition table, or NULL for failure
397 * scsi_block_requests - prevent further commands being queued to given host
399 * @shost: pointer to host to block commands on
414 * scsi_host_alloc - create a scsi host adapter instance and perform basic
416 * @sht: pointer to scsi host template
417 * @privsize: extra bytes to allocate in hostdata array (which is the
420 * Returns pointer to new Scsi_Host instance or NULL on failure
424 * Notes: When this call returns to the LLD, the SCSI bus scan on
428 * Both associated refcounting objects have their refcount set to 1.
439 * scsi_host_get - increment Scsi_Host instance refcount
440 * @shost: pointer to struct Scsi_Host instance
444 * Might block: currently may block but may be changed to not block
446 * Notes: Actually increments the counts in two sub-objects
454 * scsi_host_put - decrement Scsi_Host instance refcount, free if 0
455 * @shost: pointer to struct Scsi_Host instance
459 * Might block: currently may block but may be changed to not block
461 * Notes: Actually decrements the counts in two sub-objects. If the
473 * scsi_remove_device - detach and remove a SCSI device
474 * @sdev: a pointer to a scsi device instance
476 * Returns value: 0 on success, -EINVAL if device not attached
483 * lead to the sdev_destroy() callback being invoked. sdev is an
492 * scsi_remove_host - detach and remove all SCSI devices owned by host
493 * @shost: a pointer to a scsi host instance
500 * model" is being used. It should be called _prior_ to
509 * scsi_report_bus_reset - report scsi _bus_ reset observed
510 * @shost: a pointer to a scsi host involved
517 * Notes: This only needs to be called if the reset is one which
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
529 * scsi_scan_host - scan SCSI bus
530 * @shost: a pointer to a scsi host instance
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
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
565 * scsi_unblock_requests - allow further commands to be queued to given host
567 * @shost: pointer to host to unblock commands on
583 is passed to scsi_host_alloc().
592 A pointer to this function should be placed in the 'sdev_configure' member
593 of a "struct scsi_host_template" instance. A pointer to such an instance
594 should be passed to the mid level's scsi_host_alloc().
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
623 * bios_param - fetch head, sector, cylinder info for a disk
624 * @sdev: pointer to scsi device context (defined in
626 * @bdev: pointer to block device context (defined in fs.h)
628 * @params: three element array to place output:
641 * pre-initialized with made up values just in case this function
651 * eh_timed_out - The timer for the command has just fired
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
677 * eh_abort_handler - abort command associated with scp
678 * @scp: identifies command to be aborted
694 * eh_bus_reset_handler - issue SCSI bus reset
712 * eh_device_reset_handler - issue SCSI device reset
713 * @scp: identifies SCSI device to be reset
730 * eh_host_reset_handler - reset host (host bus adapter)
752 * info - supply information about given host: driver name plus data
753 * to distinguish given host
754 * @shp: host to supply information about
756 * Return ASCII null terminated string. [This driver is assumed to
757 * manage the memory pointed to and maintain it, typically for the
778 * ioctl - driver can respond to ioctls
781 * @arg: pointer to read or write data from. Since it points to
787 * positive value indicates success and is returned to the user space.
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.
807 * proc_info - supports /proc/scsi/{driver_name}/{host_no}
808 * @buffer: anchor point to output to (0==writeto1_read0) or fetch from
810 * @start: where "interesting" data is written to. Ignored when
816 * @writeto1_read0: 1 -> data coming from user space towards driver
818 * 0 -> user what data from this driver
822 * output to buffer past offset.
828 * Notes: Driven from scsi_proc.c which interfaces to proc_fs. proc_fs
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
849 * On both of these returns, the mid-layer will requeue the I/O
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
854 * outstanding commands to it). Commands to other devices continue
855 * to be processed normally.
857 * - if the return is SCSI_MLQUEUE_HOST_BUSY, all I/O to the host
860 * commands to the host).
867 * flagged by setting scp->result to an appropriate value,
868 * invoking the scp->scsi_done callback, and then returning 0
871 * command) then this function should place 0 in scp->result and
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
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
890 * will not wait for IO to complete. Hence the scp->scsi_done
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
898 * CONDITION is placed in "result" when the scp->scsi_done
901 * array. The scsi_cmnd::sense_buffer array is zeroed prior to
902 * the mid level queuing a command to an LLD.
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)
914 * Returns 0 if ok. Any other return is assumed to be an error and
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
935 * sdev_configure - driver fine tuning for given device just after it
936 * has been first scanned (i.e. it responded to an
940 * Returns 0 if ok. Any other return is assumed to be an error and
948 * Notes: Allows the driver to inspect the response to the initial
958 * sdev_destroy - given device is about to be shut down. All
960 * @sdp: device that is about to be shut down
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
972 * could be re-attached in the future in which case a new instance
985 -------------------------
988 way members that are not explicitly initialized will be set to 0 or NULL.
992 - name of driver (may contain spaces, please limit to
996 - name used in "/proc/scsi/<proc_name>/<host_no>" and
999 to a Unix file name.
1002 - primary callback that the mid level uses to inject
1006 - a unique value that identifies the vendor supplying
1008 vendor-specific message requests. Value consists of an
1009 identifier type and a vendor-specific value.
1020 ----------------
1029 - system wide unique number that is used for identifying
1032 - must be greater than 0; do not send more than can_queue
1033 commands to the adapter.
1035 - scsi id of host (scsi initiator) or -1 if not known
1037 - maximum scatter gather elements allowed by host.
1038 Set this to SG_ALL or less to avoid chained SG lists.
1041 - maximum number of sectors (usually 512 bytes) allowed
1043 to a setting of SCSI_DEFAULT_MAX_SECTORS (defined in
1044 scsi_host.h) which is currently set to 1024. So for a
1049 - maximum number of commands that can be queued on devices
1050 controlled by the host. Overridden by LLD calls to
1053 - pointer to driver's struct scsi_host_template from which
1055 hostt->proc_name
1056 - name of LLD. This is the driver name that sysfs uses
1058 - pointer to driver's struct scsi_transport_template instance
1061 - area reserved for LLD at end of struct Scsi_Host. Size
1062 is set by the second argument (named 'privsize') to
1068 ------------------
1070 on a host. Scsi devices connected to a host are uniquely identified by a
1075 ----------------
1076 Instances of this structure convey SCSI commands to the LLD and responses
1077 back to the mid level. The SCSI mid level will ensure that no more SCSI
1084 - array containing SCSI command
1086 - length (in bytes) of SCSI command
1088 - direction of data transfer in data phase. See
1089 "enum dma_data_direction" in include/linux/dma-mapping.h
1091 - should be set by LLD prior to calling 'done'. A value
1093 data (if any) has been transferred to or from the SCSI
1099 - an array (maximum size: SCSI_SENSE_BUFFERSIZE bytes) that
1101 is set to CHECK_CONDITION (2). When CHECK_CONDITION is
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
1108 always "auto-sense".
1110 - pointer to scsi_device object that this command is
1113 - an LLD should set this unsigned integer to the requested
1116 preset to 0 so an LLD can ignore it if it cannot detect
1118 should set 'resid_len' prior to invoking 'done'. The most
1122 - LLD should place (DID_ERROR << 16) in 'result' if
1125 do just output an error message to the log rather than
1126 report a DID_ERROR. Better for an LLD to implement
1133 data has been received then the safest approach is to indicate no bytes have
1134 been received. For example: to indicate that no valid data has been received
1139 where 'SCpnt' is a pointer to a scsi_cmnd object. To indicate only three 512
1142 scsi_set_resid(SCpnt, scsi_bufflen(SCpnt) - (3 * 512));
1152 is initialized to point at default_lock. Thereafter lock and unlock
1153 operations performed by the mid level use the struct Scsi_Host::host_lock
1160 Autosense (or auto-sense) is defined in the SAM-2 document as "the
1161 automatic return of sense data to the application client coincident
1167 to perform an extra data in phase on such responses
1170 Either way, when a status of CHECK CONDITION is detected, the mid level
1173 then autosense is assumed to have taken place. If it has another value (and
1174 this byte is initialized to 0 before each command) then the mid level will
1180 to perform autosense.
1186 relevant to LLDs is struct Scsi_Host::host_lock and there is
1199 struct SHT has been renamed to struct scsi_host_template.
1202 to support it.
1207 The following people have contributed to this document:
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>