scsi.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  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/devfs_fs_kernel.h>
  51. #include <linux/unistd.h>
  52. #include <linux/spinlock.h>
  53. #include <linux/kmod.h>
  54. #include <linux/interrupt.h>
  55. #include <linux/notifier.h>
  56. #include <linux/cpu.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/scsi_request.h>
  65. #include "scsi_priv.h"
  66. #include "scsi_logging.h"
  67. static void scsi_done(struct scsi_cmnd *cmd);
  68. static int scsi_retry_command(struct scsi_cmnd *cmd);
  69. /*
  70. * Definitions and constants.
  71. */
  72. #define MIN_RESET_DELAY (2*HZ)
  73. /* Do not call reset on error if we just did a reset within 15 sec. */
  74. #define MIN_RESET_PERIOD (15*HZ)
  75. /*
  76. * Macro to determine the size of SCSI command. This macro takes vendor
  77. * unique commands into account. SCSI commands in groups 6 and 7 are
  78. * vendor unique and we will depend upon the command length being
  79. * supplied correctly in cmd_len.
  80. */
  81. #define CDB_SIZE(cmd) (((((cmd)->cmnd[0] >> 5) & 7) < 6) ? \
  82. COMMAND_SIZE((cmd)->cmnd[0]) : (cmd)->cmd_len)
  83. /*
  84. * Note - the initial logging level can be set here to log events at boot time.
  85. * After the system is up, you may enable logging via the /proc interface.
  86. */
  87. unsigned int scsi_logging_level;
  88. #if defined(CONFIG_SCSI_LOGGING)
  89. EXPORT_SYMBOL(scsi_logging_level);
  90. #endif
  91. const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] = {
  92. "Direct-Access ",
  93. "Sequential-Access",
  94. "Printer ",
  95. "Processor ",
  96. "WORM ",
  97. "CD-ROM ",
  98. "Scanner ",
  99. "Optical Device ",
  100. "Medium Changer ",
  101. "Communications ",
  102. "Unknown ",
  103. "Unknown ",
  104. "RAID ",
  105. "Enclosure ",
  106. };
  107. EXPORT_SYMBOL(scsi_device_types);
  108. /*
  109. * Function: scsi_allocate_request
  110. *
  111. * Purpose: Allocate a request descriptor.
  112. *
  113. * Arguments: device - device for which we want a request
  114. * gfp_mask - allocation flags passed to kmalloc
  115. *
  116. * Lock status: No locks assumed to be held. This function is SMP-safe.
  117. *
  118. * Returns: Pointer to request block.
  119. */
  120. struct scsi_request *scsi_allocate_request(struct scsi_device *sdev,
  121. int gfp_mask)
  122. {
  123. const int offset = ALIGN(sizeof(struct scsi_request), 4);
  124. const int size = offset + sizeof(struct request);
  125. struct scsi_request *sreq;
  126. sreq = kmalloc(size, gfp_mask);
  127. if (likely(sreq != NULL)) {
  128. memset(sreq, 0, size);
  129. sreq->sr_request = (struct request *)(((char *)sreq) + offset);
  130. sreq->sr_device = sdev;
  131. sreq->sr_host = sdev->host;
  132. sreq->sr_magic = SCSI_REQ_MAGIC;
  133. sreq->sr_data_direction = DMA_BIDIRECTIONAL;
  134. }
  135. return sreq;
  136. }
  137. EXPORT_SYMBOL(scsi_allocate_request);
  138. void __scsi_release_request(struct scsi_request *sreq)
  139. {
  140. struct request *req = sreq->sr_request;
  141. /* unlikely because the tag was usually ended earlier by the
  142. * mid-layer. However, for layering reasons ULD's don't end
  143. * the tag of commands they generate. */
  144. if (unlikely(blk_rq_tagged(req))) {
  145. unsigned long flags;
  146. struct request_queue *q = req->q;
  147. spin_lock_irqsave(q->queue_lock, flags);
  148. blk_queue_end_tag(q, req);
  149. spin_unlock_irqrestore(q->queue_lock, flags);
  150. }
  151. if (likely(sreq->sr_command != NULL)) {
  152. struct scsi_cmnd *cmd = sreq->sr_command;
  153. sreq->sr_command = NULL;
  154. scsi_next_command(cmd);
  155. }
  156. }
  157. /*
  158. * Function: scsi_release_request
  159. *
  160. * Purpose: Release a request descriptor.
  161. *
  162. * Arguments: sreq - request to release
  163. *
  164. * Lock status: No locks assumed to be held. This function is SMP-safe.
  165. */
  166. void scsi_release_request(struct scsi_request *sreq)
  167. {
  168. __scsi_release_request(sreq);
  169. kfree(sreq);
  170. }
  171. EXPORT_SYMBOL(scsi_release_request);
  172. struct scsi_host_cmd_pool {
  173. kmem_cache_t *slab;
  174. unsigned int users;
  175. char *name;
  176. unsigned int slab_flags;
  177. unsigned int gfp_mask;
  178. };
  179. static struct scsi_host_cmd_pool scsi_cmd_pool = {
  180. .name = "scsi_cmd_cache",
  181. .slab_flags = SLAB_HWCACHE_ALIGN,
  182. };
  183. static struct scsi_host_cmd_pool scsi_cmd_dma_pool = {
  184. .name = "scsi_cmd_cache(DMA)",
  185. .slab_flags = SLAB_HWCACHE_ALIGN|SLAB_CACHE_DMA,
  186. .gfp_mask = __GFP_DMA,
  187. };
  188. static DECLARE_MUTEX(host_cmd_pool_mutex);
  189. static struct scsi_cmnd *__scsi_get_command(struct Scsi_Host *shost,
  190. int gfp_mask)
  191. {
  192. struct scsi_cmnd *cmd;
  193. cmd = kmem_cache_alloc(shost->cmd_pool->slab,
  194. gfp_mask | shost->cmd_pool->gfp_mask);
  195. if (unlikely(!cmd)) {
  196. unsigned long flags;
  197. spin_lock_irqsave(&shost->free_list_lock, flags);
  198. if (likely(!list_empty(&shost->free_list))) {
  199. cmd = list_entry(shost->free_list.next,
  200. struct scsi_cmnd, list);
  201. list_del_init(&cmd->list);
  202. }
  203. spin_unlock_irqrestore(&shost->free_list_lock, flags);
  204. }
  205. return cmd;
  206. }
  207. /*
  208. * Function: scsi_get_command()
  209. *
  210. * Purpose: Allocate and setup a scsi command block
  211. *
  212. * Arguments: dev - parent scsi device
  213. * gfp_mask- allocator flags
  214. *
  215. * Returns: The allocated scsi command structure.
  216. */
  217. struct scsi_cmnd *scsi_get_command(struct scsi_device *dev, int gfp_mask)
  218. {
  219. struct scsi_cmnd *cmd;
  220. /* Bail if we can't get a reference to the device */
  221. if (!get_device(&dev->sdev_gendev))
  222. return NULL;
  223. cmd = __scsi_get_command(dev->host, gfp_mask);
  224. if (likely(cmd != NULL)) {
  225. unsigned long flags;
  226. memset(cmd, 0, sizeof(*cmd));
  227. cmd->device = dev;
  228. cmd->state = SCSI_STATE_UNUSED;
  229. cmd->owner = SCSI_OWNER_NOBODY;
  230. init_timer(&cmd->eh_timeout);
  231. INIT_LIST_HEAD(&cmd->list);
  232. spin_lock_irqsave(&dev->list_lock, flags);
  233. list_add_tail(&cmd->list, &dev->cmd_list);
  234. spin_unlock_irqrestore(&dev->list_lock, flags);
  235. } else
  236. put_device(&dev->sdev_gendev);
  237. return cmd;
  238. }
  239. EXPORT_SYMBOL(scsi_get_command);
  240. /*
  241. * Function: scsi_put_command()
  242. *
  243. * Purpose: Free a scsi command block
  244. *
  245. * Arguments: cmd - command block to free
  246. *
  247. * Returns: Nothing.
  248. *
  249. * Notes: The command must not belong to any lists.
  250. */
  251. void scsi_put_command(struct scsi_cmnd *cmd)
  252. {
  253. struct scsi_device *sdev = cmd->device;
  254. struct Scsi_Host *shost = sdev->host;
  255. unsigned long flags;
  256. /* serious error if the command hasn't come from a device list */
  257. spin_lock_irqsave(&cmd->device->list_lock, flags);
  258. BUG_ON(list_empty(&cmd->list));
  259. list_del_init(&cmd->list);
  260. spin_unlock(&cmd->device->list_lock);
  261. /* changing locks here, don't need to restore the irq state */
  262. spin_lock(&shost->free_list_lock);
  263. if (unlikely(list_empty(&shost->free_list))) {
  264. list_add(&cmd->list, &shost->free_list);
  265. cmd = NULL;
  266. }
  267. spin_unlock_irqrestore(&shost->free_list_lock, flags);
  268. if (likely(cmd != NULL))
  269. kmem_cache_free(shost->cmd_pool->slab, cmd);
  270. put_device(&sdev->sdev_gendev);
  271. }
  272. EXPORT_SYMBOL(scsi_put_command);
  273. /*
  274. * Function: scsi_setup_command_freelist()
  275. *
  276. * Purpose: Setup the command freelist for a scsi host.
  277. *
  278. * Arguments: shost - host to allocate the freelist for.
  279. *
  280. * Returns: Nothing.
  281. */
  282. int scsi_setup_command_freelist(struct Scsi_Host *shost)
  283. {
  284. struct scsi_host_cmd_pool *pool;
  285. struct scsi_cmnd *cmd;
  286. spin_lock_init(&shost->free_list_lock);
  287. INIT_LIST_HEAD(&shost->free_list);
  288. /*
  289. * Select a command slab for this host and create it if not
  290. * yet existant.
  291. */
  292. down(&host_cmd_pool_mutex);
  293. pool = (shost->unchecked_isa_dma ? &scsi_cmd_dma_pool : &scsi_cmd_pool);
  294. if (!pool->users) {
  295. pool->slab = kmem_cache_create(pool->name,
  296. sizeof(struct scsi_cmnd), 0,
  297. pool->slab_flags, NULL, NULL);
  298. if (!pool->slab)
  299. goto fail;
  300. }
  301. pool->users++;
  302. shost->cmd_pool = pool;
  303. up(&host_cmd_pool_mutex);
  304. /*
  305. * Get one backup command for this host.
  306. */
  307. cmd = kmem_cache_alloc(shost->cmd_pool->slab,
  308. GFP_KERNEL | shost->cmd_pool->gfp_mask);
  309. if (!cmd)
  310. goto fail2;
  311. list_add(&cmd->list, &shost->free_list);
  312. return 0;
  313. fail2:
  314. if (!--pool->users)
  315. kmem_cache_destroy(pool->slab);
  316. return -ENOMEM;
  317. fail:
  318. up(&host_cmd_pool_mutex);
  319. return -ENOMEM;
  320. }
  321. /*
  322. * Function: scsi_destroy_command_freelist()
  323. *
  324. * Purpose: Release the command freelist for a scsi host.
  325. *
  326. * Arguments: shost - host that's freelist is going to be destroyed
  327. */
  328. void scsi_destroy_command_freelist(struct Scsi_Host *shost)
  329. {
  330. while (!list_empty(&shost->free_list)) {
  331. struct scsi_cmnd *cmd;
  332. cmd = list_entry(shost->free_list.next, struct scsi_cmnd, list);
  333. list_del_init(&cmd->list);
  334. kmem_cache_free(shost->cmd_pool->slab, cmd);
  335. }
  336. down(&host_cmd_pool_mutex);
  337. if (!--shost->cmd_pool->users)
  338. kmem_cache_destroy(shost->cmd_pool->slab);
  339. up(&host_cmd_pool_mutex);
  340. }
  341. #ifdef CONFIG_SCSI_LOGGING
  342. void scsi_log_send(struct scsi_cmnd *cmd)
  343. {
  344. unsigned int level;
  345. struct scsi_device *sdev;
  346. /*
  347. * If ML QUEUE log level is greater than or equal to:
  348. *
  349. * 1: nothing (match completion)
  350. *
  351. * 2: log opcode + command of all commands
  352. *
  353. * 3: same as 2 plus dump cmd address
  354. *
  355. * 4: same as 3 plus dump extra junk
  356. */
  357. if (unlikely(scsi_logging_level)) {
  358. level = SCSI_LOG_LEVEL(SCSI_LOG_MLQUEUE_SHIFT,
  359. SCSI_LOG_MLQUEUE_BITS);
  360. if (level > 1) {
  361. sdev = cmd->device;
  362. printk(KERN_INFO "scsi <%d:%d:%d:%d> send ",
  363. sdev->host->host_no, sdev->channel, sdev->id,
  364. sdev->lun);
  365. if (level > 2)
  366. printk("0x%p ", cmd);
  367. /*
  368. * spaces to match disposition and cmd->result
  369. * output in scsi_log_completion.
  370. */
  371. printk(" ");
  372. scsi_print_command(cmd);
  373. if (level > 3) {
  374. printk(KERN_INFO "buffer = 0x%p, bufflen = %d,"
  375. " done = 0x%p, queuecommand 0x%p\n",
  376. cmd->buffer, cmd->bufflen,
  377. cmd->done,
  378. sdev->host->hostt->queuecommand);
  379. }
  380. }
  381. }
  382. }
  383. void scsi_log_completion(struct scsi_cmnd *cmd, int disposition)
  384. {
  385. unsigned int level;
  386. struct scsi_device *sdev;
  387. /*
  388. * If ML COMPLETE log level is greater than or equal to:
  389. *
  390. * 1: log disposition, result, opcode + command, and conditionally
  391. * sense data for failures or non SUCCESS dispositions.
  392. *
  393. * 2: same as 1 but for all command completions.
  394. *
  395. * 3: same as 2 plus dump cmd address
  396. *
  397. * 4: same as 3 plus dump extra junk
  398. */
  399. if (unlikely(scsi_logging_level)) {
  400. level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
  401. SCSI_LOG_MLCOMPLETE_BITS);
  402. if (((level > 0) && (cmd->result || disposition != SUCCESS)) ||
  403. (level > 1)) {
  404. sdev = cmd->device;
  405. printk(KERN_INFO "scsi <%d:%d:%d:%d> done ",
  406. sdev->host->host_no, sdev->channel, sdev->id,
  407. sdev->lun);
  408. if (level > 2)
  409. printk("0x%p ", cmd);
  410. /*
  411. * Dump truncated values, so we usually fit within
  412. * 80 chars.
  413. */
  414. switch (disposition) {
  415. case SUCCESS:
  416. printk("SUCCESS");
  417. break;
  418. case NEEDS_RETRY:
  419. printk("RETRY ");
  420. break;
  421. case ADD_TO_MLQUEUE:
  422. printk("MLQUEUE");
  423. break;
  424. case FAILED:
  425. printk("FAILED ");
  426. break;
  427. case TIMEOUT_ERROR:
  428. /*
  429. * If called via scsi_times_out.
  430. */
  431. printk("TIMEOUT");
  432. break;
  433. default:
  434. printk("UNKNOWN");
  435. }
  436. printk(" %8x ", cmd->result);
  437. scsi_print_command(cmd);
  438. if (status_byte(cmd->result) & CHECK_CONDITION) {
  439. /*
  440. * XXX The scsi_print_sense formatting/prefix
  441. * doesn't match this function.
  442. */
  443. scsi_print_sense("", cmd);
  444. }
  445. if (level > 3) {
  446. printk(KERN_INFO "scsi host busy %d failed %d\n",
  447. sdev->host->host_busy,
  448. sdev->host->host_failed);
  449. }
  450. }
  451. }
  452. }
  453. #endif
  454. /*
  455. * Assign a serial number and pid to the request for error recovery
  456. * and debugging purposes. Protected by the Host_Lock of host.
  457. */
  458. static inline void scsi_cmd_get_serial(struct Scsi_Host *host, struct scsi_cmnd *cmd)
  459. {
  460. cmd->serial_number = host->cmd_serial_number++;
  461. if (cmd->serial_number == 0)
  462. cmd->serial_number = host->cmd_serial_number++;
  463. cmd->pid = host->cmd_pid++;
  464. if (cmd->pid == 0)
  465. cmd->pid = host->cmd_pid++;
  466. }
  467. /*
  468. * Function: scsi_dispatch_command
  469. *
  470. * Purpose: Dispatch a command to the low-level driver.
  471. *
  472. * Arguments: cmd - command block we are dispatching.
  473. *
  474. * Notes:
  475. */
  476. int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
  477. {
  478. struct Scsi_Host *host = cmd->device->host;
  479. unsigned long flags = 0;
  480. unsigned long timeout;
  481. int rtn = 0;
  482. /* check if the device is still usable */
  483. if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
  484. /* in SDEV_DEL we error all commands. DID_NO_CONNECT
  485. * returns an immediate error upwards, and signals
  486. * that the device is no longer present */
  487. cmd->result = DID_NO_CONNECT << 16;
  488. atomic_inc(&cmd->device->iorequest_cnt);
  489. __scsi_done(cmd);
  490. /* return 0 (because the command has been processed) */
  491. goto out;
  492. }
  493. /* Check to see if the scsi lld put this device into state SDEV_BLOCK. */
  494. if (unlikely(cmd->device->sdev_state == SDEV_BLOCK)) {
  495. /*
  496. * in SDEV_BLOCK, the command is just put back on the device
  497. * queue. The suspend state has already blocked the queue so
  498. * future requests should not occur until the device
  499. * transitions out of the suspend state.
  500. */
  501. scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
  502. SCSI_LOG_MLQUEUE(3, printk("queuecommand : device blocked \n"));
  503. /*
  504. * NOTE: rtn is still zero here because we don't need the
  505. * queue to be plugged on return (it's already stopped)
  506. */
  507. goto out;
  508. }
  509. /*
  510. * If SCSI-2 or lower, store the LUN value in cmnd.
  511. */
  512. if (cmd->device->scsi_level <= SCSI_2) {
  513. cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
  514. (cmd->device->lun << 5 & 0xe0);
  515. }
  516. /*
  517. * We will wait MIN_RESET_DELAY clock ticks after the last reset so
  518. * we can avoid the drive not being ready.
  519. */
  520. timeout = host->last_reset + MIN_RESET_DELAY;
  521. if (host->resetting && time_before(jiffies, timeout)) {
  522. int ticks_remaining = timeout - jiffies;
  523. /*
  524. * NOTE: This may be executed from within an interrupt
  525. * handler! This is bad, but for now, it'll do. The irq
  526. * level of the interrupt handler has been masked out by the
  527. * platform dependent interrupt handling code already, so the
  528. * sti() here will not cause another call to the SCSI host's
  529. * interrupt handler (assuming there is one irq-level per
  530. * host).
  531. */
  532. while (--ticks_remaining >= 0)
  533. mdelay(1 + 999 / HZ);
  534. host->resetting = 0;
  535. }
  536. /*
  537. * AK: unlikely race here: for some reason the timer could
  538. * expire before the serial number is set up below.
  539. */
  540. scsi_add_timer(cmd, cmd->timeout_per_command, scsi_times_out);
  541. scsi_log_send(cmd);
  542. /*
  543. * We will use a queued command if possible, otherwise we will
  544. * emulate the queuing and calling of completion function ourselves.
  545. */
  546. cmd->state = SCSI_STATE_QUEUED;
  547. cmd->owner = SCSI_OWNER_LOWLEVEL;
  548. atomic_inc(&cmd->device->iorequest_cnt);
  549. /*
  550. * Before we queue this command, check if the command
  551. * length exceeds what the host adapter can handle.
  552. */
  553. if (CDB_SIZE(cmd) > cmd->device->host->max_cmd_len) {
  554. SCSI_LOG_MLQUEUE(3,
  555. printk("queuecommand : command too long.\n"));
  556. cmd->result = (DID_ABORT << 16);
  557. scsi_done(cmd);
  558. goto out;
  559. }
  560. spin_lock_irqsave(host->host_lock, flags);
  561. scsi_cmd_get_serial(host, cmd);
  562. if (unlikely(test_bit(SHOST_CANCEL, &host->shost_state))) {
  563. cmd->result = (DID_NO_CONNECT << 16);
  564. scsi_done(cmd);
  565. } else {
  566. rtn = host->hostt->queuecommand(cmd, scsi_done);
  567. }
  568. spin_unlock_irqrestore(host->host_lock, flags);
  569. if (rtn) {
  570. if (scsi_delete_timer(cmd)) {
  571. atomic_inc(&cmd->device->iodone_cnt);
  572. scsi_queue_insert(cmd,
  573. (rtn == SCSI_MLQUEUE_DEVICE_BUSY) ?
  574. rtn : SCSI_MLQUEUE_HOST_BUSY);
  575. }
  576. SCSI_LOG_MLQUEUE(3,
  577. printk("queuecommand : request rejected\n"));
  578. }
  579. out:
  580. SCSI_LOG_MLQUEUE(3, printk("leaving scsi_dispatch_cmnd()\n"));
  581. return rtn;
  582. }
  583. /*
  584. * Function: scsi_init_cmd_from_req
  585. *
  586. * Purpose: Queue a SCSI command
  587. * Purpose: Initialize a struct scsi_cmnd from a struct scsi_request
  588. *
  589. * Arguments: cmd - command descriptor.
  590. * sreq - Request from the queue.
  591. *
  592. * Lock status: None needed.
  593. *
  594. * Returns: Nothing.
  595. *
  596. * Notes: Mainly transfer data from the request structure to the
  597. * command structure. The request structure is allocated
  598. * using the normal memory allocator, and requests can pile
  599. * up to more or less any depth. The command structure represents
  600. * a consumable resource, as these are allocated into a pool
  601. * when the SCSI subsystem initializes. The preallocation is
  602. * required so that in low-memory situations a disk I/O request
  603. * won't cause the memory manager to try and write out a page.
  604. * The request structure is generally used by ioctls and character
  605. * devices.
  606. */
  607. void scsi_init_cmd_from_req(struct scsi_cmnd *cmd, struct scsi_request *sreq)
  608. {
  609. sreq->sr_command = cmd;
  610. cmd->owner = SCSI_OWNER_MIDLEVEL;
  611. cmd->cmd_len = sreq->sr_cmd_len;
  612. cmd->use_sg = sreq->sr_use_sg;
  613. cmd->request = sreq->sr_request;
  614. memcpy(cmd->data_cmnd, sreq->sr_cmnd, sizeof(cmd->data_cmnd));
  615. cmd->serial_number = 0;
  616. cmd->bufflen = sreq->sr_bufflen;
  617. cmd->buffer = sreq->sr_buffer;
  618. cmd->retries = 0;
  619. cmd->allowed = sreq->sr_allowed;
  620. cmd->done = sreq->sr_done;
  621. cmd->timeout_per_command = sreq->sr_timeout_per_command;
  622. cmd->sc_data_direction = sreq->sr_data_direction;
  623. cmd->sglist_len = sreq->sr_sglist_len;
  624. cmd->underflow = sreq->sr_underflow;
  625. cmd->sc_request = sreq;
  626. memcpy(cmd->cmnd, sreq->sr_cmnd, sizeof(sreq->sr_cmnd));
  627. /*
  628. * Zero the sense buffer. Some host adapters automatically request
  629. * sense on error. 0 is not a valid sense code.
  630. */
  631. memset(cmd->sense_buffer, 0, sizeof(sreq->sr_sense_buffer));
  632. cmd->request_buffer = sreq->sr_buffer;
  633. cmd->request_bufflen = sreq->sr_bufflen;
  634. cmd->old_use_sg = cmd->use_sg;
  635. if (cmd->cmd_len == 0)
  636. cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]);
  637. cmd->old_cmd_len = cmd->cmd_len;
  638. cmd->sc_old_data_direction = cmd->sc_data_direction;
  639. cmd->old_underflow = cmd->underflow;
  640. /*
  641. * Start the timer ticking.
  642. */
  643. cmd->result = 0;
  644. SCSI_LOG_MLQUEUE(3, printk("Leaving scsi_init_cmd_from_req()\n"));
  645. }
  646. /*
  647. * Per-CPU I/O completion queue.
  648. */
  649. static DEFINE_PER_CPU(struct list_head, scsi_done_q);
  650. /**
  651. * scsi_done - Enqueue the finished SCSI command into the done queue.
  652. * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
  653. * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
  654. *
  655. * This function is the mid-level's (SCSI Core) interrupt routine, which
  656. * regains ownership of the SCSI command (de facto) from a LLDD, and enqueues
  657. * the command to the done queue for further processing.
  658. *
  659. * This is the producer of the done queue who enqueues at the tail.
  660. *
  661. * This function is interrupt context safe.
  662. */
  663. static void scsi_done(struct scsi_cmnd *cmd)
  664. {
  665. /*
  666. * We don't have to worry about this one timing out any more.
  667. * If we are unable to remove the timer, then the command
  668. * has already timed out. In which case, we have no choice but to
  669. * let the timeout function run, as we have no idea where in fact
  670. * that function could really be. It might be on another processor,
  671. * etc, etc.
  672. */
  673. if (!scsi_delete_timer(cmd))
  674. return;
  675. __scsi_done(cmd);
  676. }
  677. /* Private entry to scsi_done() to complete a command when the timer
  678. * isn't running --- used by scsi_times_out */
  679. void __scsi_done(struct scsi_cmnd *cmd)
  680. {
  681. unsigned long flags;
  682. /*
  683. * Set the serial numbers back to zero
  684. */
  685. cmd->serial_number = 0;
  686. cmd->state = SCSI_STATE_BHQUEUE;
  687. cmd->owner = SCSI_OWNER_BH_HANDLER;
  688. atomic_inc(&cmd->device->iodone_cnt);
  689. if (cmd->result)
  690. atomic_inc(&cmd->device->ioerr_cnt);
  691. /*
  692. * Next, enqueue the command into the done queue.
  693. * It is a per-CPU queue, so we just disable local interrupts
  694. * and need no spinlock.
  695. */
  696. local_irq_save(flags);
  697. list_add_tail(&cmd->eh_entry, &__get_cpu_var(scsi_done_q));
  698. raise_softirq_irqoff(SCSI_SOFTIRQ);
  699. local_irq_restore(flags);
  700. }
  701. /**
  702. * scsi_softirq - Perform post-interrupt processing of finished SCSI commands.
  703. *
  704. * This is the consumer of the done queue.
  705. *
  706. * This is called with all interrupts enabled. This should reduce
  707. * interrupt latency, stack depth, and reentrancy of the low-level
  708. * drivers.
  709. */
  710. static void scsi_softirq(struct softirq_action *h)
  711. {
  712. int disposition;
  713. LIST_HEAD(local_q);
  714. local_irq_disable();
  715. list_splice_init(&__get_cpu_var(scsi_done_q), &local_q);
  716. local_irq_enable();
  717. while (!list_empty(&local_q)) {
  718. struct scsi_cmnd *cmd = list_entry(local_q.next,
  719. struct scsi_cmnd, eh_entry);
  720. list_del_init(&cmd->eh_entry);
  721. disposition = scsi_decide_disposition(cmd);
  722. scsi_log_completion(cmd, disposition);
  723. switch (disposition) {
  724. case SUCCESS:
  725. scsi_finish_command(cmd);
  726. break;
  727. case NEEDS_RETRY:
  728. scsi_retry_command(cmd);
  729. break;
  730. case ADD_TO_MLQUEUE:
  731. scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
  732. break;
  733. default:
  734. if (!scsi_eh_scmd_add(cmd, 0))
  735. scsi_finish_command(cmd);
  736. }
  737. }
  738. }
  739. /*
  740. * Function: scsi_retry_command
  741. *
  742. * Purpose: Send a command back to the low level to be retried.
  743. *
  744. * Notes: This command is always executed in the context of the
  745. * bottom half handler, or the error handler thread. Low
  746. * level drivers should not become re-entrant as a result of
  747. * this.
  748. */
  749. static int scsi_retry_command(struct scsi_cmnd *cmd)
  750. {
  751. /*
  752. * Restore the SCSI command state.
  753. */
  754. scsi_setup_cmd_retry(cmd);
  755. /*
  756. * Zero the sense information from the last time we tried
  757. * this command.
  758. */
  759. memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
  760. return scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
  761. }
  762. /*
  763. * Function: scsi_finish_command
  764. *
  765. * Purpose: Pass command off to upper layer for finishing of I/O
  766. * request, waking processes that are waiting on results,
  767. * etc.
  768. */
  769. void scsi_finish_command(struct scsi_cmnd *cmd)
  770. {
  771. struct scsi_device *sdev = cmd->device;
  772. struct Scsi_Host *shost = sdev->host;
  773. struct scsi_request *sreq;
  774. scsi_device_unbusy(sdev);
  775. /*
  776. * Clear the flags which say that the device/host is no longer
  777. * capable of accepting new commands. These are set in scsi_queue.c
  778. * for both the queue full condition on a device, and for a
  779. * host full condition on the host.
  780. *
  781. * XXX(hch): What about locking?
  782. */
  783. shost->host_blocked = 0;
  784. sdev->device_blocked = 0;
  785. /*
  786. * If we have valid sense information, then some kind of recovery
  787. * must have taken place. Make a note of this.
  788. */
  789. if (SCSI_SENSE_VALID(cmd))
  790. cmd->result |= (DRIVER_SENSE << 24);
  791. SCSI_LOG_MLCOMPLETE(4, printk("Notifying upper driver of completion "
  792. "for device %d %x\n", sdev->id, cmd->result));
  793. cmd->owner = SCSI_OWNER_HIGHLEVEL;
  794. cmd->state = SCSI_STATE_FINISHED;
  795. /*
  796. * We can get here with use_sg=0, causing a panic in the upper level
  797. */
  798. cmd->use_sg = cmd->old_use_sg;
  799. /*
  800. * If there is an associated request structure, copy the data over
  801. * before we call the completion function.
  802. */
  803. sreq = cmd->sc_request;
  804. if (sreq) {
  805. sreq->sr_result = sreq->sr_command->result;
  806. if (sreq->sr_result) {
  807. memcpy(sreq->sr_sense_buffer,
  808. sreq->sr_command->sense_buffer,
  809. sizeof(sreq->sr_sense_buffer));
  810. }
  811. }
  812. cmd->done(cmd);
  813. }
  814. EXPORT_SYMBOL(scsi_finish_command);
  815. /*
  816. * Function: scsi_adjust_queue_depth()
  817. *
  818. * Purpose: Allow low level drivers to tell us to change the queue depth
  819. * on a specific SCSI device
  820. *
  821. * Arguments: sdev - SCSI Device in question
  822. * tagged - Do we use tagged queueing (non-0) or do we treat
  823. * this device as an untagged device (0)
  824. * tags - Number of tags allowed if tagged queueing enabled,
  825. * or number of commands the low level driver can
  826. * queue up in non-tagged mode (as per cmd_per_lun).
  827. *
  828. * Returns: Nothing
  829. *
  830. * Lock Status: None held on entry
  831. *
  832. * Notes: Low level drivers may call this at any time and we will do
  833. * the right thing depending on whether or not the device is
  834. * currently active and whether or not it even has the
  835. * command blocks built yet.
  836. */
  837. void scsi_adjust_queue_depth(struct scsi_device *sdev, int tagged, int tags)
  838. {
  839. unsigned long flags;
  840. /*
  841. * refuse to set tagged depth to an unworkable size
  842. */
  843. if (tags <= 0)
  844. return;
  845. spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
  846. /* Check to see if the queue is managed by the block layer
  847. * if it is, and we fail to adjust the depth, exit */
  848. if (blk_queue_tagged(sdev->request_queue) &&
  849. blk_queue_resize_tags(sdev->request_queue, tags) != 0)
  850. goto out;
  851. sdev->queue_depth = tags;
  852. switch (tagged) {
  853. case MSG_ORDERED_TAG:
  854. sdev->ordered_tags = 1;
  855. sdev->simple_tags = 1;
  856. break;
  857. case MSG_SIMPLE_TAG:
  858. sdev->ordered_tags = 0;
  859. sdev->simple_tags = 1;
  860. break;
  861. default:
  862. printk(KERN_WARNING "(scsi%d:%d:%d:%d) "
  863. "scsi_adjust_queue_depth, bad queue type, "
  864. "disabled\n", sdev->host->host_no,
  865. sdev->channel, sdev->id, sdev->lun);
  866. case 0:
  867. sdev->ordered_tags = sdev->simple_tags = 0;
  868. sdev->queue_depth = tags;
  869. break;
  870. }
  871. out:
  872. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  873. }
  874. EXPORT_SYMBOL(scsi_adjust_queue_depth);
  875. /*
  876. * Function: scsi_track_queue_full()
  877. *
  878. * Purpose: This function will track successive QUEUE_FULL events on a
  879. * specific SCSI device to determine if and when there is a
  880. * need to adjust the queue depth on the device.
  881. *
  882. * Arguments: sdev - SCSI Device in question
  883. * depth - Current number of outstanding SCSI commands on
  884. * this device, not counting the one returned as
  885. * QUEUE_FULL.
  886. *
  887. * Returns: 0 - No change needed
  888. * >0 - Adjust queue depth to this new depth
  889. * -1 - Drop back to untagged operation using host->cmd_per_lun
  890. * as the untagged command depth
  891. *
  892. * Lock Status: None held on entry
  893. *
  894. * Notes: Low level drivers may call this at any time and we will do
  895. * "The Right Thing." We are interrupt context safe.
  896. */
  897. int scsi_track_queue_full(struct scsi_device *sdev, int depth)
  898. {
  899. if ((jiffies >> 4) == sdev->last_queue_full_time)
  900. return 0;
  901. sdev->last_queue_full_time = (jiffies >> 4);
  902. if (sdev->last_queue_full_depth != depth) {
  903. sdev->last_queue_full_count = 1;
  904. sdev->last_queue_full_depth = depth;
  905. } else {
  906. sdev->last_queue_full_count++;
  907. }
  908. if (sdev->last_queue_full_count <= 10)
  909. return 0;
  910. if (sdev->last_queue_full_depth < 8) {
  911. /* Drop back to untagged */
  912. scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
  913. return -1;
  914. }
  915. if (sdev->ordered_tags)
  916. scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, depth);
  917. else
  918. scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
  919. return depth;
  920. }
  921. EXPORT_SYMBOL(scsi_track_queue_full);
  922. /**
  923. * scsi_device_get - get an addition reference to a scsi_device
  924. * @sdev: device to get a reference to
  925. *
  926. * Gets a reference to the scsi_device and increments the use count
  927. * of the underlying LLDD module. You must hold host_lock of the
  928. * parent Scsi_Host or already have a reference when calling this.
  929. */
  930. int scsi_device_get(struct scsi_device *sdev)
  931. {
  932. if (sdev->sdev_state == SDEV_DEL || sdev->sdev_state == SDEV_CANCEL)
  933. return -ENXIO;
  934. if (!get_device(&sdev->sdev_gendev))
  935. return -ENXIO;
  936. if (!try_module_get(sdev->host->hostt->module)) {
  937. put_device(&sdev->sdev_gendev);
  938. return -ENXIO;
  939. }
  940. return 0;
  941. }
  942. EXPORT_SYMBOL(scsi_device_get);
  943. /**
  944. * scsi_device_put - release a reference to a scsi_device
  945. * @sdev: device to release a reference on.
  946. *
  947. * Release a reference to the scsi_device and decrements the use count
  948. * of the underlying LLDD module. The device is freed once the last
  949. * user vanishes.
  950. */
  951. void scsi_device_put(struct scsi_device *sdev)
  952. {
  953. module_put(sdev->host->hostt->module);
  954. put_device(&sdev->sdev_gendev);
  955. }
  956. EXPORT_SYMBOL(scsi_device_put);
  957. /* helper for shost_for_each_device, thus not documented */
  958. struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost,
  959. struct scsi_device *prev)
  960. {
  961. struct list_head *list = (prev ? &prev->siblings : &shost->__devices);
  962. struct scsi_device *next = NULL;
  963. unsigned long flags;
  964. spin_lock_irqsave(shost->host_lock, flags);
  965. while (list->next != &shost->__devices) {
  966. next = list_entry(list->next, struct scsi_device, siblings);
  967. /* skip devices that we can't get a reference to */
  968. if (!scsi_device_get(next))
  969. break;
  970. next = NULL;
  971. list = list->next;
  972. }
  973. spin_unlock_irqrestore(shost->host_lock, flags);
  974. if (prev)
  975. scsi_device_put(prev);
  976. return next;
  977. }
  978. EXPORT_SYMBOL(__scsi_iterate_devices);
  979. /**
  980. * starget_for_each_device - helper to walk all devices of a target
  981. * @starget: target whose devices we want to iterate over.
  982. *
  983. * This traverses over each devices of @shost. The devices have
  984. * a reference that must be released by scsi_host_put when breaking
  985. * out of the loop.
  986. */
  987. void starget_for_each_device(struct scsi_target *starget, void * data,
  988. void (*fn)(struct scsi_device *, void *))
  989. {
  990. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  991. struct scsi_device *sdev;
  992. shost_for_each_device(sdev, shost) {
  993. if ((sdev->channel == starget->channel) &&
  994. (sdev->id == starget->id))
  995. fn(sdev, data);
  996. }
  997. }
  998. EXPORT_SYMBOL(starget_for_each_device);
  999. /**
  1000. * __scsi_device_lookup_by_target - find a device given the target (UNLOCKED)
  1001. * @starget: SCSI target pointer
  1002. * @lun: SCSI Logical Unit Number
  1003. *
  1004. * Looks up the scsi_device with the specified @lun for a give
  1005. * @starget. The returned scsi_device does not have an additional
  1006. * reference. You must hold the host's host_lock over this call and
  1007. * any access to the returned scsi_device.
  1008. *
  1009. * Note: The only reason why drivers would want to use this is because
  1010. * they're need to access the device list in irq context. Otherwise you
  1011. * really want to use scsi_device_lookup_by_target instead.
  1012. **/
  1013. struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget,
  1014. uint lun)
  1015. {
  1016. struct scsi_device *sdev;
  1017. list_for_each_entry(sdev, &starget->devices, same_target_siblings) {
  1018. if (sdev->lun ==lun)
  1019. return sdev;
  1020. }
  1021. return NULL;
  1022. }
  1023. EXPORT_SYMBOL(__scsi_device_lookup_by_target);
  1024. /**
  1025. * scsi_device_lookup_by_target - find a device given the target
  1026. * @starget: SCSI target pointer
  1027. * @lun: SCSI Logical Unit Number
  1028. *
  1029. * Looks up the scsi_device with the specified @channel, @id, @lun for a
  1030. * give host. The returned scsi_device has an additional reference that
  1031. * needs to be release with scsi_host_put once you're done with it.
  1032. **/
  1033. struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget,
  1034. uint lun)
  1035. {
  1036. struct scsi_device *sdev;
  1037. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1038. unsigned long flags;
  1039. spin_lock_irqsave(shost->host_lock, flags);
  1040. sdev = __scsi_device_lookup_by_target(starget, lun);
  1041. if (sdev && scsi_device_get(sdev))
  1042. sdev = NULL;
  1043. spin_unlock_irqrestore(shost->host_lock, flags);
  1044. return sdev;
  1045. }
  1046. EXPORT_SYMBOL(scsi_device_lookup_by_target);
  1047. /**
  1048. * scsi_device_lookup - find a device given the host (UNLOCKED)
  1049. * @shost: SCSI host pointer
  1050. * @channel: SCSI channel (zero if only one channel)
  1051. * @pun: SCSI target number (physical unit number)
  1052. * @lun: SCSI Logical Unit Number
  1053. *
  1054. * Looks up the scsi_device with the specified @channel, @id, @lun for a
  1055. * give host. The returned scsi_device does not have an additional reference.
  1056. * You must hold the host's host_lock over this call and any access to the
  1057. * returned scsi_device.
  1058. *
  1059. * Note: The only reason why drivers would want to use this is because
  1060. * they're need to access the device list in irq context. Otherwise you
  1061. * really want to use scsi_device_lookup instead.
  1062. **/
  1063. struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost,
  1064. uint channel, uint id, uint lun)
  1065. {
  1066. struct scsi_device *sdev;
  1067. list_for_each_entry(sdev, &shost->__devices, siblings) {
  1068. if (sdev->channel == channel && sdev->id == id &&
  1069. sdev->lun ==lun)
  1070. return sdev;
  1071. }
  1072. return NULL;
  1073. }
  1074. EXPORT_SYMBOL(__scsi_device_lookup);
  1075. /**
  1076. * scsi_device_lookup - find a device given the host
  1077. * @shost: SCSI host pointer
  1078. * @channel: SCSI channel (zero if only one channel)
  1079. * @id: SCSI target number (physical unit number)
  1080. * @lun: SCSI Logical Unit Number
  1081. *
  1082. * Looks up the scsi_device with the specified @channel, @id, @lun for a
  1083. * give host. The returned scsi_device has an additional reference that
  1084. * needs to be release with scsi_host_put once you're done with it.
  1085. **/
  1086. struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost,
  1087. uint channel, uint id, uint lun)
  1088. {
  1089. struct scsi_device *sdev;
  1090. unsigned long flags;
  1091. spin_lock_irqsave(shost->host_lock, flags);
  1092. sdev = __scsi_device_lookup(shost, channel, id, lun);
  1093. if (sdev && scsi_device_get(sdev))
  1094. sdev = NULL;
  1095. spin_unlock_irqrestore(shost->host_lock, flags);
  1096. return sdev;
  1097. }
  1098. EXPORT_SYMBOL(scsi_device_lookup);
  1099. /**
  1100. * scsi_device_cancel - cancel outstanding IO to this device
  1101. * @sdev: Pointer to struct scsi_device
  1102. * @recovery: Boolean instructing function to recover device or not.
  1103. *
  1104. **/
  1105. int scsi_device_cancel(struct scsi_device *sdev, int recovery)
  1106. {
  1107. struct scsi_cmnd *scmd;
  1108. LIST_HEAD(active_list);
  1109. struct list_head *lh, *lh_sf;
  1110. unsigned long flags;
  1111. scsi_device_set_state(sdev, SDEV_CANCEL);
  1112. spin_lock_irqsave(&sdev->list_lock, flags);
  1113. list_for_each_entry(scmd, &sdev->cmd_list, list) {
  1114. if (scmd->request && scmd->request->rq_status != RQ_INACTIVE) {
  1115. /*
  1116. * If we are unable to remove the timer, it means
  1117. * that the command has already timed out or
  1118. * finished.
  1119. */
  1120. if (!scsi_delete_timer(scmd))
  1121. continue;
  1122. list_add_tail(&scmd->eh_entry, &active_list);
  1123. }
  1124. }
  1125. spin_unlock_irqrestore(&sdev->list_lock, flags);
  1126. if (!list_empty(&active_list)) {
  1127. list_for_each_safe(lh, lh_sf, &active_list) {
  1128. scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
  1129. list_del_init(lh);
  1130. if (recovery) {
  1131. scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD);
  1132. } else {
  1133. scmd->result = (DID_ABORT << 16);
  1134. scsi_finish_command(scmd);
  1135. }
  1136. }
  1137. }
  1138. return 0;
  1139. }
  1140. EXPORT_SYMBOL(scsi_device_cancel);
  1141. #ifdef CONFIG_HOTPLUG_CPU
  1142. static int scsi_cpu_notify(struct notifier_block *self,
  1143. unsigned long action, void *hcpu)
  1144. {
  1145. int cpu = (unsigned long)hcpu;
  1146. switch(action) {
  1147. case CPU_DEAD:
  1148. /* Drain scsi_done_q. */
  1149. local_irq_disable();
  1150. list_splice_init(&per_cpu(scsi_done_q, cpu),
  1151. &__get_cpu_var(scsi_done_q));
  1152. raise_softirq_irqoff(SCSI_SOFTIRQ);
  1153. local_irq_enable();
  1154. break;
  1155. default:
  1156. break;
  1157. }
  1158. return NOTIFY_OK;
  1159. }
  1160. static struct notifier_block __devinitdata scsi_cpu_nb = {
  1161. .notifier_call = scsi_cpu_notify,
  1162. };
  1163. #define register_scsi_cpu() register_cpu_notifier(&scsi_cpu_nb)
  1164. #define unregister_scsi_cpu() unregister_cpu_notifier(&scsi_cpu_nb)
  1165. #else
  1166. #define register_scsi_cpu()
  1167. #define unregister_scsi_cpu()
  1168. #endif /* CONFIG_HOTPLUG_CPU */
  1169. MODULE_DESCRIPTION("SCSI core");
  1170. MODULE_LICENSE("GPL");
  1171. module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR);
  1172. MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels");
  1173. static int __init init_scsi(void)
  1174. {
  1175. int error, i;
  1176. error = scsi_init_queue();
  1177. if (error)
  1178. return error;
  1179. error = scsi_init_procfs();
  1180. if (error)
  1181. goto cleanup_queue;
  1182. error = scsi_init_devinfo();
  1183. if (error)
  1184. goto cleanup_procfs;
  1185. error = scsi_init_hosts();
  1186. if (error)
  1187. goto cleanup_devlist;
  1188. error = scsi_init_sysctl();
  1189. if (error)
  1190. goto cleanup_hosts;
  1191. error = scsi_sysfs_register();
  1192. if (error)
  1193. goto cleanup_sysctl;
  1194. for (i = 0; i < NR_CPUS; i++)
  1195. INIT_LIST_HEAD(&per_cpu(scsi_done_q, i));
  1196. devfs_mk_dir("scsi");
  1197. open_softirq(SCSI_SOFTIRQ, scsi_softirq, NULL);
  1198. register_scsi_cpu();
  1199. printk(KERN_NOTICE "SCSI subsystem initialized\n");
  1200. return 0;
  1201. cleanup_sysctl:
  1202. scsi_exit_sysctl();
  1203. cleanup_hosts:
  1204. scsi_exit_hosts();
  1205. cleanup_devlist:
  1206. scsi_exit_devinfo();
  1207. cleanup_procfs:
  1208. scsi_exit_procfs();
  1209. cleanup_queue:
  1210. scsi_exit_queue();
  1211. printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n",
  1212. -error);
  1213. return error;
  1214. }
  1215. static void __exit exit_scsi(void)
  1216. {
  1217. scsi_sysfs_unregister();
  1218. scsi_exit_sysctl();
  1219. scsi_exit_hosts();
  1220. scsi_exit_devinfo();
  1221. devfs_remove("scsi");
  1222. scsi_exit_procfs();
  1223. scsi_exit_queue();
  1224. unregister_scsi_cpu();
  1225. }
  1226. subsys_initcall(init_scsi);
  1227. module_exit(exit_scsi);