scsi.c 32 KB

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