scsi.c 36 KB

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