scsi.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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_done - Enqueue the finished SCSI command into the done queue.
  518. * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
  519. * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
  520. *
  521. * This function is the mid-level's (SCSI Core) interrupt routine, which
  522. * regains ownership of the SCSI command (de facto) from a LLDD, and enqueues
  523. * the command to the done queue for further processing.
  524. *
  525. * This is the producer of the done queue who enqueues at the tail.
  526. *
  527. * This function is interrupt context safe.
  528. */
  529. static void scsi_done(struct scsi_cmnd *cmd)
  530. {
  531. /*
  532. * We don't have to worry about this one timing out any more.
  533. * If we are unable to remove the timer, then the command
  534. * has already timed out. In which case, we have no choice but to
  535. * let the timeout function run, as we have no idea where in fact
  536. * that function could really be. It might be on another processor,
  537. * etc, etc.
  538. */
  539. if (!scsi_delete_timer(cmd))
  540. return;
  541. __scsi_done(cmd);
  542. }
  543. /* Private entry to scsi_done() to complete a command when the timer
  544. * isn't running --- used by scsi_times_out */
  545. void __scsi_done(struct scsi_cmnd *cmd)
  546. {
  547. struct request *rq = cmd->request;
  548. /*
  549. * Set the serial numbers back to zero
  550. */
  551. cmd->serial_number = 0;
  552. atomic_inc(&cmd->device->iodone_cnt);
  553. if (cmd->result)
  554. atomic_inc(&cmd->device->ioerr_cnt);
  555. BUG_ON(!rq);
  556. /*
  557. * The uptodate/nbytes values don't matter, as we allow partial
  558. * completes and thus will check this in the softirq callback
  559. */
  560. rq->completion_data = cmd;
  561. blk_complete_request(rq);
  562. }
  563. /*
  564. * Function: scsi_retry_command
  565. *
  566. * Purpose: Send a command back to the low level to be retried.
  567. *
  568. * Notes: This command is always executed in the context of the
  569. * bottom half handler, or the error handler thread. Low
  570. * level drivers should not become re-entrant as a result of
  571. * this.
  572. */
  573. int scsi_retry_command(struct scsi_cmnd *cmd)
  574. {
  575. /*
  576. * Restore the SCSI command state.
  577. */
  578. scsi_setup_cmd_retry(cmd);
  579. /*
  580. * Zero the sense information from the last time we tried
  581. * this command.
  582. */
  583. memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
  584. return scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
  585. }
  586. /*
  587. * Function: scsi_finish_command
  588. *
  589. * Purpose: Pass command off to upper layer for finishing of I/O
  590. * request, waking processes that are waiting on results,
  591. * etc.
  592. */
  593. void scsi_finish_command(struct scsi_cmnd *cmd)
  594. {
  595. struct scsi_device *sdev = cmd->device;
  596. struct Scsi_Host *shost = sdev->host;
  597. scsi_device_unbusy(sdev);
  598. /*
  599. * Clear the flags which say that the device/host is no longer
  600. * capable of accepting new commands. These are set in scsi_queue.c
  601. * for both the queue full condition on a device, and for a
  602. * host full condition on the host.
  603. *
  604. * XXX(hch): What about locking?
  605. */
  606. shost->host_blocked = 0;
  607. sdev->device_blocked = 0;
  608. /*
  609. * If we have valid sense information, then some kind of recovery
  610. * must have taken place. Make a note of this.
  611. */
  612. if (SCSI_SENSE_VALID(cmd))
  613. cmd->result |= (DRIVER_SENSE << 24);
  614. SCSI_LOG_MLCOMPLETE(4, sdev_printk(KERN_INFO, sdev,
  615. "Notifying upper driver of completion "
  616. "(result %x)\n", cmd->result));
  617. /*
  618. * We can get here with use_sg=0, causing a panic in the upper level
  619. */
  620. cmd->use_sg = cmd->old_use_sg;
  621. cmd->done(cmd);
  622. }
  623. EXPORT_SYMBOL(scsi_finish_command);
  624. /*
  625. * Function: scsi_adjust_queue_depth()
  626. *
  627. * Purpose: Allow low level drivers to tell us to change the queue depth
  628. * on a specific SCSI device
  629. *
  630. * Arguments: sdev - SCSI Device in question
  631. * tagged - Do we use tagged queueing (non-0) or do we treat
  632. * this device as an untagged device (0)
  633. * tags - Number of tags allowed if tagged queueing enabled,
  634. * or number of commands the low level driver can
  635. * queue up in non-tagged mode (as per cmd_per_lun).
  636. *
  637. * Returns: Nothing
  638. *
  639. * Lock Status: None held on entry
  640. *
  641. * Notes: Low level drivers may call this at any time and we will do
  642. * the right thing depending on whether or not the device is
  643. * currently active and whether or not it even has the
  644. * command blocks built yet.
  645. */
  646. void scsi_adjust_queue_depth(struct scsi_device *sdev, int tagged, int tags)
  647. {
  648. unsigned long flags;
  649. /*
  650. * refuse to set tagged depth to an unworkable size
  651. */
  652. if (tags <= 0)
  653. return;
  654. spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
  655. /* Check to see if the queue is managed by the block layer
  656. * if it is, and we fail to adjust the depth, exit */
  657. if (blk_queue_tagged(sdev->request_queue) &&
  658. blk_queue_resize_tags(sdev->request_queue, tags) != 0)
  659. goto out;
  660. sdev->queue_depth = tags;
  661. switch (tagged) {
  662. case MSG_ORDERED_TAG:
  663. sdev->ordered_tags = 1;
  664. sdev->simple_tags = 1;
  665. break;
  666. case MSG_SIMPLE_TAG:
  667. sdev->ordered_tags = 0;
  668. sdev->simple_tags = 1;
  669. break;
  670. default:
  671. sdev_printk(KERN_WARNING, sdev,
  672. "scsi_adjust_queue_depth, bad queue type, "
  673. "disabled\n");
  674. case 0:
  675. sdev->ordered_tags = sdev->simple_tags = 0;
  676. sdev->queue_depth = tags;
  677. break;
  678. }
  679. out:
  680. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  681. }
  682. EXPORT_SYMBOL(scsi_adjust_queue_depth);
  683. /*
  684. * Function: scsi_track_queue_full()
  685. *
  686. * Purpose: This function will track successive QUEUE_FULL events on a
  687. * specific SCSI device to determine if and when there is a
  688. * need to adjust the queue depth on the device.
  689. *
  690. * Arguments: sdev - SCSI Device in question
  691. * depth - Current number of outstanding SCSI commands on
  692. * this device, not counting the one returned as
  693. * QUEUE_FULL.
  694. *
  695. * Returns: 0 - No change needed
  696. * >0 - Adjust queue depth to this new depth
  697. * -1 - Drop back to untagged operation using host->cmd_per_lun
  698. * as the untagged command depth
  699. *
  700. * Lock Status: None held on entry
  701. *
  702. * Notes: Low level drivers may call this at any time and we will do
  703. * "The Right Thing." We are interrupt context safe.
  704. */
  705. int scsi_track_queue_full(struct scsi_device *sdev, int depth)
  706. {
  707. if ((jiffies >> 4) == sdev->last_queue_full_time)
  708. return 0;
  709. sdev->last_queue_full_time = (jiffies >> 4);
  710. if (sdev->last_queue_full_depth != depth) {
  711. sdev->last_queue_full_count = 1;
  712. sdev->last_queue_full_depth = depth;
  713. } else {
  714. sdev->last_queue_full_count++;
  715. }
  716. if (sdev->last_queue_full_count <= 10)
  717. return 0;
  718. if (sdev->last_queue_full_depth < 8) {
  719. /* Drop back to untagged */
  720. scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
  721. return -1;
  722. }
  723. if (sdev->ordered_tags)
  724. scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, depth);
  725. else
  726. scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
  727. return depth;
  728. }
  729. EXPORT_SYMBOL(scsi_track_queue_full);
  730. /**
  731. * scsi_device_get - get an addition reference to a scsi_device
  732. * @sdev: device to get a reference to
  733. *
  734. * Gets a reference to the scsi_device and increments the use count
  735. * of the underlying LLDD module. You must hold host_lock of the
  736. * parent Scsi_Host or already have a reference when calling this.
  737. */
  738. int scsi_device_get(struct scsi_device *sdev)
  739. {
  740. if (sdev->sdev_state == SDEV_DEL || sdev->sdev_state == SDEV_CANCEL)
  741. return -ENXIO;
  742. if (!get_device(&sdev->sdev_gendev))
  743. return -ENXIO;
  744. if (!try_module_get(sdev->host->hostt->module)) {
  745. put_device(&sdev->sdev_gendev);
  746. return -ENXIO;
  747. }
  748. return 0;
  749. }
  750. EXPORT_SYMBOL(scsi_device_get);
  751. /**
  752. * scsi_device_put - release a reference to a scsi_device
  753. * @sdev: device to release a reference on.
  754. *
  755. * Release a reference to the scsi_device and decrements the use count
  756. * of the underlying LLDD module. The device is freed once the last
  757. * user vanishes.
  758. */
  759. void scsi_device_put(struct scsi_device *sdev)
  760. {
  761. module_put(sdev->host->hostt->module);
  762. put_device(&sdev->sdev_gendev);
  763. }
  764. EXPORT_SYMBOL(scsi_device_put);
  765. /* helper for shost_for_each_device, thus not documented */
  766. struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost,
  767. struct scsi_device *prev)
  768. {
  769. struct list_head *list = (prev ? &prev->siblings : &shost->__devices);
  770. struct scsi_device *next = NULL;
  771. unsigned long flags;
  772. spin_lock_irqsave(shost->host_lock, flags);
  773. while (list->next != &shost->__devices) {
  774. next = list_entry(list->next, struct scsi_device, siblings);
  775. /* skip devices that we can't get a reference to */
  776. if (!scsi_device_get(next))
  777. break;
  778. next = NULL;
  779. list = list->next;
  780. }
  781. spin_unlock_irqrestore(shost->host_lock, flags);
  782. if (prev)
  783. scsi_device_put(prev);
  784. return next;
  785. }
  786. EXPORT_SYMBOL(__scsi_iterate_devices);
  787. /**
  788. * starget_for_each_device - helper to walk all devices of a target
  789. * @starget: target whose devices we want to iterate over.
  790. *
  791. * This traverses over each devices of @shost. The devices have
  792. * a reference that must be released by scsi_host_put when breaking
  793. * out of the loop.
  794. */
  795. void starget_for_each_device(struct scsi_target *starget, void * data,
  796. void (*fn)(struct scsi_device *, void *))
  797. {
  798. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  799. struct scsi_device *sdev;
  800. shost_for_each_device(sdev, shost) {
  801. if ((sdev->channel == starget->channel) &&
  802. (sdev->id == starget->id))
  803. fn(sdev, data);
  804. }
  805. }
  806. EXPORT_SYMBOL(starget_for_each_device);
  807. /**
  808. * __scsi_device_lookup_by_target - find a device given the target (UNLOCKED)
  809. * @starget: SCSI target pointer
  810. * @lun: SCSI Logical Unit Number
  811. *
  812. * Looks up the scsi_device with the specified @lun for a give
  813. * @starget. The returned scsi_device does not have an additional
  814. * reference. You must hold the host's host_lock over this call and
  815. * any access to the returned scsi_device.
  816. *
  817. * Note: The only reason why drivers would want to use this is because
  818. * they're need to access the device list in irq context. Otherwise you
  819. * really want to use scsi_device_lookup_by_target instead.
  820. **/
  821. struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget,
  822. uint lun)
  823. {
  824. struct scsi_device *sdev;
  825. list_for_each_entry(sdev, &starget->devices, same_target_siblings) {
  826. if (sdev->lun ==lun)
  827. return sdev;
  828. }
  829. return NULL;
  830. }
  831. EXPORT_SYMBOL(__scsi_device_lookup_by_target);
  832. /**
  833. * scsi_device_lookup_by_target - find a device given the target
  834. * @starget: SCSI target pointer
  835. * @lun: SCSI Logical Unit Number
  836. *
  837. * Looks up the scsi_device with the specified @channel, @id, @lun for a
  838. * give host. The returned scsi_device has an additional reference that
  839. * needs to be release with scsi_host_put once you're done with it.
  840. **/
  841. struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget,
  842. uint lun)
  843. {
  844. struct scsi_device *sdev;
  845. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  846. unsigned long flags;
  847. spin_lock_irqsave(shost->host_lock, flags);
  848. sdev = __scsi_device_lookup_by_target(starget, lun);
  849. if (sdev && scsi_device_get(sdev))
  850. sdev = NULL;
  851. spin_unlock_irqrestore(shost->host_lock, flags);
  852. return sdev;
  853. }
  854. EXPORT_SYMBOL(scsi_device_lookup_by_target);
  855. /**
  856. * scsi_device_lookup - find a device given the host (UNLOCKED)
  857. * @shost: SCSI host pointer
  858. * @channel: SCSI channel (zero if only one channel)
  859. * @pun: SCSI target number (physical unit number)
  860. * @lun: SCSI Logical Unit Number
  861. *
  862. * Looks up the scsi_device with the specified @channel, @id, @lun for a
  863. * give host. The returned scsi_device does not have an additional reference.
  864. * You must hold the host's host_lock over this call and any access to the
  865. * returned scsi_device.
  866. *
  867. * Note: The only reason why drivers would want to use this is because
  868. * they're need to access the device list in irq context. Otherwise you
  869. * really want to use scsi_device_lookup instead.
  870. **/
  871. struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost,
  872. uint channel, uint id, uint lun)
  873. {
  874. struct scsi_device *sdev;
  875. list_for_each_entry(sdev, &shost->__devices, siblings) {
  876. if (sdev->channel == channel && sdev->id == id &&
  877. sdev->lun ==lun)
  878. return sdev;
  879. }
  880. return NULL;
  881. }
  882. EXPORT_SYMBOL(__scsi_device_lookup);
  883. /**
  884. * scsi_device_lookup - find a device given the host
  885. * @shost: SCSI host pointer
  886. * @channel: SCSI channel (zero if only one channel)
  887. * @id: SCSI target number (physical unit number)
  888. * @lun: SCSI Logical Unit Number
  889. *
  890. * Looks up the scsi_device with the specified @channel, @id, @lun for a
  891. * give host. The returned scsi_device has an additional reference that
  892. * needs to be release with scsi_host_put once you're done with it.
  893. **/
  894. struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost,
  895. uint channel, uint id, uint lun)
  896. {
  897. struct scsi_device *sdev;
  898. unsigned long flags;
  899. spin_lock_irqsave(shost->host_lock, flags);
  900. sdev = __scsi_device_lookup(shost, channel, id, lun);
  901. if (sdev && scsi_device_get(sdev))
  902. sdev = NULL;
  903. spin_unlock_irqrestore(shost->host_lock, flags);
  904. return sdev;
  905. }
  906. EXPORT_SYMBOL(scsi_device_lookup);
  907. /**
  908. * scsi_device_cancel - cancel outstanding IO to this device
  909. * @sdev: Pointer to struct scsi_device
  910. * @recovery: Boolean instructing function to recover device or not.
  911. *
  912. **/
  913. int scsi_device_cancel(struct scsi_device *sdev, int recovery)
  914. {
  915. struct scsi_cmnd *scmd;
  916. LIST_HEAD(active_list);
  917. struct list_head *lh, *lh_sf;
  918. unsigned long flags;
  919. scsi_device_set_state(sdev, SDEV_CANCEL);
  920. spin_lock_irqsave(&sdev->list_lock, flags);
  921. list_for_each_entry(scmd, &sdev->cmd_list, list) {
  922. if (scmd->request && scmd->request->rq_status != RQ_INACTIVE) {
  923. /*
  924. * If we are unable to remove the timer, it means
  925. * that the command has already timed out or
  926. * finished.
  927. */
  928. if (!scsi_delete_timer(scmd))
  929. continue;
  930. list_add_tail(&scmd->eh_entry, &active_list);
  931. }
  932. }
  933. spin_unlock_irqrestore(&sdev->list_lock, flags);
  934. if (!list_empty(&active_list)) {
  935. list_for_each_safe(lh, lh_sf, &active_list) {
  936. scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
  937. list_del_init(lh);
  938. if (recovery &&
  939. !scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD)) {
  940. scmd->result = (DID_ABORT << 16);
  941. scsi_finish_command(scmd);
  942. }
  943. }
  944. }
  945. return 0;
  946. }
  947. EXPORT_SYMBOL(scsi_device_cancel);
  948. MODULE_DESCRIPTION("SCSI core");
  949. MODULE_LICENSE("GPL");
  950. module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR);
  951. MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels");
  952. static int __init init_scsi(void)
  953. {
  954. int error, i;
  955. error = scsi_init_queue();
  956. if (error)
  957. return error;
  958. error = scsi_init_procfs();
  959. if (error)
  960. goto cleanup_queue;
  961. error = scsi_init_devinfo();
  962. if (error)
  963. goto cleanup_procfs;
  964. error = scsi_init_hosts();
  965. if (error)
  966. goto cleanup_devlist;
  967. error = scsi_init_sysctl();
  968. if (error)
  969. goto cleanup_hosts;
  970. error = scsi_sysfs_register();
  971. if (error)
  972. goto cleanup_sysctl;
  973. for_each_possible_cpu(i)
  974. INIT_LIST_HEAD(&per_cpu(scsi_done_q, i));
  975. printk(KERN_NOTICE "SCSI subsystem initialized\n");
  976. return 0;
  977. cleanup_sysctl:
  978. scsi_exit_sysctl();
  979. cleanup_hosts:
  980. scsi_exit_hosts();
  981. cleanup_devlist:
  982. scsi_exit_devinfo();
  983. cleanup_procfs:
  984. scsi_exit_procfs();
  985. cleanup_queue:
  986. scsi_exit_queue();
  987. printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n",
  988. -error);
  989. return error;
  990. }
  991. static void __exit exit_scsi(void)
  992. {
  993. scsi_sysfs_unregister();
  994. scsi_exit_sysctl();
  995. scsi_exit_hosts();
  996. scsi_exit_devinfo();
  997. scsi_exit_procfs();
  998. scsi_exit_queue();
  999. }
  1000. subsys_initcall(init_scsi);
  1001. module_exit(exit_scsi);