scsi.c 30 KB

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