scsi.c 37 KB

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