scsi.c 37 KB

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