scsi.c 32 KB

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