scsi_lib.c 54 KB

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