scsi.c 37 KB

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