scsi_lib.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249
  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 kmem_cache_t *scsi_io_context_cache;
  274. static void scsi_end_async(struct request *req, int uptodate)
  275. {
  276. struct scsi_io_context *sioc = req->end_io_data;
  277. if (sioc->done)
  278. sioc->done(sioc->data, sioc->sense, req->errors, req->data_len);
  279. kmem_cache_free(scsi_io_context_cache, sioc);
  280. __blk_put_request(req->q, req);
  281. }
  282. static int scsi_merge_bio(struct request *rq, struct bio *bio)
  283. {
  284. struct request_queue *q = rq->q;
  285. bio->bi_flags &= ~(1 << BIO_SEG_VALID);
  286. if (rq_data_dir(rq) == WRITE)
  287. bio->bi_rw |= (1 << BIO_RW);
  288. blk_queue_bounce(q, &bio);
  289. if (!rq->bio)
  290. blk_rq_bio_prep(q, rq, bio);
  291. else if (!q->back_merge_fn(q, rq, bio))
  292. return -EINVAL;
  293. else {
  294. rq->biotail->bi_next = bio;
  295. rq->biotail = bio;
  296. rq->hard_nr_sectors += bio_sectors(bio);
  297. rq->nr_sectors = rq->hard_nr_sectors;
  298. }
  299. return 0;
  300. }
  301. static int scsi_bi_endio(struct bio *bio, unsigned int bytes_done, int error)
  302. {
  303. if (bio->bi_size)
  304. return 1;
  305. bio_put(bio);
  306. return 0;
  307. }
  308. /**
  309. * scsi_req_map_sg - map a scatterlist into a request
  310. * @rq: request to fill
  311. * @sg: scatterlist
  312. * @nsegs: number of elements
  313. * @bufflen: len of buffer
  314. * @gfp: memory allocation flags
  315. *
  316. * scsi_req_map_sg maps a scatterlist into a request so that the
  317. * request can be sent to the block layer. We do not trust the scatterlist
  318. * sent to use, as some ULDs use that struct to only organize the pages.
  319. */
  320. static int scsi_req_map_sg(struct request *rq, struct scatterlist *sgl,
  321. int nsegs, unsigned bufflen, gfp_t gfp)
  322. {
  323. struct request_queue *q = rq->q;
  324. int nr_pages = (bufflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  325. unsigned int data_len = 0, len, bytes, off;
  326. struct page *page;
  327. struct bio *bio = NULL;
  328. int i, err, nr_vecs = 0;
  329. for (i = 0; i < nsegs; i++) {
  330. page = sgl[i].page;
  331. off = sgl[i].offset;
  332. len = sgl[i].length;
  333. data_len += len;
  334. while (len > 0) {
  335. bytes = min_t(unsigned int, len, PAGE_SIZE - off);
  336. if (!bio) {
  337. nr_vecs = min_t(int, BIO_MAX_PAGES, nr_pages);
  338. nr_pages -= nr_vecs;
  339. bio = bio_alloc(gfp, nr_vecs);
  340. if (!bio) {
  341. err = -ENOMEM;
  342. goto free_bios;
  343. }
  344. bio->bi_end_io = scsi_bi_endio;
  345. }
  346. if (bio_add_pc_page(q, bio, page, bytes, off) !=
  347. bytes) {
  348. bio_put(bio);
  349. err = -EINVAL;
  350. goto free_bios;
  351. }
  352. if (bio->bi_vcnt >= nr_vecs) {
  353. err = scsi_merge_bio(rq, bio);
  354. if (err) {
  355. bio_endio(bio, bio->bi_size, 0);
  356. goto free_bios;
  357. }
  358. bio = NULL;
  359. }
  360. page++;
  361. len -= bytes;
  362. off = 0;
  363. }
  364. }
  365. rq->buffer = rq->data = NULL;
  366. rq->data_len = data_len;
  367. return 0;
  368. free_bios:
  369. while ((bio = rq->bio) != NULL) {
  370. rq->bio = bio->bi_next;
  371. /*
  372. * call endio instead of bio_put incase it was bounced
  373. */
  374. bio_endio(bio, bio->bi_size, 0);
  375. }
  376. return err;
  377. }
  378. /**
  379. * scsi_execute_async - insert request
  380. * @sdev: scsi device
  381. * @cmd: scsi command
  382. * @data_direction: data direction
  383. * @buffer: data buffer (this can be a kernel buffer or scatterlist)
  384. * @bufflen: len of buffer
  385. * @use_sg: if buffer is a scatterlist this is the number of elements
  386. * @timeout: request timeout in seconds
  387. * @retries: number of times to retry request
  388. * @flags: or into request flags
  389. **/
  390. int scsi_execute_async(struct scsi_device *sdev, const unsigned char *cmd,
  391. int data_direction, void *buffer, unsigned bufflen,
  392. int use_sg, int timeout, int retries, void *privdata,
  393. void (*done)(void *, char *, int, int), gfp_t gfp)
  394. {
  395. struct request *req;
  396. struct scsi_io_context *sioc;
  397. int err = 0;
  398. int write = (data_direction == DMA_TO_DEVICE);
  399. sioc = kmem_cache_alloc(scsi_io_context_cache, gfp);
  400. if (!sioc)
  401. return DRIVER_ERROR << 24;
  402. memset(sioc, 0, sizeof(*sioc));
  403. req = blk_get_request(sdev->request_queue, write, gfp);
  404. if (!req)
  405. goto free_sense;
  406. req->flags |= REQ_BLOCK_PC | REQ_QUIET;
  407. if (use_sg)
  408. err = scsi_req_map_sg(req, buffer, use_sg, bufflen, gfp);
  409. else if (bufflen)
  410. err = blk_rq_map_kern(req->q, req, buffer, bufflen, gfp);
  411. if (err)
  412. goto free_req;
  413. req->cmd_len = COMMAND_SIZE(cmd[0]);
  414. memcpy(req->cmd, cmd, req->cmd_len);
  415. req->sense = sioc->sense;
  416. req->sense_len = 0;
  417. req->timeout = timeout;
  418. req->retries = retries;
  419. req->end_io_data = sioc;
  420. sioc->data = privdata;
  421. sioc->done = done;
  422. blk_execute_rq_nowait(req->q, NULL, req, 1, scsi_end_async);
  423. return 0;
  424. free_req:
  425. blk_put_request(req);
  426. free_sense:
  427. kfree(sioc);
  428. return DRIVER_ERROR << 24;
  429. }
  430. EXPORT_SYMBOL_GPL(scsi_execute_async);
  431. /*
  432. * Function: scsi_init_cmd_errh()
  433. *
  434. * Purpose: Initialize cmd fields related to error handling.
  435. *
  436. * Arguments: cmd - command that is ready to be queued.
  437. *
  438. * Returns: Nothing
  439. *
  440. * Notes: This function has the job of initializing a number of
  441. * fields related to error handling. Typically this will
  442. * be called once for each command, as required.
  443. */
  444. static int scsi_init_cmd_errh(struct scsi_cmnd *cmd)
  445. {
  446. cmd->serial_number = 0;
  447. memset(cmd->sense_buffer, 0, sizeof cmd->sense_buffer);
  448. if (cmd->cmd_len == 0)
  449. cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]);
  450. /*
  451. * We need saved copies of a number of fields - this is because
  452. * error handling may need to overwrite these with different values
  453. * to run different commands, and once error handling is complete,
  454. * we will need to restore these values prior to running the actual
  455. * command.
  456. */
  457. cmd->old_use_sg = cmd->use_sg;
  458. cmd->old_cmd_len = cmd->cmd_len;
  459. cmd->sc_old_data_direction = cmd->sc_data_direction;
  460. cmd->old_underflow = cmd->underflow;
  461. memcpy(cmd->data_cmnd, cmd->cmnd, sizeof(cmd->cmnd));
  462. cmd->buffer = cmd->request_buffer;
  463. cmd->bufflen = cmd->request_bufflen;
  464. return 1;
  465. }
  466. /*
  467. * Function: scsi_setup_cmd_retry()
  468. *
  469. * Purpose: Restore the command state for a retry
  470. *
  471. * Arguments: cmd - command to be restored
  472. *
  473. * Returns: Nothing
  474. *
  475. * Notes: Immediately prior to retrying a command, we need
  476. * to restore certain fields that we saved above.
  477. */
  478. void scsi_setup_cmd_retry(struct scsi_cmnd *cmd)
  479. {
  480. memcpy(cmd->cmnd, cmd->data_cmnd, sizeof(cmd->data_cmnd));
  481. cmd->request_buffer = cmd->buffer;
  482. cmd->request_bufflen = cmd->bufflen;
  483. cmd->use_sg = cmd->old_use_sg;
  484. cmd->cmd_len = cmd->old_cmd_len;
  485. cmd->sc_data_direction = cmd->sc_old_data_direction;
  486. cmd->underflow = cmd->old_underflow;
  487. }
  488. void scsi_device_unbusy(struct scsi_device *sdev)
  489. {
  490. struct Scsi_Host *shost = sdev->host;
  491. unsigned long flags;
  492. spin_lock_irqsave(shost->host_lock, flags);
  493. shost->host_busy--;
  494. if (unlikely(scsi_host_in_recovery(shost) &&
  495. shost->host_failed))
  496. scsi_eh_wakeup(shost);
  497. spin_unlock(shost->host_lock);
  498. spin_lock(sdev->request_queue->queue_lock);
  499. sdev->device_busy--;
  500. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  501. }
  502. /*
  503. * Called for single_lun devices on IO completion. Clear starget_sdev_user,
  504. * and call blk_run_queue for all the scsi_devices on the target -
  505. * including current_sdev first.
  506. *
  507. * Called with *no* scsi locks held.
  508. */
  509. static void scsi_single_lun_run(struct scsi_device *current_sdev)
  510. {
  511. struct Scsi_Host *shost = current_sdev->host;
  512. struct scsi_device *sdev, *tmp;
  513. struct scsi_target *starget = scsi_target(current_sdev);
  514. unsigned long flags;
  515. spin_lock_irqsave(shost->host_lock, flags);
  516. starget->starget_sdev_user = NULL;
  517. spin_unlock_irqrestore(shost->host_lock, flags);
  518. /*
  519. * Call blk_run_queue for all LUNs on the target, starting with
  520. * current_sdev. We race with others (to set starget_sdev_user),
  521. * but in most cases, we will be first. Ideally, each LU on the
  522. * target would get some limited time or requests on the target.
  523. */
  524. blk_run_queue(current_sdev->request_queue);
  525. spin_lock_irqsave(shost->host_lock, flags);
  526. if (starget->starget_sdev_user)
  527. goto out;
  528. list_for_each_entry_safe(sdev, tmp, &starget->devices,
  529. same_target_siblings) {
  530. if (sdev == current_sdev)
  531. continue;
  532. if (scsi_device_get(sdev))
  533. continue;
  534. spin_unlock_irqrestore(shost->host_lock, flags);
  535. blk_run_queue(sdev->request_queue);
  536. spin_lock_irqsave(shost->host_lock, flags);
  537. scsi_device_put(sdev);
  538. }
  539. out:
  540. spin_unlock_irqrestore(shost->host_lock, flags);
  541. }
  542. /*
  543. * Function: scsi_run_queue()
  544. *
  545. * Purpose: Select a proper request queue to serve next
  546. *
  547. * Arguments: q - last request's queue
  548. *
  549. * Returns: Nothing
  550. *
  551. * Notes: The previous command was completely finished, start
  552. * a new one if possible.
  553. */
  554. static void scsi_run_queue(struct request_queue *q)
  555. {
  556. struct scsi_device *sdev = q->queuedata;
  557. struct Scsi_Host *shost = sdev->host;
  558. unsigned long flags;
  559. if (sdev->single_lun)
  560. scsi_single_lun_run(sdev);
  561. spin_lock_irqsave(shost->host_lock, flags);
  562. while (!list_empty(&shost->starved_list) &&
  563. !shost->host_blocked && !shost->host_self_blocked &&
  564. !((shost->can_queue > 0) &&
  565. (shost->host_busy >= shost->can_queue))) {
  566. /*
  567. * As long as shost is accepting commands and we have
  568. * starved queues, call blk_run_queue. scsi_request_fn
  569. * drops the queue_lock and can add us back to the
  570. * starved_list.
  571. *
  572. * host_lock protects the starved_list and starved_entry.
  573. * scsi_request_fn must get the host_lock before checking
  574. * or modifying starved_list or starved_entry.
  575. */
  576. sdev = list_entry(shost->starved_list.next,
  577. struct scsi_device, starved_entry);
  578. list_del_init(&sdev->starved_entry);
  579. spin_unlock_irqrestore(shost->host_lock, flags);
  580. blk_run_queue(sdev->request_queue);
  581. spin_lock_irqsave(shost->host_lock, flags);
  582. if (unlikely(!list_empty(&sdev->starved_entry)))
  583. /*
  584. * sdev lost a race, and was put back on the
  585. * starved list. This is unlikely but without this
  586. * in theory we could loop forever.
  587. */
  588. break;
  589. }
  590. spin_unlock_irqrestore(shost->host_lock, flags);
  591. blk_run_queue(q);
  592. }
  593. /*
  594. * Function: scsi_requeue_command()
  595. *
  596. * Purpose: Handle post-processing of completed commands.
  597. *
  598. * Arguments: q - queue to operate on
  599. * cmd - command that may need to be requeued.
  600. *
  601. * Returns: Nothing
  602. *
  603. * Notes: After command completion, there may be blocks left
  604. * over which weren't finished by the previous command
  605. * this can be for a number of reasons - the main one is
  606. * I/O errors in the middle of the request, in which case
  607. * we need to request the blocks that come after the bad
  608. * sector.
  609. * Notes: Upon return, cmd is a stale pointer.
  610. */
  611. static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
  612. {
  613. struct request *req = cmd->request;
  614. unsigned long flags;
  615. scsi_unprep_request(req);
  616. spin_lock_irqsave(q->queue_lock, flags);
  617. blk_requeue_request(q, req);
  618. spin_unlock_irqrestore(q->queue_lock, flags);
  619. scsi_run_queue(q);
  620. }
  621. void scsi_next_command(struct scsi_cmnd *cmd)
  622. {
  623. struct scsi_device *sdev = cmd->device;
  624. struct request_queue *q = sdev->request_queue;
  625. /* need to hold a reference on the device before we let go of the cmd */
  626. get_device(&sdev->sdev_gendev);
  627. scsi_put_command(cmd);
  628. scsi_run_queue(q);
  629. /* ok to remove device now */
  630. put_device(&sdev->sdev_gendev);
  631. }
  632. void scsi_run_host_queues(struct Scsi_Host *shost)
  633. {
  634. struct scsi_device *sdev;
  635. shost_for_each_device(sdev, shost)
  636. scsi_run_queue(sdev->request_queue);
  637. }
  638. /*
  639. * Function: scsi_end_request()
  640. *
  641. * Purpose: Post-processing of completed commands (usually invoked at end
  642. * of upper level post-processing and scsi_io_completion).
  643. *
  644. * Arguments: cmd - command that is complete.
  645. * uptodate - 1 if I/O indicates success, <= 0 for I/O error.
  646. * bytes - number of bytes of completed I/O
  647. * requeue - indicates whether we should requeue leftovers.
  648. *
  649. * Lock status: Assumed that lock is not held upon entry.
  650. *
  651. * Returns: cmd if requeue required, NULL otherwise.
  652. *
  653. * Notes: This is called for block device requests in order to
  654. * mark some number of sectors as complete.
  655. *
  656. * We are guaranteeing that the request queue will be goosed
  657. * at some point during this call.
  658. * Notes: If cmd was requeued, upon return it will be a stale pointer.
  659. */
  660. static struct scsi_cmnd *scsi_end_request(struct scsi_cmnd *cmd, int uptodate,
  661. int bytes, int requeue)
  662. {
  663. request_queue_t *q = cmd->device->request_queue;
  664. struct request *req = cmd->request;
  665. unsigned long flags;
  666. /*
  667. * If there are blocks left over at the end, set up the command
  668. * to queue the remainder of them.
  669. */
  670. if (end_that_request_chunk(req, uptodate, bytes)) {
  671. int leftover = (req->hard_nr_sectors << 9);
  672. if (blk_pc_request(req))
  673. leftover = req->data_len;
  674. /* kill remainder if no retrys */
  675. if (!uptodate && blk_noretry_request(req))
  676. end_that_request_chunk(req, 0, leftover);
  677. else {
  678. if (requeue) {
  679. /*
  680. * Bleah. Leftovers again. Stick the
  681. * leftovers in the front of the
  682. * queue, and goose the queue again.
  683. */
  684. scsi_requeue_command(q, cmd);
  685. cmd = NULL;
  686. }
  687. return cmd;
  688. }
  689. }
  690. add_disk_randomness(req->rq_disk);
  691. spin_lock_irqsave(q->queue_lock, flags);
  692. if (blk_rq_tagged(req))
  693. blk_queue_end_tag(q, req);
  694. end_that_request_last(req, uptodate);
  695. spin_unlock_irqrestore(q->queue_lock, flags);
  696. /*
  697. * This will goose the queue request function at the end, so we don't
  698. * need to worry about launching another command.
  699. */
  700. scsi_next_command(cmd);
  701. return NULL;
  702. }
  703. static struct scatterlist *scsi_alloc_sgtable(struct scsi_cmnd *cmd, gfp_t gfp_mask)
  704. {
  705. struct scsi_host_sg_pool *sgp;
  706. struct scatterlist *sgl;
  707. BUG_ON(!cmd->use_sg);
  708. switch (cmd->use_sg) {
  709. case 1 ... 8:
  710. cmd->sglist_len = 0;
  711. break;
  712. case 9 ... 16:
  713. cmd->sglist_len = 1;
  714. break;
  715. case 17 ... 32:
  716. cmd->sglist_len = 2;
  717. break;
  718. #if (SCSI_MAX_PHYS_SEGMENTS > 32)
  719. case 33 ... 64:
  720. cmd->sglist_len = 3;
  721. break;
  722. #if (SCSI_MAX_PHYS_SEGMENTS > 64)
  723. case 65 ... 128:
  724. cmd->sglist_len = 4;
  725. break;
  726. #if (SCSI_MAX_PHYS_SEGMENTS > 128)
  727. case 129 ... 256:
  728. cmd->sglist_len = 5;
  729. break;
  730. #endif
  731. #endif
  732. #endif
  733. default:
  734. return NULL;
  735. }
  736. sgp = scsi_sg_pools + cmd->sglist_len;
  737. sgl = mempool_alloc(sgp->pool, gfp_mask);
  738. return sgl;
  739. }
  740. static void scsi_free_sgtable(struct scatterlist *sgl, int index)
  741. {
  742. struct scsi_host_sg_pool *sgp;
  743. BUG_ON(index >= SG_MEMPOOL_NR);
  744. sgp = scsi_sg_pools + index;
  745. mempool_free(sgl, sgp->pool);
  746. }
  747. /*
  748. * Function: scsi_release_buffers()
  749. *
  750. * Purpose: Completion processing for block device I/O requests.
  751. *
  752. * Arguments: cmd - command that we are bailing.
  753. *
  754. * Lock status: Assumed that no lock is held upon entry.
  755. *
  756. * Returns: Nothing
  757. *
  758. * Notes: In the event that an upper level driver rejects a
  759. * command, we must release resources allocated during
  760. * the __init_io() function. Primarily this would involve
  761. * the scatter-gather table, and potentially any bounce
  762. * buffers.
  763. */
  764. static void scsi_release_buffers(struct scsi_cmnd *cmd)
  765. {
  766. struct request *req = cmd->request;
  767. /*
  768. * Free up any indirection buffers we allocated for DMA purposes.
  769. */
  770. if (cmd->use_sg)
  771. scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
  772. else if (cmd->request_buffer != req->buffer)
  773. kfree(cmd->request_buffer);
  774. /*
  775. * Zero these out. They now point to freed memory, and it is
  776. * dangerous to hang onto the pointers.
  777. */
  778. cmd->buffer = NULL;
  779. cmd->bufflen = 0;
  780. cmd->request_buffer = NULL;
  781. cmd->request_bufflen = 0;
  782. }
  783. /*
  784. * Function: scsi_io_completion()
  785. *
  786. * Purpose: Completion processing for block device I/O requests.
  787. *
  788. * Arguments: cmd - command that is finished.
  789. *
  790. * Lock status: Assumed that no lock is held upon entry.
  791. *
  792. * Returns: Nothing
  793. *
  794. * Notes: This function is matched in terms of capabilities to
  795. * the function that created the scatter-gather list.
  796. * In other words, if there are no bounce buffers
  797. * (the normal case for most drivers), we don't need
  798. * the logic to deal with cleaning up afterwards.
  799. *
  800. * We must do one of several things here:
  801. *
  802. * a) Call scsi_end_request. This will finish off the
  803. * specified number of sectors. If we are done, the
  804. * command block will be released, and the queue
  805. * function will be goosed. If we are not done, then
  806. * scsi_end_request will directly goose the queue.
  807. *
  808. * b) We can just use scsi_requeue_command() here. This would
  809. * be used if we just wanted to retry, for example.
  810. */
  811. void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes,
  812. unsigned int block_bytes)
  813. {
  814. int result = cmd->result;
  815. int this_count = cmd->bufflen;
  816. request_queue_t *q = cmd->device->request_queue;
  817. struct request *req = cmd->request;
  818. int clear_errors = 1;
  819. struct scsi_sense_hdr sshdr;
  820. int sense_valid = 0;
  821. int sense_deferred = 0;
  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_issue_flush_fn(request_queue_t *q, struct gendisk *disk,
  1070. sector_t *error_sector)
  1071. {
  1072. struct scsi_device *sdev = q->queuedata;
  1073. struct scsi_driver *drv;
  1074. if (sdev->sdev_state != SDEV_RUNNING)
  1075. return -ENXIO;
  1076. drv = *(struct scsi_driver **) disk->private_data;
  1077. if (drv->issue_flush)
  1078. return drv->issue_flush(&sdev->sdev_gendev, error_sector);
  1079. return -EOPNOTSUPP;
  1080. }
  1081. static void scsi_blk_pc_done(struct scsi_cmnd *cmd)
  1082. {
  1083. BUG_ON(!blk_pc_request(cmd->request));
  1084. /*
  1085. * This will complete the whole command with uptodate=1 so
  1086. * as far as the block layer is concerned the command completed
  1087. * successfully. Since this is a REQ_BLOCK_PC command the
  1088. * caller should check the request's errors value
  1089. */
  1090. scsi_io_completion(cmd, cmd->bufflen, 0);
  1091. }
  1092. static void scsi_setup_blk_pc_cmnd(struct scsi_cmnd *cmd)
  1093. {
  1094. struct request *req = cmd->request;
  1095. BUG_ON(sizeof(req->cmd) > sizeof(cmd->cmnd));
  1096. memcpy(cmd->cmnd, req->cmd, sizeof(cmd->cmnd));
  1097. cmd->cmd_len = req->cmd_len;
  1098. if (!req->data_len)
  1099. cmd->sc_data_direction = DMA_NONE;
  1100. else if (rq_data_dir(req) == WRITE)
  1101. cmd->sc_data_direction = DMA_TO_DEVICE;
  1102. else
  1103. cmd->sc_data_direction = DMA_FROM_DEVICE;
  1104. cmd->transfersize = req->data_len;
  1105. cmd->allowed = req->retries;
  1106. cmd->timeout_per_command = req->timeout;
  1107. cmd->done = scsi_blk_pc_done;
  1108. }
  1109. static int scsi_prep_fn(struct request_queue *q, struct request *req)
  1110. {
  1111. struct scsi_device *sdev = q->queuedata;
  1112. struct scsi_cmnd *cmd;
  1113. int specials_only = 0;
  1114. /*
  1115. * Just check to see if the device is online. If it isn't, we
  1116. * refuse to process any commands. The device must be brought
  1117. * online before trying any recovery commands
  1118. */
  1119. if (unlikely(!scsi_device_online(sdev))) {
  1120. sdev_printk(KERN_ERR, sdev,
  1121. "rejecting I/O to offline device\n");
  1122. goto kill;
  1123. }
  1124. if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
  1125. /* OK, we're not in a running state don't prep
  1126. * user commands */
  1127. if (sdev->sdev_state == SDEV_DEL) {
  1128. /* Device is fully deleted, no commands
  1129. * at all allowed down */
  1130. sdev_printk(KERN_ERR, sdev,
  1131. "rejecting I/O to dead device\n");
  1132. goto kill;
  1133. }
  1134. /* OK, we only allow special commands (i.e. not
  1135. * user initiated ones */
  1136. specials_only = sdev->sdev_state;
  1137. }
  1138. /*
  1139. * Find the actual device driver associated with this command.
  1140. * The SPECIAL requests are things like character device or
  1141. * ioctls, which did not originate from ll_rw_blk. Note that
  1142. * the special field is also used to indicate the cmd for
  1143. * the remainder of a partially fulfilled request that can
  1144. * come up when there is a medium error. We have to treat
  1145. * these two cases differently. We differentiate by looking
  1146. * at request->cmd, as this tells us the real story.
  1147. */
  1148. if (req->flags & REQ_SPECIAL && req->special) {
  1149. struct scsi_request *sreq = req->special;
  1150. if (sreq->sr_magic == SCSI_REQ_MAGIC) {
  1151. cmd = scsi_get_command(sreq->sr_device, GFP_ATOMIC);
  1152. if (unlikely(!cmd))
  1153. goto defer;
  1154. scsi_init_cmd_from_req(cmd, sreq);
  1155. } else
  1156. cmd = req->special;
  1157. } else if (req->flags & (REQ_CMD | REQ_BLOCK_PC)) {
  1158. if(unlikely(specials_only) && !(req->flags & REQ_SPECIAL)) {
  1159. if(specials_only == SDEV_QUIESCE ||
  1160. specials_only == SDEV_BLOCK)
  1161. goto defer;
  1162. sdev_printk(KERN_ERR, sdev,
  1163. "rejecting I/O to device being removed\n");
  1164. goto kill;
  1165. }
  1166. /*
  1167. * Now try and find a command block that we can use.
  1168. */
  1169. if (!req->special) {
  1170. cmd = scsi_get_command(sdev, GFP_ATOMIC);
  1171. if (unlikely(!cmd))
  1172. goto defer;
  1173. } else
  1174. cmd = req->special;
  1175. /* pull a tag out of the request if we have one */
  1176. cmd->tag = req->tag;
  1177. } else {
  1178. blk_dump_rq_flags(req, "SCSI bad req");
  1179. goto kill;
  1180. }
  1181. /* note the overloading of req->special. When the tag
  1182. * is active it always means cmd. If the tag goes
  1183. * back for re-queueing, it may be reset */
  1184. req->special = cmd;
  1185. cmd->request = req;
  1186. /*
  1187. * FIXME: drop the lock here because the functions below
  1188. * expect to be called without the queue lock held. Also,
  1189. * previously, we dequeued the request before dropping the
  1190. * lock. We hope REQ_STARTED prevents anything untoward from
  1191. * happening now.
  1192. */
  1193. if (req->flags & (REQ_CMD | REQ_BLOCK_PC)) {
  1194. int ret;
  1195. /*
  1196. * This will do a couple of things:
  1197. * 1) Fill in the actual SCSI command.
  1198. * 2) Fill in any other upper-level specific fields
  1199. * (timeout).
  1200. *
  1201. * If this returns 0, it means that the request failed
  1202. * (reading past end of disk, reading offline device,
  1203. * etc). This won't actually talk to the device, but
  1204. * some kinds of consistency checking may cause the
  1205. * request to be rejected immediately.
  1206. */
  1207. /*
  1208. * This sets up the scatter-gather table (allocating if
  1209. * required).
  1210. */
  1211. ret = scsi_init_io(cmd);
  1212. switch(ret) {
  1213. /* For BLKPREP_KILL/DEFER the cmd was released */
  1214. case BLKPREP_KILL:
  1215. goto kill;
  1216. case BLKPREP_DEFER:
  1217. goto defer;
  1218. }
  1219. /*
  1220. * Initialize the actual SCSI command for this request.
  1221. */
  1222. if (req->flags & REQ_BLOCK_PC) {
  1223. scsi_setup_blk_pc_cmnd(cmd);
  1224. } else if (req->rq_disk) {
  1225. struct scsi_driver *drv;
  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. }
  1233. }
  1234. /*
  1235. * The request is now prepped, no need to come back here
  1236. */
  1237. req->flags |= REQ_DONTPREP;
  1238. return BLKPREP_OK;
  1239. defer:
  1240. /* If we defer, the elv_next_request() returns NULL, but the
  1241. * queue must be restarted, so we plug here if no returning
  1242. * command will automatically do that. */
  1243. if (sdev->device_busy == 0)
  1244. blk_plug_device(q);
  1245. return BLKPREP_DEFER;
  1246. kill:
  1247. req->errors = DID_NO_CONNECT << 16;
  1248. return BLKPREP_KILL;
  1249. }
  1250. /*
  1251. * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
  1252. * return 0.
  1253. *
  1254. * Called with the queue_lock held.
  1255. */
  1256. static inline int scsi_dev_queue_ready(struct request_queue *q,
  1257. struct scsi_device *sdev)
  1258. {
  1259. if (sdev->device_busy >= sdev->queue_depth)
  1260. return 0;
  1261. if (sdev->device_busy == 0 && sdev->device_blocked) {
  1262. /*
  1263. * unblock after device_blocked iterates to zero
  1264. */
  1265. if (--sdev->device_blocked == 0) {
  1266. SCSI_LOG_MLQUEUE(3,
  1267. sdev_printk(KERN_INFO, sdev,
  1268. "unblocking device at zero depth\n"));
  1269. } else {
  1270. blk_plug_device(q);
  1271. return 0;
  1272. }
  1273. }
  1274. if (sdev->device_blocked)
  1275. return 0;
  1276. return 1;
  1277. }
  1278. /*
  1279. * scsi_host_queue_ready: if we can send requests to shost, return 1 else
  1280. * return 0. We must end up running the queue again whenever 0 is
  1281. * returned, else IO can hang.
  1282. *
  1283. * Called with host_lock held.
  1284. */
  1285. static inline int scsi_host_queue_ready(struct request_queue *q,
  1286. struct Scsi_Host *shost,
  1287. struct scsi_device *sdev)
  1288. {
  1289. if (scsi_host_in_recovery(shost))
  1290. return 0;
  1291. if (shost->host_busy == 0 && shost->host_blocked) {
  1292. /*
  1293. * unblock after host_blocked iterates to zero
  1294. */
  1295. if (--shost->host_blocked == 0) {
  1296. SCSI_LOG_MLQUEUE(3,
  1297. printk("scsi%d unblocking host at zero depth\n",
  1298. shost->host_no));
  1299. } else {
  1300. blk_plug_device(q);
  1301. return 0;
  1302. }
  1303. }
  1304. if ((shost->can_queue > 0 && shost->host_busy >= shost->can_queue) ||
  1305. shost->host_blocked || shost->host_self_blocked) {
  1306. if (list_empty(&sdev->starved_entry))
  1307. list_add_tail(&sdev->starved_entry, &shost->starved_list);
  1308. return 0;
  1309. }
  1310. /* We're OK to process the command, so we can't be starved */
  1311. if (!list_empty(&sdev->starved_entry))
  1312. list_del_init(&sdev->starved_entry);
  1313. return 1;
  1314. }
  1315. /*
  1316. * Kill a request for a dead device
  1317. */
  1318. static void scsi_kill_request(struct request *req, request_queue_t *q)
  1319. {
  1320. struct scsi_cmnd *cmd = req->special;
  1321. blkdev_dequeue_request(req);
  1322. if (unlikely(cmd == NULL)) {
  1323. printk(KERN_CRIT "impossible request in %s.\n",
  1324. __FUNCTION__);
  1325. BUG();
  1326. }
  1327. scsi_init_cmd_errh(cmd);
  1328. cmd->result = DID_NO_CONNECT << 16;
  1329. atomic_inc(&cmd->device->iorequest_cnt);
  1330. __scsi_done(cmd);
  1331. }
  1332. static void scsi_softirq_done(struct request *rq)
  1333. {
  1334. struct scsi_cmnd *cmd = rq->completion_data;
  1335. unsigned long wait_for = cmd->allowed * cmd->timeout_per_command;
  1336. int disposition;
  1337. INIT_LIST_HEAD(&cmd->eh_entry);
  1338. disposition = scsi_decide_disposition(cmd);
  1339. if (disposition != SUCCESS &&
  1340. time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
  1341. sdev_printk(KERN_ERR, cmd->device,
  1342. "timing out command, waited %lus\n",
  1343. wait_for/HZ);
  1344. disposition = SUCCESS;
  1345. }
  1346. scsi_log_completion(cmd, disposition);
  1347. switch (disposition) {
  1348. case SUCCESS:
  1349. scsi_finish_command(cmd);
  1350. break;
  1351. case NEEDS_RETRY:
  1352. scsi_retry_command(cmd);
  1353. break;
  1354. case ADD_TO_MLQUEUE:
  1355. scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
  1356. break;
  1357. default:
  1358. if (!scsi_eh_scmd_add(cmd, 0))
  1359. scsi_finish_command(cmd);
  1360. }
  1361. }
  1362. /*
  1363. * Function: scsi_request_fn()
  1364. *
  1365. * Purpose: Main strategy routine for SCSI.
  1366. *
  1367. * Arguments: q - Pointer to actual queue.
  1368. *
  1369. * Returns: Nothing
  1370. *
  1371. * Lock status: IO request lock assumed to be held when called.
  1372. */
  1373. static void scsi_request_fn(struct request_queue *q)
  1374. {
  1375. struct scsi_device *sdev = q->queuedata;
  1376. struct Scsi_Host *shost;
  1377. struct scsi_cmnd *cmd;
  1378. struct request *req;
  1379. if (!sdev) {
  1380. printk("scsi: killing requests for dead queue\n");
  1381. while ((req = elv_next_request(q)) != NULL)
  1382. scsi_kill_request(req, q);
  1383. return;
  1384. }
  1385. if(!get_device(&sdev->sdev_gendev))
  1386. /* We must be tearing the block queue down already */
  1387. return;
  1388. /*
  1389. * To start with, we keep looping until the queue is empty, or until
  1390. * the host is no longer able to accept any more requests.
  1391. */
  1392. shost = sdev->host;
  1393. while (!blk_queue_plugged(q)) {
  1394. int rtn;
  1395. /*
  1396. * get next queueable request. We do this early to make sure
  1397. * that the request is fully prepared even if we cannot
  1398. * accept it.
  1399. */
  1400. req = elv_next_request(q);
  1401. if (!req || !scsi_dev_queue_ready(q, sdev))
  1402. break;
  1403. if (unlikely(!scsi_device_online(sdev))) {
  1404. sdev_printk(KERN_ERR, sdev,
  1405. "rejecting I/O to offline device\n");
  1406. scsi_kill_request(req, q);
  1407. continue;
  1408. }
  1409. /*
  1410. * Remove the request from the request list.
  1411. */
  1412. if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
  1413. blkdev_dequeue_request(req);
  1414. sdev->device_busy++;
  1415. spin_unlock(q->queue_lock);
  1416. cmd = req->special;
  1417. if (unlikely(cmd == NULL)) {
  1418. printk(KERN_CRIT "impossible request in %s.\n"
  1419. "please mail a stack trace to "
  1420. "linux-scsi@vger.kernel.org",
  1421. __FUNCTION__);
  1422. BUG();
  1423. }
  1424. spin_lock(shost->host_lock);
  1425. if (!scsi_host_queue_ready(q, shost, sdev))
  1426. goto not_ready;
  1427. if (sdev->single_lun) {
  1428. if (scsi_target(sdev)->starget_sdev_user &&
  1429. scsi_target(sdev)->starget_sdev_user != sdev)
  1430. goto not_ready;
  1431. scsi_target(sdev)->starget_sdev_user = sdev;
  1432. }
  1433. shost->host_busy++;
  1434. /*
  1435. * XXX(hch): This is rather suboptimal, scsi_dispatch_cmd will
  1436. * take the lock again.
  1437. */
  1438. spin_unlock_irq(shost->host_lock);
  1439. /*
  1440. * Finally, initialize any error handling parameters, and set up
  1441. * the timers for timeouts.
  1442. */
  1443. scsi_init_cmd_errh(cmd);
  1444. /*
  1445. * Dispatch the command to the low-level driver.
  1446. */
  1447. rtn = scsi_dispatch_cmd(cmd);
  1448. spin_lock_irq(q->queue_lock);
  1449. if(rtn) {
  1450. /* we're refusing the command; because of
  1451. * the way locks get dropped, we need to
  1452. * check here if plugging is required */
  1453. if(sdev->device_busy == 0)
  1454. blk_plug_device(q);
  1455. break;
  1456. }
  1457. }
  1458. goto out;
  1459. not_ready:
  1460. spin_unlock_irq(shost->host_lock);
  1461. /*
  1462. * lock q, handle tag, requeue req, and decrement device_busy. We
  1463. * must return with queue_lock held.
  1464. *
  1465. * Decrementing device_busy without checking it is OK, as all such
  1466. * cases (host limits or settings) should run the queue at some
  1467. * later time.
  1468. */
  1469. spin_lock_irq(q->queue_lock);
  1470. blk_requeue_request(q, req);
  1471. sdev->device_busy--;
  1472. if(sdev->device_busy == 0)
  1473. blk_plug_device(q);
  1474. out:
  1475. /* must be careful here...if we trigger the ->remove() function
  1476. * we cannot be holding the q lock */
  1477. spin_unlock_irq(q->queue_lock);
  1478. put_device(&sdev->sdev_gendev);
  1479. spin_lock_irq(q->queue_lock);
  1480. }
  1481. u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
  1482. {
  1483. struct device *host_dev;
  1484. u64 bounce_limit = 0xffffffff;
  1485. if (shost->unchecked_isa_dma)
  1486. return BLK_BOUNCE_ISA;
  1487. /*
  1488. * Platforms with virtual-DMA translation
  1489. * hardware have no practical limit.
  1490. */
  1491. if (!PCI_DMA_BUS_IS_PHYS)
  1492. return BLK_BOUNCE_ANY;
  1493. host_dev = scsi_get_device(shost);
  1494. if (host_dev && host_dev->dma_mask)
  1495. bounce_limit = *host_dev->dma_mask;
  1496. return bounce_limit;
  1497. }
  1498. EXPORT_SYMBOL(scsi_calculate_bounce_limit);
  1499. struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
  1500. {
  1501. struct Scsi_Host *shost = sdev->host;
  1502. struct request_queue *q;
  1503. q = blk_init_queue(scsi_request_fn, NULL);
  1504. if (!q)
  1505. return NULL;
  1506. blk_queue_prep_rq(q, scsi_prep_fn);
  1507. blk_queue_max_hw_segments(q, shost->sg_tablesize);
  1508. blk_queue_max_phys_segments(q, SCSI_MAX_PHYS_SEGMENTS);
  1509. blk_queue_max_sectors(q, shost->max_sectors);
  1510. blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
  1511. blk_queue_segment_boundary(q, shost->dma_boundary);
  1512. blk_queue_issue_flush_fn(q, scsi_issue_flush_fn);
  1513. blk_queue_softirq_done(q, scsi_softirq_done);
  1514. if (!shost->use_clustering)
  1515. clear_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
  1516. return q;
  1517. }
  1518. void scsi_free_queue(struct request_queue *q)
  1519. {
  1520. blk_cleanup_queue(q);
  1521. }
  1522. /*
  1523. * Function: scsi_block_requests()
  1524. *
  1525. * Purpose: Utility function used by low-level drivers to prevent further
  1526. * commands from being queued to the device.
  1527. *
  1528. * Arguments: shost - Host in question
  1529. *
  1530. * Returns: Nothing
  1531. *
  1532. * Lock status: No locks are assumed held.
  1533. *
  1534. * Notes: There is no timer nor any other means by which the requests
  1535. * get unblocked other than the low-level driver calling
  1536. * scsi_unblock_requests().
  1537. */
  1538. void scsi_block_requests(struct Scsi_Host *shost)
  1539. {
  1540. shost->host_self_blocked = 1;
  1541. }
  1542. EXPORT_SYMBOL(scsi_block_requests);
  1543. /*
  1544. * Function: scsi_unblock_requests()
  1545. *
  1546. * Purpose: Utility function used by low-level drivers to allow further
  1547. * commands from being queued to the device.
  1548. *
  1549. * Arguments: shost - Host in question
  1550. *
  1551. * Returns: Nothing
  1552. *
  1553. * Lock status: No locks are assumed held.
  1554. *
  1555. * Notes: There is no timer nor any other means by which the requests
  1556. * get unblocked other than the low-level driver calling
  1557. * scsi_unblock_requests().
  1558. *
  1559. * This is done as an API function so that changes to the
  1560. * internals of the scsi mid-layer won't require wholesale
  1561. * changes to drivers that use this feature.
  1562. */
  1563. void scsi_unblock_requests(struct Scsi_Host *shost)
  1564. {
  1565. shost->host_self_blocked = 0;
  1566. scsi_run_host_queues(shost);
  1567. }
  1568. EXPORT_SYMBOL(scsi_unblock_requests);
  1569. int __init scsi_init_queue(void)
  1570. {
  1571. int i;
  1572. scsi_io_context_cache = kmem_cache_create("scsi_io_context",
  1573. sizeof(struct scsi_io_context),
  1574. 0, 0, NULL, NULL);
  1575. if (!scsi_io_context_cache) {
  1576. printk(KERN_ERR "SCSI: can't init scsi io context cache\n");
  1577. return -ENOMEM;
  1578. }
  1579. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1580. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1581. int size = sgp->size * sizeof(struct scatterlist);
  1582. sgp->slab = kmem_cache_create(sgp->name, size, 0,
  1583. SLAB_HWCACHE_ALIGN, NULL, NULL);
  1584. if (!sgp->slab) {
  1585. printk(KERN_ERR "SCSI: can't init sg slab %s\n",
  1586. sgp->name);
  1587. }
  1588. sgp->pool = mempool_create(SG_MEMPOOL_SIZE,
  1589. mempool_alloc_slab, mempool_free_slab,
  1590. sgp->slab);
  1591. if (!sgp->pool) {
  1592. printk(KERN_ERR "SCSI: can't init sg mempool %s\n",
  1593. sgp->name);
  1594. }
  1595. }
  1596. return 0;
  1597. }
  1598. void scsi_exit_queue(void)
  1599. {
  1600. int i;
  1601. kmem_cache_destroy(scsi_io_context_cache);
  1602. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1603. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1604. mempool_destroy(sgp->pool);
  1605. kmem_cache_destroy(sgp->slab);
  1606. }
  1607. }
  1608. /**
  1609. * scsi_mode_sense - issue a mode sense, falling back from 10 to
  1610. * six bytes if necessary.
  1611. * @sdev: SCSI device to be queried
  1612. * @dbd: set if mode sense will allow block descriptors to be returned
  1613. * @modepage: mode page being requested
  1614. * @buffer: request buffer (may not be smaller than eight bytes)
  1615. * @len: length of request buffer.
  1616. * @timeout: command timeout
  1617. * @retries: number of retries before failing
  1618. * @data: returns a structure abstracting the mode header data
  1619. * @sense: place to put sense data (or NULL if no sense to be collected).
  1620. * must be SCSI_SENSE_BUFFERSIZE big.
  1621. *
  1622. * Returns zero if unsuccessful, or the header offset (either 4
  1623. * or 8 depending on whether a six or ten byte command was
  1624. * issued) if successful.
  1625. **/
  1626. int
  1627. scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
  1628. unsigned char *buffer, int len, int timeout, int retries,
  1629. struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr) {
  1630. unsigned char cmd[12];
  1631. int use_10_for_ms;
  1632. int header_length;
  1633. int result;
  1634. struct scsi_sense_hdr my_sshdr;
  1635. memset(data, 0, sizeof(*data));
  1636. memset(&cmd[0], 0, 12);
  1637. cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
  1638. cmd[2] = modepage;
  1639. /* caller might not be interested in sense, but we need it */
  1640. if (!sshdr)
  1641. sshdr = &my_sshdr;
  1642. retry:
  1643. use_10_for_ms = sdev->use_10_for_ms;
  1644. if (use_10_for_ms) {
  1645. if (len < 8)
  1646. len = 8;
  1647. cmd[0] = MODE_SENSE_10;
  1648. cmd[8] = len;
  1649. header_length = 8;
  1650. } else {
  1651. if (len < 4)
  1652. len = 4;
  1653. cmd[0] = MODE_SENSE;
  1654. cmd[4] = len;
  1655. header_length = 4;
  1656. }
  1657. memset(buffer, 0, len);
  1658. result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
  1659. sshdr, timeout, retries);
  1660. /* This code looks awful: what it's doing is making sure an
  1661. * ILLEGAL REQUEST sense return identifies the actual command
  1662. * byte as the problem. MODE_SENSE commands can return
  1663. * ILLEGAL REQUEST if the code page isn't supported */
  1664. if (use_10_for_ms && !scsi_status_is_good(result) &&
  1665. (driver_byte(result) & DRIVER_SENSE)) {
  1666. if (scsi_sense_valid(sshdr)) {
  1667. if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
  1668. (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
  1669. /*
  1670. * Invalid command operation code
  1671. */
  1672. sdev->use_10_for_ms = 0;
  1673. goto retry;
  1674. }
  1675. }
  1676. }
  1677. if(scsi_status_is_good(result)) {
  1678. data->header_length = header_length;
  1679. if(use_10_for_ms) {
  1680. data->length = buffer[0]*256 + buffer[1] + 2;
  1681. data->medium_type = buffer[2];
  1682. data->device_specific = buffer[3];
  1683. data->longlba = buffer[4] & 0x01;
  1684. data->block_descriptor_length = buffer[6]*256
  1685. + buffer[7];
  1686. } else {
  1687. data->length = buffer[0] + 1;
  1688. data->medium_type = buffer[1];
  1689. data->device_specific = buffer[2];
  1690. data->block_descriptor_length = buffer[3];
  1691. }
  1692. }
  1693. return result;
  1694. }
  1695. EXPORT_SYMBOL(scsi_mode_sense);
  1696. int
  1697. scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries)
  1698. {
  1699. char cmd[] = {
  1700. TEST_UNIT_READY, 0, 0, 0, 0, 0,
  1701. };
  1702. struct scsi_sense_hdr sshdr;
  1703. int result;
  1704. result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, &sshdr,
  1705. timeout, retries);
  1706. if ((driver_byte(result) & DRIVER_SENSE) && sdev->removable) {
  1707. if ((scsi_sense_valid(&sshdr)) &&
  1708. ((sshdr.sense_key == UNIT_ATTENTION) ||
  1709. (sshdr.sense_key == NOT_READY))) {
  1710. sdev->changed = 1;
  1711. result = 0;
  1712. }
  1713. }
  1714. return result;
  1715. }
  1716. EXPORT_SYMBOL(scsi_test_unit_ready);
  1717. /**
  1718. * scsi_device_set_state - Take the given device through the device
  1719. * state model.
  1720. * @sdev: scsi device to change the state of.
  1721. * @state: state to change to.
  1722. *
  1723. * Returns zero if unsuccessful or an error if the requested
  1724. * transition is illegal.
  1725. **/
  1726. int
  1727. scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
  1728. {
  1729. enum scsi_device_state oldstate = sdev->sdev_state;
  1730. if (state == oldstate)
  1731. return 0;
  1732. switch (state) {
  1733. case SDEV_CREATED:
  1734. /* There are no legal states that come back to
  1735. * created. This is the manually initialised start
  1736. * state */
  1737. goto illegal;
  1738. case SDEV_RUNNING:
  1739. switch (oldstate) {
  1740. case SDEV_CREATED:
  1741. case SDEV_OFFLINE:
  1742. case SDEV_QUIESCE:
  1743. case SDEV_BLOCK:
  1744. break;
  1745. default:
  1746. goto illegal;
  1747. }
  1748. break;
  1749. case SDEV_QUIESCE:
  1750. switch (oldstate) {
  1751. case SDEV_RUNNING:
  1752. case SDEV_OFFLINE:
  1753. break;
  1754. default:
  1755. goto illegal;
  1756. }
  1757. break;
  1758. case SDEV_OFFLINE:
  1759. switch (oldstate) {
  1760. case SDEV_CREATED:
  1761. case SDEV_RUNNING:
  1762. case SDEV_QUIESCE:
  1763. case SDEV_BLOCK:
  1764. break;
  1765. default:
  1766. goto illegal;
  1767. }
  1768. break;
  1769. case SDEV_BLOCK:
  1770. switch (oldstate) {
  1771. case SDEV_CREATED:
  1772. case SDEV_RUNNING:
  1773. break;
  1774. default:
  1775. goto illegal;
  1776. }
  1777. break;
  1778. case SDEV_CANCEL:
  1779. switch (oldstate) {
  1780. case SDEV_CREATED:
  1781. case SDEV_RUNNING:
  1782. case SDEV_OFFLINE:
  1783. case SDEV_BLOCK:
  1784. break;
  1785. default:
  1786. goto illegal;
  1787. }
  1788. break;
  1789. case SDEV_DEL:
  1790. switch (oldstate) {
  1791. case SDEV_CANCEL:
  1792. break;
  1793. default:
  1794. goto illegal;
  1795. }
  1796. break;
  1797. }
  1798. sdev->sdev_state = state;
  1799. return 0;
  1800. illegal:
  1801. SCSI_LOG_ERROR_RECOVERY(1,
  1802. sdev_printk(KERN_ERR, sdev,
  1803. "Illegal state transition %s->%s\n",
  1804. scsi_device_state_name(oldstate),
  1805. scsi_device_state_name(state))
  1806. );
  1807. return -EINVAL;
  1808. }
  1809. EXPORT_SYMBOL(scsi_device_set_state);
  1810. /**
  1811. * scsi_device_quiesce - Block user issued commands.
  1812. * @sdev: scsi device to quiesce.
  1813. *
  1814. * This works by trying to transition to the SDEV_QUIESCE state
  1815. * (which must be a legal transition). When the device is in this
  1816. * state, only special requests will be accepted, all others will
  1817. * be deferred. Since special requests may also be requeued requests,
  1818. * a successful return doesn't guarantee the device will be
  1819. * totally quiescent.
  1820. *
  1821. * Must be called with user context, may sleep.
  1822. *
  1823. * Returns zero if unsuccessful or an error if not.
  1824. **/
  1825. int
  1826. scsi_device_quiesce(struct scsi_device *sdev)
  1827. {
  1828. int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
  1829. if (err)
  1830. return err;
  1831. scsi_run_queue(sdev->request_queue);
  1832. while (sdev->device_busy) {
  1833. msleep_interruptible(200);
  1834. scsi_run_queue(sdev->request_queue);
  1835. }
  1836. return 0;
  1837. }
  1838. EXPORT_SYMBOL(scsi_device_quiesce);
  1839. /**
  1840. * scsi_device_resume - Restart user issued commands to a quiesced device.
  1841. * @sdev: scsi device to resume.
  1842. *
  1843. * Moves the device from quiesced back to running and restarts the
  1844. * queues.
  1845. *
  1846. * Must be called with user context, may sleep.
  1847. **/
  1848. void
  1849. scsi_device_resume(struct scsi_device *sdev)
  1850. {
  1851. if(scsi_device_set_state(sdev, SDEV_RUNNING))
  1852. return;
  1853. scsi_run_queue(sdev->request_queue);
  1854. }
  1855. EXPORT_SYMBOL(scsi_device_resume);
  1856. static void
  1857. device_quiesce_fn(struct scsi_device *sdev, void *data)
  1858. {
  1859. scsi_device_quiesce(sdev);
  1860. }
  1861. void
  1862. scsi_target_quiesce(struct scsi_target *starget)
  1863. {
  1864. starget_for_each_device(starget, NULL, device_quiesce_fn);
  1865. }
  1866. EXPORT_SYMBOL(scsi_target_quiesce);
  1867. static void
  1868. device_resume_fn(struct scsi_device *sdev, void *data)
  1869. {
  1870. scsi_device_resume(sdev);
  1871. }
  1872. void
  1873. scsi_target_resume(struct scsi_target *starget)
  1874. {
  1875. starget_for_each_device(starget, NULL, device_resume_fn);
  1876. }
  1877. EXPORT_SYMBOL(scsi_target_resume);
  1878. /**
  1879. * scsi_internal_device_block - internal function to put a device
  1880. * temporarily into the SDEV_BLOCK state
  1881. * @sdev: device to block
  1882. *
  1883. * Block request made by scsi lld's to temporarily stop all
  1884. * scsi commands on the specified device. Called from interrupt
  1885. * or normal process context.
  1886. *
  1887. * Returns zero if successful or error if not
  1888. *
  1889. * Notes:
  1890. * This routine transitions the device to the SDEV_BLOCK state
  1891. * (which must be a legal transition). When the device is in this
  1892. * state, all commands are deferred until the scsi lld reenables
  1893. * the device with scsi_device_unblock or device_block_tmo fires.
  1894. * This routine assumes the host_lock is held on entry.
  1895. **/
  1896. int
  1897. scsi_internal_device_block(struct scsi_device *sdev)
  1898. {
  1899. request_queue_t *q = sdev->request_queue;
  1900. unsigned long flags;
  1901. int err = 0;
  1902. err = scsi_device_set_state(sdev, SDEV_BLOCK);
  1903. if (err)
  1904. return err;
  1905. /*
  1906. * The device has transitioned to SDEV_BLOCK. Stop the
  1907. * block layer from calling the midlayer with this device's
  1908. * request queue.
  1909. */
  1910. spin_lock_irqsave(q->queue_lock, flags);
  1911. blk_stop_queue(q);
  1912. spin_unlock_irqrestore(q->queue_lock, flags);
  1913. return 0;
  1914. }
  1915. EXPORT_SYMBOL_GPL(scsi_internal_device_block);
  1916. /**
  1917. * scsi_internal_device_unblock - resume a device after a block request
  1918. * @sdev: device to resume
  1919. *
  1920. * Called by scsi lld's or the midlayer to restart the device queue
  1921. * for the previously suspended scsi device. Called from interrupt or
  1922. * normal process context.
  1923. *
  1924. * Returns zero if successful or error if not.
  1925. *
  1926. * Notes:
  1927. * This routine transitions the device to the SDEV_RUNNING state
  1928. * (which must be a legal transition) allowing the midlayer to
  1929. * goose the queue for this device. This routine assumes the
  1930. * host_lock is held upon entry.
  1931. **/
  1932. int
  1933. scsi_internal_device_unblock(struct scsi_device *sdev)
  1934. {
  1935. request_queue_t *q = sdev->request_queue;
  1936. int err;
  1937. unsigned long flags;
  1938. /*
  1939. * Try to transition the scsi device to SDEV_RUNNING
  1940. * and goose the device queue if successful.
  1941. */
  1942. err = scsi_device_set_state(sdev, SDEV_RUNNING);
  1943. if (err)
  1944. return err;
  1945. spin_lock_irqsave(q->queue_lock, flags);
  1946. blk_start_queue(q);
  1947. spin_unlock_irqrestore(q->queue_lock, flags);
  1948. return 0;
  1949. }
  1950. EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
  1951. static void
  1952. device_block(struct scsi_device *sdev, void *data)
  1953. {
  1954. scsi_internal_device_block(sdev);
  1955. }
  1956. static int
  1957. target_block(struct device *dev, void *data)
  1958. {
  1959. if (scsi_is_target_device(dev))
  1960. starget_for_each_device(to_scsi_target(dev), NULL,
  1961. device_block);
  1962. return 0;
  1963. }
  1964. void
  1965. scsi_target_block(struct device *dev)
  1966. {
  1967. if (scsi_is_target_device(dev))
  1968. starget_for_each_device(to_scsi_target(dev), NULL,
  1969. device_block);
  1970. else
  1971. device_for_each_child(dev, NULL, target_block);
  1972. }
  1973. EXPORT_SYMBOL_GPL(scsi_target_block);
  1974. static void
  1975. device_unblock(struct scsi_device *sdev, void *data)
  1976. {
  1977. scsi_internal_device_unblock(sdev);
  1978. }
  1979. static int
  1980. target_unblock(struct device *dev, void *data)
  1981. {
  1982. if (scsi_is_target_device(dev))
  1983. starget_for_each_device(to_scsi_target(dev), NULL,
  1984. device_unblock);
  1985. return 0;
  1986. }
  1987. void
  1988. scsi_target_unblock(struct device *dev)
  1989. {
  1990. if (scsi_is_target_device(dev))
  1991. starget_for_each_device(to_scsi_target(dev), NULL,
  1992. device_unblock);
  1993. else
  1994. device_for_each_child(dev, NULL, target_unblock);
  1995. }
  1996. EXPORT_SYMBOL_GPL(scsi_target_unblock);