scsi_lib.c 55 KB

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