scsi.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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. if (depth > 0) {
  545. sdev->queue_depth = depth;
  546. wmb();
  547. }
  548. return sdev->queue_depth;
  549. }
  550. EXPORT_SYMBOL(scsi_change_queue_depth);
  551. /**
  552. * scsi_track_queue_full - track QUEUE_FULL events to adjust queue depth
  553. * @sdev: SCSI Device in question
  554. * @depth: Current number of outstanding SCSI commands on this device,
  555. * not counting the one returned as QUEUE_FULL.
  556. *
  557. * Description: This function will track successive QUEUE_FULL events on a
  558. * specific SCSI device to determine if and when there is a
  559. * need to adjust the queue depth on the device.
  560. *
  561. * Returns: 0 - No change needed, >0 - Adjust queue depth to this new depth,
  562. * -1 - Drop back to untagged operation using host->cmd_per_lun
  563. * as the untagged command depth
  564. *
  565. * Lock Status: None held on entry
  566. *
  567. * Notes: Low level drivers may call this at any time and we will do
  568. * "The Right Thing." We are interrupt context safe.
  569. */
  570. int scsi_track_queue_full(struct scsi_device *sdev, int depth)
  571. {
  572. /*
  573. * Don't let QUEUE_FULLs on the same
  574. * jiffies count, they could all be from
  575. * same event.
  576. */
  577. if ((jiffies >> 4) == (sdev->last_queue_full_time >> 4))
  578. return 0;
  579. sdev->last_queue_full_time = jiffies;
  580. if (sdev->last_queue_full_depth != depth) {
  581. sdev->last_queue_full_count = 1;
  582. sdev->last_queue_full_depth = depth;
  583. } else {
  584. sdev->last_queue_full_count++;
  585. }
  586. if (sdev->last_queue_full_count <= 10)
  587. return 0;
  588. return scsi_change_queue_depth(sdev, depth);
  589. }
  590. EXPORT_SYMBOL(scsi_track_queue_full);
  591. /**
  592. * scsi_vpd_inquiry - Request a device provide us with a VPD page
  593. * @sdev: The device to ask
  594. * @buffer: Where to put the result
  595. * @page: Which Vital Product Data to return
  596. * @len: The length of the buffer
  597. *
  598. * This is an internal helper function. You probably want to use
  599. * scsi_get_vpd_page instead.
  600. *
  601. * Returns size of the vpd page on success or a negative error number.
  602. */
  603. static int scsi_vpd_inquiry(struct scsi_device *sdev, unsigned char *buffer,
  604. u8 page, unsigned len)
  605. {
  606. int result;
  607. unsigned char cmd[16];
  608. if (len < 4)
  609. return -EINVAL;
  610. cmd[0] = INQUIRY;
  611. cmd[1] = 1; /* EVPD */
  612. cmd[2] = page;
  613. cmd[3] = len >> 8;
  614. cmd[4] = len & 0xff;
  615. cmd[5] = 0; /* Control byte */
  616. /*
  617. * I'm not convinced we need to try quite this hard to get VPD, but
  618. * all the existing users tried this hard.
  619. */
  620. result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer,
  621. len, NULL, 30 * HZ, 3, NULL);
  622. if (result)
  623. return -EIO;
  624. /* Sanity check that we got the page back that we asked for */
  625. if (buffer[1] != page)
  626. return -EIO;
  627. return get_unaligned_be16(&buffer[2]) + 4;
  628. }
  629. /**
  630. * scsi_get_vpd_page - Get Vital Product Data from a SCSI device
  631. * @sdev: The device to ask
  632. * @page: Which Vital Product Data to return
  633. * @buf: where to store the VPD
  634. * @buf_len: number of bytes in the VPD buffer area
  635. *
  636. * SCSI devices may optionally supply Vital Product Data. Each 'page'
  637. * of VPD is defined in the appropriate SCSI document (eg SPC, SBC).
  638. * If the device supports this VPD page, this routine returns a pointer
  639. * to a buffer containing the data from that page. The caller is
  640. * responsible for calling kfree() on this pointer when it is no longer
  641. * needed. If we cannot retrieve the VPD page this routine returns %NULL.
  642. */
  643. int scsi_get_vpd_page(struct scsi_device *sdev, u8 page, unsigned char *buf,
  644. int buf_len)
  645. {
  646. int i, result;
  647. if (sdev->skip_vpd_pages)
  648. goto fail;
  649. /* Ask for all the pages supported by this device */
  650. result = scsi_vpd_inquiry(sdev, buf, 0, buf_len);
  651. if (result < 4)
  652. goto fail;
  653. /* If the user actually wanted this page, we can skip the rest */
  654. if (page == 0)
  655. return 0;
  656. for (i = 4; i < min(result, buf_len); i++)
  657. if (buf[i] == page)
  658. goto found;
  659. if (i < result && i >= buf_len)
  660. /* ran off the end of the buffer, give us benefit of doubt */
  661. goto found;
  662. /* The device claims it doesn't support the requested page */
  663. goto fail;
  664. found:
  665. result = scsi_vpd_inquiry(sdev, buf, page, buf_len);
  666. if (result < 0)
  667. goto fail;
  668. return 0;
  669. fail:
  670. return -EINVAL;
  671. }
  672. EXPORT_SYMBOL_GPL(scsi_get_vpd_page);
  673. /**
  674. * scsi_attach_vpd - Attach Vital Product Data to a SCSI device structure
  675. * @sdev: The device to ask
  676. *
  677. * Attach the 'Device Identification' VPD page (0x83) and the
  678. * 'Unit Serial Number' VPD page (0x80) to a SCSI device
  679. * structure. This information can be used to identify the device
  680. * uniquely.
  681. */
  682. void scsi_attach_vpd(struct scsi_device *sdev)
  683. {
  684. int result, i;
  685. int vpd_len = SCSI_VPD_PG_LEN;
  686. int pg80_supported = 0;
  687. int pg83_supported = 0;
  688. unsigned char __rcu *vpd_buf, *orig_vpd_buf = NULL;
  689. if (!scsi_device_supports_vpd(sdev))
  690. return;
  691. retry_pg0:
  692. vpd_buf = kmalloc(vpd_len, GFP_KERNEL);
  693. if (!vpd_buf)
  694. return;
  695. /* Ask for all the pages supported by this device */
  696. result = scsi_vpd_inquiry(sdev, vpd_buf, 0, vpd_len);
  697. if (result < 0) {
  698. kfree(vpd_buf);
  699. return;
  700. }
  701. if (result > vpd_len) {
  702. vpd_len = result;
  703. kfree(vpd_buf);
  704. goto retry_pg0;
  705. }
  706. for (i = 4; i < result; i++) {
  707. if (vpd_buf[i] == 0x80)
  708. pg80_supported = 1;
  709. if (vpd_buf[i] == 0x83)
  710. pg83_supported = 1;
  711. }
  712. kfree(vpd_buf);
  713. vpd_len = SCSI_VPD_PG_LEN;
  714. if (pg80_supported) {
  715. retry_pg80:
  716. vpd_buf = kmalloc(vpd_len, GFP_KERNEL);
  717. if (!vpd_buf)
  718. return;
  719. result = scsi_vpd_inquiry(sdev, vpd_buf, 0x80, vpd_len);
  720. if (result < 0) {
  721. kfree(vpd_buf);
  722. return;
  723. }
  724. if (result > vpd_len) {
  725. vpd_len = result;
  726. kfree(vpd_buf);
  727. goto retry_pg80;
  728. }
  729. mutex_lock(&sdev->inquiry_mutex);
  730. orig_vpd_buf = sdev->vpd_pg80;
  731. sdev->vpd_pg80_len = result;
  732. rcu_assign_pointer(sdev->vpd_pg80, vpd_buf);
  733. mutex_unlock(&sdev->inquiry_mutex);
  734. synchronize_rcu();
  735. if (orig_vpd_buf) {
  736. kfree(orig_vpd_buf);
  737. orig_vpd_buf = NULL;
  738. }
  739. vpd_len = SCSI_VPD_PG_LEN;
  740. }
  741. if (pg83_supported) {
  742. retry_pg83:
  743. vpd_buf = kmalloc(vpd_len, GFP_KERNEL);
  744. if (!vpd_buf)
  745. return;
  746. result = scsi_vpd_inquiry(sdev, vpd_buf, 0x83, vpd_len);
  747. if (result < 0) {
  748. kfree(vpd_buf);
  749. return;
  750. }
  751. if (result > vpd_len) {
  752. vpd_len = result;
  753. kfree(vpd_buf);
  754. goto retry_pg83;
  755. }
  756. mutex_lock(&sdev->inquiry_mutex);
  757. orig_vpd_buf = sdev->vpd_pg83;
  758. sdev->vpd_pg83_len = result;
  759. rcu_assign_pointer(sdev->vpd_pg83, vpd_buf);
  760. mutex_unlock(&sdev->inquiry_mutex);
  761. synchronize_rcu();
  762. if (orig_vpd_buf)
  763. kfree(orig_vpd_buf);
  764. }
  765. }
  766. /**
  767. * scsi_report_opcode - Find out if a given command opcode is supported
  768. * @sdev: scsi device to query
  769. * @buffer: scratch buffer (must be at least 20 bytes long)
  770. * @len: length of buffer
  771. * @opcode: opcode for command to look up
  772. *
  773. * Uses the REPORT SUPPORTED OPERATION CODES to look up the given
  774. * opcode. Returns -EINVAL if RSOC fails, 0 if the command opcode is
  775. * unsupported and 1 if the device claims to support the command.
  776. */
  777. int scsi_report_opcode(struct scsi_device *sdev, unsigned char *buffer,
  778. unsigned int len, unsigned char opcode)
  779. {
  780. unsigned char cmd[16];
  781. struct scsi_sense_hdr sshdr;
  782. int result;
  783. if (sdev->no_report_opcodes || sdev->scsi_level < SCSI_SPC_3)
  784. return -EINVAL;
  785. memset(cmd, 0, 16);
  786. cmd[0] = MAINTENANCE_IN;
  787. cmd[1] = MI_REPORT_SUPPORTED_OPERATION_CODES;
  788. cmd[2] = 1; /* One command format */
  789. cmd[3] = opcode;
  790. put_unaligned_be32(len, &cmd[6]);
  791. memset(buffer, 0, len);
  792. result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
  793. &sshdr, 30 * HZ, 3, NULL);
  794. if (result && scsi_sense_valid(&sshdr) &&
  795. sshdr.sense_key == ILLEGAL_REQUEST &&
  796. (sshdr.asc == 0x20 || sshdr.asc == 0x24) && sshdr.ascq == 0x00)
  797. return -EINVAL;
  798. if ((buffer[1] & 3) == 3) /* Command supported */
  799. return 1;
  800. return 0;
  801. }
  802. EXPORT_SYMBOL(scsi_report_opcode);
  803. /**
  804. * scsi_device_get - get an additional reference to a scsi_device
  805. * @sdev: device to get a reference to
  806. *
  807. * Description: Gets a reference to the scsi_device and increments the use count
  808. * of the underlying LLDD module. You must hold host_lock of the
  809. * parent Scsi_Host or already have a reference when calling this.
  810. *
  811. * This will fail if a device is deleted or cancelled, or when the LLD module
  812. * is in the process of being unloaded.
  813. */
  814. int scsi_device_get(struct scsi_device *sdev)
  815. {
  816. if (sdev->sdev_state == SDEV_DEL || sdev->sdev_state == SDEV_CANCEL)
  817. goto fail;
  818. if (!get_device(&sdev->sdev_gendev))
  819. goto fail;
  820. if (!try_module_get(sdev->host->hostt->module))
  821. goto fail_put_device;
  822. return 0;
  823. fail_put_device:
  824. put_device(&sdev->sdev_gendev);
  825. fail:
  826. return -ENXIO;
  827. }
  828. EXPORT_SYMBOL(scsi_device_get);
  829. /**
  830. * scsi_device_put - release a reference to a scsi_device
  831. * @sdev: device to release a reference on.
  832. *
  833. * Description: Release a reference to the scsi_device and decrements the use
  834. * count of the underlying LLDD module. The device is freed once the last
  835. * user vanishes.
  836. */
  837. void scsi_device_put(struct scsi_device *sdev)
  838. {
  839. module_put(sdev->host->hostt->module);
  840. put_device(&sdev->sdev_gendev);
  841. }
  842. EXPORT_SYMBOL(scsi_device_put);
  843. /* helper for shost_for_each_device, see that for documentation */
  844. struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost,
  845. struct scsi_device *prev)
  846. {
  847. struct list_head *list = (prev ? &prev->siblings : &shost->__devices);
  848. struct scsi_device *next = NULL;
  849. unsigned long flags;
  850. spin_lock_irqsave(shost->host_lock, flags);
  851. while (list->next != &shost->__devices) {
  852. next = list_entry(list->next, struct scsi_device, siblings);
  853. /* skip devices that we can't get a reference to */
  854. if (!scsi_device_get(next))
  855. break;
  856. next = NULL;
  857. list = list->next;
  858. }
  859. spin_unlock_irqrestore(shost->host_lock, flags);
  860. if (prev)
  861. scsi_device_put(prev);
  862. return next;
  863. }
  864. EXPORT_SYMBOL(__scsi_iterate_devices);
  865. /**
  866. * starget_for_each_device - helper to walk all devices of a target
  867. * @starget: target whose devices we want to iterate over.
  868. * @data: Opaque passed to each function call.
  869. * @fn: Function to call on each device
  870. *
  871. * This traverses over each device of @starget. The devices have
  872. * a reference that must be released by scsi_host_put when breaking
  873. * out of the loop.
  874. */
  875. void starget_for_each_device(struct scsi_target *starget, void *data,
  876. void (*fn)(struct scsi_device *, void *))
  877. {
  878. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  879. struct scsi_device *sdev;
  880. shost_for_each_device(sdev, shost) {
  881. if ((sdev->channel == starget->channel) &&
  882. (sdev->id == starget->id))
  883. fn(sdev, data);
  884. }
  885. }
  886. EXPORT_SYMBOL(starget_for_each_device);
  887. /**
  888. * __starget_for_each_device - helper to walk all devices of a target (UNLOCKED)
  889. * @starget: target whose devices we want to iterate over.
  890. * @data: parameter for callback @fn()
  891. * @fn: callback function that is invoked for each device
  892. *
  893. * This traverses over each device of @starget. It does _not_
  894. * take a reference on the scsi_device, so the whole loop must be
  895. * protected by shost->host_lock.
  896. *
  897. * Note: The only reason why drivers would want to use this is because
  898. * they need to access the device list in irq context. Otherwise you
  899. * really want to use starget_for_each_device instead.
  900. **/
  901. void __starget_for_each_device(struct scsi_target *starget, void *data,
  902. void (*fn)(struct scsi_device *, void *))
  903. {
  904. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  905. struct scsi_device *sdev;
  906. __shost_for_each_device(sdev, shost) {
  907. if ((sdev->channel == starget->channel) &&
  908. (sdev->id == starget->id))
  909. fn(sdev, data);
  910. }
  911. }
  912. EXPORT_SYMBOL(__starget_for_each_device);
  913. /**
  914. * __scsi_device_lookup_by_target - find a device given the target (UNLOCKED)
  915. * @starget: SCSI target pointer
  916. * @lun: SCSI Logical Unit Number
  917. *
  918. * Description: Looks up the scsi_device with the specified @lun for a given
  919. * @starget. The returned scsi_device does not have an additional
  920. * reference. You must hold the host's host_lock over this call and
  921. * any access to the returned scsi_device. A scsi_device in state
  922. * SDEV_DEL is skipped.
  923. *
  924. * Note: The only reason why drivers should use this is because
  925. * they need to access the device list in irq context. Otherwise you
  926. * really want to use scsi_device_lookup_by_target instead.
  927. **/
  928. struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget,
  929. u64 lun)
  930. {
  931. struct scsi_device *sdev;
  932. list_for_each_entry(sdev, &starget->devices, same_target_siblings) {
  933. if (sdev->sdev_state == SDEV_DEL)
  934. continue;
  935. if (sdev->lun ==lun)
  936. return sdev;
  937. }
  938. return NULL;
  939. }
  940. EXPORT_SYMBOL(__scsi_device_lookup_by_target);
  941. /**
  942. * scsi_device_lookup_by_target - find a device given the target
  943. * @starget: SCSI target pointer
  944. * @lun: SCSI Logical Unit Number
  945. *
  946. * Description: Looks up the scsi_device with the specified @lun for a given
  947. * @starget. The returned scsi_device has an additional reference that
  948. * needs to be released with scsi_device_put once you're done with it.
  949. **/
  950. struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget,
  951. u64 lun)
  952. {
  953. struct scsi_device *sdev;
  954. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  955. unsigned long flags;
  956. spin_lock_irqsave(shost->host_lock, flags);
  957. sdev = __scsi_device_lookup_by_target(starget, lun);
  958. if (sdev && scsi_device_get(sdev))
  959. sdev = NULL;
  960. spin_unlock_irqrestore(shost->host_lock, flags);
  961. return sdev;
  962. }
  963. EXPORT_SYMBOL(scsi_device_lookup_by_target);
  964. /**
  965. * __scsi_device_lookup - find a device given the host (UNLOCKED)
  966. * @shost: SCSI host pointer
  967. * @channel: SCSI channel (zero if only one channel)
  968. * @id: SCSI target number (physical unit number)
  969. * @lun: SCSI Logical Unit Number
  970. *
  971. * Description: Looks up the scsi_device with the specified @channel, @id, @lun
  972. * for a given host. The returned scsi_device does not have an additional
  973. * reference. You must hold the host's host_lock over this call and any access
  974. * to the returned scsi_device.
  975. *
  976. * Note: The only reason why drivers would want to use this is because
  977. * they need to access the device list in irq context. Otherwise you
  978. * really want to use scsi_device_lookup instead.
  979. **/
  980. struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost,
  981. uint channel, uint id, u64 lun)
  982. {
  983. struct scsi_device *sdev;
  984. list_for_each_entry(sdev, &shost->__devices, siblings) {
  985. if (sdev->channel == channel && sdev->id == id &&
  986. sdev->lun ==lun)
  987. return sdev;
  988. }
  989. return NULL;
  990. }
  991. EXPORT_SYMBOL(__scsi_device_lookup);
  992. /**
  993. * scsi_device_lookup - find a device given the host
  994. * @shost: SCSI host pointer
  995. * @channel: SCSI channel (zero if only one channel)
  996. * @id: SCSI target number (physical unit number)
  997. * @lun: SCSI Logical Unit Number
  998. *
  999. * Description: Looks up the scsi_device with the specified @channel, @id, @lun
  1000. * for a given host. The returned scsi_device has an additional reference that
  1001. * needs to be released with scsi_device_put once you're done with it.
  1002. **/
  1003. struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost,
  1004. uint channel, uint id, u64 lun)
  1005. {
  1006. struct scsi_device *sdev;
  1007. unsigned long flags;
  1008. spin_lock_irqsave(shost->host_lock, flags);
  1009. sdev = __scsi_device_lookup(shost, channel, id, lun);
  1010. if (sdev && scsi_device_get(sdev))
  1011. sdev = NULL;
  1012. spin_unlock_irqrestore(shost->host_lock, flags);
  1013. return sdev;
  1014. }
  1015. EXPORT_SYMBOL(scsi_device_lookup);
  1016. MODULE_DESCRIPTION("SCSI core");
  1017. MODULE_LICENSE("GPL");
  1018. module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR);
  1019. MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels");
  1020. #ifdef CONFIG_SCSI_MQ_DEFAULT
  1021. bool scsi_use_blk_mq = true;
  1022. #else
  1023. bool scsi_use_blk_mq = false;
  1024. #endif
  1025. module_param_named(use_blk_mq, scsi_use_blk_mq, bool, S_IWUSR | S_IRUGO);
  1026. static int __init init_scsi(void)
  1027. {
  1028. int error;
  1029. error = scsi_init_queue();
  1030. if (error)
  1031. return error;
  1032. error = scsi_init_procfs();
  1033. if (error)
  1034. goto cleanup_queue;
  1035. error = scsi_init_devinfo();
  1036. if (error)
  1037. goto cleanup_procfs;
  1038. error = scsi_init_hosts();
  1039. if (error)
  1040. goto cleanup_devlist;
  1041. error = scsi_init_sysctl();
  1042. if (error)
  1043. goto cleanup_hosts;
  1044. error = scsi_sysfs_register();
  1045. if (error)
  1046. goto cleanup_sysctl;
  1047. scsi_netlink_init();
  1048. printk(KERN_NOTICE "SCSI subsystem initialized\n");
  1049. return 0;
  1050. cleanup_sysctl:
  1051. scsi_exit_sysctl();
  1052. cleanup_hosts:
  1053. scsi_exit_hosts();
  1054. cleanup_devlist:
  1055. scsi_exit_devinfo();
  1056. cleanup_procfs:
  1057. scsi_exit_procfs();
  1058. cleanup_queue:
  1059. scsi_exit_queue();
  1060. printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n",
  1061. -error);
  1062. return error;
  1063. }
  1064. static void __exit exit_scsi(void)
  1065. {
  1066. scsi_netlink_exit();
  1067. scsi_sysfs_unregister();
  1068. scsi_exit_sysctl();
  1069. scsi_exit_hosts();
  1070. scsi_exit_devinfo();
  1071. scsi_exit_procfs();
  1072. scsi_exit_queue();
  1073. async_unregister_domain(&scsi_sd_probe_domain);
  1074. }
  1075. subsys_initcall(init_scsi);
  1076. module_exit(exit_scsi);