scsi_lib.c 53 KB

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