scsi.c 39 KB

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