scsi_lib.c 53 KB

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