scsi.c 37 KB

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