scsi_lib.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016
  1. /*
  2. * scsi_lib.c Copyright (C) 1999 Eric Youngdale
  3. *
  4. * SCSI queueing library.
  5. * Initial versions: Eric Youngdale (eric@andante.org).
  6. * Based upon conversations with large numbers
  7. * of people at Linux Expo.
  8. */
  9. #include <linux/bio.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/completion.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mempool.h>
  14. #include <linux/slab.h>
  15. #include <linux/init.h>
  16. #include <linux/pci.h>
  17. #include <linux/delay.h>
  18. #include <scsi/scsi.h>
  19. #include <scsi/scsi_dbg.h>
  20. #include <scsi/scsi_device.h>
  21. #include <scsi/scsi_driver.h>
  22. #include <scsi/scsi_eh.h>
  23. #include <scsi/scsi_host.h>
  24. #include <scsi/scsi_request.h>
  25. #include "scsi_priv.h"
  26. #include "scsi_logging.h"
  27. #define SG_MEMPOOL_NR (sizeof(scsi_sg_pools)/sizeof(struct scsi_host_sg_pool))
  28. #define SG_MEMPOOL_SIZE 32
  29. struct scsi_host_sg_pool {
  30. size_t size;
  31. char *name;
  32. kmem_cache_t *slab;
  33. mempool_t *pool;
  34. };
  35. #if (SCSI_MAX_PHYS_SEGMENTS < 32)
  36. #error SCSI_MAX_PHYS_SEGMENTS is too small
  37. #endif
  38. #define SP(x) { x, "sgpool-" #x }
  39. static struct scsi_host_sg_pool scsi_sg_pools[] = {
  40. SP(8),
  41. SP(16),
  42. SP(32),
  43. #if (SCSI_MAX_PHYS_SEGMENTS > 32)
  44. SP(64),
  45. #if (SCSI_MAX_PHYS_SEGMENTS > 64)
  46. SP(128),
  47. #if (SCSI_MAX_PHYS_SEGMENTS > 128)
  48. SP(256),
  49. #if (SCSI_MAX_PHYS_SEGMENTS > 256)
  50. #error SCSI_MAX_PHYS_SEGMENTS is too large
  51. #endif
  52. #endif
  53. #endif
  54. #endif
  55. };
  56. #undef SP
  57. /*
  58. * Function: scsi_insert_special_req()
  59. *
  60. * Purpose: Insert pre-formed request into request queue.
  61. *
  62. * Arguments: sreq - request that is ready to be queued.
  63. * at_head - boolean. True if we should insert at head
  64. * of queue, false if we should insert at tail.
  65. *
  66. * Lock status: Assumed that lock is not held upon entry.
  67. *
  68. * Returns: Nothing
  69. *
  70. * Notes: This function is called from character device and from
  71. * ioctl types of functions where the caller knows exactly
  72. * what SCSI command needs to be issued. The idea is that
  73. * we merely inject the command into the queue (at the head
  74. * for now), and then call the queue request function to actually
  75. * process it.
  76. */
  77. int scsi_insert_special_req(struct scsi_request *sreq, int at_head)
  78. {
  79. /*
  80. * Because users of this function are apt to reuse requests with no
  81. * modification, we have to sanitise the request flags here
  82. */
  83. sreq->sr_request->flags &= ~REQ_DONTPREP;
  84. blk_insert_request(sreq->sr_device->request_queue, sreq->sr_request,
  85. at_head, sreq);
  86. return 0;
  87. }
  88. static void scsi_run_queue(struct request_queue *q);
  89. /*
  90. * Function: scsi_queue_insert()
  91. *
  92. * Purpose: Insert a command in the midlevel queue.
  93. *
  94. * Arguments: cmd - command that we are adding to queue.
  95. * reason - why we are inserting command to queue.
  96. *
  97. * Lock status: Assumed that lock is not held upon entry.
  98. *
  99. * Returns: Nothing.
  100. *
  101. * Notes: We do this for one of two cases. Either the host is busy
  102. * and it cannot accept any more commands for the time being,
  103. * or the device returned QUEUE_FULL and can accept no more
  104. * commands.
  105. * Notes: This could be called either from an interrupt context or a
  106. * normal process context.
  107. */
  108. int scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
  109. {
  110. struct Scsi_Host *host = cmd->device->host;
  111. struct scsi_device *device = cmd->device;
  112. struct request_queue *q = device->request_queue;
  113. unsigned long flags;
  114. SCSI_LOG_MLQUEUE(1,
  115. printk("Inserting command %p into mlqueue\n", cmd));
  116. /*
  117. * Set the appropriate busy bit for the device/host.
  118. *
  119. * If the host/device isn't busy, assume that something actually
  120. * completed, and that we should be able to queue a command now.
  121. *
  122. * Note that the prior mid-layer assumption that any host could
  123. * always queue at least one command is now broken. The mid-layer
  124. * will implement a user specifiable stall (see
  125. * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
  126. * if a command is requeued with no other commands outstanding
  127. * either for the device or for the host.
  128. */
  129. if (reason == SCSI_MLQUEUE_HOST_BUSY)
  130. host->host_blocked = host->max_host_blocked;
  131. else if (reason == SCSI_MLQUEUE_DEVICE_BUSY)
  132. device->device_blocked = device->max_device_blocked;
  133. /*
  134. * Decrement the counters, since these commands are no longer
  135. * active on the host/device.
  136. */
  137. scsi_device_unbusy(device);
  138. /*
  139. * Requeue this command. It will go before all other commands
  140. * that are already in the queue.
  141. *
  142. * NOTE: there is magic here about the way the queue is plugged if
  143. * we have no outstanding commands.
  144. *
  145. * Although we *don't* plug the queue, we call the request
  146. * function. The SCSI request function detects the blocked condition
  147. * and plugs the queue appropriately.
  148. */
  149. spin_lock_irqsave(q->queue_lock, flags);
  150. blk_requeue_request(q, cmd->request);
  151. spin_unlock_irqrestore(q->queue_lock, flags);
  152. scsi_run_queue(q);
  153. return 0;
  154. }
  155. /*
  156. * Function: scsi_do_req
  157. *
  158. * Purpose: Queue a SCSI request
  159. *
  160. * Arguments: sreq - command descriptor.
  161. * cmnd - actual SCSI command to be performed.
  162. * buffer - data buffer.
  163. * bufflen - size of data buffer.
  164. * done - completion function to be run.
  165. * timeout - how long to let it run before timeout.
  166. * retries - number of retries we allow.
  167. *
  168. * Lock status: No locks held upon entry.
  169. *
  170. * Returns: Nothing.
  171. *
  172. * Notes: This function is only used for queueing requests for things
  173. * like ioctls and character device requests - this is because
  174. * we essentially just inject a request into the queue for the
  175. * device.
  176. *
  177. * In order to support the scsi_device_quiesce function, we
  178. * now inject requests on the *head* of the device queue
  179. * rather than the tail.
  180. */
  181. void scsi_do_req(struct scsi_request *sreq, const void *cmnd,
  182. void *buffer, unsigned bufflen,
  183. void (*done)(struct scsi_cmnd *),
  184. int timeout, int retries)
  185. {
  186. /*
  187. * If the upper level driver is reusing these things, then
  188. * we should release the low-level block now. Another one will
  189. * be allocated later when this request is getting queued.
  190. */
  191. __scsi_release_request(sreq);
  192. /*
  193. * Our own function scsi_done (which marks the host as not busy,
  194. * disables the timeout counter, etc) will be called by us or by the
  195. * scsi_hosts[host].queuecommand() function needs to also call
  196. * the completion function for the high level driver.
  197. */
  198. memcpy(sreq->sr_cmnd, cmnd, sizeof(sreq->sr_cmnd));
  199. sreq->sr_bufflen = bufflen;
  200. sreq->sr_buffer = buffer;
  201. sreq->sr_allowed = retries;
  202. sreq->sr_done = done;
  203. sreq->sr_timeout_per_command = timeout;
  204. if (sreq->sr_cmd_len == 0)
  205. sreq->sr_cmd_len = COMMAND_SIZE(sreq->sr_cmnd[0]);
  206. /*
  207. * head injection *required* here otherwise quiesce won't work
  208. */
  209. scsi_insert_special_req(sreq, 1);
  210. }
  211. EXPORT_SYMBOL(scsi_do_req);
  212. static void scsi_wait_done(struct scsi_cmnd *cmd)
  213. {
  214. struct request *req = cmd->request;
  215. struct request_queue *q = cmd->device->request_queue;
  216. unsigned long flags;
  217. req->rq_status = RQ_SCSI_DONE; /* Busy, but indicate request done */
  218. spin_lock_irqsave(q->queue_lock, flags);
  219. if (blk_rq_tagged(req))
  220. blk_queue_end_tag(q, req);
  221. spin_unlock_irqrestore(q->queue_lock, flags);
  222. if (req->waiting)
  223. complete(req->waiting);
  224. }
  225. /* This is the end routine we get to if a command was never attached
  226. * to the request. Simply complete the request without changing
  227. * rq_status; this will cause a DRIVER_ERROR. */
  228. static void scsi_wait_req_end_io(struct request *req)
  229. {
  230. BUG_ON(!req->waiting);
  231. complete(req->waiting);
  232. }
  233. void scsi_wait_req(struct scsi_request *sreq, const void *cmnd, void *buffer,
  234. unsigned bufflen, int timeout, int retries)
  235. {
  236. DECLARE_COMPLETION(wait);
  237. sreq->sr_request->waiting = &wait;
  238. sreq->sr_request->rq_status = RQ_SCSI_BUSY;
  239. sreq->sr_request->end_io = scsi_wait_req_end_io;
  240. scsi_do_req(sreq, cmnd, buffer, bufflen, scsi_wait_done,
  241. timeout, retries);
  242. wait_for_completion(&wait);
  243. sreq->sr_request->waiting = NULL;
  244. if (sreq->sr_request->rq_status != RQ_SCSI_DONE)
  245. sreq->sr_result |= (DRIVER_ERROR << 24);
  246. __scsi_release_request(sreq);
  247. }
  248. EXPORT_SYMBOL(scsi_wait_req);
  249. /*
  250. * Function: scsi_init_cmd_errh()
  251. *
  252. * Purpose: Initialize cmd fields related to error handling.
  253. *
  254. * Arguments: cmd - command that is ready to be queued.
  255. *
  256. * Returns: Nothing
  257. *
  258. * Notes: This function has the job of initializing a number of
  259. * fields related to error handling. Typically this will
  260. * be called once for each command, as required.
  261. */
  262. static int scsi_init_cmd_errh(struct scsi_cmnd *cmd)
  263. {
  264. cmd->serial_number = 0;
  265. memset(cmd->sense_buffer, 0, sizeof cmd->sense_buffer);
  266. if (cmd->cmd_len == 0)
  267. cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]);
  268. /*
  269. * We need saved copies of a number of fields - this is because
  270. * error handling may need to overwrite these with different values
  271. * to run different commands, and once error handling is complete,
  272. * we will need to restore these values prior to running the actual
  273. * command.
  274. */
  275. cmd->old_use_sg = cmd->use_sg;
  276. cmd->old_cmd_len = cmd->cmd_len;
  277. cmd->sc_old_data_direction = cmd->sc_data_direction;
  278. cmd->old_underflow = cmd->underflow;
  279. memcpy(cmd->data_cmnd, cmd->cmnd, sizeof(cmd->cmnd));
  280. cmd->buffer = cmd->request_buffer;
  281. cmd->bufflen = cmd->request_bufflen;
  282. return 1;
  283. }
  284. /*
  285. * Function: scsi_setup_cmd_retry()
  286. *
  287. * Purpose: Restore the command state for a retry
  288. *
  289. * Arguments: cmd - command to be restored
  290. *
  291. * Returns: Nothing
  292. *
  293. * Notes: Immediately prior to retrying a command, we need
  294. * to restore certain fields that we saved above.
  295. */
  296. void scsi_setup_cmd_retry(struct scsi_cmnd *cmd)
  297. {
  298. memcpy(cmd->cmnd, cmd->data_cmnd, sizeof(cmd->data_cmnd));
  299. cmd->request_buffer = cmd->buffer;
  300. cmd->request_bufflen = cmd->bufflen;
  301. cmd->use_sg = cmd->old_use_sg;
  302. cmd->cmd_len = cmd->old_cmd_len;
  303. cmd->sc_data_direction = cmd->sc_old_data_direction;
  304. cmd->underflow = cmd->old_underflow;
  305. }
  306. void scsi_device_unbusy(struct scsi_device *sdev)
  307. {
  308. struct Scsi_Host *shost = sdev->host;
  309. unsigned long flags;
  310. spin_lock_irqsave(shost->host_lock, flags);
  311. shost->host_busy--;
  312. if (unlikely(test_bit(SHOST_RECOVERY, &shost->shost_state) &&
  313. shost->host_failed))
  314. scsi_eh_wakeup(shost);
  315. spin_unlock(shost->host_lock);
  316. spin_lock(sdev->request_queue->queue_lock);
  317. sdev->device_busy--;
  318. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  319. }
  320. /*
  321. * Called for single_lun devices on IO completion. Clear starget_sdev_user,
  322. * and call blk_run_queue for all the scsi_devices on the target -
  323. * including current_sdev first.
  324. *
  325. * Called with *no* scsi locks held.
  326. */
  327. static void scsi_single_lun_run(struct scsi_device *current_sdev)
  328. {
  329. struct Scsi_Host *shost = current_sdev->host;
  330. struct scsi_device *sdev, *tmp;
  331. struct scsi_target *starget = scsi_target(current_sdev);
  332. unsigned long flags;
  333. spin_lock_irqsave(shost->host_lock, flags);
  334. starget->starget_sdev_user = NULL;
  335. spin_unlock_irqrestore(shost->host_lock, flags);
  336. /*
  337. * Call blk_run_queue for all LUNs on the target, starting with
  338. * current_sdev. We race with others (to set starget_sdev_user),
  339. * but in most cases, we will be first. Ideally, each LU on the
  340. * target would get some limited time or requests on the target.
  341. */
  342. blk_run_queue(current_sdev->request_queue);
  343. spin_lock_irqsave(shost->host_lock, flags);
  344. if (starget->starget_sdev_user)
  345. goto out;
  346. list_for_each_entry_safe(sdev, tmp, &starget->devices,
  347. same_target_siblings) {
  348. if (sdev == current_sdev)
  349. continue;
  350. if (scsi_device_get(sdev))
  351. continue;
  352. spin_unlock_irqrestore(shost->host_lock, flags);
  353. blk_run_queue(sdev->request_queue);
  354. spin_lock_irqsave(shost->host_lock, flags);
  355. scsi_device_put(sdev);
  356. }
  357. out:
  358. spin_unlock_irqrestore(shost->host_lock, flags);
  359. }
  360. /*
  361. * Function: scsi_run_queue()
  362. *
  363. * Purpose: Select a proper request queue to serve next
  364. *
  365. * Arguments: q - last request's queue
  366. *
  367. * Returns: Nothing
  368. *
  369. * Notes: The previous command was completely finished, start
  370. * a new one if possible.
  371. */
  372. static void scsi_run_queue(struct request_queue *q)
  373. {
  374. struct scsi_device *sdev = q->queuedata;
  375. struct Scsi_Host *shost = sdev->host;
  376. unsigned long flags;
  377. if (sdev->single_lun)
  378. scsi_single_lun_run(sdev);
  379. spin_lock_irqsave(shost->host_lock, flags);
  380. while (!list_empty(&shost->starved_list) &&
  381. !shost->host_blocked && !shost->host_self_blocked &&
  382. !((shost->can_queue > 0) &&
  383. (shost->host_busy >= shost->can_queue))) {
  384. /*
  385. * As long as shost is accepting commands and we have
  386. * starved queues, call blk_run_queue. scsi_request_fn
  387. * drops the queue_lock and can add us back to the
  388. * starved_list.
  389. *
  390. * host_lock protects the starved_list and starved_entry.
  391. * scsi_request_fn must get the host_lock before checking
  392. * or modifying starved_list or starved_entry.
  393. */
  394. sdev = list_entry(shost->starved_list.next,
  395. struct scsi_device, starved_entry);
  396. list_del_init(&sdev->starved_entry);
  397. spin_unlock_irqrestore(shost->host_lock, flags);
  398. blk_run_queue(sdev->request_queue);
  399. spin_lock_irqsave(shost->host_lock, flags);
  400. if (unlikely(!list_empty(&sdev->starved_entry)))
  401. /*
  402. * sdev lost a race, and was put back on the
  403. * starved list. This is unlikely but without this
  404. * in theory we could loop forever.
  405. */
  406. break;
  407. }
  408. spin_unlock_irqrestore(shost->host_lock, flags);
  409. blk_run_queue(q);
  410. }
  411. /*
  412. * Function: scsi_requeue_command()
  413. *
  414. * Purpose: Handle post-processing of completed commands.
  415. *
  416. * Arguments: q - queue to operate on
  417. * cmd - command that may need to be requeued.
  418. *
  419. * Returns: Nothing
  420. *
  421. * Notes: After command completion, there may be blocks left
  422. * over which weren't finished by the previous command
  423. * this can be for a number of reasons - the main one is
  424. * I/O errors in the middle of the request, in which case
  425. * we need to request the blocks that come after the bad
  426. * sector.
  427. */
  428. static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
  429. {
  430. unsigned long flags;
  431. cmd->request->flags &= ~REQ_DONTPREP;
  432. spin_lock_irqsave(q->queue_lock, flags);
  433. blk_requeue_request(q, cmd->request);
  434. spin_unlock_irqrestore(q->queue_lock, flags);
  435. scsi_run_queue(q);
  436. }
  437. void scsi_next_command(struct scsi_cmnd *cmd)
  438. {
  439. struct request_queue *q = cmd->device->request_queue;
  440. scsi_put_command(cmd);
  441. scsi_run_queue(q);
  442. }
  443. void scsi_run_host_queues(struct Scsi_Host *shost)
  444. {
  445. struct scsi_device *sdev;
  446. shost_for_each_device(sdev, shost)
  447. scsi_run_queue(sdev->request_queue);
  448. }
  449. /*
  450. * Function: scsi_end_request()
  451. *
  452. * Purpose: Post-processing of completed commands (usually invoked at end
  453. * of upper level post-processing and scsi_io_completion).
  454. *
  455. * Arguments: cmd - command that is complete.
  456. * uptodate - 1 if I/O indicates success, <= 0 for I/O error.
  457. * bytes - number of bytes of completed I/O
  458. * requeue - indicates whether we should requeue leftovers.
  459. *
  460. * Lock status: Assumed that lock is not held upon entry.
  461. *
  462. * Returns: cmd if requeue done or required, NULL otherwise
  463. *
  464. * Notes: This is called for block device requests in order to
  465. * mark some number of sectors as complete.
  466. *
  467. * We are guaranteeing that the request queue will be goosed
  468. * at some point during this call.
  469. */
  470. static struct scsi_cmnd *scsi_end_request(struct scsi_cmnd *cmd, int uptodate,
  471. int bytes, int requeue)
  472. {
  473. request_queue_t *q = cmd->device->request_queue;
  474. struct request *req = cmd->request;
  475. unsigned long flags;
  476. /*
  477. * If there are blocks left over at the end, set up the command
  478. * to queue the remainder of them.
  479. */
  480. if (end_that_request_chunk(req, uptodate, bytes)) {
  481. int leftover = (req->hard_nr_sectors << 9);
  482. if (blk_pc_request(req))
  483. leftover = req->data_len;
  484. /* kill remainder if no retrys */
  485. if (!uptodate && blk_noretry_request(req))
  486. end_that_request_chunk(req, 0, leftover);
  487. else {
  488. if (requeue)
  489. /*
  490. * Bleah. Leftovers again. Stick the
  491. * leftovers in the front of the
  492. * queue, and goose the queue again.
  493. */
  494. scsi_requeue_command(q, cmd);
  495. return cmd;
  496. }
  497. }
  498. add_disk_randomness(req->rq_disk);
  499. spin_lock_irqsave(q->queue_lock, flags);
  500. if (blk_rq_tagged(req))
  501. blk_queue_end_tag(q, req);
  502. end_that_request_last(req);
  503. spin_unlock_irqrestore(q->queue_lock, flags);
  504. /*
  505. * This will goose the queue request function at the end, so we don't
  506. * need to worry about launching another command.
  507. */
  508. scsi_next_command(cmd);
  509. return NULL;
  510. }
  511. static struct scatterlist *scsi_alloc_sgtable(struct scsi_cmnd *cmd, int gfp_mask)
  512. {
  513. struct scsi_host_sg_pool *sgp;
  514. struct scatterlist *sgl;
  515. BUG_ON(!cmd->use_sg);
  516. switch (cmd->use_sg) {
  517. case 1 ... 8:
  518. cmd->sglist_len = 0;
  519. break;
  520. case 9 ... 16:
  521. cmd->sglist_len = 1;
  522. break;
  523. case 17 ... 32:
  524. cmd->sglist_len = 2;
  525. break;
  526. #if (SCSI_MAX_PHYS_SEGMENTS > 32)
  527. case 33 ... 64:
  528. cmd->sglist_len = 3;
  529. break;
  530. #if (SCSI_MAX_PHYS_SEGMENTS > 64)
  531. case 65 ... 128:
  532. cmd->sglist_len = 4;
  533. break;
  534. #if (SCSI_MAX_PHYS_SEGMENTS > 128)
  535. case 129 ... 256:
  536. cmd->sglist_len = 5;
  537. break;
  538. #endif
  539. #endif
  540. #endif
  541. default:
  542. return NULL;
  543. }
  544. sgp = scsi_sg_pools + cmd->sglist_len;
  545. sgl = mempool_alloc(sgp->pool, gfp_mask);
  546. return sgl;
  547. }
  548. static void scsi_free_sgtable(struct scatterlist *sgl, int index)
  549. {
  550. struct scsi_host_sg_pool *sgp;
  551. BUG_ON(index > SG_MEMPOOL_NR);
  552. sgp = scsi_sg_pools + index;
  553. mempool_free(sgl, sgp->pool);
  554. }
  555. /*
  556. * Function: scsi_release_buffers()
  557. *
  558. * Purpose: Completion processing for block device I/O requests.
  559. *
  560. * Arguments: cmd - command that we are bailing.
  561. *
  562. * Lock status: Assumed that no lock is held upon entry.
  563. *
  564. * Returns: Nothing
  565. *
  566. * Notes: In the event that an upper level driver rejects a
  567. * command, we must release resources allocated during
  568. * the __init_io() function. Primarily this would involve
  569. * the scatter-gather table, and potentially any bounce
  570. * buffers.
  571. */
  572. static void scsi_release_buffers(struct scsi_cmnd *cmd)
  573. {
  574. struct request *req = cmd->request;
  575. /*
  576. * Free up any indirection buffers we allocated for DMA purposes.
  577. */
  578. if (cmd->use_sg)
  579. scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
  580. else if (cmd->request_buffer != req->buffer)
  581. kfree(cmd->request_buffer);
  582. /*
  583. * Zero these out. They now point to freed memory, and it is
  584. * dangerous to hang onto the pointers.
  585. */
  586. cmd->buffer = NULL;
  587. cmd->bufflen = 0;
  588. cmd->request_buffer = NULL;
  589. cmd->request_bufflen = 0;
  590. }
  591. /*
  592. * Function: scsi_io_completion()
  593. *
  594. * Purpose: Completion processing for block device I/O requests.
  595. *
  596. * Arguments: cmd - command that is finished.
  597. *
  598. * Lock status: Assumed that no lock is held upon entry.
  599. *
  600. * Returns: Nothing
  601. *
  602. * Notes: This function is matched in terms of capabilities to
  603. * the function that created the scatter-gather list.
  604. * In other words, if there are no bounce buffers
  605. * (the normal case for most drivers), we don't need
  606. * the logic to deal with cleaning up afterwards.
  607. *
  608. * We must do one of several things here:
  609. *
  610. * a) Call scsi_end_request. This will finish off the
  611. * specified number of sectors. If we are done, the
  612. * command block will be released, and the queue
  613. * function will be goosed. If we are not done, then
  614. * scsi_end_request will directly goose the queue.
  615. *
  616. * b) We can just use scsi_requeue_command() here. This would
  617. * be used if we just wanted to retry, for example.
  618. */
  619. void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes,
  620. unsigned int block_bytes)
  621. {
  622. int result = cmd->result;
  623. int this_count = cmd->bufflen;
  624. request_queue_t *q = cmd->device->request_queue;
  625. struct request *req = cmd->request;
  626. int clear_errors = 1;
  627. struct scsi_sense_hdr sshdr;
  628. int sense_valid = 0;
  629. int sense_deferred = 0;
  630. if (blk_complete_barrier_rq(q, req, good_bytes >> 9))
  631. return;
  632. /*
  633. * Free up any indirection buffers we allocated for DMA purposes.
  634. * For the case of a READ, we need to copy the data out of the
  635. * bounce buffer and into the real buffer.
  636. */
  637. if (cmd->use_sg)
  638. scsi_free_sgtable(cmd->buffer, cmd->sglist_len);
  639. else if (cmd->buffer != req->buffer) {
  640. if (rq_data_dir(req) == READ) {
  641. unsigned long flags;
  642. char *to = bio_kmap_irq(req->bio, &flags);
  643. memcpy(to, cmd->buffer, cmd->bufflen);
  644. bio_kunmap_irq(to, &flags);
  645. }
  646. kfree(cmd->buffer);
  647. }
  648. if (result) {
  649. sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
  650. if (sense_valid)
  651. sense_deferred = scsi_sense_is_deferred(&sshdr);
  652. }
  653. if (blk_pc_request(req)) { /* SG_IO ioctl from block level */
  654. req->errors = result;
  655. if (result) {
  656. clear_errors = 0;
  657. if (sense_valid && req->sense) {
  658. /*
  659. * SG_IO wants current and deferred errors
  660. */
  661. int len = 8 + cmd->sense_buffer[7];
  662. if (len > SCSI_SENSE_BUFFERSIZE)
  663. len = SCSI_SENSE_BUFFERSIZE;
  664. memcpy(req->sense, cmd->sense_buffer, len);
  665. req->sense_len = len;
  666. }
  667. } else
  668. req->data_len = cmd->resid;
  669. }
  670. /*
  671. * Zero these out. They now point to freed memory, and it is
  672. * dangerous to hang onto the pointers.
  673. */
  674. cmd->buffer = NULL;
  675. cmd->bufflen = 0;
  676. cmd->request_buffer = NULL;
  677. cmd->request_bufflen = 0;
  678. /*
  679. * Next deal with any sectors which we were able to correctly
  680. * handle.
  681. */
  682. if (good_bytes >= 0) {
  683. SCSI_LOG_HLCOMPLETE(1, printk("%ld sectors total, %d bytes done.\n",
  684. req->nr_sectors, good_bytes));
  685. SCSI_LOG_HLCOMPLETE(1, printk("use_sg is %d\n", cmd->use_sg));
  686. if (clear_errors)
  687. req->errors = 0;
  688. /*
  689. * If multiple sectors are requested in one buffer, then
  690. * they will have been finished off by the first command.
  691. * If not, then we have a multi-buffer command.
  692. *
  693. * If block_bytes != 0, it means we had a medium error
  694. * of some sort, and that we want to mark some number of
  695. * sectors as not uptodate. Thus we want to inhibit
  696. * requeueing right here - we will requeue down below
  697. * when we handle the bad sectors.
  698. */
  699. cmd = scsi_end_request(cmd, 1, good_bytes, result == 0);
  700. /*
  701. * If the command completed without error, then either finish off the
  702. * rest of the command, or start a new one.
  703. */
  704. if (result == 0 || cmd == NULL ) {
  705. return;
  706. }
  707. }
  708. /*
  709. * Now, if we were good little boys and girls, Santa left us a request
  710. * sense buffer. We can extract information from this, so we
  711. * can choose a block to remap, etc.
  712. */
  713. if (sense_valid && !sense_deferred) {
  714. switch (sshdr.sense_key) {
  715. case UNIT_ATTENTION:
  716. if (cmd->device->removable) {
  717. /* detected disc change. set a bit
  718. * and quietly refuse further access.
  719. */
  720. cmd->device->changed = 1;
  721. cmd = scsi_end_request(cmd, 0,
  722. this_count, 1);
  723. return;
  724. } else {
  725. /*
  726. * Must have been a power glitch, or a
  727. * bus reset. Could not have been a
  728. * media change, so we just retry the
  729. * request and see what happens.
  730. */
  731. scsi_requeue_command(q, cmd);
  732. return;
  733. }
  734. break;
  735. case ILLEGAL_REQUEST:
  736. /*
  737. * If we had an ILLEGAL REQUEST returned, then we may
  738. * have performed an unsupported command. The only
  739. * thing this should be would be a ten byte read where
  740. * only a six byte read was supported. Also, on a
  741. * system where READ CAPACITY failed, we may have read
  742. * past the end of the disk.
  743. */
  744. if (cmd->device->use_10_for_rw &&
  745. (cmd->cmnd[0] == READ_10 ||
  746. cmd->cmnd[0] == WRITE_10)) {
  747. cmd->device->use_10_for_rw = 0;
  748. /*
  749. * This will cause a retry with a 6-byte
  750. * command.
  751. */
  752. scsi_requeue_command(q, cmd);
  753. result = 0;
  754. } else {
  755. cmd = scsi_end_request(cmd, 0, this_count, 1);
  756. return;
  757. }
  758. break;
  759. case NOT_READY:
  760. /*
  761. * If the device is in the process of becoming ready,
  762. * retry.
  763. */
  764. if (sshdr.asc == 0x04 && sshdr.ascq == 0x01) {
  765. scsi_requeue_command(q, cmd);
  766. return;
  767. }
  768. printk(KERN_INFO "Device %s not ready.\n",
  769. req->rq_disk ? req->rq_disk->disk_name : "");
  770. cmd = scsi_end_request(cmd, 0, this_count, 1);
  771. return;
  772. case VOLUME_OVERFLOW:
  773. printk(KERN_INFO "Volume overflow <%d %d %d %d> CDB: ",
  774. cmd->device->host->host_no,
  775. (int)cmd->device->channel,
  776. (int)cmd->device->id, (int)cmd->device->lun);
  777. __scsi_print_command(cmd->data_cmnd);
  778. scsi_print_sense("", cmd);
  779. cmd = scsi_end_request(cmd, 0, block_bytes, 1);
  780. return;
  781. default:
  782. break;
  783. }
  784. } /* driver byte != 0 */
  785. if (host_byte(result) == DID_RESET) {
  786. /*
  787. * Third party bus reset or reset for error
  788. * recovery reasons. Just retry the request
  789. * and see what happens.
  790. */
  791. scsi_requeue_command(q, cmd);
  792. return;
  793. }
  794. if (result) {
  795. printk(KERN_INFO "SCSI error : <%d %d %d %d> return code "
  796. "= 0x%x\n", cmd->device->host->host_no,
  797. cmd->device->channel,
  798. cmd->device->id,
  799. cmd->device->lun, result);
  800. if (driver_byte(result) & DRIVER_SENSE)
  801. scsi_print_sense("", cmd);
  802. /*
  803. * Mark a single buffer as not uptodate. Queue the remainder.
  804. * We sometimes get this cruft in the event that a medium error
  805. * isn't properly reported.
  806. */
  807. block_bytes = req->hard_cur_sectors << 9;
  808. if (!block_bytes)
  809. block_bytes = req->data_len;
  810. cmd = scsi_end_request(cmd, 0, block_bytes, 1);
  811. }
  812. }
  813. EXPORT_SYMBOL(scsi_io_completion);
  814. /*
  815. * Function: scsi_init_io()
  816. *
  817. * Purpose: SCSI I/O initialize function.
  818. *
  819. * Arguments: cmd - Command descriptor we wish to initialize
  820. *
  821. * Returns: 0 on success
  822. * BLKPREP_DEFER if the failure is retryable
  823. * BLKPREP_KILL if the failure is fatal
  824. */
  825. static int scsi_init_io(struct scsi_cmnd *cmd)
  826. {
  827. struct request *req = cmd->request;
  828. struct scatterlist *sgpnt;
  829. int count;
  830. /*
  831. * if this is a rq->data based REQ_BLOCK_PC, setup for a non-sg xfer
  832. */
  833. if ((req->flags & REQ_BLOCK_PC) && !req->bio) {
  834. cmd->request_bufflen = req->data_len;
  835. cmd->request_buffer = req->data;
  836. req->buffer = req->data;
  837. cmd->use_sg = 0;
  838. return 0;
  839. }
  840. /*
  841. * we used to not use scatter-gather for single segment request,
  842. * but now we do (it makes highmem I/O easier to support without
  843. * kmapping pages)
  844. */
  845. cmd->use_sg = req->nr_phys_segments;
  846. /*
  847. * if sg table allocation fails, requeue request later.
  848. */
  849. sgpnt = scsi_alloc_sgtable(cmd, GFP_ATOMIC);
  850. if (unlikely(!sgpnt))
  851. return BLKPREP_DEFER;
  852. cmd->request_buffer = (char *) sgpnt;
  853. cmd->request_bufflen = req->nr_sectors << 9;
  854. if (blk_pc_request(req))
  855. cmd->request_bufflen = req->data_len;
  856. req->buffer = NULL;
  857. /*
  858. * Next, walk the list, and fill in the addresses and sizes of
  859. * each segment.
  860. */
  861. count = blk_rq_map_sg(req->q, req, cmd->request_buffer);
  862. /*
  863. * mapped well, send it off
  864. */
  865. if (likely(count <= cmd->use_sg)) {
  866. cmd->use_sg = count;
  867. return 0;
  868. }
  869. printk(KERN_ERR "Incorrect number of segments after building list\n");
  870. printk(KERN_ERR "counted %d, received %d\n", count, cmd->use_sg);
  871. printk(KERN_ERR "req nr_sec %lu, cur_nr_sec %u\n", req->nr_sectors,
  872. req->current_nr_sectors);
  873. /* release the command and kill it */
  874. scsi_release_buffers(cmd);
  875. scsi_put_command(cmd);
  876. return BLKPREP_KILL;
  877. }
  878. static int scsi_prepare_flush_fn(request_queue_t *q, struct request *rq)
  879. {
  880. struct scsi_device *sdev = q->queuedata;
  881. struct scsi_driver *drv;
  882. if (sdev->sdev_state == SDEV_RUNNING) {
  883. drv = *(struct scsi_driver **) rq->rq_disk->private_data;
  884. if (drv->prepare_flush)
  885. return drv->prepare_flush(q, rq);
  886. }
  887. return 0;
  888. }
  889. static void scsi_end_flush_fn(request_queue_t *q, struct request *rq)
  890. {
  891. struct scsi_device *sdev = q->queuedata;
  892. struct request *flush_rq = rq->end_io_data;
  893. struct scsi_driver *drv;
  894. if (flush_rq->errors) {
  895. printk("scsi: barrier error, disabling flush support\n");
  896. blk_queue_ordered(q, QUEUE_ORDERED_NONE);
  897. }
  898. if (sdev->sdev_state == SDEV_RUNNING) {
  899. drv = *(struct scsi_driver **) rq->rq_disk->private_data;
  900. drv->end_flush(q, rq);
  901. }
  902. }
  903. static int scsi_issue_flush_fn(request_queue_t *q, struct gendisk *disk,
  904. sector_t *error_sector)
  905. {
  906. struct scsi_device *sdev = q->queuedata;
  907. struct scsi_driver *drv;
  908. if (sdev->sdev_state != SDEV_RUNNING)
  909. return -ENXIO;
  910. drv = *(struct scsi_driver **) disk->private_data;
  911. if (drv->issue_flush)
  912. return drv->issue_flush(&sdev->sdev_gendev, error_sector);
  913. return -EOPNOTSUPP;
  914. }
  915. static int scsi_prep_fn(struct request_queue *q, struct request *req)
  916. {
  917. struct scsi_device *sdev = q->queuedata;
  918. struct scsi_cmnd *cmd;
  919. int specials_only = 0;
  920. /*
  921. * Just check to see if the device is online. If it isn't, we
  922. * refuse to process any commands. The device must be brought
  923. * online before trying any recovery commands
  924. */
  925. if (unlikely(!scsi_device_online(sdev))) {
  926. printk(KERN_ERR "scsi%d (%d:%d): rejecting I/O to offline device\n",
  927. sdev->host->host_no, sdev->id, sdev->lun);
  928. return BLKPREP_KILL;
  929. }
  930. if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
  931. /* OK, we're not in a running state don't prep
  932. * user commands */
  933. if (sdev->sdev_state == SDEV_DEL) {
  934. /* Device is fully deleted, no commands
  935. * at all allowed down */
  936. printk(KERN_ERR "scsi%d (%d:%d): rejecting I/O to dead device\n",
  937. sdev->host->host_no, sdev->id, sdev->lun);
  938. return BLKPREP_KILL;
  939. }
  940. /* OK, we only allow special commands (i.e. not
  941. * user initiated ones */
  942. specials_only = sdev->sdev_state;
  943. }
  944. /*
  945. * Find the actual device driver associated with this command.
  946. * The SPECIAL requests are things like character device or
  947. * ioctls, which did not originate from ll_rw_blk. Note that
  948. * the special field is also used to indicate the cmd for
  949. * the remainder of a partially fulfilled request that can
  950. * come up when there is a medium error. We have to treat
  951. * these two cases differently. We differentiate by looking
  952. * at request->cmd, as this tells us the real story.
  953. */
  954. if (req->flags & REQ_SPECIAL) {
  955. struct scsi_request *sreq = req->special;
  956. if (sreq->sr_magic == SCSI_REQ_MAGIC) {
  957. cmd = scsi_get_command(sreq->sr_device, GFP_ATOMIC);
  958. if (unlikely(!cmd))
  959. goto defer;
  960. scsi_init_cmd_from_req(cmd, sreq);
  961. } else
  962. cmd = req->special;
  963. } else if (req->flags & (REQ_CMD | REQ_BLOCK_PC)) {
  964. if(unlikely(specials_only)) {
  965. if(specials_only == SDEV_QUIESCE ||
  966. specials_only == SDEV_BLOCK)
  967. return BLKPREP_DEFER;
  968. printk(KERN_ERR "scsi%d (%d:%d): rejecting I/O to device being removed\n",
  969. sdev->host->host_no, sdev->id, sdev->lun);
  970. return BLKPREP_KILL;
  971. }
  972. /*
  973. * Now try and find a command block that we can use.
  974. */
  975. if (!req->special) {
  976. cmd = scsi_get_command(sdev, GFP_ATOMIC);
  977. if (unlikely(!cmd))
  978. goto defer;
  979. } else
  980. cmd = req->special;
  981. /* pull a tag out of the request if we have one */
  982. cmd->tag = req->tag;
  983. } else {
  984. blk_dump_rq_flags(req, "SCSI bad req");
  985. return BLKPREP_KILL;
  986. }
  987. /* note the overloading of req->special. When the tag
  988. * is active it always means cmd. If the tag goes
  989. * back for re-queueing, it may be reset */
  990. req->special = cmd;
  991. cmd->request = req;
  992. /*
  993. * FIXME: drop the lock here because the functions below
  994. * expect to be called without the queue lock held. Also,
  995. * previously, we dequeued the request before dropping the
  996. * lock. We hope REQ_STARTED prevents anything untoward from
  997. * happening now.
  998. */
  999. if (req->flags & (REQ_CMD | REQ_BLOCK_PC)) {
  1000. struct scsi_driver *drv;
  1001. int ret;
  1002. /*
  1003. * This will do a couple of things:
  1004. * 1) Fill in the actual SCSI command.
  1005. * 2) Fill in any other upper-level specific fields
  1006. * (timeout).
  1007. *
  1008. * If this returns 0, it means that the request failed
  1009. * (reading past end of disk, reading offline device,
  1010. * etc). This won't actually talk to the device, but
  1011. * some kinds of consistency checking may cause the
  1012. * request to be rejected immediately.
  1013. */
  1014. /*
  1015. * This sets up the scatter-gather table (allocating if
  1016. * required).
  1017. */
  1018. ret = scsi_init_io(cmd);
  1019. if (ret) /* BLKPREP_KILL return also releases the command */
  1020. return ret;
  1021. /*
  1022. * Initialize the actual SCSI command for this request.
  1023. */
  1024. drv = *(struct scsi_driver **)req->rq_disk->private_data;
  1025. if (unlikely(!drv->init_command(cmd))) {
  1026. scsi_release_buffers(cmd);
  1027. scsi_put_command(cmd);
  1028. return BLKPREP_KILL;
  1029. }
  1030. }
  1031. /*
  1032. * The request is now prepped, no need to come back here
  1033. */
  1034. req->flags |= REQ_DONTPREP;
  1035. return BLKPREP_OK;
  1036. defer:
  1037. /* If we defer, the elv_next_request() returns NULL, but the
  1038. * queue must be restarted, so we plug here if no returning
  1039. * command will automatically do that. */
  1040. if (sdev->device_busy == 0)
  1041. blk_plug_device(q);
  1042. return BLKPREP_DEFER;
  1043. }
  1044. /*
  1045. * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
  1046. * return 0.
  1047. *
  1048. * Called with the queue_lock held.
  1049. */
  1050. static inline int scsi_dev_queue_ready(struct request_queue *q,
  1051. struct scsi_device *sdev)
  1052. {
  1053. if (sdev->device_busy >= sdev->queue_depth)
  1054. return 0;
  1055. if (sdev->device_busy == 0 && sdev->device_blocked) {
  1056. /*
  1057. * unblock after device_blocked iterates to zero
  1058. */
  1059. if (--sdev->device_blocked == 0) {
  1060. SCSI_LOG_MLQUEUE(3,
  1061. printk("scsi%d (%d:%d) unblocking device at"
  1062. " zero depth\n", sdev->host->host_no,
  1063. sdev->id, sdev->lun));
  1064. } else {
  1065. blk_plug_device(q);
  1066. return 0;
  1067. }
  1068. }
  1069. if (sdev->device_blocked)
  1070. return 0;
  1071. return 1;
  1072. }
  1073. /*
  1074. * scsi_host_queue_ready: if we can send requests to shost, return 1 else
  1075. * return 0. We must end up running the queue again whenever 0 is
  1076. * returned, else IO can hang.
  1077. *
  1078. * Called with host_lock held.
  1079. */
  1080. static inline int scsi_host_queue_ready(struct request_queue *q,
  1081. struct Scsi_Host *shost,
  1082. struct scsi_device *sdev)
  1083. {
  1084. if (test_bit(SHOST_RECOVERY, &shost->shost_state))
  1085. return 0;
  1086. if (shost->host_busy == 0 && shost->host_blocked) {
  1087. /*
  1088. * unblock after host_blocked iterates to zero
  1089. */
  1090. if (--shost->host_blocked == 0) {
  1091. SCSI_LOG_MLQUEUE(3,
  1092. printk("scsi%d unblocking host at zero depth\n",
  1093. shost->host_no));
  1094. } else {
  1095. blk_plug_device(q);
  1096. return 0;
  1097. }
  1098. }
  1099. if ((shost->can_queue > 0 && shost->host_busy >= shost->can_queue) ||
  1100. shost->host_blocked || shost->host_self_blocked) {
  1101. if (list_empty(&sdev->starved_entry))
  1102. list_add_tail(&sdev->starved_entry, &shost->starved_list);
  1103. return 0;
  1104. }
  1105. /* We're OK to process the command, so we can't be starved */
  1106. if (!list_empty(&sdev->starved_entry))
  1107. list_del_init(&sdev->starved_entry);
  1108. return 1;
  1109. }
  1110. /*
  1111. * Kill requests for a dead device
  1112. */
  1113. static void scsi_kill_requests(request_queue_t *q)
  1114. {
  1115. struct request *req;
  1116. while ((req = elv_next_request(q)) != NULL) {
  1117. blkdev_dequeue_request(req);
  1118. req->flags |= REQ_QUIET;
  1119. while (end_that_request_first(req, 0, req->nr_sectors))
  1120. ;
  1121. end_that_request_last(req);
  1122. }
  1123. }
  1124. /*
  1125. * Function: scsi_request_fn()
  1126. *
  1127. * Purpose: Main strategy routine for SCSI.
  1128. *
  1129. * Arguments: q - Pointer to actual queue.
  1130. *
  1131. * Returns: Nothing
  1132. *
  1133. * Lock status: IO request lock assumed to be held when called.
  1134. */
  1135. static void scsi_request_fn(struct request_queue *q)
  1136. {
  1137. struct scsi_device *sdev = q->queuedata;
  1138. struct Scsi_Host *shost;
  1139. struct scsi_cmnd *cmd;
  1140. struct request *req;
  1141. if (!sdev) {
  1142. printk("scsi: killing requests for dead queue\n");
  1143. scsi_kill_requests(q);
  1144. return;
  1145. }
  1146. if(!get_device(&sdev->sdev_gendev))
  1147. /* We must be tearing the block queue down already */
  1148. return;
  1149. /*
  1150. * To start with, we keep looping until the queue is empty, or until
  1151. * the host is no longer able to accept any more requests.
  1152. */
  1153. shost = sdev->host;
  1154. while (!blk_queue_plugged(q)) {
  1155. int rtn;
  1156. /*
  1157. * get next queueable request. We do this early to make sure
  1158. * that the request is fully prepared even if we cannot
  1159. * accept it.
  1160. */
  1161. req = elv_next_request(q);
  1162. if (!req || !scsi_dev_queue_ready(q, sdev))
  1163. break;
  1164. if (unlikely(!scsi_device_online(sdev))) {
  1165. printk(KERN_ERR "scsi%d (%d:%d): rejecting I/O to offline device\n",
  1166. sdev->host->host_no, sdev->id, sdev->lun);
  1167. blkdev_dequeue_request(req);
  1168. req->flags |= REQ_QUIET;
  1169. while (end_that_request_first(req, 0, req->nr_sectors))
  1170. ;
  1171. end_that_request_last(req);
  1172. continue;
  1173. }
  1174. /*
  1175. * Remove the request from the request list.
  1176. */
  1177. if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
  1178. blkdev_dequeue_request(req);
  1179. sdev->device_busy++;
  1180. spin_unlock(q->queue_lock);
  1181. spin_lock(shost->host_lock);
  1182. if (!scsi_host_queue_ready(q, shost, sdev))
  1183. goto not_ready;
  1184. if (sdev->single_lun) {
  1185. if (scsi_target(sdev)->starget_sdev_user &&
  1186. scsi_target(sdev)->starget_sdev_user != sdev)
  1187. goto not_ready;
  1188. scsi_target(sdev)->starget_sdev_user = sdev;
  1189. }
  1190. shost->host_busy++;
  1191. /*
  1192. * XXX(hch): This is rather suboptimal, scsi_dispatch_cmd will
  1193. * take the lock again.
  1194. */
  1195. spin_unlock_irq(shost->host_lock);
  1196. cmd = req->special;
  1197. if (unlikely(cmd == NULL)) {
  1198. printk(KERN_CRIT "impossible request in %s.\n"
  1199. "please mail a stack trace to "
  1200. "linux-scsi@vger.kernel.org",
  1201. __FUNCTION__);
  1202. BUG();
  1203. }
  1204. /*
  1205. * Finally, initialize any error handling parameters, and set up
  1206. * the timers for timeouts.
  1207. */
  1208. scsi_init_cmd_errh(cmd);
  1209. /*
  1210. * Dispatch the command to the low-level driver.
  1211. */
  1212. rtn = scsi_dispatch_cmd(cmd);
  1213. spin_lock_irq(q->queue_lock);
  1214. if(rtn) {
  1215. /* we're refusing the command; because of
  1216. * the way locks get dropped, we need to
  1217. * check here if plugging is required */
  1218. if(sdev->device_busy == 0)
  1219. blk_plug_device(q);
  1220. break;
  1221. }
  1222. }
  1223. goto out;
  1224. not_ready:
  1225. spin_unlock_irq(shost->host_lock);
  1226. /*
  1227. * lock q, handle tag, requeue req, and decrement device_busy. We
  1228. * must return with queue_lock held.
  1229. *
  1230. * Decrementing device_busy without checking it is OK, as all such
  1231. * cases (host limits or settings) should run the queue at some
  1232. * later time.
  1233. */
  1234. spin_lock_irq(q->queue_lock);
  1235. blk_requeue_request(q, req);
  1236. sdev->device_busy--;
  1237. if(sdev->device_busy == 0)
  1238. blk_plug_device(q);
  1239. out:
  1240. /* must be careful here...if we trigger the ->remove() function
  1241. * we cannot be holding the q lock */
  1242. spin_unlock_irq(q->queue_lock);
  1243. put_device(&sdev->sdev_gendev);
  1244. spin_lock_irq(q->queue_lock);
  1245. }
  1246. u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
  1247. {
  1248. struct device *host_dev;
  1249. u64 bounce_limit = 0xffffffff;
  1250. if (shost->unchecked_isa_dma)
  1251. return BLK_BOUNCE_ISA;
  1252. /*
  1253. * Platforms with virtual-DMA translation
  1254. * hardware have no practical limit.
  1255. */
  1256. if (!PCI_DMA_BUS_IS_PHYS)
  1257. return BLK_BOUNCE_ANY;
  1258. host_dev = scsi_get_device(shost);
  1259. if (host_dev && host_dev->dma_mask)
  1260. bounce_limit = *host_dev->dma_mask;
  1261. return bounce_limit;
  1262. }
  1263. EXPORT_SYMBOL(scsi_calculate_bounce_limit);
  1264. struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
  1265. {
  1266. struct Scsi_Host *shost = sdev->host;
  1267. struct request_queue *q;
  1268. q = blk_init_queue(scsi_request_fn, NULL);
  1269. if (!q)
  1270. return NULL;
  1271. blk_queue_prep_rq(q, scsi_prep_fn);
  1272. blk_queue_max_hw_segments(q, shost->sg_tablesize);
  1273. blk_queue_max_phys_segments(q, SCSI_MAX_PHYS_SEGMENTS);
  1274. blk_queue_max_sectors(q, shost->max_sectors);
  1275. blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
  1276. blk_queue_segment_boundary(q, shost->dma_boundary);
  1277. blk_queue_issue_flush_fn(q, scsi_issue_flush_fn);
  1278. /*
  1279. * ordered tags are superior to flush ordering
  1280. */
  1281. if (shost->ordered_tag)
  1282. blk_queue_ordered(q, QUEUE_ORDERED_TAG);
  1283. else if (shost->ordered_flush) {
  1284. blk_queue_ordered(q, QUEUE_ORDERED_FLUSH);
  1285. q->prepare_flush_fn = scsi_prepare_flush_fn;
  1286. q->end_flush_fn = scsi_end_flush_fn;
  1287. }
  1288. if (!shost->use_clustering)
  1289. clear_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
  1290. return q;
  1291. }
  1292. void scsi_free_queue(struct request_queue *q)
  1293. {
  1294. blk_cleanup_queue(q);
  1295. }
  1296. /*
  1297. * Function: scsi_block_requests()
  1298. *
  1299. * Purpose: Utility function used by low-level drivers to prevent further
  1300. * commands from being queued to the device.
  1301. *
  1302. * Arguments: shost - Host in question
  1303. *
  1304. * Returns: Nothing
  1305. *
  1306. * Lock status: No locks are assumed held.
  1307. *
  1308. * Notes: There is no timer nor any other means by which the requests
  1309. * get unblocked other than the low-level driver calling
  1310. * scsi_unblock_requests().
  1311. */
  1312. void scsi_block_requests(struct Scsi_Host *shost)
  1313. {
  1314. shost->host_self_blocked = 1;
  1315. }
  1316. EXPORT_SYMBOL(scsi_block_requests);
  1317. /*
  1318. * Function: scsi_unblock_requests()
  1319. *
  1320. * Purpose: Utility function used by low-level drivers to allow further
  1321. * commands from being queued to the device.
  1322. *
  1323. * Arguments: shost - Host in question
  1324. *
  1325. * Returns: Nothing
  1326. *
  1327. * Lock status: No locks are assumed held.
  1328. *
  1329. * Notes: There is no timer nor any other means by which the requests
  1330. * get unblocked other than the low-level driver calling
  1331. * scsi_unblock_requests().
  1332. *
  1333. * This is done as an API function so that changes to the
  1334. * internals of the scsi mid-layer won't require wholesale
  1335. * changes to drivers that use this feature.
  1336. */
  1337. void scsi_unblock_requests(struct Scsi_Host *shost)
  1338. {
  1339. shost->host_self_blocked = 0;
  1340. scsi_run_host_queues(shost);
  1341. }
  1342. EXPORT_SYMBOL(scsi_unblock_requests);
  1343. int __init scsi_init_queue(void)
  1344. {
  1345. int i;
  1346. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1347. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1348. int size = sgp->size * sizeof(struct scatterlist);
  1349. sgp->slab = kmem_cache_create(sgp->name, size, 0,
  1350. SLAB_HWCACHE_ALIGN, NULL, NULL);
  1351. if (!sgp->slab) {
  1352. printk(KERN_ERR "SCSI: can't init sg slab %s\n",
  1353. sgp->name);
  1354. }
  1355. sgp->pool = mempool_create(SG_MEMPOOL_SIZE,
  1356. mempool_alloc_slab, mempool_free_slab,
  1357. sgp->slab);
  1358. if (!sgp->pool) {
  1359. printk(KERN_ERR "SCSI: can't init sg mempool %s\n",
  1360. sgp->name);
  1361. }
  1362. }
  1363. return 0;
  1364. }
  1365. void scsi_exit_queue(void)
  1366. {
  1367. int i;
  1368. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1369. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1370. mempool_destroy(sgp->pool);
  1371. kmem_cache_destroy(sgp->slab);
  1372. }
  1373. }
  1374. /**
  1375. * __scsi_mode_sense - issue a mode sense, falling back from 10 to
  1376. * six bytes if necessary.
  1377. * @sreq: SCSI request to fill in with the MODE_SENSE
  1378. * @dbd: set if mode sense will allow block descriptors to be returned
  1379. * @modepage: mode page being requested
  1380. * @buffer: request buffer (may not be smaller than eight bytes)
  1381. * @len: length of request buffer.
  1382. * @timeout: command timeout
  1383. * @retries: number of retries before failing
  1384. * @data: returns a structure abstracting the mode header data
  1385. *
  1386. * Returns zero if unsuccessful, or the header offset (either 4
  1387. * or 8 depending on whether a six or ten byte command was
  1388. * issued) if successful.
  1389. **/
  1390. int
  1391. __scsi_mode_sense(struct scsi_request *sreq, int dbd, int modepage,
  1392. unsigned char *buffer, int len, int timeout, int retries,
  1393. struct scsi_mode_data *data) {
  1394. unsigned char cmd[12];
  1395. int use_10_for_ms;
  1396. int header_length;
  1397. memset(data, 0, sizeof(*data));
  1398. memset(&cmd[0], 0, 12);
  1399. cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
  1400. cmd[2] = modepage;
  1401. retry:
  1402. use_10_for_ms = sreq->sr_device->use_10_for_ms;
  1403. if (use_10_for_ms) {
  1404. if (len < 8)
  1405. len = 8;
  1406. cmd[0] = MODE_SENSE_10;
  1407. cmd[8] = len;
  1408. header_length = 8;
  1409. } else {
  1410. if (len < 4)
  1411. len = 4;
  1412. cmd[0] = MODE_SENSE;
  1413. cmd[4] = len;
  1414. header_length = 4;
  1415. }
  1416. sreq->sr_cmd_len = 0;
  1417. memset(sreq->sr_sense_buffer, 0, sizeof(sreq->sr_sense_buffer));
  1418. sreq->sr_data_direction = DMA_FROM_DEVICE;
  1419. memset(buffer, 0, len);
  1420. scsi_wait_req(sreq, cmd, buffer, len, timeout, retries);
  1421. /* This code looks awful: what it's doing is making sure an
  1422. * ILLEGAL REQUEST sense return identifies the actual command
  1423. * byte as the problem. MODE_SENSE commands can return
  1424. * ILLEGAL REQUEST if the code page isn't supported */
  1425. if (use_10_for_ms && !scsi_status_is_good(sreq->sr_result) &&
  1426. (driver_byte(sreq->sr_result) & DRIVER_SENSE)) {
  1427. struct scsi_sense_hdr sshdr;
  1428. if (scsi_request_normalize_sense(sreq, &sshdr)) {
  1429. if ((sshdr.sense_key == ILLEGAL_REQUEST) &&
  1430. (sshdr.asc == 0x20) && (sshdr.ascq == 0)) {
  1431. /*
  1432. * Invalid command operation code
  1433. */
  1434. sreq->sr_device->use_10_for_ms = 0;
  1435. goto retry;
  1436. }
  1437. }
  1438. }
  1439. if(scsi_status_is_good(sreq->sr_result)) {
  1440. data->header_length = header_length;
  1441. if(use_10_for_ms) {
  1442. data->length = buffer[0]*256 + buffer[1] + 2;
  1443. data->medium_type = buffer[2];
  1444. data->device_specific = buffer[3];
  1445. data->longlba = buffer[4] & 0x01;
  1446. data->block_descriptor_length = buffer[6]*256
  1447. + buffer[7];
  1448. } else {
  1449. data->length = buffer[0] + 1;
  1450. data->medium_type = buffer[1];
  1451. data->device_specific = buffer[2];
  1452. data->block_descriptor_length = buffer[3];
  1453. }
  1454. }
  1455. return sreq->sr_result;
  1456. }
  1457. EXPORT_SYMBOL(__scsi_mode_sense);
  1458. /**
  1459. * scsi_mode_sense - issue a mode sense, falling back from 10 to
  1460. * six bytes if necessary.
  1461. * @sdev: scsi device to send command to.
  1462. * @dbd: set if mode sense will disable block descriptors in the return
  1463. * @modepage: mode page being requested
  1464. * @buffer: request buffer (may not be smaller than eight bytes)
  1465. * @len: length of request buffer.
  1466. * @timeout: command timeout
  1467. * @retries: number of retries before failing
  1468. *
  1469. * Returns zero if unsuccessful, or the header offset (either 4
  1470. * or 8 depending on whether a six or ten byte command was
  1471. * issued) if successful.
  1472. **/
  1473. int
  1474. scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
  1475. unsigned char *buffer, int len, int timeout, int retries,
  1476. struct scsi_mode_data *data)
  1477. {
  1478. struct scsi_request *sreq = scsi_allocate_request(sdev, GFP_KERNEL);
  1479. int ret;
  1480. if (!sreq)
  1481. return -1;
  1482. ret = __scsi_mode_sense(sreq, dbd, modepage, buffer, len,
  1483. timeout, retries, data);
  1484. scsi_release_request(sreq);
  1485. return ret;
  1486. }
  1487. EXPORT_SYMBOL(scsi_mode_sense);
  1488. int
  1489. scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries)
  1490. {
  1491. struct scsi_request *sreq;
  1492. char cmd[] = {
  1493. TEST_UNIT_READY, 0, 0, 0, 0, 0,
  1494. };
  1495. int result;
  1496. sreq = scsi_allocate_request(sdev, GFP_KERNEL);
  1497. if (!sreq)
  1498. return -ENOMEM;
  1499. sreq->sr_data_direction = DMA_NONE;
  1500. scsi_wait_req(sreq, cmd, NULL, 0, timeout, retries);
  1501. if ((driver_byte(sreq->sr_result) & DRIVER_SENSE) && sdev->removable) {
  1502. struct scsi_sense_hdr sshdr;
  1503. if ((scsi_request_normalize_sense(sreq, &sshdr)) &&
  1504. ((sshdr.sense_key == UNIT_ATTENTION) ||
  1505. (sshdr.sense_key == NOT_READY))) {
  1506. sdev->changed = 1;
  1507. sreq->sr_result = 0;
  1508. }
  1509. }
  1510. result = sreq->sr_result;
  1511. scsi_release_request(sreq);
  1512. return result;
  1513. }
  1514. EXPORT_SYMBOL(scsi_test_unit_ready);
  1515. /**
  1516. * scsi_device_set_state - Take the given device through the device
  1517. * state model.
  1518. * @sdev: scsi device to change the state of.
  1519. * @state: state to change to.
  1520. *
  1521. * Returns zero if unsuccessful or an error if the requested
  1522. * transition is illegal.
  1523. **/
  1524. int
  1525. scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
  1526. {
  1527. enum scsi_device_state oldstate = sdev->sdev_state;
  1528. if (state == oldstate)
  1529. return 0;
  1530. switch (state) {
  1531. case SDEV_CREATED:
  1532. /* There are no legal states that come back to
  1533. * created. This is the manually initialised start
  1534. * state */
  1535. goto illegal;
  1536. case SDEV_RUNNING:
  1537. switch (oldstate) {
  1538. case SDEV_CREATED:
  1539. case SDEV_OFFLINE:
  1540. case SDEV_QUIESCE:
  1541. case SDEV_BLOCK:
  1542. break;
  1543. default:
  1544. goto illegal;
  1545. }
  1546. break;
  1547. case SDEV_QUIESCE:
  1548. switch (oldstate) {
  1549. case SDEV_RUNNING:
  1550. case SDEV_OFFLINE:
  1551. break;
  1552. default:
  1553. goto illegal;
  1554. }
  1555. break;
  1556. case SDEV_OFFLINE:
  1557. switch (oldstate) {
  1558. case SDEV_CREATED:
  1559. case SDEV_RUNNING:
  1560. case SDEV_QUIESCE:
  1561. case SDEV_BLOCK:
  1562. break;
  1563. default:
  1564. goto illegal;
  1565. }
  1566. break;
  1567. case SDEV_BLOCK:
  1568. switch (oldstate) {
  1569. case SDEV_CREATED:
  1570. case SDEV_RUNNING:
  1571. break;
  1572. default:
  1573. goto illegal;
  1574. }
  1575. break;
  1576. case SDEV_CANCEL:
  1577. switch (oldstate) {
  1578. case SDEV_CREATED:
  1579. case SDEV_RUNNING:
  1580. case SDEV_OFFLINE:
  1581. case SDEV_BLOCK:
  1582. break;
  1583. default:
  1584. goto illegal;
  1585. }
  1586. break;
  1587. case SDEV_DEL:
  1588. switch (oldstate) {
  1589. case SDEV_CANCEL:
  1590. break;
  1591. default:
  1592. goto illegal;
  1593. }
  1594. break;
  1595. }
  1596. sdev->sdev_state = state;
  1597. return 0;
  1598. illegal:
  1599. SCSI_LOG_ERROR_RECOVERY(1,
  1600. dev_printk(KERN_ERR, &sdev->sdev_gendev,
  1601. "Illegal state transition %s->%s\n",
  1602. scsi_device_state_name(oldstate),
  1603. scsi_device_state_name(state))
  1604. );
  1605. return -EINVAL;
  1606. }
  1607. EXPORT_SYMBOL(scsi_device_set_state);
  1608. /**
  1609. * scsi_device_quiesce - Block user issued commands.
  1610. * @sdev: scsi device to quiesce.
  1611. *
  1612. * This works by trying to transition to the SDEV_QUIESCE state
  1613. * (which must be a legal transition). When the device is in this
  1614. * state, only special requests will be accepted, all others will
  1615. * be deferred. Since special requests may also be requeued requests,
  1616. * a successful return doesn't guarantee the device will be
  1617. * totally quiescent.
  1618. *
  1619. * Must be called with user context, may sleep.
  1620. *
  1621. * Returns zero if unsuccessful or an error if not.
  1622. **/
  1623. int
  1624. scsi_device_quiesce(struct scsi_device *sdev)
  1625. {
  1626. int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
  1627. if (err)
  1628. return err;
  1629. scsi_run_queue(sdev->request_queue);
  1630. while (sdev->device_busy) {
  1631. msleep_interruptible(200);
  1632. scsi_run_queue(sdev->request_queue);
  1633. }
  1634. return 0;
  1635. }
  1636. EXPORT_SYMBOL(scsi_device_quiesce);
  1637. /**
  1638. * scsi_device_resume - Restart user issued commands to a quiesced device.
  1639. * @sdev: scsi device to resume.
  1640. *
  1641. * Moves the device from quiesced back to running and restarts the
  1642. * queues.
  1643. *
  1644. * Must be called with user context, may sleep.
  1645. **/
  1646. void
  1647. scsi_device_resume(struct scsi_device *sdev)
  1648. {
  1649. if(scsi_device_set_state(sdev, SDEV_RUNNING))
  1650. return;
  1651. scsi_run_queue(sdev->request_queue);
  1652. }
  1653. EXPORT_SYMBOL(scsi_device_resume);
  1654. static void
  1655. device_quiesce_fn(struct scsi_device *sdev, void *data)
  1656. {
  1657. scsi_device_quiesce(sdev);
  1658. }
  1659. void
  1660. scsi_target_quiesce(struct scsi_target *starget)
  1661. {
  1662. starget_for_each_device(starget, NULL, device_quiesce_fn);
  1663. }
  1664. EXPORT_SYMBOL(scsi_target_quiesce);
  1665. static void
  1666. device_resume_fn(struct scsi_device *sdev, void *data)
  1667. {
  1668. scsi_device_resume(sdev);
  1669. }
  1670. void
  1671. scsi_target_resume(struct scsi_target *starget)
  1672. {
  1673. starget_for_each_device(starget, NULL, device_resume_fn);
  1674. }
  1675. EXPORT_SYMBOL(scsi_target_resume);
  1676. /**
  1677. * scsi_internal_device_block - internal function to put a device
  1678. * temporarily into the SDEV_BLOCK state
  1679. * @sdev: device to block
  1680. *
  1681. * Block request made by scsi lld's to temporarily stop all
  1682. * scsi commands on the specified device. Called from interrupt
  1683. * or normal process context.
  1684. *
  1685. * Returns zero if successful or error if not
  1686. *
  1687. * Notes:
  1688. * This routine transitions the device to the SDEV_BLOCK state
  1689. * (which must be a legal transition). When the device is in this
  1690. * state, all commands are deferred until the scsi lld reenables
  1691. * the device with scsi_device_unblock or device_block_tmo fires.
  1692. * This routine assumes the host_lock is held on entry.
  1693. **/
  1694. int
  1695. scsi_internal_device_block(struct scsi_device *sdev)
  1696. {
  1697. request_queue_t *q = sdev->request_queue;
  1698. unsigned long flags;
  1699. int err = 0;
  1700. err = scsi_device_set_state(sdev, SDEV_BLOCK);
  1701. if (err)
  1702. return err;
  1703. /*
  1704. * The device has transitioned to SDEV_BLOCK. Stop the
  1705. * block layer from calling the midlayer with this device's
  1706. * request queue.
  1707. */
  1708. spin_lock_irqsave(q->queue_lock, flags);
  1709. blk_stop_queue(q);
  1710. spin_unlock_irqrestore(q->queue_lock, flags);
  1711. return 0;
  1712. }
  1713. EXPORT_SYMBOL_GPL(scsi_internal_device_block);
  1714. /**
  1715. * scsi_internal_device_unblock - resume a device after a block request
  1716. * @sdev: device to resume
  1717. *
  1718. * Called by scsi lld's or the midlayer to restart the device queue
  1719. * for the previously suspended scsi device. Called from interrupt or
  1720. * normal process context.
  1721. *
  1722. * Returns zero if successful or error if not.
  1723. *
  1724. * Notes:
  1725. * This routine transitions the device to the SDEV_RUNNING state
  1726. * (which must be a legal transition) allowing the midlayer to
  1727. * goose the queue for this device. This routine assumes the
  1728. * host_lock is held upon entry.
  1729. **/
  1730. int
  1731. scsi_internal_device_unblock(struct scsi_device *sdev)
  1732. {
  1733. request_queue_t *q = sdev->request_queue;
  1734. int err;
  1735. unsigned long flags;
  1736. /*
  1737. * Try to transition the scsi device to SDEV_RUNNING
  1738. * and goose the device queue if successful.
  1739. */
  1740. err = scsi_device_set_state(sdev, SDEV_RUNNING);
  1741. if (err)
  1742. return err;
  1743. spin_lock_irqsave(q->queue_lock, flags);
  1744. blk_start_queue(q);
  1745. spin_unlock_irqrestore(q->queue_lock, flags);
  1746. return 0;
  1747. }
  1748. EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
  1749. static void
  1750. device_block(struct scsi_device *sdev, void *data)
  1751. {
  1752. scsi_internal_device_block(sdev);
  1753. }
  1754. static int
  1755. target_block(struct device *dev, void *data)
  1756. {
  1757. if (scsi_is_target_device(dev))
  1758. starget_for_each_device(to_scsi_target(dev), NULL,
  1759. device_block);
  1760. return 0;
  1761. }
  1762. void
  1763. scsi_target_block(struct device *dev)
  1764. {
  1765. if (scsi_is_target_device(dev))
  1766. starget_for_each_device(to_scsi_target(dev), NULL,
  1767. device_block);
  1768. else
  1769. device_for_each_child(dev, NULL, target_block);
  1770. }
  1771. EXPORT_SYMBOL_GPL(scsi_target_block);
  1772. static void
  1773. device_unblock(struct scsi_device *sdev, void *data)
  1774. {
  1775. scsi_internal_device_unblock(sdev);
  1776. }
  1777. static int
  1778. target_unblock(struct device *dev, void *data)
  1779. {
  1780. if (scsi_is_target_device(dev))
  1781. starget_for_each_device(to_scsi_target(dev), NULL,
  1782. device_unblock);
  1783. return 0;
  1784. }
  1785. void
  1786. scsi_target_unblock(struct device *dev)
  1787. {
  1788. if (scsi_is_target_device(dev))
  1789. starget_for_each_device(to_scsi_target(dev), NULL,
  1790. device_unblock);
  1791. else
  1792. device_for_each_child(dev, NULL, target_unblock);
  1793. }
  1794. EXPORT_SYMBOL_GPL(scsi_target_unblock);