scsi_mid_low_api.txt 60 KB

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