scsi.c 31 KB

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