scsi_lib.c 57 KB

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