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