scsi.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  1. /*
  2. * scsi.c Copyright (C) 1992 Drew Eckhardt
  3. * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
  4. * Copyright (C) 2002, 2003 Christoph Hellwig
  5. *
  6. * generic mid-level SCSI driver
  7. * Initial versions: Drew Eckhardt
  8. * Subsequent revisions: Eric Youngdale
  9. *
  10. * <drew@colorado.edu>
  11. *
  12. * Bug correction thanks go to :
  13. * Rik Faith <faith@cs.unc.edu>
  14. * Tommy Thorn <tthorn>
  15. * Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de>
  16. *
  17. * Modified by Eric Youngdale eric@andante.org or ericy@gnu.ai.mit.edu to
  18. * add scatter-gather, multiple outstanding request, and other
  19. * enhancements.
  20. *
  21. * Native multichannel, wide scsi, /proc/scsi and hot plugging
  22. * support added by Michael Neuffer <mike@i-connect.net>
  23. *
  24. * Added request_module("scsi_hostadapter") for kerneld:
  25. * (Put an "alias scsi_hostadapter your_hostadapter" in /etc/modprobe.conf)
  26. * Bjorn Ekwall <bj0rn@blox.se>
  27. * (changed to kmod)
  28. *
  29. * Major improvements to the timeout, abort, and reset processing,
  30. * as well as performance modifications for large queue depths by
  31. * Leonard N. Zubkoff <lnz@dandelion.com>
  32. *
  33. * Converted cli() code to spinlocks, Ingo Molnar
  34. *
  35. * Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli
  36. *
  37. * out_of_space hacks, D. Gilbert (dpg) 990608
  38. */
  39. #include <linux/module.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/kernel.h>
  42. #include <linux/timer.h>
  43. #include <linux/string.h>
  44. #include <linux/slab.h>
  45. #include <linux/blkdev.h>
  46. #include <linux/delay.h>
  47. #include <linux/init.h>
  48. #include <linux/completion.h>
  49. #include <linux/unistd.h>
  50. #include <linux/spinlock.h>
  51. #include <linux/kmod.h>
  52. #include <linux/interrupt.h>
  53. #include <linux/notifier.h>
  54. #include <linux/cpu.h>
  55. #include <linux/mutex.h>
  56. #include <scsi/scsi.h>
  57. #include <scsi/scsi_cmnd.h>
  58. #include <scsi/scsi_dbg.h>
  59. #include <scsi/scsi_device.h>
  60. #include <scsi/scsi_driver.h>
  61. #include <scsi/scsi_eh.h>
  62. #include <scsi/scsi_host.h>
  63. #include <scsi/scsi_tcq.h>
  64. #include "scsi_priv.h"
  65. #include "scsi_logging.h"
  66. static void scsi_done(struct scsi_cmnd *cmd);
  67. /*
  68. * Definitions and constants.
  69. */
  70. #define MIN_RESET_DELAY (2*HZ)
  71. /* Do not call reset on error if we just did a reset within 15 sec. */
  72. #define MIN_RESET_PERIOD (15*HZ)
  73. /*
  74. * Note - the initial logging level can be set here to log events at boot time.
  75. * After the system is up, you may enable logging via the /proc interface.
  76. */
  77. unsigned int scsi_logging_level;
  78. #if defined(CONFIG_SCSI_LOGGING)
  79. EXPORT_SYMBOL(scsi_logging_level);
  80. #endif
  81. /* NB: These are exposed through /proc/scsi/scsi and form part of the ABI.
  82. * You may not alter any existing entry (although adding new ones is
  83. * encouraged once assigned by ANSI/INCITS T10
  84. */
  85. static const char *const scsi_device_types[] = {
  86. "Direct-Access ",
  87. "Sequential-Access",
  88. "Printer ",
  89. "Processor ",
  90. "WORM ",
  91. "CD-ROM ",
  92. "Scanner ",
  93. "Optical Device ",
  94. "Medium Changer ",
  95. "Communications ",
  96. "ASC IT8 ",
  97. "ASC IT8 ",
  98. "RAID ",
  99. "Enclosure ",
  100. "Direct-Access-RBC",
  101. "Optical card ",
  102. "Bridge controller",
  103. "Object storage ",
  104. "Automation/Drive ",
  105. };
  106. /**
  107. * scsi_device_type - Return 17 char string indicating device type.
  108. * @type: type number to look up
  109. */
  110. const char * scsi_device_type(unsigned type)
  111. {
  112. if (type == 0x1e)
  113. return "Well-known LUN ";
  114. if (type == 0x1f)
  115. return "No Device ";
  116. if (type >= ARRAY_SIZE(scsi_device_types))
  117. return "Unknown ";
  118. return scsi_device_types[type];
  119. }
  120. EXPORT_SYMBOL(scsi_device_type);
  121. struct scsi_host_cmd_pool {
  122. struct kmem_cache *cmd_slab;
  123. struct kmem_cache *sense_slab;
  124. unsigned int users;
  125. char *cmd_name;
  126. char *sense_name;
  127. unsigned int slab_flags;
  128. gfp_t gfp_mask;
  129. };
  130. static struct scsi_host_cmd_pool scsi_cmd_pool = {
  131. .cmd_name = "scsi_cmd_cache",
  132. .sense_name = "scsi_sense_cache",
  133. .slab_flags = SLAB_HWCACHE_ALIGN,
  134. };
  135. static struct scsi_host_cmd_pool scsi_cmd_dma_pool = {
  136. .cmd_name = "scsi_cmd_cache(DMA)",
  137. .sense_name = "scsi_sense_cache(DMA)",
  138. .slab_flags = SLAB_HWCACHE_ALIGN|SLAB_CACHE_DMA,
  139. .gfp_mask = __GFP_DMA,
  140. };
  141. static DEFINE_MUTEX(host_cmd_pool_mutex);
  142. /**
  143. * scsi_pool_alloc_command - internal function to get a fully allocated command
  144. * @pool: slab pool to allocate the command from
  145. * @gfp_mask: mask for the allocation
  146. *
  147. * Returns a fully allocated command (with the allied sense buffer) or
  148. * NULL on failure
  149. */
  150. static struct scsi_cmnd *
  151. scsi_pool_alloc_command(struct scsi_host_cmd_pool *pool, gfp_t gfp_mask)
  152. {
  153. struct scsi_cmnd *cmd;
  154. cmd = kmem_cache_alloc(pool->cmd_slab, gfp_mask | pool->gfp_mask);
  155. if (!cmd)
  156. return NULL;
  157. memset(cmd, 0, sizeof(*cmd));
  158. cmd->sense_buffer = kmem_cache_alloc(pool->sense_slab,
  159. gfp_mask | pool->gfp_mask);
  160. if (!cmd->sense_buffer) {
  161. kmem_cache_free(pool->cmd_slab, cmd);
  162. return NULL;
  163. }
  164. return cmd;
  165. }
  166. /**
  167. * scsi_pool_free_command - internal function to release a command
  168. * @pool: slab pool to allocate the command from
  169. * @cmd: command to release
  170. *
  171. * the command must previously have been allocated by
  172. * scsi_pool_alloc_command.
  173. */
  174. static void
  175. scsi_pool_free_command(struct scsi_host_cmd_pool *pool,
  176. struct scsi_cmnd *cmd)
  177. {
  178. kmem_cache_free(pool->sense_slab, cmd->sense_buffer);
  179. kmem_cache_free(pool->cmd_slab, cmd);
  180. }
  181. /**
  182. * __scsi_get_command - Allocate a struct scsi_cmnd
  183. * @shost: host to transmit command
  184. * @gfp_mask: allocation mask
  185. *
  186. * Description: allocate a struct scsi_cmd from host's slab, recycling from the
  187. * host's free_list if necessary.
  188. */
  189. struct scsi_cmnd *__scsi_get_command(struct Scsi_Host *shost, gfp_t gfp_mask)
  190. {
  191. struct scsi_cmnd *cmd;
  192. unsigned char *buf;
  193. cmd = scsi_pool_alloc_command(shost->cmd_pool, gfp_mask);
  194. if (unlikely(!cmd)) {
  195. unsigned long flags;
  196. spin_lock_irqsave(&shost->free_list_lock, flags);
  197. if (likely(!list_empty(&shost->free_list))) {
  198. cmd = list_entry(shost->free_list.next,
  199. struct scsi_cmnd, list);
  200. list_del_init(&cmd->list);
  201. }
  202. spin_unlock_irqrestore(&shost->free_list_lock, flags);
  203. if (cmd) {
  204. buf = cmd->sense_buffer;
  205. memset(cmd, 0, sizeof(*cmd));
  206. cmd->sense_buffer = buf;
  207. }
  208. }
  209. return cmd;
  210. }
  211. EXPORT_SYMBOL_GPL(__scsi_get_command);
  212. /**
  213. * scsi_get_command - Allocate and setup a scsi command block
  214. * @dev: parent scsi device
  215. * @gfp_mask: allocator flags
  216. *
  217. * Returns: The allocated scsi command structure.
  218. */
  219. struct scsi_cmnd *scsi_get_command(struct scsi_device *dev, gfp_t gfp_mask)
  220. {
  221. struct scsi_cmnd *cmd;
  222. /* Bail if we can't get a reference to the device */
  223. if (!get_device(&dev->sdev_gendev))
  224. return NULL;
  225. cmd = __scsi_get_command(dev->host, gfp_mask);
  226. if (likely(cmd != NULL)) {
  227. unsigned long flags;
  228. cmd->device = dev;
  229. init_timer(&cmd->eh_timeout);
  230. INIT_LIST_HEAD(&cmd->list);
  231. spin_lock_irqsave(&dev->list_lock, flags);
  232. list_add_tail(&cmd->list, &dev->cmd_list);
  233. spin_unlock_irqrestore(&dev->list_lock, flags);
  234. cmd->jiffies_at_alloc = jiffies;
  235. } else
  236. put_device(&dev->sdev_gendev);
  237. return cmd;
  238. }
  239. EXPORT_SYMBOL(scsi_get_command);
  240. /**
  241. * __scsi_put_command - Free a struct scsi_cmnd
  242. * @shost: dev->host
  243. * @cmd: Command to free
  244. * @dev: parent scsi device
  245. */
  246. void __scsi_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd,
  247. struct device *dev)
  248. {
  249. unsigned long flags;
  250. /* changing locks here, don't need to restore the irq state */
  251. spin_lock_irqsave(&shost->free_list_lock, flags);
  252. if (unlikely(list_empty(&shost->free_list))) {
  253. list_add(&cmd->list, &shost->free_list);
  254. cmd = NULL;
  255. }
  256. spin_unlock_irqrestore(&shost->free_list_lock, flags);
  257. if (likely(cmd != NULL))
  258. scsi_pool_free_command(shost->cmd_pool, cmd);
  259. put_device(dev);
  260. }
  261. EXPORT_SYMBOL(__scsi_put_command);
  262. /**
  263. * scsi_put_command - Free a scsi command block
  264. * @cmd: command block to free
  265. *
  266. * Returns: Nothing.
  267. *
  268. * Notes: The command must not belong to any lists.
  269. */
  270. void scsi_put_command(struct scsi_cmnd *cmd)
  271. {
  272. struct scsi_device *sdev = cmd->device;
  273. unsigned long flags;
  274. /* serious error if the command hasn't come from a device list */
  275. spin_lock_irqsave(&cmd->device->list_lock, flags);
  276. BUG_ON(list_empty(&cmd->list));
  277. list_del_init(&cmd->list);
  278. spin_unlock_irqrestore(&cmd->device->list_lock, flags);
  279. __scsi_put_command(cmd->device->host, cmd, &sdev->sdev_gendev);
  280. }
  281. EXPORT_SYMBOL(scsi_put_command);
  282. static struct scsi_host_cmd_pool *scsi_get_host_cmd_pool(gfp_t gfp_mask)
  283. {
  284. struct scsi_host_cmd_pool *retval = NULL, *pool;
  285. /*
  286. * Select a command slab for this host and create it if not
  287. * yet existent.
  288. */
  289. mutex_lock(&host_cmd_pool_mutex);
  290. pool = (gfp_mask & __GFP_DMA) ? &scsi_cmd_dma_pool :
  291. &scsi_cmd_pool;
  292. if (!pool->users) {
  293. pool->cmd_slab = kmem_cache_create(pool->cmd_name,
  294. sizeof(struct scsi_cmnd), 0,
  295. pool->slab_flags, NULL);
  296. if (!pool->cmd_slab)
  297. goto fail;
  298. pool->sense_slab = kmem_cache_create(pool->sense_name,
  299. SCSI_SENSE_BUFFERSIZE, 0,
  300. pool->slab_flags, NULL);
  301. if (!pool->sense_slab) {
  302. kmem_cache_destroy(pool->cmd_slab);
  303. goto fail;
  304. }
  305. }
  306. pool->users++;
  307. retval = pool;
  308. fail:
  309. mutex_unlock(&host_cmd_pool_mutex);
  310. return retval;
  311. }
  312. static void scsi_put_host_cmd_pool(gfp_t gfp_mask)
  313. {
  314. struct scsi_host_cmd_pool *pool;
  315. mutex_lock(&host_cmd_pool_mutex);
  316. pool = (gfp_mask & __GFP_DMA) ? &scsi_cmd_dma_pool :
  317. &scsi_cmd_pool;
  318. /*
  319. * This may happen if a driver has a mismatched get and put
  320. * of the command pool; the driver should be implicated in
  321. * the stack trace
  322. */
  323. BUG_ON(pool->users == 0);
  324. if (!--pool->users) {
  325. kmem_cache_destroy(pool->cmd_slab);
  326. kmem_cache_destroy(pool->sense_slab);
  327. }
  328. mutex_unlock(&host_cmd_pool_mutex);
  329. }
  330. /**
  331. * scsi_allocate_command - get a fully allocated SCSI command
  332. * @gfp_mask: allocation mask
  333. *
  334. * This function is for use outside of the normal host based pools.
  335. * It allocates the relevant command and takes an additional reference
  336. * on the pool it used. This function *must* be paired with
  337. * scsi_free_command which also has the identical mask, otherwise the
  338. * free pool counts will eventually go wrong and you'll trigger a bug.
  339. *
  340. * This function should *only* be used by drivers that need a static
  341. * command allocation at start of day for internal functions.
  342. */
  343. struct scsi_cmnd *scsi_allocate_command(gfp_t gfp_mask)
  344. {
  345. struct scsi_host_cmd_pool *pool = scsi_get_host_cmd_pool(gfp_mask);
  346. if (!pool)
  347. return NULL;
  348. return scsi_pool_alloc_command(pool, gfp_mask);
  349. }
  350. EXPORT_SYMBOL(scsi_allocate_command);
  351. /**
  352. * scsi_free_command - free a command allocated by scsi_allocate_command
  353. * @gfp_mask: mask used in the original allocation
  354. * @cmd: command to free
  355. *
  356. * Note: using the original allocation mask is vital because that's
  357. * what determines which command pool we use to free the command. Any
  358. * mismatch will cause the system to BUG eventually.
  359. */
  360. void scsi_free_command(gfp_t gfp_mask, struct scsi_cmnd *cmd)
  361. {
  362. struct scsi_host_cmd_pool *pool = scsi_get_host_cmd_pool(gfp_mask);
  363. /*
  364. * this could trigger if the mask to scsi_allocate_command
  365. * doesn't match this mask. Otherwise we're guaranteed that this
  366. * succeeds because scsi_allocate_command must have taken a reference
  367. * on the pool
  368. */
  369. BUG_ON(!pool);
  370. scsi_pool_free_command(pool, cmd);
  371. /*
  372. * scsi_put_host_cmd_pool is called twice; once to release the
  373. * reference we took above, and once to release the reference
  374. * originally taken by scsi_allocate_command
  375. */
  376. scsi_put_host_cmd_pool(gfp_mask);
  377. scsi_put_host_cmd_pool(gfp_mask);
  378. }
  379. EXPORT_SYMBOL(scsi_free_command);
  380. /**
  381. * scsi_setup_command_freelist - Setup the command freelist for a scsi host.
  382. * @shost: host to allocate the freelist for.
  383. *
  384. * Description: The command freelist protects against system-wide out of memory
  385. * deadlock by preallocating one SCSI command structure for each host, so the
  386. * system can always write to a swap file on a device associated with that host.
  387. *
  388. * Returns: Nothing.
  389. */
  390. int scsi_setup_command_freelist(struct Scsi_Host *shost)
  391. {
  392. struct scsi_cmnd *cmd;
  393. const gfp_t gfp_mask = shost->unchecked_isa_dma ? GFP_DMA : GFP_KERNEL;
  394. spin_lock_init(&shost->free_list_lock);
  395. INIT_LIST_HEAD(&shost->free_list);
  396. shost->cmd_pool = scsi_get_host_cmd_pool(gfp_mask);
  397. if (!shost->cmd_pool)
  398. return -ENOMEM;
  399. /*
  400. * Get one backup command for this host.
  401. */
  402. cmd = scsi_pool_alloc_command(shost->cmd_pool, gfp_mask);
  403. if (!cmd) {
  404. scsi_put_host_cmd_pool(gfp_mask);
  405. shost->cmd_pool = NULL;
  406. return -ENOMEM;
  407. }
  408. list_add(&cmd->list, &shost->free_list);
  409. return 0;
  410. }
  411. /**
  412. * scsi_destroy_command_freelist - Release the command freelist for a scsi host.
  413. * @shost: host whose freelist is going to be destroyed
  414. */
  415. void scsi_destroy_command_freelist(struct Scsi_Host *shost)
  416. {
  417. /*
  418. * If cmd_pool is NULL the free list was not initialized, so
  419. * do not attempt to release resources.
  420. */
  421. if (!shost->cmd_pool)
  422. return;
  423. while (!list_empty(&shost->free_list)) {
  424. struct scsi_cmnd *cmd;
  425. cmd = list_entry(shost->free_list.next, struct scsi_cmnd, list);
  426. list_del_init(&cmd->list);
  427. scsi_pool_free_command(shost->cmd_pool, cmd);
  428. }
  429. shost->cmd_pool = NULL;
  430. scsi_put_host_cmd_pool(shost->unchecked_isa_dma ? GFP_DMA : GFP_KERNEL);
  431. }
  432. #ifdef CONFIG_SCSI_LOGGING
  433. void scsi_log_send(struct scsi_cmnd *cmd)
  434. {
  435. unsigned int level;
  436. /*
  437. * If ML QUEUE log level is greater than or equal to:
  438. *
  439. * 1: nothing (match completion)
  440. *
  441. * 2: log opcode + command of all commands
  442. *
  443. * 3: same as 2 plus dump cmd address
  444. *
  445. * 4: same as 3 plus dump extra junk
  446. */
  447. if (unlikely(scsi_logging_level)) {
  448. level = SCSI_LOG_LEVEL(SCSI_LOG_MLQUEUE_SHIFT,
  449. SCSI_LOG_MLQUEUE_BITS);
  450. if (level > 1) {
  451. scmd_printk(KERN_INFO, cmd, "Send: ");
  452. if (level > 2)
  453. printk("0x%p ", cmd);
  454. printk("\n");
  455. scsi_print_command(cmd);
  456. if (level > 3) {
  457. printk(KERN_INFO "buffer = 0x%p, bufflen = %d,"
  458. " queuecommand 0x%p\n",
  459. scsi_sglist(cmd), scsi_bufflen(cmd),
  460. cmd->device->host->hostt->queuecommand);
  461. }
  462. }
  463. }
  464. }
  465. void scsi_log_completion(struct scsi_cmnd *cmd, int disposition)
  466. {
  467. unsigned int level;
  468. /*
  469. * If ML COMPLETE log level is greater than or equal to:
  470. *
  471. * 1: log disposition, result, opcode + command, and conditionally
  472. * sense data for failures or non SUCCESS dispositions.
  473. *
  474. * 2: same as 1 but for all command completions.
  475. *
  476. * 3: same as 2 plus dump cmd address
  477. *
  478. * 4: same as 3 plus dump extra junk
  479. */
  480. if (unlikely(scsi_logging_level)) {
  481. level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
  482. SCSI_LOG_MLCOMPLETE_BITS);
  483. if (((level > 0) && (cmd->result || disposition != SUCCESS)) ||
  484. (level > 1)) {
  485. scmd_printk(KERN_INFO, cmd, "Done: ");
  486. if (level > 2)
  487. printk("0x%p ", cmd);
  488. /*
  489. * Dump truncated values, so we usually fit within
  490. * 80 chars.
  491. */
  492. switch (disposition) {
  493. case SUCCESS:
  494. printk("SUCCESS\n");
  495. break;
  496. case NEEDS_RETRY:
  497. printk("RETRY\n");
  498. break;
  499. case ADD_TO_MLQUEUE:
  500. printk("MLQUEUE\n");
  501. break;
  502. case FAILED:
  503. printk("FAILED\n");
  504. break;
  505. case TIMEOUT_ERROR:
  506. /*
  507. * If called via scsi_times_out.
  508. */
  509. printk("TIMEOUT\n");
  510. break;
  511. default:
  512. printk("UNKNOWN\n");
  513. }
  514. scsi_print_result(cmd);
  515. scsi_print_command(cmd);
  516. if (status_byte(cmd->result) & CHECK_CONDITION)
  517. scsi_print_sense("", cmd);
  518. if (level > 3)
  519. scmd_printk(KERN_INFO, cmd,
  520. "scsi host busy %d failed %d\n",
  521. cmd->device->host->host_busy,
  522. cmd->device->host->host_failed);
  523. }
  524. }
  525. }
  526. #endif
  527. /**
  528. * scsi_cmd_get_serial - Assign a serial number to a command
  529. * @host: the scsi host
  530. * @cmd: command to assign serial number to
  531. *
  532. * Description: a serial number identifies a request for error recovery
  533. * and debugging purposes. Protected by the Host_Lock of host.
  534. */
  535. static inline void scsi_cmd_get_serial(struct Scsi_Host *host, struct scsi_cmnd *cmd)
  536. {
  537. cmd->serial_number = host->cmd_serial_number++;
  538. if (cmd->serial_number == 0)
  539. cmd->serial_number = host->cmd_serial_number++;
  540. }
  541. /**
  542. * scsi_dispatch_command - Dispatch a command to the low-level driver.
  543. * @cmd: command block we are dispatching.
  544. *
  545. * Return: nonzero return request was rejected and device's queue needs to be
  546. * plugged.
  547. */
  548. int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
  549. {
  550. struct Scsi_Host *host = cmd->device->host;
  551. unsigned long flags = 0;
  552. unsigned long timeout;
  553. int rtn = 0;
  554. /* check if the device is still usable */
  555. if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
  556. /* in SDEV_DEL we error all commands. DID_NO_CONNECT
  557. * returns an immediate error upwards, and signals
  558. * that the device is no longer present */
  559. cmd->result = DID_NO_CONNECT << 16;
  560. atomic_inc(&cmd->device->iorequest_cnt);
  561. __scsi_done(cmd);
  562. /* return 0 (because the command has been processed) */
  563. goto out;
  564. }
  565. /* Check to see if the scsi lld put this device into state SDEV_BLOCK. */
  566. if (unlikely(cmd->device->sdev_state == SDEV_BLOCK)) {
  567. /*
  568. * in SDEV_BLOCK, the command is just put back on the device
  569. * queue. The suspend state has already blocked the queue so
  570. * future requests should not occur until the device
  571. * transitions out of the suspend state.
  572. */
  573. scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
  574. SCSI_LOG_MLQUEUE(3, printk("queuecommand : device blocked \n"));
  575. /*
  576. * NOTE: rtn is still zero here because we don't need the
  577. * queue to be plugged on return (it's already stopped)
  578. */
  579. goto out;
  580. }
  581. /*
  582. * If SCSI-2 or lower, store the LUN value in cmnd.
  583. */
  584. if (cmd->device->scsi_level <= SCSI_2 &&
  585. cmd->device->scsi_level != SCSI_UNKNOWN) {
  586. cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
  587. (cmd->device->lun << 5 & 0xe0);
  588. }
  589. /*
  590. * We will wait MIN_RESET_DELAY clock ticks after the last reset so
  591. * we can avoid the drive not being ready.
  592. */
  593. timeout = host->last_reset + MIN_RESET_DELAY;
  594. if (host->resetting && time_before(jiffies, timeout)) {
  595. int ticks_remaining = timeout - jiffies;
  596. /*
  597. * NOTE: This may be executed from within an interrupt
  598. * handler! This is bad, but for now, it'll do. The irq
  599. * level of the interrupt handler has been masked out by the
  600. * platform dependent interrupt handling code already, so the
  601. * sti() here will not cause another call to the SCSI host's
  602. * interrupt handler (assuming there is one irq-level per
  603. * host).
  604. */
  605. while (--ticks_remaining >= 0)
  606. mdelay(1 + 999 / HZ);
  607. host->resetting = 0;
  608. }
  609. /*
  610. * AK: unlikely race here: for some reason the timer could
  611. * expire before the serial number is set up below.
  612. */
  613. scsi_add_timer(cmd, cmd->timeout_per_command, scsi_times_out);
  614. scsi_log_send(cmd);
  615. /*
  616. * We will use a queued command if possible, otherwise we will
  617. * emulate the queuing and calling of completion function ourselves.
  618. */
  619. atomic_inc(&cmd->device->iorequest_cnt);
  620. /*
  621. * Before we queue this command, check if the command
  622. * length exceeds what the host adapter can handle.
  623. */
  624. if (cmd->cmd_len > cmd->device->host->max_cmd_len) {
  625. SCSI_LOG_MLQUEUE(3,
  626. printk("queuecommand : command too long. "
  627. "cdb_size=%d host->max_cmd_len=%d\n",
  628. cmd->cmd_len, cmd->device->host->max_cmd_len));
  629. cmd->result = (DID_ABORT << 16);
  630. scsi_done(cmd);
  631. goto out;
  632. }
  633. spin_lock_irqsave(host->host_lock, flags);
  634. scsi_cmd_get_serial(host, cmd);
  635. if (unlikely(host->shost_state == SHOST_DEL)) {
  636. cmd->result = (DID_NO_CONNECT << 16);
  637. scsi_done(cmd);
  638. } else {
  639. rtn = host->hostt->queuecommand(cmd, scsi_done);
  640. }
  641. spin_unlock_irqrestore(host->host_lock, flags);
  642. if (rtn) {
  643. if (scsi_delete_timer(cmd)) {
  644. atomic_inc(&cmd->device->iodone_cnt);
  645. scsi_queue_insert(cmd,
  646. (rtn == SCSI_MLQUEUE_DEVICE_BUSY) ?
  647. rtn : SCSI_MLQUEUE_HOST_BUSY);
  648. }
  649. SCSI_LOG_MLQUEUE(3,
  650. printk("queuecommand : request rejected\n"));
  651. }
  652. out:
  653. SCSI_LOG_MLQUEUE(3, printk("leaving scsi_dispatch_cmnd()\n"));
  654. return rtn;
  655. }
  656. /**
  657. * scsi_req_abort_cmd -- Request command recovery for the specified command
  658. * @cmd: pointer to the SCSI command of interest
  659. *
  660. * This function requests that SCSI Core start recovery for the
  661. * command by deleting the timer and adding the command to the eh
  662. * queue. It can be called by either LLDDs or SCSI Core. LLDDs who
  663. * implement their own error recovery MAY ignore the timeout event if
  664. * they generated scsi_req_abort_cmd.
  665. */
  666. void scsi_req_abort_cmd(struct scsi_cmnd *cmd)
  667. {
  668. if (!scsi_delete_timer(cmd))
  669. return;
  670. scsi_times_out(cmd);
  671. }
  672. EXPORT_SYMBOL(scsi_req_abort_cmd);
  673. /**
  674. * scsi_done - Enqueue the finished SCSI command into the done queue.
  675. * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
  676. * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
  677. *
  678. * Description: This function is the mid-level's (SCSI Core) interrupt routine,
  679. * which regains ownership of the SCSI command (de facto) from a LLDD, and
  680. * enqueues the command to the done queue for further processing.
  681. *
  682. * This is the producer of the done queue who enqueues at the tail.
  683. *
  684. * This function is interrupt context safe.
  685. */
  686. static void scsi_done(struct scsi_cmnd *cmd)
  687. {
  688. /*
  689. * We don't have to worry about this one timing out anymore.
  690. * If we are unable to remove the timer, then the command
  691. * has already timed out. In which case, we have no choice but to
  692. * let the timeout function run, as we have no idea where in fact
  693. * that function could really be. It might be on another processor,
  694. * etc, etc.
  695. */
  696. if (!scsi_delete_timer(cmd))
  697. return;
  698. __scsi_done(cmd);
  699. }
  700. /* Private entry to scsi_done() to complete a command when the timer
  701. * isn't running --- used by scsi_times_out */
  702. void __scsi_done(struct scsi_cmnd *cmd)
  703. {
  704. struct request *rq = cmd->request;
  705. /*
  706. * Set the serial numbers back to zero
  707. */
  708. cmd->serial_number = 0;
  709. atomic_inc(&cmd->device->iodone_cnt);
  710. if (cmd->result)
  711. atomic_inc(&cmd->device->ioerr_cnt);
  712. BUG_ON(!rq);
  713. /*
  714. * The uptodate/nbytes values don't matter, as we allow partial
  715. * completes and thus will check this in the softirq callback
  716. */
  717. rq->completion_data = cmd;
  718. blk_complete_request(rq);
  719. }
  720. /* Move this to a header if it becomes more generally useful */
  721. static struct scsi_driver *scsi_cmd_to_driver(struct scsi_cmnd *cmd)
  722. {
  723. return *(struct scsi_driver **)cmd->request->rq_disk->private_data;
  724. }
  725. /**
  726. * scsi_finish_command - cleanup and pass command back to upper layer
  727. * @cmd: the command
  728. *
  729. * Description: Pass command off to upper layer for finishing of I/O
  730. * request, waking processes that are waiting on results,
  731. * etc.
  732. */
  733. void scsi_finish_command(struct scsi_cmnd *cmd)
  734. {
  735. struct scsi_device *sdev = cmd->device;
  736. struct Scsi_Host *shost = sdev->host;
  737. struct scsi_driver *drv;
  738. unsigned int good_bytes;
  739. scsi_device_unbusy(sdev);
  740. /*
  741. * Clear the flags which say that the device/host is no longer
  742. * capable of accepting new commands. These are set in scsi_queue.c
  743. * for both the queue full condition on a device, and for a
  744. * host full condition on the host.
  745. *
  746. * XXX(hch): What about locking?
  747. */
  748. shost->host_blocked = 0;
  749. sdev->device_blocked = 0;
  750. /*
  751. * If we have valid sense information, then some kind of recovery
  752. * must have taken place. Make a note of this.
  753. */
  754. if (SCSI_SENSE_VALID(cmd))
  755. cmd->result |= (DRIVER_SENSE << 24);
  756. SCSI_LOG_MLCOMPLETE(4, sdev_printk(KERN_INFO, sdev,
  757. "Notifying upper driver of completion "
  758. "(result %x)\n", cmd->result));
  759. good_bytes = scsi_bufflen(cmd);
  760. if (cmd->request->cmd_type != REQ_TYPE_BLOCK_PC) {
  761. int old_good_bytes = good_bytes;
  762. drv = scsi_cmd_to_driver(cmd);
  763. if (drv->done)
  764. good_bytes = drv->done(cmd);
  765. /*
  766. * USB may not give sense identifying bad sector and
  767. * simply return a residue instead, so subtract off the
  768. * residue if drv->done() error processing indicates no
  769. * change to the completion length.
  770. */
  771. if (good_bytes == old_good_bytes)
  772. good_bytes -= scsi_get_resid(cmd);
  773. }
  774. scsi_io_completion(cmd, good_bytes);
  775. }
  776. EXPORT_SYMBOL(scsi_finish_command);
  777. /**
  778. * scsi_adjust_queue_depth - Let low level drivers change a device's queue depth
  779. * @sdev: SCSI Device in question
  780. * @tagged: Do we use tagged queueing (non-0) or do we treat
  781. * this device as an untagged device (0)
  782. * @tags: Number of tags allowed if tagged queueing enabled,
  783. * or number of commands the low level driver can
  784. * queue up in non-tagged mode (as per cmd_per_lun).
  785. *
  786. * Returns: Nothing
  787. *
  788. * Lock Status: None held on entry
  789. *
  790. * Notes: Low level drivers may call this at any time and we will do
  791. * the right thing depending on whether or not the device is
  792. * currently active and whether or not it even has the
  793. * command blocks built yet.
  794. */
  795. void scsi_adjust_queue_depth(struct scsi_device *sdev, int tagged, int tags)
  796. {
  797. unsigned long flags;
  798. /*
  799. * refuse to set tagged depth to an unworkable size
  800. */
  801. if (tags <= 0)
  802. return;
  803. spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
  804. /* Check to see if the queue is managed by the block layer.
  805. * If it is, and we fail to adjust the depth, exit. */
  806. if (blk_queue_tagged(sdev->request_queue) &&
  807. blk_queue_resize_tags(sdev->request_queue, tags) != 0)
  808. goto out;
  809. sdev->queue_depth = tags;
  810. switch (tagged) {
  811. case MSG_ORDERED_TAG:
  812. sdev->ordered_tags = 1;
  813. sdev->simple_tags = 1;
  814. break;
  815. case MSG_SIMPLE_TAG:
  816. sdev->ordered_tags = 0;
  817. sdev->simple_tags = 1;
  818. break;
  819. default:
  820. sdev_printk(KERN_WARNING, sdev,
  821. "scsi_adjust_queue_depth, bad queue type, "
  822. "disabled\n");
  823. case 0:
  824. sdev->ordered_tags = sdev->simple_tags = 0;
  825. sdev->queue_depth = tags;
  826. break;
  827. }
  828. out:
  829. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  830. }
  831. EXPORT_SYMBOL(scsi_adjust_queue_depth);
  832. /**
  833. * scsi_track_queue_full - track QUEUE_FULL events to adjust queue depth
  834. * @sdev: SCSI Device in question
  835. * @depth: Current number of outstanding SCSI commands on this device,
  836. * not counting the one returned as QUEUE_FULL.
  837. *
  838. * Description: This function will track successive QUEUE_FULL events on a
  839. * specific SCSI device to determine if and when there is a
  840. * need to adjust the queue depth on the device.
  841. *
  842. * Returns: 0 - No change needed, >0 - Adjust queue depth to this new depth,
  843. * -1 - Drop back to untagged operation using host->cmd_per_lun
  844. * as the untagged command depth
  845. *
  846. * Lock Status: None held on entry
  847. *
  848. * Notes: Low level drivers may call this at any time and we will do
  849. * "The Right Thing." We are interrupt context safe.
  850. */
  851. int scsi_track_queue_full(struct scsi_device *sdev, int depth)
  852. {
  853. if ((jiffies >> 4) == sdev->last_queue_full_time)
  854. return 0;
  855. sdev->last_queue_full_time = (jiffies >> 4);
  856. if (sdev->last_queue_full_depth != depth) {
  857. sdev->last_queue_full_count = 1;
  858. sdev->last_queue_full_depth = depth;
  859. } else {
  860. sdev->last_queue_full_count++;
  861. }
  862. if (sdev->last_queue_full_count <= 10)
  863. return 0;
  864. if (sdev->last_queue_full_depth < 8) {
  865. /* Drop back to untagged */
  866. scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
  867. return -1;
  868. }
  869. if (sdev->ordered_tags)
  870. scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, depth);
  871. else
  872. scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
  873. return depth;
  874. }
  875. EXPORT_SYMBOL(scsi_track_queue_full);
  876. /**
  877. * scsi_device_get - get an additional reference to a scsi_device
  878. * @sdev: device to get a reference to
  879. *
  880. * Description: Gets a reference to the scsi_device and increments the use count
  881. * of the underlying LLDD module. You must hold host_lock of the
  882. * parent Scsi_Host or already have a reference when calling this.
  883. */
  884. int scsi_device_get(struct scsi_device *sdev)
  885. {
  886. if (sdev->sdev_state == SDEV_DEL)
  887. return -ENXIO;
  888. if (!get_device(&sdev->sdev_gendev))
  889. return -ENXIO;
  890. /* We can fail this if we're doing SCSI operations
  891. * from module exit (like cache flush) */
  892. try_module_get(sdev->host->hostt->module);
  893. return 0;
  894. }
  895. EXPORT_SYMBOL(scsi_device_get);
  896. /**
  897. * scsi_device_put - release a reference to a scsi_device
  898. * @sdev: device to release a reference on.
  899. *
  900. * Description: Release a reference to the scsi_device and decrements the use
  901. * count of the underlying LLDD module. The device is freed once the last
  902. * user vanishes.
  903. */
  904. void scsi_device_put(struct scsi_device *sdev)
  905. {
  906. #ifdef CONFIG_MODULE_UNLOAD
  907. struct module *module = sdev->host->hostt->module;
  908. /* The module refcount will be zero if scsi_device_get()
  909. * was called from a module removal routine */
  910. if (module && module_refcount(module) != 0)
  911. module_put(module);
  912. #endif
  913. put_device(&sdev->sdev_gendev);
  914. }
  915. EXPORT_SYMBOL(scsi_device_put);
  916. /* helper for shost_for_each_device, see that for documentation */
  917. struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost,
  918. struct scsi_device *prev)
  919. {
  920. struct list_head *list = (prev ? &prev->siblings : &shost->__devices);
  921. struct scsi_device *next = NULL;
  922. unsigned long flags;
  923. spin_lock_irqsave(shost->host_lock, flags);
  924. while (list->next != &shost->__devices) {
  925. next = list_entry(list->next, struct scsi_device, siblings);
  926. /* skip devices that we can't get a reference to */
  927. if (!scsi_device_get(next))
  928. break;
  929. next = NULL;
  930. list = list->next;
  931. }
  932. spin_unlock_irqrestore(shost->host_lock, flags);
  933. if (prev)
  934. scsi_device_put(prev);
  935. return next;
  936. }
  937. EXPORT_SYMBOL(__scsi_iterate_devices);
  938. /**
  939. * starget_for_each_device - helper to walk all devices of a target
  940. * @starget: target whose devices we want to iterate over.
  941. * @data: Opaque passed to each function call.
  942. * @fn: Function to call on each device
  943. *
  944. * This traverses over each device of @starget. The devices have
  945. * a reference that must be released by scsi_host_put when breaking
  946. * out of the loop.
  947. */
  948. void starget_for_each_device(struct scsi_target *starget, void *data,
  949. void (*fn)(struct scsi_device *, void *))
  950. {
  951. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  952. struct scsi_device *sdev;
  953. shost_for_each_device(sdev, shost) {
  954. if ((sdev->channel == starget->channel) &&
  955. (sdev->id == starget->id))
  956. fn(sdev, data);
  957. }
  958. }
  959. EXPORT_SYMBOL(starget_for_each_device);
  960. /**
  961. * __starget_for_each_device - helper to walk all devices of a target (UNLOCKED)
  962. * @starget: target whose devices we want to iterate over.
  963. * @data: parameter for callback @fn()
  964. * @fn: callback function that is invoked for each device
  965. *
  966. * This traverses over each device of @starget. It does _not_
  967. * take a reference on the scsi_device, so the whole loop must be
  968. * protected by shost->host_lock.
  969. *
  970. * Note: The only reason why drivers would want to use this is because
  971. * they need to access the device list in irq context. Otherwise you
  972. * really want to use starget_for_each_device instead.
  973. **/
  974. void __starget_for_each_device(struct scsi_target *starget, void *data,
  975. void (*fn)(struct scsi_device *, void *))
  976. {
  977. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  978. struct scsi_device *sdev;
  979. __shost_for_each_device(sdev, shost) {
  980. if ((sdev->channel == starget->channel) &&
  981. (sdev->id == starget->id))
  982. fn(sdev, data);
  983. }
  984. }
  985. EXPORT_SYMBOL(__starget_for_each_device);
  986. /**
  987. * __scsi_device_lookup_by_target - find a device given the target (UNLOCKED)
  988. * @starget: SCSI target pointer
  989. * @lun: SCSI Logical Unit Number
  990. *
  991. * Description: Looks up the scsi_device with the specified @lun for a given
  992. * @starget. The returned scsi_device does not have an additional
  993. * reference. You must hold the host's host_lock over this call and
  994. * any access to the returned scsi_device.
  995. *
  996. * Note: The only reason why drivers should use this is because
  997. * they need to access the device list in irq context. Otherwise you
  998. * really want to use scsi_device_lookup_by_target instead.
  999. **/
  1000. struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget,
  1001. uint lun)
  1002. {
  1003. struct scsi_device *sdev;
  1004. list_for_each_entry(sdev, &starget->devices, same_target_siblings) {
  1005. if (sdev->lun ==lun)
  1006. return sdev;
  1007. }
  1008. return NULL;
  1009. }
  1010. EXPORT_SYMBOL(__scsi_device_lookup_by_target);
  1011. /**
  1012. * scsi_device_lookup_by_target - find a device given the target
  1013. * @starget: SCSI target pointer
  1014. * @lun: SCSI Logical Unit Number
  1015. *
  1016. * Description: Looks up the scsi_device with the specified @channel, @id, @lun
  1017. * for a given host. The returned scsi_device has an additional reference that
  1018. * needs to be released with scsi_device_put once you're done with it.
  1019. **/
  1020. struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget,
  1021. uint lun)
  1022. {
  1023. struct scsi_device *sdev;
  1024. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1025. unsigned long flags;
  1026. spin_lock_irqsave(shost->host_lock, flags);
  1027. sdev = __scsi_device_lookup_by_target(starget, lun);
  1028. if (sdev && scsi_device_get(sdev))
  1029. sdev = NULL;
  1030. spin_unlock_irqrestore(shost->host_lock, flags);
  1031. return sdev;
  1032. }
  1033. EXPORT_SYMBOL(scsi_device_lookup_by_target);
  1034. /**
  1035. * __scsi_device_lookup - find a device given the host (UNLOCKED)
  1036. * @shost: SCSI host pointer
  1037. * @channel: SCSI channel (zero if only one channel)
  1038. * @id: SCSI target number (physical unit number)
  1039. * @lun: SCSI Logical Unit Number
  1040. *
  1041. * Description: Looks up the scsi_device with the specified @channel, @id, @lun
  1042. * for a given host. The returned scsi_device does not have an additional
  1043. * reference. You must hold the host's host_lock over this call and any access
  1044. * to the returned scsi_device.
  1045. *
  1046. * Note: The only reason why drivers would want to use this is because
  1047. * they need to access the device list in irq context. Otherwise you
  1048. * really want to use scsi_device_lookup instead.
  1049. **/
  1050. struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost,
  1051. uint channel, uint id, uint lun)
  1052. {
  1053. struct scsi_device *sdev;
  1054. list_for_each_entry(sdev, &shost->__devices, siblings) {
  1055. if (sdev->channel == channel && sdev->id == id &&
  1056. sdev->lun ==lun)
  1057. return sdev;
  1058. }
  1059. return NULL;
  1060. }
  1061. EXPORT_SYMBOL(__scsi_device_lookup);
  1062. /**
  1063. * scsi_device_lookup - find a device given the host
  1064. * @shost: SCSI host pointer
  1065. * @channel: SCSI channel (zero if only one channel)
  1066. * @id: SCSI target number (physical unit number)
  1067. * @lun: SCSI Logical Unit Number
  1068. *
  1069. * Description: Looks up the scsi_device with the specified @channel, @id, @lun
  1070. * for a given host. The returned scsi_device has an additional reference that
  1071. * needs to be released with scsi_device_put once you're done with it.
  1072. **/
  1073. struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost,
  1074. uint channel, uint id, uint lun)
  1075. {
  1076. struct scsi_device *sdev;
  1077. unsigned long flags;
  1078. spin_lock_irqsave(shost->host_lock, flags);
  1079. sdev = __scsi_device_lookup(shost, channel, id, lun);
  1080. if (sdev && scsi_device_get(sdev))
  1081. sdev = NULL;
  1082. spin_unlock_irqrestore(shost->host_lock, flags);
  1083. return sdev;
  1084. }
  1085. EXPORT_SYMBOL(scsi_device_lookup);
  1086. MODULE_DESCRIPTION("SCSI core");
  1087. MODULE_LICENSE("GPL");
  1088. module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR);
  1089. MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels");
  1090. static int __init init_scsi(void)
  1091. {
  1092. int error;
  1093. error = scsi_init_queue();
  1094. if (error)
  1095. return error;
  1096. error = scsi_init_procfs();
  1097. if (error)
  1098. goto cleanup_queue;
  1099. error = scsi_init_devinfo();
  1100. if (error)
  1101. goto cleanup_procfs;
  1102. error = scsi_init_hosts();
  1103. if (error)
  1104. goto cleanup_devlist;
  1105. error = scsi_init_sysctl();
  1106. if (error)
  1107. goto cleanup_hosts;
  1108. error = scsi_sysfs_register();
  1109. if (error)
  1110. goto cleanup_sysctl;
  1111. scsi_netlink_init();
  1112. printk(KERN_NOTICE "SCSI subsystem initialized\n");
  1113. return 0;
  1114. cleanup_sysctl:
  1115. scsi_exit_sysctl();
  1116. cleanup_hosts:
  1117. scsi_exit_hosts();
  1118. cleanup_devlist:
  1119. scsi_exit_devinfo();
  1120. cleanup_procfs:
  1121. scsi_exit_procfs();
  1122. cleanup_queue:
  1123. scsi_exit_queue();
  1124. printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n",
  1125. -error);
  1126. return error;
  1127. }
  1128. static void __exit exit_scsi(void)
  1129. {
  1130. scsi_netlink_exit();
  1131. scsi_sysfs_unregister();
  1132. scsi_exit_sysctl();
  1133. scsi_exit_hosts();
  1134. scsi_exit_devinfo();
  1135. scsi_exit_procfs();
  1136. scsi_exit_queue();
  1137. }
  1138. subsys_initcall(init_scsi);
  1139. module_exit(exit_scsi);