scsi_lib.c 54 KB

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