scsi.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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 *vpd_buf;
  689. if (sdev->skip_vpd_pages)
  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. sdev->vpd_pg80_len = result;
  730. sdev->vpd_pg80 = vpd_buf;
  731. vpd_len = SCSI_VPD_PG_LEN;
  732. }
  733. if (pg83_supported) {
  734. retry_pg83:
  735. vpd_buf = kmalloc(vpd_len, GFP_KERNEL);
  736. if (!vpd_buf)
  737. return;
  738. result = scsi_vpd_inquiry(sdev, vpd_buf, 0x83, vpd_len);
  739. if (result < 0) {
  740. kfree(vpd_buf);
  741. return;
  742. }
  743. if (result > vpd_len) {
  744. vpd_len = result;
  745. kfree(vpd_buf);
  746. goto retry_pg83;
  747. }
  748. sdev->vpd_pg83_len = result;
  749. sdev->vpd_pg83 = vpd_buf;
  750. }
  751. }
  752. /**
  753. * scsi_report_opcode - Find out if a given command opcode is supported
  754. * @sdev: scsi device to query
  755. * @buffer: scratch buffer (must be at least 20 bytes long)
  756. * @len: length of buffer
  757. * @opcode: opcode for command to look up
  758. *
  759. * Uses the REPORT SUPPORTED OPERATION CODES to look up the given
  760. * opcode. Returns -EINVAL if RSOC fails, 0 if the command opcode is
  761. * unsupported and 1 if the device claims to support the command.
  762. */
  763. int scsi_report_opcode(struct scsi_device *sdev, unsigned char *buffer,
  764. unsigned int len, unsigned char opcode)
  765. {
  766. unsigned char cmd[16];
  767. struct scsi_sense_hdr sshdr;
  768. int result;
  769. if (sdev->no_report_opcodes || sdev->scsi_level < SCSI_SPC_3)
  770. return -EINVAL;
  771. memset(cmd, 0, 16);
  772. cmd[0] = MAINTENANCE_IN;
  773. cmd[1] = MI_REPORT_SUPPORTED_OPERATION_CODES;
  774. cmd[2] = 1; /* One command format */
  775. cmd[3] = opcode;
  776. put_unaligned_be32(len, &cmd[6]);
  777. memset(buffer, 0, len);
  778. result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
  779. &sshdr, 30 * HZ, 3, NULL);
  780. if (result && scsi_sense_valid(&sshdr) &&
  781. sshdr.sense_key == ILLEGAL_REQUEST &&
  782. (sshdr.asc == 0x20 || sshdr.asc == 0x24) && sshdr.ascq == 0x00)
  783. return -EINVAL;
  784. if ((buffer[1] & 3) == 3) /* Command supported */
  785. return 1;
  786. return 0;
  787. }
  788. EXPORT_SYMBOL(scsi_report_opcode);
  789. /**
  790. * scsi_device_get - get an additional reference to a scsi_device
  791. * @sdev: device to get a reference to
  792. *
  793. * Description: Gets a reference to the scsi_device and increments the use count
  794. * of the underlying LLDD module. You must hold host_lock of the
  795. * parent Scsi_Host or already have a reference when calling this.
  796. *
  797. * This will fail if a device is deleted or cancelled, or when the LLD module
  798. * is in the process of being unloaded.
  799. */
  800. int scsi_device_get(struct scsi_device *sdev)
  801. {
  802. if (sdev->sdev_state == SDEV_DEL || sdev->sdev_state == SDEV_CANCEL)
  803. goto fail;
  804. if (!get_device(&sdev->sdev_gendev))
  805. goto fail;
  806. if (!try_module_get(sdev->host->hostt->module))
  807. goto fail_put_device;
  808. return 0;
  809. fail_put_device:
  810. put_device(&sdev->sdev_gendev);
  811. fail:
  812. return -ENXIO;
  813. }
  814. EXPORT_SYMBOL(scsi_device_get);
  815. /**
  816. * scsi_device_put - release a reference to a scsi_device
  817. * @sdev: device to release a reference on.
  818. *
  819. * Description: Release a reference to the scsi_device and decrements the use
  820. * count of the underlying LLDD module. The device is freed once the last
  821. * user vanishes.
  822. */
  823. void scsi_device_put(struct scsi_device *sdev)
  824. {
  825. module_put(sdev->host->hostt->module);
  826. put_device(&sdev->sdev_gendev);
  827. }
  828. EXPORT_SYMBOL(scsi_device_put);
  829. /* helper for shost_for_each_device, see that for documentation */
  830. struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost,
  831. struct scsi_device *prev)
  832. {
  833. struct list_head *list = (prev ? &prev->siblings : &shost->__devices);
  834. struct scsi_device *next = NULL;
  835. unsigned long flags;
  836. spin_lock_irqsave(shost->host_lock, flags);
  837. while (list->next != &shost->__devices) {
  838. next = list_entry(list->next, struct scsi_device, siblings);
  839. /* skip devices that we can't get a reference to */
  840. if (!scsi_device_get(next))
  841. break;
  842. next = NULL;
  843. list = list->next;
  844. }
  845. spin_unlock_irqrestore(shost->host_lock, flags);
  846. if (prev)
  847. scsi_device_put(prev);
  848. return next;
  849. }
  850. EXPORT_SYMBOL(__scsi_iterate_devices);
  851. /**
  852. * starget_for_each_device - helper to walk all devices of a target
  853. * @starget: target whose devices we want to iterate over.
  854. * @data: Opaque passed to each function call.
  855. * @fn: Function to call on each device
  856. *
  857. * This traverses over each device of @starget. The devices have
  858. * a reference that must be released by scsi_host_put when breaking
  859. * out of the loop.
  860. */
  861. void starget_for_each_device(struct scsi_target *starget, void *data,
  862. void (*fn)(struct scsi_device *, void *))
  863. {
  864. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  865. struct scsi_device *sdev;
  866. shost_for_each_device(sdev, shost) {
  867. if ((sdev->channel == starget->channel) &&
  868. (sdev->id == starget->id))
  869. fn(sdev, data);
  870. }
  871. }
  872. EXPORT_SYMBOL(starget_for_each_device);
  873. /**
  874. * __starget_for_each_device - helper to walk all devices of a target (UNLOCKED)
  875. * @starget: target whose devices we want to iterate over.
  876. * @data: parameter for callback @fn()
  877. * @fn: callback function that is invoked for each device
  878. *
  879. * This traverses over each device of @starget. It does _not_
  880. * take a reference on the scsi_device, so the whole loop must be
  881. * protected by shost->host_lock.
  882. *
  883. * Note: The only reason why drivers would want to use this is because
  884. * they need to access the device list in irq context. Otherwise you
  885. * really want to use starget_for_each_device instead.
  886. **/
  887. void __starget_for_each_device(struct scsi_target *starget, void *data,
  888. void (*fn)(struct scsi_device *, void *))
  889. {
  890. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  891. struct scsi_device *sdev;
  892. __shost_for_each_device(sdev, shost) {
  893. if ((sdev->channel == starget->channel) &&
  894. (sdev->id == starget->id))
  895. fn(sdev, data);
  896. }
  897. }
  898. EXPORT_SYMBOL(__starget_for_each_device);
  899. /**
  900. * __scsi_device_lookup_by_target - find a device given the target (UNLOCKED)
  901. * @starget: SCSI target pointer
  902. * @lun: SCSI Logical Unit Number
  903. *
  904. * Description: Looks up the scsi_device with the specified @lun for a given
  905. * @starget. The returned scsi_device does not have an additional
  906. * reference. You must hold the host's host_lock over this call and
  907. * any access to the returned scsi_device. A scsi_device in state
  908. * SDEV_DEL is skipped.
  909. *
  910. * Note: The only reason why drivers should use this is because
  911. * they need to access the device list in irq context. Otherwise you
  912. * really want to use scsi_device_lookup_by_target instead.
  913. **/
  914. struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget,
  915. u64 lun)
  916. {
  917. struct scsi_device *sdev;
  918. list_for_each_entry(sdev, &starget->devices, same_target_siblings) {
  919. if (sdev->sdev_state == SDEV_DEL)
  920. continue;
  921. if (sdev->lun ==lun)
  922. return sdev;
  923. }
  924. return NULL;
  925. }
  926. EXPORT_SYMBOL(__scsi_device_lookup_by_target);
  927. /**
  928. * scsi_device_lookup_by_target - find a device given the target
  929. * @starget: SCSI target pointer
  930. * @lun: SCSI Logical Unit Number
  931. *
  932. * Description: Looks up the scsi_device with the specified @lun for a given
  933. * @starget. The returned scsi_device has an additional reference that
  934. * needs to be released with scsi_device_put once you're done with it.
  935. **/
  936. struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget,
  937. u64 lun)
  938. {
  939. struct scsi_device *sdev;
  940. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  941. unsigned long flags;
  942. spin_lock_irqsave(shost->host_lock, flags);
  943. sdev = __scsi_device_lookup_by_target(starget, lun);
  944. if (sdev && scsi_device_get(sdev))
  945. sdev = NULL;
  946. spin_unlock_irqrestore(shost->host_lock, flags);
  947. return sdev;
  948. }
  949. EXPORT_SYMBOL(scsi_device_lookup_by_target);
  950. /**
  951. * __scsi_device_lookup - find a device given the host (UNLOCKED)
  952. * @shost: SCSI host pointer
  953. * @channel: SCSI channel (zero if only one channel)
  954. * @id: SCSI target number (physical unit number)
  955. * @lun: SCSI Logical Unit Number
  956. *
  957. * Description: Looks up the scsi_device with the specified @channel, @id, @lun
  958. * for a given host. The returned scsi_device does not have an additional
  959. * reference. You must hold the host's host_lock over this call and any access
  960. * to the returned scsi_device.
  961. *
  962. * Note: The only reason why drivers would want to use this is because
  963. * they need to access the device list in irq context. Otherwise you
  964. * really want to use scsi_device_lookup instead.
  965. **/
  966. struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost,
  967. uint channel, uint id, u64 lun)
  968. {
  969. struct scsi_device *sdev;
  970. list_for_each_entry(sdev, &shost->__devices, siblings) {
  971. if (sdev->channel == channel && sdev->id == id &&
  972. sdev->lun ==lun)
  973. return sdev;
  974. }
  975. return NULL;
  976. }
  977. EXPORT_SYMBOL(__scsi_device_lookup);
  978. /**
  979. * scsi_device_lookup - find a device given the host
  980. * @shost: SCSI host pointer
  981. * @channel: SCSI channel (zero if only one channel)
  982. * @id: SCSI target number (physical unit number)
  983. * @lun: SCSI Logical Unit Number
  984. *
  985. * Description: Looks up the scsi_device with the specified @channel, @id, @lun
  986. * for a given host. The returned scsi_device has an additional reference that
  987. * needs to be released with scsi_device_put once you're done with it.
  988. **/
  989. struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost,
  990. uint channel, uint id, u64 lun)
  991. {
  992. struct scsi_device *sdev;
  993. unsigned long flags;
  994. spin_lock_irqsave(shost->host_lock, flags);
  995. sdev = __scsi_device_lookup(shost, channel, id, lun);
  996. if (sdev && scsi_device_get(sdev))
  997. sdev = NULL;
  998. spin_unlock_irqrestore(shost->host_lock, flags);
  999. return sdev;
  1000. }
  1001. EXPORT_SYMBOL(scsi_device_lookup);
  1002. MODULE_DESCRIPTION("SCSI core");
  1003. MODULE_LICENSE("GPL");
  1004. module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR);
  1005. MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels");
  1006. #ifdef CONFIG_SCSI_MQ_DEFAULT
  1007. bool scsi_use_blk_mq = true;
  1008. #else
  1009. bool scsi_use_blk_mq = false;
  1010. #endif
  1011. module_param_named(use_blk_mq, scsi_use_blk_mq, bool, S_IWUSR | S_IRUGO);
  1012. static int __init init_scsi(void)
  1013. {
  1014. int error;
  1015. error = scsi_init_queue();
  1016. if (error)
  1017. return error;
  1018. error = scsi_init_procfs();
  1019. if (error)
  1020. goto cleanup_queue;
  1021. error = scsi_init_devinfo();
  1022. if (error)
  1023. goto cleanup_procfs;
  1024. error = scsi_init_hosts();
  1025. if (error)
  1026. goto cleanup_devlist;
  1027. error = scsi_init_sysctl();
  1028. if (error)
  1029. goto cleanup_hosts;
  1030. error = scsi_sysfs_register();
  1031. if (error)
  1032. goto cleanup_sysctl;
  1033. scsi_netlink_init();
  1034. printk(KERN_NOTICE "SCSI subsystem initialized\n");
  1035. return 0;
  1036. cleanup_sysctl:
  1037. scsi_exit_sysctl();
  1038. cleanup_hosts:
  1039. scsi_exit_hosts();
  1040. cleanup_devlist:
  1041. scsi_exit_devinfo();
  1042. cleanup_procfs:
  1043. scsi_exit_procfs();
  1044. cleanup_queue:
  1045. scsi_exit_queue();
  1046. printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n",
  1047. -error);
  1048. return error;
  1049. }
  1050. static void __exit exit_scsi(void)
  1051. {
  1052. scsi_netlink_exit();
  1053. scsi_sysfs_unregister();
  1054. scsi_exit_sysctl();
  1055. scsi_exit_hosts();
  1056. scsi_exit_devinfo();
  1057. scsi_exit_procfs();
  1058. scsi_exit_queue();
  1059. async_unregister_domain(&scsi_sd_probe_domain);
  1060. }
  1061. subsys_initcall(init_scsi);
  1062. module_exit(exit_scsi);