scsi.c 31 KB

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