ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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.0-test4.
39
40 Documentation
41 =============
42 There is a SCSI documentation directory within the kernel source tree, 
43 typically /usr/src/linux/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 ^^ struct Scsi_Host actually has 2 reference counts which are manipulated
327 in parallel by these functions.
328
329
330 Conventions
331 ===========
332 First, Linus Torvalds's thoughts on C coding style can be found in the
333 Documentation/CodingStyle file. 
334
335 Next, there is a movement to "outlaw" typedefs introducing synonyms for 
336 struct tags. Both can be still found in the SCSI subsystem, but
337 the typedefs have been moved to a single file, scsi_typedefs.h to
338 make their future removal easier, for example: 
339 "typedef struct scsi_host_template Scsi_Host_Template;"
340
341 Also, most C99 enhancements are encouraged to the extent they are supported
342 by the relevant gcc compilers. So C99 style structure and array
343 initializers are encouraged where appropriate. Don't go too far,
344 VLAs are not properly supported yet.  An exception to this is the use of
345 "//" style comments; /*...*/ comments are still preferred in Linux.
346
347 Well written, tested and documented code, need not be re-formatted to
348 comply with the above conventions. For example, the aic7xxx driver
349 comes to Linux from FreeBSD and Adaptec's own labs. No doubt FreeBSD
350 and Adaptec have their own coding conventions.
351
352
353 Mid level supplied functions
354 ============================
355 These functions are supplied by the SCSI mid level for use by LLDs.
356 The names (i.e. entry points) of these functions are exported (mainly in 
357 scsi_syms.c) so an LLD that is a module can access them. The kernel will
358 arrange for the SCSI mid level to be loaded and initialized before any LLD
359 is initialized. The functions below are listed alphabetically and their
360 names all start with "scsi_".
361
362 Summary:
363    scsi_add_device - creates new scsi device (lu) instance
364    scsi_add_host - perform sysfs registration and SCSI bus scan.
365    scsi_add_timer - (re-)start timer on a SCSI command.
366    scsi_adjust_queue_depth - change the queue depth on a SCSI device
367    scsi_assign_lock - replace default host_lock with given lock
368    scsi_bios_ptable - return copy of block device's partition table
369    scsi_block_requests - prevent further commands being queued to given host
370    scsi_delete_timer - cancel timer on a SCSI command.
371    scsi_host_alloc - return a new scsi_host instance whose refcount==1
372    scsi_host_get - increments Scsi_Host instance's refcount
373    scsi_host_put - decrements Scsi_Host instance's refcount (free if 0)
374    scsi_partsize - parse partition table into cylinders, heads + sectors
375    scsi_register - create and register a scsi host adapter instance.
376    scsi_remove_device - detach and remove a SCSI device
377    scsi_remove_host - detach and remove all SCSI devices owned by host
378    scsi_report_bus_reset - report scsi _bus_ reset observed
379    scsi_set_device - place device reference in host structure
380    scsi_to_pci_dma_dir - convert SCSI subsystem direction flag to PCI
381    scsi_to_sbus_dma_dir - convert SCSI subsystem direction flag to SBUS
382    scsi_track_queue_full - track successive QUEUE_FULL events 
383    scsi_unblock_requests - allow further commands to be queued to given host
384    scsi_unregister - [calls scsi_host_put()]
385
386
387 Details:
388
389 /**
390  * scsi_add_device - creates new scsi device (lu) instance
391  * @shost:   pointer to scsi host instance
392  * @channel: channel number (rarely other than 0)
393  * @id:      target id number
394  * @lun:     logical unit number
395  *
396  *      Returns pointer to new struct scsi_device instance or 
397  *      ERR_PTR(-ENODEV) (or some other bent pointer) if something is
398  *      wrong (e.g. no lu responds at given address)
399  *
400  *      Might block: yes
401  *
402  *      Notes: This call is usually performed internally during a scsi
403  *      bus scan when an HBA is added (i.e. scsi_add_host()). So it
404  *      should only be called if the HBA becomes aware of a new scsi
405  *      device (lu) after scsi_add_host() has completed. If successful
406  *      this call we lead to slave_alloc() and slave_configure() callbacks
407  *      into the LLD.
408  *
409  *      Defined in: drivers/scsi/scsi_scan.c
410  **/
411 struct scsi_device * scsi_add_device(struct Scsi_Host *shost, 
412                                      unsigned int channel,
413                                      unsigned int id, unsigned int lun)
414
415
416 /**
417  * scsi_add_host - perform sysfs registration and SCSI bus scan.
418  * @shost:   pointer to scsi host instance
419  * @dev:     pointer to struct device host instance
420  *
421  *      Returns 0 on success, negative errno of failure (e.g. -ENOMEM)
422  *
423  *      Might block: no
424  *
425  *      Notes: Only required in "hotplug initialization model" after a
426  *      successful call to scsi_host_alloc().
427  *
428  *      Defined in: drivers/scsi/hosts.c
429  **/
430 int scsi_add_host(struct Scsi_Host *shost, struct device * dev)
431
432
433 /**
434  * scsi_add_timer - (re-)start timer on a SCSI command.
435  * @scmd:    pointer to scsi command instance
436  * @timeout: duration of timeout in "jiffies"
437  * @complete: pointer to function to call if timeout expires
438  *
439  *      Returns nothing
440  *
441  *      Might block: no
442  *
443  *      Notes: Each scsi command has its own timer, and as it is added
444  *      to the queue, we set up the timer. When the command completes, 
445  *      we cancel the timer. An LLD can use this function to change
446  *      the existing timeout value.
447  *
448  *      Defined in: drivers/scsi/scsi_error.c
449  **/
450 void scsi_add_timer(struct scsi_cmnd *scmd, int timeout, 
451                     void (*complete)(struct scsi_cmnd *))
452
453
454 /**
455  * scsi_adjust_queue_depth - change the queue depth on a SCSI device
456  * @SDpnt:      pointer to SCSI device to change queue depth on
457  * @tagged:     0 - no tagged queuing
458  *              MSG_SIMPLE_TAG - simple (unordered) tagged queuing
459  *              MSG_ORDERED_TAG - ordered tagged queuing
460  * @tags        Number of tags allowed if tagged queuing enabled,
461  *              or number of commands the LLD can queue up
462  *              in non-tagged mode (as per cmd_per_lun).
463  *
464  *      Returns nothing
465  *
466  *      Might block: no
467  *
468  *      Notes: Can be invoked any time on a SCSI device controlled by this
469  *      LLD. [Specifically during and after slave_configure() and prior to
470  *      slave_destroy().] Can safely be invoked from interrupt code. Actual
471  *      queue depth change may be delayed until the next command is being
472  *      processed.
473  *
474  *      Defined in: drivers/scsi/scsi.c [see source code for more notes]
475  *
476  **/
477 void scsi_adjust_queue_depth(struct scsi_device * SDpnt, int tagged, 
478                              int num_tags)
479
480
481 /**
482  * scsi_assign_lock - replace default host_lock with given lock
483  * @shost: a pointer to a scsi host instance
484  * @lock: pointer to lock to replace host_lock for this host
485  *
486  *      Returns nothing
487  *
488  *      Might block: no
489  *
490  *      Defined in: include/scsi/scsi_host.h .
491  **/
492 void scsi_assign_lock(struct Scsi_Host *shost, spinlock_t *lock)
493
494
495 /**
496  * scsi_bios_ptable - return copy of block device's partition table
497  * @dev:        pointer to block device
498  *
499  *      Returns pointer to partition table, or NULL for failure
500  *
501  *      Might block: yes
502  *
503  *      Notes: Caller owns memory returned (free with kfree() )
504  *
505  *      Defined in: drivers/scsi/scsicam.c
506  **/
507 unsigned char *scsi_bios_ptable(struct block_device *dev)
508
509
510 /**
511  * scsi_block_requests - prevent further commands being queued to given host
512  *
513  * @SHpnt: pointer to host to block commands on
514  *
515  *      Returns nothing
516  *
517  *      Might block: no
518  *
519  *      Notes: There is no timer nor any other means by which the requests
520  *      get unblocked other than the LLD calling scsi_unblock_requests().
521  *
522  *      Defined in: drivers/scsi/scsi_lib.c
523 **/
524 void scsi_block_requests(struct Scsi_Host * SHpnt)
525
526
527 /**
528  * scsi_delete_timer - cancel timer on a SCSI command.
529  * @scmd:    pointer to scsi command instance
530  *
531  *      Returns 1 if able to cancel timer else 0 (i.e. too late or already
532  *      cancelled).
533  *
534  *      Might block: no [may in the future if it invokes del_timer_sync()]
535  *
536  *      Notes: All commands issued by upper levels already have a timeout
537  *      associated with them. An LLD can use this function to cancel the
538  *      timer.
539  *
540  *      Defined in: drivers/scsi/scsi_error.c
541  **/
542 int scsi_delete_timer(struct scsi_cmnd *scmd)
543
544
545 /**
546  * scsi_host_alloc - create and register a scsi host adapter instance.
547  * @shost_tp:   pointer to scsi host template
548  * @xtr_bytes:  extra bytes to allocate in hostdata array (which is the
549  *              last member of the returned Scsi_Host instance)
550  *
551  *      Returns pointer to new Scsi_Host instance or NULL on failure
552  *
553  *      Might block: yes
554  *
555  *      Notes: When this call returns to the LLD, the SCSI bus scan on
556  *      this host has _not_ yet been done.
557  *      The hostdata array (by default zero length) is a per host scratch 
558  *      area for the LLD.
559  *      Both associated refcounting objects have their refcount set to 1.
560  *
561  *      Defined in: drivers/scsi/hosts.c .
562  **/
563 struct Scsi_Host * scsi_host_alloc(struct scsi_host_template *, int xtr_bytes)
564
565
566 /**
567  * scsi_host_get - increment Scsi_Host instance refcount
568  * @shost:   pointer to struct Scsi_Host instance
569  *
570  *      Returns nothing
571  *
572  *      Might block: currently may block but may be changed to not block
573  *
574  *      Notes: Actually increments the counts in two sub-objects
575  *
576  *      Defined in: drivers/scsi/hosts.c
577  **/
578 void scsi_host_get(struct Scsi_Host *shost)
579
580
581 /**
582  * scsi_host_put - decrement Scsi_Host instance refcount, free if 0
583  * @shost:   pointer to struct Scsi_Host instance
584  *
585  *      Returns nothing
586  *
587  *      Might block: currently may block but may be changed to not block
588  *
589  *      Notes: Actually decrements the counts in two sub-objects. If the
590  *      latter refcount reaches 0, the Scsi_Host instance is freed.
591  *      The LLD need not worry exactly when the Scsi_Host instance is
592  *      freed, it just shouldn't access the instance after it has balanced
593  *      out its refcount usage.
594  *
595  *      Defined in: drivers/scsi/hosts.c
596  **/
597 void scsi_host_put(struct Scsi_Host *shost)
598
599
600 /**
601  * scsi_partsize - parse partition table into cylinders, heads + sectors
602  * @buf: pointer to partition table
603  * @capacity: size of (total) disk in 512 byte sectors
604  * @cyls: outputs number of cylinders calculated via this pointer
605  * @hds: outputs number of heads calculated via this pointer
606  * @secs: outputs number of sectors calculated via this pointer
607  *
608  *      Returns 0 on success, -1 on failure
609  *
610  *      Might block: no
611  *
612  *      Notes: Caller owns memory returned (free with kfree() )
613  *
614  *      Defined in: drivers/scsi/scsicam.c
615  **/
616 int scsi_partsize(unsigned char *buf, unsigned long capacity,
617                   unsigned int *cyls, unsigned int *hds, unsigned int *secs)
618
619
620 /**
621  * scsi_register - create and register a scsi host adapter instance.
622  * @shost_tp:   pointer to scsi host template
623  * @xtr_bytes:  extra bytes to allocate in hostdata array (which is the
624  *              last member of the returned Scsi_Host instance)
625  *
626  *      Returns pointer to new Scsi_Host instance or NULL on failure
627  *
628  *      Might block: yes
629  *
630  *      Notes: When this call returns to the LLD, the SCSI bus scan on
631  *      this host has _not_ yet been done.
632  *      The hostdata array (by default zero length) is a per host scratch 
633  *      area for the LLD.
634  *
635  *      Defined in: drivers/scsi/hosts.c .
636  **/
637 struct Scsi_Host * scsi_register(struct scsi_host_template *, int xtr_bytes)
638
639
640 /**
641  * scsi_remove_device - detach and remove a SCSI device
642  * @sdev:      a pointer to a scsi device instance
643  *
644  *      Returns value: 0 on success, -EINVAL if device not attached
645  *
646  *      Might block: yes
647  *
648  *      Notes: If an LLD becomes aware that a scsi device (lu) has
649  *      been removed but its host is still present then it can request
650  *      the removal of that scsi device. If successful this call will
651  *      lead to the slave_destroy() callback being invoked. sdev is an 
652  *      invalid pointer after this call.
653  *
654  *      Defined in: drivers/scsi/scsi_sysfs.c .
655  **/
656 int scsi_remove_device(struct scsi_device *sdev)
657
658
659 /**
660  * scsi_remove_host - detach and remove all SCSI devices owned by host
661  * @shost:      a pointer to a scsi host instance
662  *
663  *      Returns value: 0 on success, 1 on failure (e.g. LLD busy ??)
664  *
665  *      Might block: yes
666  *
667  *      Notes: Should only be invoked if the "hotplug initialization
668  *      model" is being used. It should be called _prior_ to  
669  *      scsi_unregister().
670  *
671  *      Defined in: drivers/scsi/hosts.c .
672  **/
673 int scsi_remove_host(struct Scsi_Host *shost)
674
675
676 /**
677  * scsi_report_bus_reset - report scsi _bus_ reset observed
678  * @shost: a pointer to a scsi host involved
679  * @channel: channel (within) host on which scsi bus reset occurred
680  *
681  *      Returns nothing
682  *
683  *      Might block: no
684  *
685  *      Notes: This only needs to be called if the reset is one which
686  *      originates from an unknown location.  Resets originated by the 
687  *      mid level itself don't need to call this, but there should be 
688  *      no harm.  The main purpose of this is to make sure that a
689  *      CHECK_CONDITION is properly treated.
690  *
691  *      Defined in: drivers/scsi/scsi_error.c .
692  **/
693 void scsi_report_bus_reset(struct Scsi_Host * shost, int channel)
694
695
696 /**
697  * scsi_set_device - place device reference in host structure
698  * @shost: a pointer to a scsi host instance
699  * @pdev: pointer to device instance to assign
700  *
701  *      Returns nothing
702  *
703  *      Might block: no
704  *
705  *      Defined in: include/scsi/scsi_host.h .
706  **/
707 void scsi_set_device(struct Scsi_Host * shost, struct device * dev)
708
709
710 /**
711  * scsi_to_pci_dma_dir - convert SCSI subsystem direction flag to PCI
712  * @scsi_data_direction: SCSI subsystem direction flag
713  *
714  *      Returns PCI_DMA_TODEVICE given SCSI_DATA_WRITE,
715  *              PCI_DMA_FROMDEVICE given SCSI_DATA_READ
716  *              PCI_DMA_BIDIRECTIONAL given SCSI_DATA_UNKNOWN
717  *              else returns PCI_DMA_NONE
718  *
719  *      Might block: no
720  *
721  *      Notes: The SCSI subsystem now uses the same values for these
722  *      constants as the PCI subsystem so this function is a nop.
723  *
724  *      Defined in: drivers/scsi/scsi.h .
725  **/
726 int scsi_to_pci_dma_dir(unsigned char scsi_data_direction)
727
728
729 /**
730  * scsi_to_sbus_dma_dir - convert SCSI subsystem direction flag to SBUS
731  * @scsi_data_direction: SCSI subsystem direction flag
732  *
733  *      Returns SBUS_DMA_TODEVICE given SCSI_DATA_WRITE,
734  *              SBUS_DMA_FROMDEVICE given SCSI_DATA_READ
735  *              SBUS_DMA_BIDIRECTIONAL given SCSI_DATA_UNKNOWN
736  *              else returns SBUS_DMA_NONE
737  *
738  *      Notes: The SCSI subsystem now uses the same values for these
739  *      constants as the SBUS subsystem so this function is a nop.
740  *
741  *      Might block: no
742  *
743  *      Defined in: drivers/scsi/scsi.h .
744  **/
745 int scsi_to_sbus_dma_dir(unsigned char scsi_data_direction)
746
747
748 /**
749  * scsi_track_queue_full - track successive QUEUE_FULL events on given
750  *                      device to determine if and when there is a need
751  *                      to adjust the queue depth on the device.
752  * @SDptr: pointer to SCSI device instance
753  * @depth: Current number of outstanding SCSI commands on this device,
754  *         not counting the one returned as QUEUE_FULL.
755  *
756  *      Returns 0  - no change needed
757  *              >0 - adjust queue depth to this new depth
758  *              -1 - drop back to untagged operation using host->cmd_per_lun
759  *                   as the untagged command depth
760  *
761  *      Might block: no
762  *
763  *      Notes: LLDs may call this at any time and we will do "The Right
764  *              Thing"; interrupt context safe. 
765  *
766  *      Defined in: drivers/scsi/scsi.c .
767  **/
768 int scsi_track_queue_full(Scsi_Device *SDptr, int depth)
769
770
771 /**
772  * scsi_unblock_requests - allow further commands to be queued to given host
773  *
774  * @SHpnt: pointer to host to unblock commands on
775  *
776  *      Returns nothing
777  *
778  *      Might block: no
779  *
780  *      Defined in: drivers/scsi/scsi_lib.c .
781 **/
782 void scsi_unblock_requests(struct Scsi_Host * SHpnt)
783
784
785 /**
786  * scsi_unregister - unregister and free memory used by host instance
787  * @shp:        pointer to scsi host instance to unregister.
788  *
789  *      Returns nothing
790  *
791  *      Might block: no
792  *
793  *      Notes: Should not be invoked if the "hotplug initialization
794  *      model" is being used. Called internally by exit_this_scsi_driver()
795  *      in the "passive initialization model". Hence a LLD has no need to
796  *      call this function directly.
797  *
798  *      Defined in: drivers/scsi/hosts.c .
799  **/
800 void scsi_unregister(struct Scsi_Host * shp)
801
802
803
804
805 Interface Functions
806 ===================
807 Interface functions are supplied (defined) by LLDs and their function
808 pointers are placed in an instance of struct scsi_host_template which
809 is passed to scsi_host_alloc() [or scsi_register() / init_this_scsi_driver()].
810 Some are mandatory. Interface functions should be declared static. The
811 accepted convention is that driver "xyz" will declare its slave_configure() 
812 function as:
813     static int xyz_slave_configure(struct scsi_device * sdev);
814 and so forth for all interface functions listed below.
815
816 A pointer to this function should be placed in the 'slave_configure' member
817 of a "struct scsi_host_template" instance. A pointer to such an instance
818 should be passed to the mid level's scsi_host_alloc() [or scsi_register() /
819 init_this_scsi_driver()].
820
821 The interface functions are also described in the include/scsi/scsi_host.h
822 file immediately above their definition point in "struct scsi_host_template".
823 In some cases more detail is given in scsi_host.h than below.
824
825 The interface functions are listed below in alphabetical order.
826
827 Summary:
828    bios_param - fetch head, sector, cylinder info for a disk
829    detect - detects HBAs this driver wants to control
830    eh_abort_handler - abort given command
831    eh_bus_reset_handler - issue SCSI bus reset
832    eh_device_reset_handler - issue SCSI device reset
833    eh_host_reset_handler - reset host (host bus adapter)
834    eh_strategy_handler - driver supplied alternate to scsi_unjam_host()
835    info - supply information about given host
836    ioctl - driver can respond to ioctls
837    proc_info - supports /proc/scsi/{driver_name}/{host_no}
838    queuecommand - queue scsi command, invoke 'done' on completion
839    release - release all resources associated with given host
840    slave_alloc - prior to any commands being sent to a new device 
841    slave_configure - driver fine tuning for given device after attach
842    slave_destroy - given device is about to be shut down
843
844
845 Details:
846
847 /**
848  *      bios_param - fetch head, sector, cylinder info for a disk
849  *      @sdev: pointer to scsi device context (defined in 
850  *             include/scsi/scsi_device.h)
851  *      @bdev: pointer to block device context (defined in fs.h)
852  *      @capacity:  device size (in 512 byte sectors)
853  *      @params: three element array to place output:
854  *              params[0] number of heads (max 255)
855  *              params[1] number of sectors (max 63)
856  *              params[2] number of cylinders 
857  *
858  *      Return value is ignored
859  *
860  *      Locks: none
861  *
862  *      Calling context: process (sd)
863  *
864  *      Notes: an arbitrary geometry (based on READ CAPACITY) is used
865  *      if this function is not provided. The params array is
866  *      pre-initialized with made up values just in case this function 
867  *      doesn't output anything.
868  *
869  *      Optionally defined in: LLD
870  **/
871     int bios_param(struct scsi_device * sdev, struct block_device *bdev,
872                    sector_t capacity, int params[3])
873
874
875 /**
876  *      detect - detects HBAs this driver wants to control
877  *      @shtp: host template for this driver.
878  *
879  *      Returns number of hosts this driver wants to control. 0 means no
880  *      suitable hosts found.
881  *
882  *      Locks: none held
883  *
884  *      Calling context: process [invoked from init_this_scsi_driver()]
885  *
886  *      Notes: First function called from the SCSI mid level on this
887  *      driver. Upper level drivers (e.g. sd) may not (yet) be present.
888  *      For each host found, this method should call scsi_register() 
889  *      [see hosts.c].
890  *
891  *      Defined in: LLD (required if "passive initialization mode" is used,
892  *                       not invoked in "hotplug initialization mode")
893  **/
894     int detect(struct scsi_host_template * shtp)
895
896
897 /**
898  *      eh_abort_handler - abort command associated with scp
899  *      @scp: identifies command to be aborted
900  *
901  *      Returns SUCCESS if command aborted else FAILED
902  *
903  *      Locks: struct Scsi_Host::host_lock held (with irqsave) on entry 
904  *      and assumed to be held on return.
905  *
906  *      Calling context: kernel thread
907  *
908  *      Notes: Invoked from scsi_eh thread. No other commands will be
909  *      queued on current host during eh.
910  *
911  *      Optionally defined in: LLD
912  **/
913      int eh_abort_handler(struct scsi_cmnd * scp)
914
915
916 /**
917  *      eh_bus_reset_handler - issue SCSI bus reset
918  *      @scp: SCSI bus that contains this device should be reset
919  *
920  *      Returns SUCCESS if command aborted else FAILED
921  *
922  *      Locks: struct Scsi_Host::host_lock held (with irqsave) on entry 
923  *      and assumed to be held on return.
924  *
925  *      Calling context: kernel thread
926  *
927  *      Notes: Invoked from scsi_eh thread. No other commands will be
928  *      queued on current host during eh.
929  *
930  *      Optionally defined in: LLD
931  **/
932      int eh_bus_reset_handler(struct scsi_cmnd * scp)
933
934
935 /**
936  *      eh_device_reset_handler - issue SCSI device reset
937  *      @scp: identifies SCSI device to be reset
938  *
939  *      Returns SUCCESS if command aborted else FAILED
940  *
941  *      Locks: struct Scsi_Host::host_lock held (with irqsave) on entry
942  *      and assumed to be held on return.
943  *
944  *      Calling context: kernel thread
945  *
946  *      Notes: Invoked from scsi_eh thread. No other commands will be
947  *      queued on current host during eh.
948  *
949  *      Optionally defined in: LLD
950  **/
951      int eh_device_reset_handler(struct scsi_cmnd * scp)
952
953
954 /**
955  *      eh_host_reset_handler - reset host (host bus adapter)
956  *      @scp: SCSI host that contains this device should be reset
957  *
958  *      Returns SUCCESS if command aborted else FAILED
959  *
960  *      Locks: struct Scsi_Host::host_lock held (with irqsave) on entry
961  *      and assumed to be held on return.
962  *
963  *      Calling context: kernel thread
964  *
965  *      Notes: Invoked from scsi_eh thread. No other commands will be
966  *      queued on current host during eh. 
967  *      With the default eh_strategy in place, if none of the _abort_, 
968  *      _device_reset_, _bus_reset_ or this eh handler function are 
969  *      defined (or they all return FAILED) then the device in question 
970  *      will be set offline whenever eh is invoked.
971  *
972  *      Optionally defined in: LLD
973  **/
974      int eh_host_reset_handler(struct scsi_cmnd * scp)
975
976
977 /**
978  *      eh_strategy_handler - driver supplied alternate to scsi_unjam_host()
979  *      @shp: host on which error has occurred
980  *
981  *      Returns TRUE if host unjammed, else FALSE.
982  *
983  *      Locks: none
984  *
985  *      Calling context: kernel thread
986  *
987  *      Notes: Invoked from scsi_eh thread. LLD supplied alternate to 
988  *      scsi_unjam_host() found in scsi_error.c
989  *
990  *      Optionally defined in: LLD
991  **/
992      int eh_strategy_handler(struct Scsi_Host * shp)
993
994
995 /**
996  *      info - supply information about given host: driver name plus data
997  *             to distinguish given host
998  *      @shp: host to supply information about
999  *
1000  *      Return ASCII null terminated string. [This driver is assumed to
1001  *      manage the memory pointed to and maintain it, typically for the
1002  *      lifetime of this host.]
1003  *
1004  *      Locks: none
1005  *
1006  *      Calling context: process
1007  *
1008  *      Notes: Often supplies PCI or ISA information such as IO addresses
1009  *      and interrupt numbers. If not supplied struct Scsi_Host::name used
1010  *      instead. It is assumed the returned information fits on one line 
1011  *      (i.e. does not included embedded newlines).
1012  *      The SCSI_IOCTL_PROBE_HOST ioctl yields the string returned by this
1013  *      function (or struct Scsi_Host::name if this function is not
1014  *      available).
1015  *      In a similar manner, init_this_scsi_driver() outputs to the console
1016  *      each host's "info" (or name) for the driver it is registering.
1017  *      Also if proc_info() is not supplied, the output of this function
1018  *      is used instead.
1019  *
1020  *      Optionally defined in: LLD
1021  **/
1022     const char * info(struct Scsi_Host * shp)
1023
1024
1025 /**
1026  *      ioctl - driver can respond to ioctls
1027  *      @sdp: device that ioctl was issued for
1028  *      @cmd: ioctl number
1029  *      @arg: pointer to read or write data from. Since it points to
1030  *            user space, should use appropriate kernel functions
1031  *            (e.g. copy_from_user() ). In the Unix style this argument
1032  *            can also be viewed as an unsigned long.
1033  *
1034  *      Returns negative "errno" value when there is a problem. 0 or a
1035  *      positive value indicates success and is returned to the user space.
1036  *
1037  *      Locks: none
1038  *
1039  *      Calling context: process
1040  *
1041  *      Notes: The SCSI subsystem uses a "trickle down" ioctl model.
1042  *      The user issues an ioctl() against an upper level driver
1043  *      (e.g. /dev/sdc) and if the upper level driver doesn't recognize
1044  *      the 'cmd' then it is passed to the SCSI mid level. If the SCSI
1045  *      mid level does not recognize it, then the LLD that controls
1046  *      the device receives the ioctl. According to recent Unix standards
1047  *      unsupported ioctl() 'cmd' numbers should return -ENOTTY.
1048  *      However the mid level returns -EINVAL for unrecognized 'cmd'
1049  *      numbers when this function is not supplied by the driver.
1050  *      Unfortunately some applications expect -EINVAL and react badly
1051  *      when -ENOTTY is returned; stick with -EINVAL.
1052  *
1053  *      Optionally defined in: LLD
1054  **/
1055     int ioctl(struct scsi_device *sdp, int cmd, void *arg)
1056
1057
1058 /**
1059  *      proc_info - supports /proc/scsi/{driver_name}/{host_no}
1060  *      @buffer: anchor point to output to (0==writeto1_read0) or fetch from
1061  *               (1==writeto1_read0).
1062  *      @start: where "interesting" data is written to. Ignored when
1063  *              1==writeto1_read0.
1064  *      @offset: offset within buffer 0==writeto1_read0 is actually
1065  *               interested in. Ignored when 1==writeto1_read0 .
1066  *      @length: maximum (or actual) extent of buffer
1067  *      @host_no: host number of interest (struct Scsi_Host::host_no)
1068  *      @writeto1_read0: 1 -> data coming from user space towards driver
1069  *                            (e.g. "echo some_string > /proc/scsi/xyz/2")
1070  *                       0 -> user what data from this driver
1071  *                            (e.g. "cat /proc/scsi/xyz/2")
1072  *
1073  *      Returns length when 1==writeto1_read0. Otherwise number of chars
1074  *      output to buffer past offset.
1075  *
1076  *      Locks: none held
1077  *
1078  *      Calling context: process
1079  *
1080  *      Notes: Driven from scsi_proc.c which interfaces to proc_fs. proc_fs
1081  *      support can now be configured out of the scsi subsystem.
1082  *
1083  *      Optionally defined in: LLD
1084  **/
1085     int proc_info(char * buffer, char ** start, off_t offset, 
1086                   int length, int hostno, int writeto1_read0)
1087
1088
1089 /**
1090  *      queuecommand - queue scsi command, invoke 'done' on completion
1091  *      @scp: pointer to scsi command object
1092  *      @done: function pointer to be invoked on completion
1093  *
1094  *      Returns 0 on success.
1095  *
1096  *      If there's a failure, return either:
1097  *
1098  *      SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
1099  *      SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
1100  *
1101  *      On both of these returns, the mid-layer will requeue the I/O
1102  *
1103  *      - if the return is SCSI_MLQUEUE_DEVICE_BUSY, only that particular
1104  *      device will be paused, and it will be unpaused when a command to
1105  *      the device returns (or after a brief delay if there are no more
1106  *      outstanding commands to it).  Commands to other devices continue
1107  *      to be processed normally.
1108  *
1109  *      - if the return is SCSI_MLQUEUE_HOST_BUSY, all I/O to the host
1110  *      is paused and will be unpaused when any command returns from
1111  *      the host (or after a brief delay if there are no outstanding
1112  *      commands to the host).
1113  *
1114  *      For compatibility with earlier versions of queuecommand, any
1115  *      other return value is treated the same as
1116  *      SCSI_MLQUEUE_HOST_BUSY.
1117  *
1118  *      Other types of errors that are detected immediately may be
1119  *      flagged by setting scp->result to an appropriate value,
1120  *      invoking the 'done' callback, and then returning 0 from this
1121  *      function. If the command is not performed immediately (and the
1122  *      LLD is starting (or will start) the given command) then this
1123  *      function should place 0 in scp->result and return 0.
1124  *
1125  *      Command ownership.  If the driver returns zero, it owns the
1126  *      command and must take responsibility for ensuring the 'done'
1127  *      callback is executed.  Note: the driver may call done before
1128  *      returning zero, but after it has called done, it may not
1129  *      return any value other than zero.  If the driver makes a
1130  *      non-zero return, it must not execute the command's done
1131  *      callback at any time.
1132  *
1133  *      Locks: struct Scsi_Host::host_lock held on entry (with "irqsave")
1134  *             and is expected to be held on return.
1135  *
1136  *      Calling context: in interrupt (soft irq) or process context
1137  *
1138  *      Notes: This function should be relatively fast. Normally it will
1139  *      not wait for IO to complete. Hence the 'done' callback is invoked 
1140  *      (often directly from an interrupt service routine) some time after
1141  *      this function has returned. In some cases (e.g. pseudo adapter 
1142  *      drivers that manufacture the response to a SCSI INQUIRY)
1143  *      the 'done' callback may be invoked before this function returns.
1144  *      If the 'done' callback is not invoked within a certain period
1145  *      the SCSI mid level will commence error processing.
1146  *      If a status of CHECK CONDITION is placed in "result" when the
1147  *      'done' callback is invoked, then the LLD driver should 
1148  *      perform autosense and fill in the struct scsi_cmnd::sense_buffer
1149  *      array. The scsi_cmnd::sense_buffer array is zeroed prior to
1150  *      the mid level queuing a command to an LLD.
1151  *
1152  *      Defined in: LLD
1153  **/
1154     int queuecommand(struct scsi_cmnd * scp, 
1155                      void (*done)(struct scsi_cmnd *))
1156
1157
1158 /**
1159  *      release - release all resources associated with given host
1160  *      @shp: host to be released.
1161  *
1162  *      Return value ignored (could soon be a function returning void).
1163  *
1164  *      Locks: none held
1165  *
1166  *      Calling context: process
1167  *
1168  *      Notes: Invoked from scsi_module.c's exit_this_scsi_driver().
1169  *      LLD's implementation of this function should call 
1170  *      scsi_unregister(shp) prior to returning.
1171  *      Only needed for old-style host templates.
1172  *
1173  *      Defined in: LLD (required in "passive initialization model",
1174  *                       should not be defined in hotplug model)
1175  **/
1176     int release(struct Scsi_Host * shp)
1177
1178
1179 /**
1180  *      slave_alloc -   prior to any commands being sent to a new device 
1181  *                      (i.e. just prior to scan) this call is made
1182  *      @sdp: pointer to new device (about to be scanned)
1183  *
1184  *      Returns 0 if ok. Any other return is assumed to be an error and
1185  *      the device is ignored.
1186  *
1187  *      Locks: none
1188  *
1189  *      Calling context: process
1190  *
1191  *      Notes: Allows the driver to allocate any resources for a device
1192  *      prior to its initial scan. The corresponding scsi device may not
1193  *      exist but the mid level is just about to scan for it (i.e. send
1194  *      and INQUIRY command plus ...). If a device is found then
1195  *      slave_configure() will be called while if a device is not found
1196  *      slave_destroy() is called.
1197  *      For more details see the include/scsi/scsi_host.h file.
1198  *
1199  *      Optionally defined in: LLD
1200  **/
1201     int slave_alloc(struct scsi_device *sdp)
1202
1203
1204 /**
1205  *      slave_configure - driver fine tuning for given device just after it
1206  *                     has been first scanned (i.e. it responded to an
1207  *                     INQUIRY)
1208  *      @sdp: device that has just been attached
1209  *
1210  *      Returns 0 if ok. Any other return is assumed to be an error and
1211  *      the device is taken offline. [offline devices will _not_ have
1212  *      slave_destroy() called on them so clean up resources.]
1213  *
1214  *      Locks: none
1215  *
1216  *      Calling context: process
1217  *
1218  *      Notes: Allows the driver to inspect the response to the initial
1219  *      INQUIRY done by the scanning code and take appropriate action.
1220  *      For more details see the include/scsi/scsi_host.h file.
1221  *
1222  *      Optionally defined in: LLD
1223  **/
1224     int slave_configure(struct scsi_device *sdp)
1225
1226
1227 /**
1228  *      slave_destroy - given device is about to be shut down. All
1229  *                      activity has ceased on this device.
1230  *      @sdp: device that is about to be shut down
1231  *
1232  *      Returns nothing
1233  *
1234  *      Locks: none
1235  *
1236  *      Calling context: process
1237  *
1238  *      Notes: Mid level structures for given device are still in place
1239  *      but are about to be torn down. Any per device resources allocated
1240  *      by this driver for given device should be freed now. No further
1241  *      commands will be sent for this sdp instance. [However the device
1242  *      could be re-attached in the future in which case a new instance
1243  *      of struct scsi_device would be supplied by future slave_alloc()
1244  *      and slave_configure() calls.]
1245  *
1246  *      Optionally defined in: LLD
1247  **/
1248     void slave_destroy(struct scsi_device *sdp)
1249
1250
1251
1252 Data Structures
1253 ===============
1254 struct scsi_host_template
1255 -------------------------
1256 There is one "struct scsi_host_template" instance per LLD ***. It is
1257 typically initialized as a file scope static in a driver's header file. That
1258 way members that are not explicitly initialized will be set to 0 or NULL.
1259 Member of interest:
1260     name         - name of driver (may contain spaces, please limit to
1261                    less than 80 characters)
1262     proc_name    - name used in "/proc/scsi/<proc_name>/<host_no>" and
1263                    by sysfs in one of its "drivers" directories. Hence
1264                    "proc_name" should only contain characters acceptable
1265                    to a Unix file name.
1266    (*queuecommand)() - primary callback that the mid level uses to inject
1267                    SCSI commands into an LLD.
1268 The structure is defined and commented in include/scsi/scsi_host.h
1269
1270 *** In extreme situations a single driver may have several instances
1271     if it controls several different classes of hardware (e.g. an LLD
1272     that handles both ISA and PCI cards and has a separate instance of
1273     struct scsi_host_template for each class).
1274
1275 struct Scsi_Host
1276 ----------------
1277 There is one struct Scsi_Host instance per host (HBA) that an LLD
1278 controls. The struct Scsi_Host structure has many members in common
1279 with "struct scsi_host_template". When a new struct Scsi_Host instance
1280 is created (in scsi_host_alloc() in hosts.c) those common members are
1281 initialized from the driver's struct scsi_host_template instance. Members
1282 of interest:
1283     host_no      - system wide unique number that is used for identifying
1284                    this host. Issued in ascending order from 0 (and the
1285                    positioning can be influenced by the scsihosts
1286                    kernel boot (or module) parameter)
1287     can_queue    - must be greater than 0; do not send more than can_queue
1288                    commands to the adapter.
1289     this_id      - scsi id of host (scsi initiator) or -1 if not known
1290     sg_tablesize - maximum scatter gather elements allowed by host.
1291                    0 implies scatter gather not supported by host
1292     max_sectors  - maximum number of sectors (usually 512 bytes) allowed
1293                    in a single SCSI command. 0 implies no maximum.
1294     cmd_per_lun  - maximum number of commands that can be queued on devices
1295                    controlled by the host. Overridden by LLD calls to
1296                    scsi_adjust_queue_depth().
1297     unchecked_isa_dma - 1=>only use bottom 16 MB of ram (ISA DMA addressing
1298                    restriction), 0=>can use full 32 bit (or better) DMA
1299                    address space
1300     use_clustering - 1=>SCSI commands in mid level's queue can be merged,
1301                      0=>disallow SCSI command merging
1302     hostt        - pointer to driver's struct scsi_host_template from which
1303                    this struct Scsi_Host instance was spawned
1304     sh_list      - a double linked list of pointers to all struct Scsi_Host
1305                    instances (currently ordered by ascending host_no)
1306     my_devices   - a double linked list of pointers to struct scsi_device 
1307                    instances that belong to this host.
1308     hostdata[0]  - area reserved for LLD at end of struct Scsi_Host. Size
1309                    is set by the second argument (named 'xtr_bytes') to
1310                    scsi_host_alloc() or scsi_register().
1311 The structure is defined in include/scsi/scsi_host.h
1312
1313 struct scsi_device
1314 ------------------
1315 Generally, there is one instance of this structure for each SCSI logical unit
1316 on a host. Scsi devices connected to a host are uniquely identified by a
1317 channel number, target id and logical unit number (lun).
1318 The structure is defined in include/scsi/scsi_device.h
1319
1320 struct scsi_cmnd
1321 ----------------
1322 Instances of this structure convey SCSI commands to the LLD and responses
1323 back to the mid level. The SCSI mid level will ensure that no more SCSI 
1324 commands become queued against the LLD than are indicated by 
1325 scsi_adjust_queue_depth() (or struct Scsi_Host::cmd_per_lun). There will 
1326 be at least one instance of struct scsi_cmnd available for each SCSI device. 
1327 The structure is defined in include/scsi/scsi_cmnd.h
1328
1329
1330 Locks
1331 =====
1332 Each struct Scsi_Host instance has a spin_lock called struct 
1333 Scsi_Host::default_lock which is initialized in scsi_host_alloc() [found in 
1334 hosts.c]. Within the same function the struct Scsi_Host::host_lock pointer
1335 is initialized to point at default_lock with the scsi_assign_lock() function.
1336 Thereafter lock and unlock operations performed by the mid level use the
1337 struct Scsi_Host::host_lock pointer.
1338
1339 LLDs can override the use of struct Scsi_Host::default_lock by
1340 using scsi_assign_lock(). The earliest opportunity to do this would
1341 be in the detect() function after it has invoked scsi_register(). It
1342 could be replaced by a coarser grain lock (e.g. per driver) or a
1343 lock of equal granularity (i.e. per host). Using finer grain locks 
1344 (e.g. per SCSI device) may be possible by juggling locks in
1345 queuecommand().
1346
1347 Autosense
1348 =========
1349 Autosense (or auto-sense) is defined in the SAM-2 document as "the
1350 automatic return of sense data to the application client coincident
1351 with the completion of a SCSI command" when a status of CHECK CONDITION
1352 occurs. LLDs should perform autosense. This should be done when the LLD
1353 detects a CHECK CONDITION status by either: 
1354     a) instructing the SCSI protocol (e.g. SCSI Parallel Interface (SPI))
1355        to perform an extra data in phase on such responses
1356     b) or, the LLD issuing a REQUEST SENSE command itself
1357
1358 Either way, when a status of CHECK CONDITION is detected, the mid level
1359 decides whether the LLD has performed autosense by checking struct 
1360 scsi_cmnd::sense_buffer[0] . If this byte has an upper nibble of 7 (or 0xf)
1361 then autosense is assumed to have taken place. If it has another value (and
1362 this byte is initialized to 0 before each command) then the mid level will
1363 issue a REQUEST SENSE command.
1364
1365 In the presence of queued commands the "nexus" that maintains sense
1366 buffer data from the command that failed until a following REQUEST SENSE
1367 may get out of synchronization. This is why it is best for the LLD
1368 to perform autosense.
1369
1370
1371 Changes since lk 2.4 series
1372 ===========================
1373 io_request_lock has been replaced by several finer grained locks. The lock 
1374 relevant to LLDs is struct Scsi_Host::host_lock and there is
1375 one per SCSI host.
1376
1377 The older error handling mechanism has been removed. This means the
1378 LLD interface functions abort() and reset() have been removed.
1379 The struct scsi_host_template::use_new_eh_code flag has been removed.
1380
1381 In the 2.4 series the SCSI subsystem configuration descriptions were 
1382 aggregated with the configuration descriptions from all other Linux 
1383 subsystems in the Documentation/Configure.help file. In the 2.6 series, 
1384 the SCSI subsystem now has its own (much smaller) drivers/scsi/Kconfig
1385 file that contains both configuration and help information.
1386
1387 struct SHT has been renamed to struct scsi_host_template.
1388
1389 Addition of the "hotplug initialization model" and many extra functions
1390 to support it.
1391
1392
1393 Credits
1394 =======
1395 The following people have contributed to this document:
1396         Mike Anderson <andmike@us.ibm.com>
1397         James Bottomley <James.Bottomley@steeleye.com> 
1398         Patrick Mansfield <patmans@us.ibm.com> 
1399         Christoph Hellwig <hch@infradead.org>
1400         Doug Ledford <dledford@redhat.com>
1401         Andries Brouwer <Andries.Brouwer@cwi.nl>
1402         Randy Dunlap <rddunlap@osdl.org>
1403
1404
1405 Douglas Gilbert
1406 dgilbert@interlog.com
1407 29th August 2003