scsi.c 30 KB

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