scsi_lib.c 53 KB

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