scsi_lib.c 53 KB

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