scsi.c 36 KB

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