scsi_mid_low_api.rst 53 KB

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