scsi_lib.c 53 KB

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