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