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