scsi_mid_low_api.txt 59 KB

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