scsi_mid_low_api.txt 59 KB

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