scsi_lib.c 55 KB

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