Debian Codegraph @

Documentation/scsi/scsi_mid_low_api.rst

raw
1 .. SPDX-License-Identifier: GPL-2.0
2
3 =============================================
4 SCSI mid_level - lower_level driver interface
5 =============================================
6
7 Introduction
8 ============
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.
18
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
24 directory).
25
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
35 ISA adapters).]
36
37 The SCSI mid level isolates an LLD from other layers such as the SCSI
38 upper layer drivers and the block layer.
39
40 This version of the document roughly matches Linux kernel version 2.6.8 .
41
42 Documentation
43 =============
44 There is a SCSI documentation directory within the kernel source tree,
45 typically Documentation/scsi . Most documents are in reStructuredText
46 format. This file is named scsi_mid_low_api.rst and can be
47 found in that directory. A more recent copy of this document may be found
48 at https://docs.kernel.org/scsi/scsi_mid_low_api.html. Many LLDs are
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
51 the SCSI subsystem in the Linux kernel 2.4 series. Two upper level
52 drivers have documents in that directory: st.rst (SCSI tape driver) and
53 scsi-generic.rst (for the sg driver).
54
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.
59
60 Driver structure
61 ================
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.
70
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.
74
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 Linux 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.
87
88 An 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
98 Those functions in group a) are listed in a section entitled "Mid level
99 supplied functions" below.
100
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.
109
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.
113
114 All functions defined within an LLD and all data defined at file scope
115 should be static. For example the sdev_init() function in an LLD
116 called "xxx" could be defined as
117 ``static int xxx_sdev_init(struct scsi_device * sdev) { /* code */ }``
118
119
120 Hotplug initialization model
121 ============================
122 In this model an LLD controls when SCSI hosts are introduced and removed
123 from the SCSI subsystem. Hosts can be introduced as early as driver
124 initialization and removed as late as driver shutdown. Typically a driver
125 will respond to a sysfs probe() callback that indicates an HBA has been
126 detected. After confirming that the new device is one that the LLD wants
127 to control, the LLD will initialize the HBA and then register a new host
128 with the SCSI mid level.
129
130 During LLD initialization the driver should register itself with the
131 appropriate IO bus on which it expects to find HBA(s) (e.g. the PCI bus).
132 This can probably be done via sysfs. Any driver parameters (especially
133 those that are writable after the driver is loaded) could also be
134 registered with sysfs at this point. The SCSI mid level first becomes
135 aware of an LLD when that LLD registers its first HBA.
136
137 At some later time, the LLD becomes aware of an HBA and what follows
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
140 scsi 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
162 If the LLD wants to adjust the default queue settings, it can invoke
163 scsi_change_queue_depth() in its sdev_configure() routine.
164
165 When an HBA is being removed it could be as part of an orderly shutdown
166 associated with the LLD module being unloaded (e.g. with the "rmmod"
167 command) or in response to a "hot unplug" indicated by sysfs()'s
168 remove() callback being invoked. In either case, the sequence is the
169 same::
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
180 It 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"
182 by the mid-level. struct Scsi_Host instances are freed from
183 scsi_host_put() when the reference count hits zero.
184
185 Hot unplugging an HBA that controls a disk which is processing SCSI
186 commands on a mounted file system is an interesting situation. Reference
187 counting logic is being introduced into the mid level to cope with many
188 of the issues involved. See the section on reference counting below.
189
190
191 The hotplug concept may be extended to SCSI devices. Currently, when an
192 HBA is added, the scsi_scan_host() function causes a scan for SCSI devices
193 attached to the HBA's SCSI transport. On newer SCSI transports the HBA
194 may become aware of a new SCSI device _after_ the scan has completed.
195 An 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
205 In a similar fashion, an LLD may become aware that a SCSI device has been
206 removed (unplugged) or the connection to it has been interrupted. Some
207 existing SCSI transports (e.g. SPI) may not become aware that a SCSI
208 device has been removed until a subsequent SCSI command fails which will
209 probably cause that device to be set offline by the mid level. An LLD that
210 detects the removal of a SCSI device can instigate its removal from
211 upper 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
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.
223 struct scsi_device instances are freed after sdev_destroy().
224
225
226 Reference Counting
227 ==================
228 The Scsi_Host structure has had reference counting infrastructure added.
229 This effectively spreads the ownership of struct Scsi_Host instances
230 across the various SCSI layers which use them. Previously such instances
231 were exclusively owned by the mid level. LLDs would not usually need to
232 directly manipulate these reference counts but there may be some cases
233 where they do.
234
235 There are 3 reference counting functions of interest associated with
236 struct 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
250 The scsi_device structure has had reference counting infrastructure added.
251 This effectively spreads the ownership of struct scsi_device instances
252 across the various SCSI layers which use them. Previously such instances
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
258 delete 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
266 Conventions
267 ===========
268 First, Linus Torvalds's thoughts on C coding style can be found in the
269 Documentation/process/coding-style.rst file.
270
271 Also, most C99 enhancements are encouraged to the extent they are supported
272 by the relevant gcc compilers. So C99 style structure and array
273 initializers are encouraged where appropriate. Don't go too far,
274 VLAs are not properly supported yet. An exception to this is the use of
275 ``//`` style comments; ``/*...*/`` comments are still preferred in Linux.
276
277 Well written, tested and documented code, need not be re-formatted to
278 comply with the above conventions. For example, the aic7xxx driver
279 comes to Linux from FreeBSD and Adaptec's own labs. No doubt FreeBSD
280 and Adaptec have their own coding conventions.
281
282
283 Mid level supplied functions
284 ============================
285 These functions are supplied by the SCSI mid level for use by LLDs.
286 The names (i.e. entry points) of these functions are exported
287 so an LLD that is a module can access them. The kernel will
288 arrange for the SCSI mid level to be loaded and initialized before any LLD
289 is initialized. The functions below are listed alphabetically and their
290 names all start with ``scsi_``.
291
292 Summary:
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
310 Details::
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 gendisk
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 gendisk *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
579 Interface Functions
580 ===================
581 Interface functions are supplied (defined) by LLDs and their function
582 pointers are placed in an instance of struct scsi_host_template which
583 is passed to scsi_host_alloc().
584 Some are mandatory. Interface functions should be declared static. The
585 accepted convention is that driver "xyz" will declare its sdev_configure()
586 function as::
587
588 static int xyz_sdev_configure(struct scsi_device * sdev);
589
590 and so forth for all interface functions listed below.
591
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().
595 .
596
597 The interface functions are also described in the include/scsi/scsi_host.h
598 file immediately above their definition point in "struct scsi_host_template".
599 In some cases more detail is given in scsi_host.h than below.
600
601 The interface functions are listed below in alphabetical order.
602
603 Summary:
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
620 Details::
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 * @disk: pointer to gendisk (defined in blkdev.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 gendisk *disk,
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 enum scsi_qc_status queuecommand(struct Scsi_Host *shost,
907 struct scsi_cmnd *scp)
908
909
910 /**
911 * sdev_init - prior to any commands being sent to a new device
912 * (i.e. just prior to scan) this call is made
913 * @sdp: pointer to new device (about to be scanned)
914 *
915 * Returns 0 if ok. Any other return is assumed to be an error and
916 * the device is ignored.
917 *
918 * Locks: none
919 *
920 * Calling context: process
921 *
922 * Notes: Allows the driver to allocate any resources for a device
923 * prior to its initial scan. The corresponding scsi device may not
924 * exist but the mid level is just about to scan for it (i.e. send
925 * and INQUIRY command plus ...). If a device is found then
926 * sdev_configure() will be called while if a device is not found
927 * sdev_destroy() is called.
928 * For more details see the include/scsi/scsi_host.h file.
929 *
930 * Optionally defined in: LLD
931 **/
932 int sdev_init(struct scsi_device *sdp)
933
934
935 /**
936 * sdev_configure - driver fine tuning for given device just after it
937 * has been first scanned (i.e. it responded to an
938 * INQUIRY)
939 * @sdp: device that has just been attached
940 *
941 * Returns 0 if ok. Any other return is assumed to be an error and
942 * the device is taken offline. [offline devices will _not_ have
943 * sdev_destroy() called on them so clean up resources.]
944 *
945 * Locks: none
946 *
947 * Calling context: process
948 *
949 * Notes: Allows the driver to inspect the response to the initial
950 * INQUIRY done by the scanning code and take appropriate action.
951 * For more details see the include/scsi/scsi_host.h file.
952 *
953 * Optionally defined in: LLD
954 **/
955 int sdev_configure(struct scsi_device *sdp)
956
957
958 /**
959 * sdev_destroy - given device is about to be shut down. All
960 * activity has ceased on this device.
961 * @sdp: device that is about to be shut down
962 *
963 * Returns nothing
964 *
965 * Locks: none
966 *
967 * Calling context: process
968 *
969 * Notes: Mid level structures for given device are still in place
970 * but are about to be torn down. Any per device resources allocated
971 * by this driver for given device should be freed now. No further
972 * commands will be sent for this sdp instance. [However the device
973 * could be re-attached in the future in which case a new instance
974 * of struct scsi_device would be supplied by future sdev_init()
975 * and sdev_configure() calls.]
976 *
977 * Optionally defined in: LLD
978 **/
979 void sdev_destroy(struct scsi_device *sdp)
980
981
982
983 Data Structures
984 ===============
985 struct scsi_host_template
986 -------------------------
987 There is one "struct scsi_host_template" instance per LLD [#]_. It is
988 typically initialized as a file scope static in a driver's header file. That
989 way members that are not explicitly initialized will be set to 0 or NULL.
990 Members of interest:
991
992 name
993 - name of driver (may contain spaces, please limit to
994 less than 80 characters)
995
996 proc_name
997 - name used in "/proc/scsi/<proc_name>/<host_no>" and
998 by sysfs in one of its "drivers" directories. Hence
999 "proc_name" should only contain characters acceptable
1000 to a Unix file name.
1001
1002 ``(*queuecommand)()``
1003 - primary callback that the mid level uses to inject
1004 SCSI commands into an LLD.
1005
1006 vendor_id
1007 - a unique value that identifies the vendor supplying
1008 the LLD for the Scsi_Host. Used most often in validating
1009 vendor-specific message requests. Value consists of an
1010 identifier type and a vendor-specific value.
1011 See scsi_netlink.h for a description of valid formats.
1012
1013 The structure is defined and commented in include/scsi/scsi_host.h
1014
1015 .. [#] In extreme situations a single driver may have several instances
1016 if it controls several different classes of hardware (e.g. an LLD
1017 that handles both ISA and PCI cards and has a separate instance of
1018 struct scsi_host_template for each class).
1019
1020 struct Scsi_Host
1021 ----------------
1022 There is one struct Scsi_Host instance per host (HBA) that an LLD
1023 controls. The struct Scsi_Host structure has many members in common
1024 with "struct scsi_host_template". When a new struct Scsi_Host instance
1025 is created (in scsi_host_alloc() in hosts.c) those common members are
1026 initialized from the driver's struct scsi_host_template instance. Members
1027 of interest:
1028
1029 host_no
1030 - system-wide unique number that is used for identifying
1031 this host. Issued in ascending order from 0.
1032 can_queue
1033 - must be greater than 0; do not send more than can_queue
1034 commands to the adapter.
1035 this_id
1036 - scsi id of host (scsi initiator) or -1 if not known
1037 sg_tablesize
1038 - maximum scatter gather elements allowed by host.
1039 Set this to SG_ALL or less to avoid chained SG lists.
1040 Must be at least 1.
1041 max_sectors
1042 - maximum number of sectors (usually 512 bytes) allowed
1043 in a single SCSI command. The default value of 0 leads
1044 to a setting of SCSI_DEFAULT_MAX_SECTORS (defined in
1045 scsi_host.h) which is currently set to 1024. So for a
1046 disk the maximum transfer size is 512 KB when max_sectors
1047 is not defined. Note that this size may not be sufficient
1048 for disk firmware uploads.
1049 cmd_per_lun
1050 - maximum number of commands that can be queued on devices
1051 controlled by the host. Overridden by LLD calls to
1052 scsi_change_queue_depth().
1053 hostt
1054 - pointer to driver's struct scsi_host_template from which
1055 this struct Scsi_Host instance was spawned
1056 hostt->proc_name
1057 - name of LLD. This is the driver name that sysfs uses.
1058 transportt
1059 - pointer to driver's struct scsi_transport_template instance
1060 (if any). FC and SPI transports currently supported.
1061 hostdata[0]
1062 - area reserved for LLD at end of struct Scsi_Host. Size
1063 is set by the second argument (named 'privsize') to
1064 scsi_host_alloc().
1065
1066 The scsi_host structure is defined in include/scsi/scsi_host.h
1067
1068 struct scsi_device
1069 ------------------
1070 Generally, there is one instance of this structure for each SCSI logical unit
1071 on a host. SCSI devices connected to a host are uniquely identified by a
1072 channel number, target id and logical unit number (lun).
1073 The structure is defined in include/scsi/scsi_device.h
1074
1075 struct scsi_cmnd
1076 ----------------
1077 Instances of this structure convey SCSI commands to the LLD and responses
1078 back to the mid level. The SCSI mid level will ensure that no more SCSI
1079 commands become queued against the LLD than are indicated by
1080 scsi_change_queue_depth() (or struct Scsi_Host::cmd_per_lun). There will
1081 be at least one instance of struct scsi_cmnd available for each SCSI device.
1082 Members of interest:
1083
1084 cmnd
1085 - array containing SCSI command
1086 cmd_len
1087 - length (in bytes) of SCSI command
1088 sc_data_direction
1089 - direction of data transfer in data phase. See
1090 "enum dma_data_direction" in include/linux/dma-mapping.h
1091 result
1092 - should be set by LLD prior to calling 'done'. A value
1093 of 0 implies a successfully completed command (and all
1094 data (if any) has been transferred to or from the SCSI
1095 target device). 'result' is a 32-bit unsigned integer that
1096 can be viewed as 2 related bytes. The SCSI status value is
1097 in the LSB. See include/scsi/scsi.h status_byte() and
1098 host_byte() macros and related constants.
1099 sense_buffer
1100 - an array (maximum size: SCSI_SENSE_BUFFERSIZE bytes) that
1101 should be written when the SCSI status (LSB of 'result')
1102 is set to CHECK_CONDITION (2). When CHECK_CONDITION is
1103 set, if the top nibble of sense_buffer[0] has the value 7
1104 then the mid level will assume the sense_buffer array
1105 contains a valid SCSI sense buffer; otherwise the mid
1106 level will issue a REQUEST_SENSE SCSI command to
1107 retrieve the sense buffer. The latter strategy is error
1108 prone in the presence of command queuing so the LLD should
1109 always "auto-sense".
1110 device
1111 - pointer to scsi_device object that this command is
1112 associated with.
1113 resid_len (access by calling scsi_set_resid() / scsi_get_resid())
1114 - an LLD should set this unsigned integer to the requested
1115 transfer length (i.e. 'request_bufflen') less the number
1116 of bytes that are actually transferred. 'resid_len' is
1117 preset to 0 so an LLD can ignore it if it cannot detect
1118 underruns (overruns should not be reported). An LLD
1119 should set 'resid_len' prior to invoking 'done'. The most
1120 interesting case is data transfers from a SCSI target
1121 device (e.g. READs) that underrun.
1122 underflow
1123 - LLD should place (DID_ERROR << 16) in 'result' if
1124 actual number of bytes transferred is less than this
1125 figure. Not many LLDs implement this check and some that
1126 do just output an error message to the log rather than
1127 report a DID_ERROR. Better for an LLD to implement
1128 'resid_len'.
1129
1130 It is recommended that a LLD set 'resid_len' on data transfers from a SCSI
1131 target device (e.g. READs). It is especially important that 'resid_len' is set
1132 when such data transfers have sense keys of MEDIUM ERROR and HARDWARE ERROR
1133 (and possibly RECOVERED ERROR). In these cases if a LLD is in doubt how much
1134 data has been received then the safest approach is to indicate no bytes have
1135 been received. For example: to indicate that no valid data has been received
1136 a LLD might use these helpers::
1137
1138 scsi_set_resid(SCpnt, scsi_bufflen(SCpnt));
1139
1140 where 'SCpnt' is a pointer to a scsi_cmnd object. To indicate only three 512
1141 bytes blocks have been received 'resid_len' could be set like this::
1142
1143 scsi_set_resid(SCpnt, scsi_bufflen(SCpnt) - (3 * 512));
1144
1145 The scsi_cmnd structure is defined in include/scsi/scsi_cmnd.h
1146
1147
1148 Locks
1149 =====
1150 Each struct Scsi_Host instance has a spin_lock called struct
1151 Scsi_Host::default_lock which is initialized in scsi_host_alloc() [found in
1152 hosts.c]. Within the same function the struct Scsi_Host::host_lock pointer
1153 is initialized to point at default_lock. Thereafter lock and unlock
1154 operations performed by the mid level use the struct Scsi_Host::host_lock
1155 pointer. Previously drivers could override the host_lock pointer but
1156 this is not allowed anymore.
1157
1158
1159 Autosense
1160 =========
1161 Autosense (or auto-sense) is defined in the SAM-2 document as "the
1162 automatic return of sense data to the application client coincident
1163 with the completion of a SCSI command" when a status of CHECK CONDITION
1164 occurs. LLDs should perform autosense. This should be done when the LLD
1165 detects a CHECK CONDITION status by either:
1166
1167 a) instructing the SCSI protocol (e.g. SCSI Parallel Interface (SPI))
1168 to perform an extra data in phase on such responses
1169 b) or, the LLD issuing a REQUEST SENSE command itself
1170
1171 Either way, when a status of CHECK CONDITION is detected, the mid level
1172 decides whether the LLD has performed autosense by checking struct
1173 scsi_cmnd::sense_buffer[0] . If this byte has an upper nibble of 7 (or 0xf)
1174 then autosense is assumed to have taken place. If it has another value (and
1175 this byte is initialized to 0 before each command) then the mid level will
1176 issue a REQUEST SENSE command.
1177
1178 In the presence of queued commands the "nexus" that maintains sense
1179 buffer data from the command that failed until a following REQUEST SENSE
1180 may get out of synchronization. This is why it is best for the LLD
1181 to perform autosense.
1182
1183
1184 Changes since Linux kernel 2.4 series
1185 =====================================
1186 io_request_lock has been replaced by several finer grained locks. The lock
1187 relevant to LLDs is struct Scsi_Host::host_lock and there is
1188 one per SCSI host.
1189
1190 The older error handling mechanism has been removed. This means the
1191 LLD interface functions abort() and reset() have been removed.
1192 The struct scsi_host_template::use_new_eh_code flag has been removed.
1193
1194 In the 2.4 series the SCSI subsystem configuration descriptions were
1195 aggregated with the configuration descriptions from all other Linux
1196 subsystems in the Documentation/Configure.help file. In the 2.6 series,
1197 the SCSI subsystem now has its own (much smaller) drivers/scsi/Kconfig
1198 file that contains both configuration and help information.
1199
1200 struct SHT has been renamed to struct scsi_host_template.
1201
1202 Addition of the "hotplug initialization model" and many extra functions
1203 to support it.
1204
1205
1206 Credits
1207 =======
1208 The following people have contributed to this document:
1209
1210 - Mike Anderson <andmike at us dot ibm dot com>
1211 - James Bottomley <James dot Bottomley at hansenpartnership dot com>
1212 - Patrick Mansfield <patmans at us dot ibm dot com>
1213 - Christoph Hellwig <hch at infradead dot org>
1214 - Doug Ledford <dledford at redhat dot com>
1215 - Andries Brouwer <Andries dot Brouwer at cwi dot nl>
1216 - Randy Dunlap <rdunlap at xenotime dot net>
1217 - Alan Stern <stern at rowland dot harvard dot edu>
1218
1219
1220 Douglas Gilbert
1221 dgilbert at interlog dot com
1222
1223 21st September 2004