scsi_lib.c 55 KB

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