scsi.c 37 KB

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