scsi.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. /*
  2. * scsi.c Copyright (C) 1992 Drew Eckhardt
  3. * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
  4. * Copyright (C) 2002, 2003 Christoph Hellwig
  5. *
  6. * generic mid-level SCSI driver
  7. * Initial versions: Drew Eckhardt
  8. * Subsequent revisions: Eric Youngdale
  9. *
  10. * <drew@colorado.edu>
  11. *
  12. * Bug correction thanks go to :
  13. * Rik Faith <faith@cs.unc.edu>
  14. * Tommy Thorn <tthorn>
  15. * Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de>
  16. *
  17. * Modified by Eric Youngdale eric@andante.org or ericy@gnu.ai.mit.edu to
  18. * add scatter-gather, multiple outstanding request, and other
  19. * enhancements.
  20. *
  21. * Native multichannel, wide scsi, /proc/scsi and hot plugging
  22. * support added by Michael Neuffer <mike@i-connect.net>
  23. *
  24. * Added request_module("scsi_hostadapter") for kerneld:
  25. * (Put an "alias scsi_hostadapter your_hostadapter" in /etc/modprobe.conf)
  26. * Bjorn Ekwall <bj0rn@blox.se>
  27. * (changed to kmod)
  28. *
  29. * Major improvements to the timeout, abort, and reset processing,
  30. * as well as performance modifications for large queue depths by
  31. * Leonard N. Zubkoff <lnz@dandelion.com>
  32. *
  33. * Converted cli() code to spinlocks, Ingo Molnar
  34. *
  35. * Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli
  36. *
  37. * out_of_space hacks, D. Gilbert (dpg) 990608
  38. */
  39. #include <linux/module.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/kernel.h>
  42. #include <linux/timer.h>
  43. #include <linux/string.h>
  44. #include <linux/slab.h>
  45. #include <linux/blkdev.h>
  46. #include <linux/delay.h>
  47. #include <linux/init.h>
  48. #include <linux/completion.h>
  49. #include <linux/unistd.h>
  50. #include <linux/spinlock.h>
  51. #include <linux/kmod.h>
  52. #include <linux/interrupt.h>
  53. #include <linux/notifier.h>
  54. #include <linux/cpu.h>
  55. #include <linux/mutex.h>
  56. #include <scsi/scsi.h>
  57. #include <scsi/scsi_cmnd.h>
  58. #include <scsi/scsi_dbg.h>
  59. #include <scsi/scsi_device.h>
  60. #include <scsi/scsi_eh.h>
  61. #include <scsi/scsi_host.h>
  62. #include <scsi/scsi_tcq.h>
  63. #include "scsi_priv.h"
  64. #include "scsi_logging.h"
  65. static void scsi_done(struct scsi_cmnd *cmd);
  66. /*
  67. * Definitions and constants.
  68. */
  69. #define MIN_RESET_DELAY (2*HZ)
  70. /* Do not call reset on error if we just did a reset within 15 sec. */
  71. #define MIN_RESET_PERIOD (15*HZ)
  72. /*
  73. * Macro to determine the size of SCSI command. This macro takes vendor
  74. * unique commands into account. SCSI commands in groups 6 and 7 are
  75. * vendor unique and we will depend upon the command length being
  76. * supplied correctly in cmd_len.
  77. */
  78. #define CDB_SIZE(cmd) (((((cmd)->cmnd[0] >> 5) & 7) < 6) ? \
  79. COMMAND_SIZE((cmd)->cmnd[0]) : (cmd)->cmd_len)
  80. /*
  81. * Note - the initial logging level can be set here to log events at boot time.
  82. * After the system is up, you may enable logging via the /proc interface.
  83. */
  84. unsigned int scsi_logging_level;
  85. #if defined(CONFIG_SCSI_LOGGING)
  86. EXPORT_SYMBOL(scsi_logging_level);
  87. #endif
  88. /* NB: These are exposed through /proc/scsi/scsi and form part of the ABI.
  89. * You may not alter any existing entry (although adding new ones is
  90. * encouraged once assigned by ANSI/INCITS T10
  91. */
  92. static const char *const scsi_device_types[] = {
  93. "Direct-Access ",
  94. "Sequential-Access",
  95. "Printer ",
  96. "Processor ",
  97. "WORM ",
  98. "CD-ROM ",
  99. "Scanner ",
  100. "Optical Device ",
  101. "Medium Changer ",
  102. "Communications ",
  103. "ASC IT8 ",
  104. "ASC IT8 ",
  105. "RAID ",
  106. "Enclosure ",
  107. "Direct-Access-RBC",
  108. "Optical card ",
  109. "Bridge controller",
  110. "Object storage ",
  111. "Automation/Drive ",
  112. };
  113. const char * scsi_device_type(unsigned type)
  114. {
  115. if (type == 0x1e)
  116. return "Well-known LUN ";
  117. if (type == 0x1f)
  118. return "No Device ";
  119. if (type >= ARRAY_SIZE(scsi_device_types))
  120. return "Unknown ";
  121. return scsi_device_types[type];
  122. }
  123. EXPORT_SYMBOL(scsi_device_type);
  124. struct scsi_host_cmd_pool {
  125. struct kmem_cache *slab;
  126. unsigned int users;
  127. char *name;
  128. unsigned int slab_flags;
  129. gfp_t gfp_mask;
  130. };
  131. static struct scsi_host_cmd_pool scsi_cmd_pool = {
  132. .name = "scsi_cmd_cache",
  133. .slab_flags = SLAB_HWCACHE_ALIGN,
  134. };
  135. static struct scsi_host_cmd_pool scsi_cmd_dma_pool = {
  136. .name = "scsi_cmd_cache(DMA)",
  137. .slab_flags = SLAB_HWCACHE_ALIGN|SLAB_CACHE_DMA,
  138. .gfp_mask = __GFP_DMA,
  139. };
  140. static DEFINE_MUTEX(host_cmd_pool_mutex);
  141. struct scsi_cmnd *__scsi_get_command(struct Scsi_Host *shost, gfp_t gfp_mask)
  142. {
  143. struct scsi_cmnd *cmd;
  144. cmd = kmem_cache_alloc(shost->cmd_pool->slab,
  145. gfp_mask | shost->cmd_pool->gfp_mask);
  146. if (unlikely(!cmd)) {
  147. unsigned long flags;
  148. spin_lock_irqsave(&shost->free_list_lock, flags);
  149. if (likely(!list_empty(&shost->free_list))) {
  150. cmd = list_entry(shost->free_list.next,
  151. struct scsi_cmnd, list);
  152. list_del_init(&cmd->list);
  153. }
  154. spin_unlock_irqrestore(&shost->free_list_lock, flags);
  155. }
  156. return cmd;
  157. }
  158. EXPORT_SYMBOL_GPL(__scsi_get_command);
  159. /*
  160. * Function: scsi_get_command()
  161. *
  162. * Purpose: Allocate and setup a scsi command block
  163. *
  164. * Arguments: dev - parent scsi device
  165. * gfp_mask- allocator flags
  166. *
  167. * Returns: The allocated scsi command structure.
  168. */
  169. struct scsi_cmnd *scsi_get_command(struct scsi_device *dev, gfp_t gfp_mask)
  170. {
  171. struct scsi_cmnd *cmd;
  172. /* Bail if we can't get a reference to the device */
  173. if (!get_device(&dev->sdev_gendev))
  174. return NULL;
  175. cmd = __scsi_get_command(dev->host, gfp_mask);
  176. if (likely(cmd != NULL)) {
  177. unsigned long flags;
  178. memset(cmd, 0, sizeof(*cmd));
  179. cmd->device = dev;
  180. init_timer(&cmd->eh_timeout);
  181. INIT_LIST_HEAD(&cmd->list);
  182. spin_lock_irqsave(&dev->list_lock, flags);
  183. list_add_tail(&cmd->list, &dev->cmd_list);
  184. spin_unlock_irqrestore(&dev->list_lock, flags);
  185. cmd->jiffies_at_alloc = jiffies;
  186. } else
  187. put_device(&dev->sdev_gendev);
  188. return cmd;
  189. }
  190. EXPORT_SYMBOL(scsi_get_command);
  191. void __scsi_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd,
  192. struct device *dev)
  193. {
  194. unsigned long flags;
  195. /* changing locks here, don't need to restore the irq state */
  196. spin_lock_irqsave(&shost->free_list_lock, flags);
  197. if (unlikely(list_empty(&shost->free_list))) {
  198. list_add(&cmd->list, &shost->free_list);
  199. cmd = NULL;
  200. }
  201. spin_unlock_irqrestore(&shost->free_list_lock, flags);
  202. if (likely(cmd != NULL))
  203. kmem_cache_free(shost->cmd_pool->slab, cmd);
  204. put_device(dev);
  205. }
  206. EXPORT_SYMBOL(__scsi_put_command);
  207. /*
  208. * Function: scsi_put_command()
  209. *
  210. * Purpose: Free a scsi command block
  211. *
  212. * Arguments: cmd - command block to free
  213. *
  214. * Returns: Nothing.
  215. *
  216. * Notes: The command must not belong to any lists.
  217. */
  218. void scsi_put_command(struct scsi_cmnd *cmd)
  219. {
  220. struct scsi_device *sdev = cmd->device;
  221. unsigned long flags;
  222. /* serious error if the command hasn't come from a device list */
  223. spin_lock_irqsave(&cmd->device->list_lock, flags);
  224. BUG_ON(list_empty(&cmd->list));
  225. list_del_init(&cmd->list);
  226. spin_unlock_irqrestore(&cmd->device->list_lock, flags);
  227. __scsi_put_command(cmd->device->host, cmd, &sdev->sdev_gendev);
  228. }
  229. EXPORT_SYMBOL(scsi_put_command);
  230. /*
  231. * Function: scsi_setup_command_freelist()
  232. *
  233. * Purpose: Setup the command freelist for a scsi host.
  234. *
  235. * Arguments: shost - host to allocate the freelist for.
  236. *
  237. * Returns: Nothing.
  238. */
  239. int scsi_setup_command_freelist(struct Scsi_Host *shost)
  240. {
  241. struct scsi_host_cmd_pool *pool;
  242. struct scsi_cmnd *cmd;
  243. spin_lock_init(&shost->free_list_lock);
  244. INIT_LIST_HEAD(&shost->free_list);
  245. /*
  246. * Select a command slab for this host and create it if not
  247. * yet existant.
  248. */
  249. mutex_lock(&host_cmd_pool_mutex);
  250. pool = (shost->unchecked_isa_dma ? &scsi_cmd_dma_pool : &scsi_cmd_pool);
  251. if (!pool->users) {
  252. pool->slab = kmem_cache_create(pool->name,
  253. sizeof(struct scsi_cmnd), 0,
  254. pool->slab_flags, NULL, NULL);
  255. if (!pool->slab)
  256. goto fail;
  257. }
  258. pool->users++;
  259. shost->cmd_pool = pool;
  260. mutex_unlock(&host_cmd_pool_mutex);
  261. /*
  262. * Get one backup command for this host.
  263. */
  264. cmd = kmem_cache_alloc(shost->cmd_pool->slab,
  265. GFP_KERNEL | shost->cmd_pool->gfp_mask);
  266. if (!cmd)
  267. goto fail2;
  268. list_add(&cmd->list, &shost->free_list);
  269. return 0;
  270. fail2:
  271. if (!--pool->users)
  272. kmem_cache_destroy(pool->slab);
  273. return -ENOMEM;
  274. fail:
  275. mutex_unlock(&host_cmd_pool_mutex);
  276. return -ENOMEM;
  277. }
  278. /*
  279. * Function: scsi_destroy_command_freelist()
  280. *
  281. * Purpose: Release the command freelist for a scsi host.
  282. *
  283. * Arguments: shost - host that's freelist is going to be destroyed
  284. */
  285. void scsi_destroy_command_freelist(struct Scsi_Host *shost)
  286. {
  287. while (!list_empty(&shost->free_list)) {
  288. struct scsi_cmnd *cmd;
  289. cmd = list_entry(shost->free_list.next, struct scsi_cmnd, list);
  290. list_del_init(&cmd->list);
  291. kmem_cache_free(shost->cmd_pool->slab, cmd);
  292. }
  293. mutex_lock(&host_cmd_pool_mutex);
  294. if (!--shost->cmd_pool->users)
  295. kmem_cache_destroy(shost->cmd_pool->slab);
  296. mutex_unlock(&host_cmd_pool_mutex);
  297. }
  298. #ifdef CONFIG_SCSI_LOGGING
  299. void scsi_log_send(struct scsi_cmnd *cmd)
  300. {
  301. unsigned int level;
  302. /*
  303. * If ML QUEUE log level is greater than or equal to:
  304. *
  305. * 1: nothing (match completion)
  306. *
  307. * 2: log opcode + command of all commands
  308. *
  309. * 3: same as 2 plus dump cmd address
  310. *
  311. * 4: same as 3 plus dump extra junk
  312. */
  313. if (unlikely(scsi_logging_level)) {
  314. level = SCSI_LOG_LEVEL(SCSI_LOG_MLQUEUE_SHIFT,
  315. SCSI_LOG_MLQUEUE_BITS);
  316. if (level > 1) {
  317. scmd_printk(KERN_INFO, cmd, "Send: ");
  318. if (level > 2)
  319. printk("0x%p ", cmd);
  320. printk("\n");
  321. scsi_print_command(cmd);
  322. if (level > 3) {
  323. printk(KERN_INFO "buffer = 0x%p, bufflen = %d,"
  324. " done = 0x%p, queuecommand 0x%p\n",
  325. cmd->request_buffer, cmd->request_bufflen,
  326. cmd->done,
  327. cmd->device->host->hostt->queuecommand);
  328. }
  329. }
  330. }
  331. }
  332. void scsi_log_completion(struct scsi_cmnd *cmd, int disposition)
  333. {
  334. unsigned int level;
  335. /*
  336. * If ML COMPLETE log level is greater than or equal to:
  337. *
  338. * 1: log disposition, result, opcode + command, and conditionally
  339. * sense data for failures or non SUCCESS dispositions.
  340. *
  341. * 2: same as 1 but for all command completions.
  342. *
  343. * 3: same as 2 plus dump cmd address
  344. *
  345. * 4: same as 3 plus dump extra junk
  346. */
  347. if (unlikely(scsi_logging_level)) {
  348. level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
  349. SCSI_LOG_MLCOMPLETE_BITS);
  350. if (((level > 0) && (cmd->result || disposition != SUCCESS)) ||
  351. (level > 1)) {
  352. scmd_printk(KERN_INFO, cmd, "Done: ");
  353. if (level > 2)
  354. printk("0x%p ", cmd);
  355. /*
  356. * Dump truncated values, so we usually fit within
  357. * 80 chars.
  358. */
  359. switch (disposition) {
  360. case SUCCESS:
  361. printk("SUCCESS\n");
  362. break;
  363. case NEEDS_RETRY:
  364. printk("RETRY\n");
  365. break;
  366. case ADD_TO_MLQUEUE:
  367. printk("MLQUEUE\n");
  368. break;
  369. case FAILED:
  370. printk("FAILED\n");
  371. break;
  372. case TIMEOUT_ERROR:
  373. /*
  374. * If called via scsi_times_out.
  375. */
  376. printk("TIMEOUT\n");
  377. break;
  378. default:
  379. printk("UNKNOWN\n");
  380. }
  381. scsi_print_result(cmd);
  382. scsi_print_command(cmd);
  383. if (status_byte(cmd->result) & CHECK_CONDITION)
  384. scsi_print_sense("", cmd);
  385. if (level > 3)
  386. scmd_printk(KERN_INFO, cmd,
  387. "scsi host busy %d failed %d\n",
  388. cmd->device->host->host_busy,
  389. cmd->device->host->host_failed);
  390. }
  391. }
  392. }
  393. #endif
  394. /*
  395. * Assign a serial number and pid to the request for error recovery
  396. * and debugging purposes. Protected by the Host_Lock of host.
  397. */
  398. static inline void scsi_cmd_get_serial(struct Scsi_Host *host, struct scsi_cmnd *cmd)
  399. {
  400. cmd->serial_number = host->cmd_serial_number++;
  401. if (cmd->serial_number == 0)
  402. cmd->serial_number = host->cmd_serial_number++;
  403. cmd->pid = host->cmd_pid++;
  404. if (cmd->pid == 0)
  405. cmd->pid = host->cmd_pid++;
  406. }
  407. /*
  408. * Function: scsi_dispatch_command
  409. *
  410. * Purpose: Dispatch a command to the low-level driver.
  411. *
  412. * Arguments: cmd - command block we are dispatching.
  413. *
  414. * Notes:
  415. */
  416. int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
  417. {
  418. struct Scsi_Host *host = cmd->device->host;
  419. unsigned long flags = 0;
  420. unsigned long timeout;
  421. int rtn = 0;
  422. /* check if the device is still usable */
  423. if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
  424. /* in SDEV_DEL we error all commands. DID_NO_CONNECT
  425. * returns an immediate error upwards, and signals
  426. * that the device is no longer present */
  427. cmd->result = DID_NO_CONNECT << 16;
  428. atomic_inc(&cmd->device->iorequest_cnt);
  429. __scsi_done(cmd);
  430. /* return 0 (because the command has been processed) */
  431. goto out;
  432. }
  433. /* Check to see if the scsi lld put this device into state SDEV_BLOCK. */
  434. if (unlikely(cmd->device->sdev_state == SDEV_BLOCK)) {
  435. /*
  436. * in SDEV_BLOCK, the command is just put back on the device
  437. * queue. The suspend state has already blocked the queue so
  438. * future requests should not occur until the device
  439. * transitions out of the suspend state.
  440. */
  441. scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
  442. SCSI_LOG_MLQUEUE(3, printk("queuecommand : device blocked \n"));
  443. /*
  444. * NOTE: rtn is still zero here because we don't need the
  445. * queue to be plugged on return (it's already stopped)
  446. */
  447. goto out;
  448. }
  449. /*
  450. * If SCSI-2 or lower, store the LUN value in cmnd.
  451. */
  452. if (cmd->device->scsi_level <= SCSI_2 &&
  453. cmd->device->scsi_level != SCSI_UNKNOWN) {
  454. cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
  455. (cmd->device->lun << 5 & 0xe0);
  456. }
  457. /*
  458. * We will wait MIN_RESET_DELAY clock ticks after the last reset so
  459. * we can avoid the drive not being ready.
  460. */
  461. timeout = host->last_reset + MIN_RESET_DELAY;
  462. if (host->resetting && time_before(jiffies, timeout)) {
  463. int ticks_remaining = timeout - jiffies;
  464. /*
  465. * NOTE: This may be executed from within an interrupt
  466. * handler! This is bad, but for now, it'll do. The irq
  467. * level of the interrupt handler has been masked out by the
  468. * platform dependent interrupt handling code already, so the
  469. * sti() here will not cause another call to the SCSI host's
  470. * interrupt handler (assuming there is one irq-level per
  471. * host).
  472. */
  473. while (--ticks_remaining >= 0)
  474. mdelay(1 + 999 / HZ);
  475. host->resetting = 0;
  476. }
  477. /*
  478. * AK: unlikely race here: for some reason the timer could
  479. * expire before the serial number is set up below.
  480. */
  481. scsi_add_timer(cmd, cmd->timeout_per_command, scsi_times_out);
  482. scsi_log_send(cmd);
  483. /*
  484. * We will use a queued command if possible, otherwise we will
  485. * emulate the queuing and calling of completion function ourselves.
  486. */
  487. atomic_inc(&cmd->device->iorequest_cnt);
  488. /*
  489. * Before we queue this command, check if the command
  490. * length exceeds what the host adapter can handle.
  491. */
  492. if (CDB_SIZE(cmd) > cmd->device->host->max_cmd_len) {
  493. SCSI_LOG_MLQUEUE(3,
  494. printk("queuecommand : command too long.\n"));
  495. cmd->result = (DID_ABORT << 16);
  496. scsi_done(cmd);
  497. goto out;
  498. }
  499. spin_lock_irqsave(host->host_lock, flags);
  500. scsi_cmd_get_serial(host, cmd);
  501. if (unlikely(host->shost_state == SHOST_DEL)) {
  502. cmd->result = (DID_NO_CONNECT << 16);
  503. scsi_done(cmd);
  504. } else {
  505. rtn = host->hostt->queuecommand(cmd, scsi_done);
  506. }
  507. spin_unlock_irqrestore(host->host_lock, flags);
  508. if (rtn) {
  509. if (scsi_delete_timer(cmd)) {
  510. atomic_inc(&cmd->device->iodone_cnt);
  511. scsi_queue_insert(cmd,
  512. (rtn == SCSI_MLQUEUE_DEVICE_BUSY) ?
  513. rtn : SCSI_MLQUEUE_HOST_BUSY);
  514. }
  515. SCSI_LOG_MLQUEUE(3,
  516. printk("queuecommand : request rejected\n"));
  517. }
  518. out:
  519. SCSI_LOG_MLQUEUE(3, printk("leaving scsi_dispatch_cmnd()\n"));
  520. return rtn;
  521. }
  522. /**
  523. * scsi_req_abort_cmd -- Request command recovery for the specified command
  524. * cmd: pointer to the SCSI command of interest
  525. *
  526. * This function requests that SCSI Core start recovery for the
  527. * command by deleting the timer and adding the command to the eh
  528. * queue. It can be called by either LLDDs or SCSI Core. LLDDs who
  529. * implement their own error recovery MAY ignore the timeout event if
  530. * they generated scsi_req_abort_cmd.
  531. */
  532. void scsi_req_abort_cmd(struct scsi_cmnd *cmd)
  533. {
  534. if (!scsi_delete_timer(cmd))
  535. return;
  536. scsi_times_out(cmd);
  537. }
  538. EXPORT_SYMBOL(scsi_req_abort_cmd);
  539. /**
  540. * scsi_done - Enqueue the finished SCSI command into the done queue.
  541. * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
  542. * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
  543. *
  544. * This function is the mid-level's (SCSI Core) interrupt routine, which
  545. * regains ownership of the SCSI command (de facto) from a LLDD, and enqueues
  546. * the command to the done queue for further processing.
  547. *
  548. * This is the producer of the done queue who enqueues at the tail.
  549. *
  550. * This function is interrupt context safe.
  551. */
  552. static void scsi_done(struct scsi_cmnd *cmd)
  553. {
  554. /*
  555. * We don't have to worry about this one timing out any more.
  556. * If we are unable to remove the timer, then the command
  557. * has already timed out. In which case, we have no choice but to
  558. * let the timeout function run, as we have no idea where in fact
  559. * that function could really be. It might be on another processor,
  560. * etc, etc.
  561. */
  562. if (!scsi_delete_timer(cmd))
  563. return;
  564. __scsi_done(cmd);
  565. }
  566. /* Private entry to scsi_done() to complete a command when the timer
  567. * isn't running --- used by scsi_times_out */
  568. void __scsi_done(struct scsi_cmnd *cmd)
  569. {
  570. struct request *rq = cmd->request;
  571. /*
  572. * Set the serial numbers back to zero
  573. */
  574. cmd->serial_number = 0;
  575. atomic_inc(&cmd->device->iodone_cnt);
  576. if (cmd->result)
  577. atomic_inc(&cmd->device->ioerr_cnt);
  578. BUG_ON(!rq);
  579. /*
  580. * The uptodate/nbytes values don't matter, as we allow partial
  581. * completes and thus will check this in the softirq callback
  582. */
  583. rq->completion_data = cmd;
  584. blk_complete_request(rq);
  585. }
  586. /*
  587. * Function: scsi_finish_command
  588. *
  589. * Purpose: Pass command off to upper layer for finishing of I/O
  590. * request, waking processes that are waiting on results,
  591. * etc.
  592. */
  593. void scsi_finish_command(struct scsi_cmnd *cmd)
  594. {
  595. struct scsi_device *sdev = cmd->device;
  596. struct Scsi_Host *shost = sdev->host;
  597. scsi_device_unbusy(sdev);
  598. /*
  599. * Clear the flags which say that the device/host is no longer
  600. * capable of accepting new commands. These are set in scsi_queue.c
  601. * for both the queue full condition on a device, and for a
  602. * host full condition on the host.
  603. *
  604. * XXX(hch): What about locking?
  605. */
  606. shost->host_blocked = 0;
  607. sdev->device_blocked = 0;
  608. /*
  609. * If we have valid sense information, then some kind of recovery
  610. * must have taken place. Make a note of this.
  611. */
  612. if (SCSI_SENSE_VALID(cmd))
  613. cmd->result |= (DRIVER_SENSE << 24);
  614. SCSI_LOG_MLCOMPLETE(4, sdev_printk(KERN_INFO, sdev,
  615. "Notifying upper driver of completion "
  616. "(result %x)\n", cmd->result));
  617. cmd->done(cmd);
  618. }
  619. EXPORT_SYMBOL(scsi_finish_command);
  620. /*
  621. * Function: scsi_adjust_queue_depth()
  622. *
  623. * Purpose: Allow low level drivers to tell us to change the queue depth
  624. * on a specific SCSI device
  625. *
  626. * Arguments: sdev - SCSI Device in question
  627. * tagged - Do we use tagged queueing (non-0) or do we treat
  628. * this device as an untagged device (0)
  629. * tags - Number of tags allowed if tagged queueing enabled,
  630. * or number of commands the low level driver can
  631. * queue up in non-tagged mode (as per cmd_per_lun).
  632. *
  633. * Returns: Nothing
  634. *
  635. * Lock Status: None held on entry
  636. *
  637. * Notes: Low level drivers may call this at any time and we will do
  638. * the right thing depending on whether or not the device is
  639. * currently active and whether or not it even has the
  640. * command blocks built yet.
  641. */
  642. void scsi_adjust_queue_depth(struct scsi_device *sdev, int tagged, int tags)
  643. {
  644. unsigned long flags;
  645. /*
  646. * refuse to set tagged depth to an unworkable size
  647. */
  648. if (tags <= 0)
  649. return;
  650. spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
  651. /* Check to see if the queue is managed by the block layer
  652. * if it is, and we fail to adjust the depth, exit */
  653. if (blk_queue_tagged(sdev->request_queue) &&
  654. blk_queue_resize_tags(sdev->request_queue, tags) != 0)
  655. goto out;
  656. sdev->queue_depth = tags;
  657. switch (tagged) {
  658. case MSG_ORDERED_TAG:
  659. sdev->ordered_tags = 1;
  660. sdev->simple_tags = 1;
  661. break;
  662. case MSG_SIMPLE_TAG:
  663. sdev->ordered_tags = 0;
  664. sdev->simple_tags = 1;
  665. break;
  666. default:
  667. sdev_printk(KERN_WARNING, sdev,
  668. "scsi_adjust_queue_depth, bad queue type, "
  669. "disabled\n");
  670. case 0:
  671. sdev->ordered_tags = sdev->simple_tags = 0;
  672. sdev->queue_depth = tags;
  673. break;
  674. }
  675. out:
  676. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  677. }
  678. EXPORT_SYMBOL(scsi_adjust_queue_depth);
  679. /*
  680. * Function: scsi_track_queue_full()
  681. *
  682. * Purpose: This function will track successive QUEUE_FULL events on a
  683. * specific SCSI device to determine if and when there is a
  684. * need to adjust the queue depth on the device.
  685. *
  686. * Arguments: sdev - SCSI Device in question
  687. * depth - Current number of outstanding SCSI commands on
  688. * this device, not counting the one returned as
  689. * QUEUE_FULL.
  690. *
  691. * Returns: 0 - No change needed
  692. * >0 - Adjust queue depth to this new depth
  693. * -1 - Drop back to untagged operation using host->cmd_per_lun
  694. * as the untagged command depth
  695. *
  696. * Lock Status: None held on entry
  697. *
  698. * Notes: Low level drivers may call this at any time and we will do
  699. * "The Right Thing." We are interrupt context safe.
  700. */
  701. int scsi_track_queue_full(struct scsi_device *sdev, int depth)
  702. {
  703. if ((jiffies >> 4) == sdev->last_queue_full_time)
  704. return 0;
  705. sdev->last_queue_full_time = (jiffies >> 4);
  706. if (sdev->last_queue_full_depth != depth) {
  707. sdev->last_queue_full_count = 1;
  708. sdev->last_queue_full_depth = depth;
  709. } else {
  710. sdev->last_queue_full_count++;
  711. }
  712. if (sdev->last_queue_full_count <= 10)
  713. return 0;
  714. if (sdev->last_queue_full_depth < 8) {
  715. /* Drop back to untagged */
  716. scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
  717. return -1;
  718. }
  719. if (sdev->ordered_tags)
  720. scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, depth);
  721. else
  722. scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
  723. return depth;
  724. }
  725. EXPORT_SYMBOL(scsi_track_queue_full);
  726. /**
  727. * scsi_device_get - get an addition reference to a scsi_device
  728. * @sdev: device to get a reference to
  729. *
  730. * Gets a reference to the scsi_device and increments the use count
  731. * of the underlying LLDD module. You must hold host_lock of the
  732. * parent Scsi_Host or already have a reference when calling this.
  733. */
  734. int scsi_device_get(struct scsi_device *sdev)
  735. {
  736. if (sdev->sdev_state == SDEV_DEL)
  737. return -ENXIO;
  738. if (!get_device(&sdev->sdev_gendev))
  739. return -ENXIO;
  740. /* We can fail this if we're doing SCSI operations
  741. * from module exit (like cache flush) */
  742. try_module_get(sdev->host->hostt->module);
  743. return 0;
  744. }
  745. EXPORT_SYMBOL(scsi_device_get);
  746. /**
  747. * scsi_device_put - release a reference to a scsi_device
  748. * @sdev: device to release a reference on.
  749. *
  750. * Release a reference to the scsi_device and decrements the use count
  751. * of the underlying LLDD module. The device is freed once the last
  752. * user vanishes.
  753. */
  754. void scsi_device_put(struct scsi_device *sdev)
  755. {
  756. #ifdef CONFIG_MODULE_UNLOAD
  757. struct module *module = sdev->host->hostt->module;
  758. /* The module refcount will be zero if scsi_device_get()
  759. * was called from a module removal routine */
  760. if (module && module_refcount(module) != 0)
  761. module_put(module);
  762. #endif
  763. put_device(&sdev->sdev_gendev);
  764. }
  765. EXPORT_SYMBOL(scsi_device_put);
  766. /* helper for shost_for_each_device, thus not documented */
  767. struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost,
  768. struct scsi_device *prev)
  769. {
  770. struct list_head *list = (prev ? &prev->siblings : &shost->__devices);
  771. struct scsi_device *next = NULL;
  772. unsigned long flags;
  773. spin_lock_irqsave(shost->host_lock, flags);
  774. while (list->next != &shost->__devices) {
  775. next = list_entry(list->next, struct scsi_device, siblings);
  776. /* skip devices that we can't get a reference to */
  777. if (!scsi_device_get(next))
  778. break;
  779. next = NULL;
  780. list = list->next;
  781. }
  782. spin_unlock_irqrestore(shost->host_lock, flags);
  783. if (prev)
  784. scsi_device_put(prev);
  785. return next;
  786. }
  787. EXPORT_SYMBOL(__scsi_iterate_devices);
  788. /**
  789. * starget_for_each_device - helper to walk all devices of a target
  790. * @starget: target whose devices we want to iterate over.
  791. *
  792. * This traverses over each devices of @shost. The devices have
  793. * a reference that must be released by scsi_host_put when breaking
  794. * out of the loop.
  795. */
  796. void starget_for_each_device(struct scsi_target *starget, void * data,
  797. void (*fn)(struct scsi_device *, void *))
  798. {
  799. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  800. struct scsi_device *sdev;
  801. shost_for_each_device(sdev, shost) {
  802. if ((sdev->channel == starget->channel) &&
  803. (sdev->id == starget->id))
  804. fn(sdev, data);
  805. }
  806. }
  807. EXPORT_SYMBOL(starget_for_each_device);
  808. /**
  809. * __scsi_device_lookup_by_target - find a device given the target (UNLOCKED)
  810. * @starget: SCSI target pointer
  811. * @lun: SCSI Logical Unit Number
  812. *
  813. * Looks up the scsi_device with the specified @lun for a give
  814. * @starget. The returned scsi_device does not have an additional
  815. * reference. You must hold the host's host_lock over this call and
  816. * any access to the returned scsi_device.
  817. *
  818. * Note: The only reason why drivers would want to use this is because
  819. * they're need to access the device list in irq context. Otherwise you
  820. * really want to use scsi_device_lookup_by_target instead.
  821. **/
  822. struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget,
  823. uint lun)
  824. {
  825. struct scsi_device *sdev;
  826. list_for_each_entry(sdev, &starget->devices, same_target_siblings) {
  827. if (sdev->lun ==lun)
  828. return sdev;
  829. }
  830. return NULL;
  831. }
  832. EXPORT_SYMBOL(__scsi_device_lookup_by_target);
  833. /**
  834. * scsi_device_lookup_by_target - find a device given the target
  835. * @starget: SCSI target pointer
  836. * @lun: SCSI Logical Unit Number
  837. *
  838. * Looks up the scsi_device with the specified @channel, @id, @lun for a
  839. * give host. The returned scsi_device has an additional reference that
  840. * needs to be release with scsi_host_put once you're done with it.
  841. **/
  842. struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget,
  843. uint lun)
  844. {
  845. struct scsi_device *sdev;
  846. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  847. unsigned long flags;
  848. spin_lock_irqsave(shost->host_lock, flags);
  849. sdev = __scsi_device_lookup_by_target(starget, lun);
  850. if (sdev && scsi_device_get(sdev))
  851. sdev = NULL;
  852. spin_unlock_irqrestore(shost->host_lock, flags);
  853. return sdev;
  854. }
  855. EXPORT_SYMBOL(scsi_device_lookup_by_target);
  856. /**
  857. * scsi_device_lookup - find a device given the host (UNLOCKED)
  858. * @shost: SCSI host pointer
  859. * @channel: SCSI channel (zero if only one channel)
  860. * @pun: SCSI target number (physical unit number)
  861. * @lun: SCSI Logical Unit Number
  862. *
  863. * Looks up the scsi_device with the specified @channel, @id, @lun for a
  864. * give host. The returned scsi_device does not have an additional reference.
  865. * You must hold the host's host_lock over this call and any access to the
  866. * returned scsi_device.
  867. *
  868. * Note: The only reason why drivers would want to use this is because
  869. * they're need to access the device list in irq context. Otherwise you
  870. * really want to use scsi_device_lookup instead.
  871. **/
  872. struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost,
  873. uint channel, uint id, uint lun)
  874. {
  875. struct scsi_device *sdev;
  876. list_for_each_entry(sdev, &shost->__devices, siblings) {
  877. if (sdev->channel == channel && sdev->id == id &&
  878. sdev->lun ==lun)
  879. return sdev;
  880. }
  881. return NULL;
  882. }
  883. EXPORT_SYMBOL(__scsi_device_lookup);
  884. /**
  885. * scsi_device_lookup - find a device given the host
  886. * @shost: SCSI host pointer
  887. * @channel: SCSI channel (zero if only one channel)
  888. * @id: SCSI target number (physical unit number)
  889. * @lun: SCSI Logical Unit Number
  890. *
  891. * Looks up the scsi_device with the specified @channel, @id, @lun for a
  892. * give host. The returned scsi_device has an additional reference that
  893. * needs to be release with scsi_host_put once you're done with it.
  894. **/
  895. struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost,
  896. uint channel, uint id, uint lun)
  897. {
  898. struct scsi_device *sdev;
  899. unsigned long flags;
  900. spin_lock_irqsave(shost->host_lock, flags);
  901. sdev = __scsi_device_lookup(shost, channel, id, lun);
  902. if (sdev && scsi_device_get(sdev))
  903. sdev = NULL;
  904. spin_unlock_irqrestore(shost->host_lock, flags);
  905. return sdev;
  906. }
  907. EXPORT_SYMBOL(scsi_device_lookup);
  908. /**
  909. * scsi_device_cancel - cancel outstanding IO to this device
  910. * @sdev: Pointer to struct scsi_device
  911. * @recovery: Boolean instructing function to recover device or not.
  912. *
  913. **/
  914. int scsi_device_cancel(struct scsi_device *sdev, int recovery)
  915. {
  916. struct scsi_cmnd *scmd;
  917. LIST_HEAD(active_list);
  918. struct list_head *lh, *lh_sf;
  919. unsigned long flags;
  920. scsi_device_set_state(sdev, SDEV_CANCEL);
  921. spin_lock_irqsave(&sdev->list_lock, flags);
  922. list_for_each_entry(scmd, &sdev->cmd_list, list) {
  923. if (scmd->request) {
  924. /*
  925. * If we are unable to remove the timer, it means
  926. * that the command has already timed out or
  927. * finished.
  928. */
  929. if (!scsi_delete_timer(scmd))
  930. continue;
  931. list_add_tail(&scmd->eh_entry, &active_list);
  932. }
  933. }
  934. spin_unlock_irqrestore(&sdev->list_lock, flags);
  935. if (!list_empty(&active_list)) {
  936. list_for_each_safe(lh, lh_sf, &active_list) {
  937. scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
  938. list_del_init(lh);
  939. if (recovery &&
  940. !scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD)) {
  941. scmd->result = (DID_ABORT << 16);
  942. scsi_finish_command(scmd);
  943. }
  944. }
  945. }
  946. return 0;
  947. }
  948. EXPORT_SYMBOL(scsi_device_cancel);
  949. MODULE_DESCRIPTION("SCSI core");
  950. MODULE_LICENSE("GPL");
  951. module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR);
  952. MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels");
  953. static int __init init_scsi(void)
  954. {
  955. int error;
  956. error = scsi_init_queue();
  957. if (error)
  958. return error;
  959. error = scsi_init_procfs();
  960. if (error)
  961. goto cleanup_queue;
  962. error = scsi_init_devinfo();
  963. if (error)
  964. goto cleanup_procfs;
  965. error = scsi_init_hosts();
  966. if (error)
  967. goto cleanup_devlist;
  968. error = scsi_init_sysctl();
  969. if (error)
  970. goto cleanup_hosts;
  971. error = scsi_sysfs_register();
  972. if (error)
  973. goto cleanup_sysctl;
  974. scsi_netlink_init();
  975. printk(KERN_NOTICE "SCSI subsystem initialized\n");
  976. return 0;
  977. cleanup_sysctl:
  978. scsi_exit_sysctl();
  979. cleanup_hosts:
  980. scsi_exit_hosts();
  981. cleanup_devlist:
  982. scsi_exit_devinfo();
  983. cleanup_procfs:
  984. scsi_exit_procfs();
  985. cleanup_queue:
  986. scsi_exit_queue();
  987. printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n",
  988. -error);
  989. return error;
  990. }
  991. static void __exit exit_scsi(void)
  992. {
  993. scsi_netlink_exit();
  994. scsi_sysfs_unregister();
  995. scsi_exit_sysctl();
  996. scsi_exit_hosts();
  997. scsi_exit_devinfo();
  998. scsi_exit_procfs();
  999. scsi_exit_queue();
  1000. }
  1001. subsys_initcall(init_scsi);
  1002. module_exit(exit_scsi);