scsi.c 32 KB

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