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_generic_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. 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. }
  1108. EXPORT_SYMBOL_GPL(scsi_setup_blk_pc_cmnd);
  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. struct scsi_driver *drv;
  1195. int ret;
  1196. /*
  1197. * This will do a couple of things:
  1198. * 1) Fill in the actual SCSI command.
  1199. * 2) Fill in any other upper-level specific fields
  1200. * (timeout).
  1201. *
  1202. * If this returns 0, it means that the request failed
  1203. * (reading past end of disk, reading offline device,
  1204. * etc). This won't actually talk to the device, but
  1205. * some kinds of consistency checking may cause the
  1206. * request to be rejected immediately.
  1207. */
  1208. /*
  1209. * This sets up the scatter-gather table (allocating if
  1210. * required).
  1211. */
  1212. ret = scsi_init_io(cmd);
  1213. switch(ret) {
  1214. /* For BLKPREP_KILL/DEFER the cmd was released */
  1215. case BLKPREP_KILL:
  1216. goto kill;
  1217. case BLKPREP_DEFER:
  1218. goto defer;
  1219. }
  1220. /*
  1221. * Initialize the actual SCSI command for this request.
  1222. */
  1223. if (req->rq_disk) {
  1224. drv = *(struct scsi_driver **)req->rq_disk->private_data;
  1225. if (unlikely(!drv->init_command(cmd))) {
  1226. scsi_release_buffers(cmd);
  1227. scsi_put_command(cmd);
  1228. goto kill;
  1229. }
  1230. } else {
  1231. scsi_setup_blk_pc_cmnd(cmd);
  1232. cmd->done = scsi_generic_done;
  1233. }
  1234. }
  1235. /*
  1236. * The request is now prepped, no need to come back here
  1237. */
  1238. req->flags |= REQ_DONTPREP;
  1239. return BLKPREP_OK;
  1240. defer:
  1241. /* If we defer, the elv_next_request() returns NULL, but the
  1242. * queue must be restarted, so we plug here if no returning
  1243. * command will automatically do that. */
  1244. if (sdev->device_busy == 0)
  1245. blk_plug_device(q);
  1246. return BLKPREP_DEFER;
  1247. kill:
  1248. req->errors = DID_NO_CONNECT << 16;
  1249. return BLKPREP_KILL;
  1250. }
  1251. /*
  1252. * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
  1253. * return 0.
  1254. *
  1255. * Called with the queue_lock held.
  1256. */
  1257. static inline int scsi_dev_queue_ready(struct request_queue *q,
  1258. struct scsi_device *sdev)
  1259. {
  1260. if (sdev->device_busy >= sdev->queue_depth)
  1261. return 0;
  1262. if (sdev->device_busy == 0 && sdev->device_blocked) {
  1263. /*
  1264. * unblock after device_blocked iterates to zero
  1265. */
  1266. if (--sdev->device_blocked == 0) {
  1267. SCSI_LOG_MLQUEUE(3,
  1268. sdev_printk(KERN_INFO, sdev,
  1269. "unblocking device at zero depth\n"));
  1270. } else {
  1271. blk_plug_device(q);
  1272. return 0;
  1273. }
  1274. }
  1275. if (sdev->device_blocked)
  1276. return 0;
  1277. return 1;
  1278. }
  1279. /*
  1280. * scsi_host_queue_ready: if we can send requests to shost, return 1 else
  1281. * return 0. We must end up running the queue again whenever 0 is
  1282. * returned, else IO can hang.
  1283. *
  1284. * Called with host_lock held.
  1285. */
  1286. static inline int scsi_host_queue_ready(struct request_queue *q,
  1287. struct Scsi_Host *shost,
  1288. struct scsi_device *sdev)
  1289. {
  1290. if (scsi_host_in_recovery(shost))
  1291. return 0;
  1292. if (shost->host_busy == 0 && shost->host_blocked) {
  1293. /*
  1294. * unblock after host_blocked iterates to zero
  1295. */
  1296. if (--shost->host_blocked == 0) {
  1297. SCSI_LOG_MLQUEUE(3,
  1298. printk("scsi%d unblocking host at zero depth\n",
  1299. shost->host_no));
  1300. } else {
  1301. blk_plug_device(q);
  1302. return 0;
  1303. }
  1304. }
  1305. if ((shost->can_queue > 0 && shost->host_busy >= shost->can_queue) ||
  1306. shost->host_blocked || shost->host_self_blocked) {
  1307. if (list_empty(&sdev->starved_entry))
  1308. list_add_tail(&sdev->starved_entry, &shost->starved_list);
  1309. return 0;
  1310. }
  1311. /* We're OK to process the command, so we can't be starved */
  1312. if (!list_empty(&sdev->starved_entry))
  1313. list_del_init(&sdev->starved_entry);
  1314. return 1;
  1315. }
  1316. /*
  1317. * Kill a request for a dead device
  1318. */
  1319. static void scsi_kill_request(struct request *req, request_queue_t *q)
  1320. {
  1321. struct scsi_cmnd *cmd = req->special;
  1322. blkdev_dequeue_request(req);
  1323. if (unlikely(cmd == NULL)) {
  1324. printk(KERN_CRIT "impossible request in %s.\n",
  1325. __FUNCTION__);
  1326. BUG();
  1327. }
  1328. scsi_init_cmd_errh(cmd);
  1329. cmd->result = DID_NO_CONNECT << 16;
  1330. atomic_inc(&cmd->device->iorequest_cnt);
  1331. __scsi_done(cmd);
  1332. }
  1333. static void scsi_softirq_done(struct request *rq)
  1334. {
  1335. struct scsi_cmnd *cmd = rq->completion_data;
  1336. unsigned long wait_for = cmd->allowed * cmd->timeout_per_command;
  1337. int disposition;
  1338. INIT_LIST_HEAD(&cmd->eh_entry);
  1339. disposition = scsi_decide_disposition(cmd);
  1340. if (disposition != SUCCESS &&
  1341. time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
  1342. sdev_printk(KERN_ERR, cmd->device,
  1343. "timing out command, waited %lus\n",
  1344. wait_for/HZ);
  1345. disposition = SUCCESS;
  1346. }
  1347. scsi_log_completion(cmd, disposition);
  1348. switch (disposition) {
  1349. case SUCCESS:
  1350. scsi_finish_command(cmd);
  1351. break;
  1352. case NEEDS_RETRY:
  1353. scsi_retry_command(cmd);
  1354. break;
  1355. case ADD_TO_MLQUEUE:
  1356. scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
  1357. break;
  1358. default:
  1359. if (!scsi_eh_scmd_add(cmd, 0))
  1360. scsi_finish_command(cmd);
  1361. }
  1362. }
  1363. /*
  1364. * Function: scsi_request_fn()
  1365. *
  1366. * Purpose: Main strategy routine for SCSI.
  1367. *
  1368. * Arguments: q - Pointer to actual queue.
  1369. *
  1370. * Returns: Nothing
  1371. *
  1372. * Lock status: IO request lock assumed to be held when called.
  1373. */
  1374. static void scsi_request_fn(struct request_queue *q)
  1375. {
  1376. struct scsi_device *sdev = q->queuedata;
  1377. struct Scsi_Host *shost;
  1378. struct scsi_cmnd *cmd;
  1379. struct request *req;
  1380. if (!sdev) {
  1381. printk("scsi: killing requests for dead queue\n");
  1382. while ((req = elv_next_request(q)) != NULL)
  1383. scsi_kill_request(req, q);
  1384. return;
  1385. }
  1386. if(!get_device(&sdev->sdev_gendev))
  1387. /* We must be tearing the block queue down already */
  1388. return;
  1389. /*
  1390. * To start with, we keep looping until the queue is empty, or until
  1391. * the host is no longer able to accept any more requests.
  1392. */
  1393. shost = sdev->host;
  1394. while (!blk_queue_plugged(q)) {
  1395. int rtn;
  1396. /*
  1397. * get next queueable request. We do this early to make sure
  1398. * that the request is fully prepared even if we cannot
  1399. * accept it.
  1400. */
  1401. req = elv_next_request(q);
  1402. if (!req || !scsi_dev_queue_ready(q, sdev))
  1403. break;
  1404. if (unlikely(!scsi_device_online(sdev))) {
  1405. sdev_printk(KERN_ERR, sdev,
  1406. "rejecting I/O to offline device\n");
  1407. scsi_kill_request(req, q);
  1408. continue;
  1409. }
  1410. /*
  1411. * Remove the request from the request list.
  1412. */
  1413. if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
  1414. blkdev_dequeue_request(req);
  1415. sdev->device_busy++;
  1416. spin_unlock(q->queue_lock);
  1417. cmd = req->special;
  1418. if (unlikely(cmd == NULL)) {
  1419. printk(KERN_CRIT "impossible request in %s.\n"
  1420. "please mail a stack trace to "
  1421. "linux-scsi@vger.kernel.org",
  1422. __FUNCTION__);
  1423. BUG();
  1424. }
  1425. spin_lock(shost->host_lock);
  1426. if (!scsi_host_queue_ready(q, shost, sdev))
  1427. goto not_ready;
  1428. if (sdev->single_lun) {
  1429. if (scsi_target(sdev)->starget_sdev_user &&
  1430. scsi_target(sdev)->starget_sdev_user != sdev)
  1431. goto not_ready;
  1432. scsi_target(sdev)->starget_sdev_user = sdev;
  1433. }
  1434. shost->host_busy++;
  1435. /*
  1436. * XXX(hch): This is rather suboptimal, scsi_dispatch_cmd will
  1437. * take the lock again.
  1438. */
  1439. spin_unlock_irq(shost->host_lock);
  1440. /*
  1441. * Finally, initialize any error handling parameters, and set up
  1442. * the timers for timeouts.
  1443. */
  1444. scsi_init_cmd_errh(cmd);
  1445. /*
  1446. * Dispatch the command to the low-level driver.
  1447. */
  1448. rtn = scsi_dispatch_cmd(cmd);
  1449. spin_lock_irq(q->queue_lock);
  1450. if(rtn) {
  1451. /* we're refusing the command; because of
  1452. * the way locks get dropped, we need to
  1453. * check here if plugging is required */
  1454. if(sdev->device_busy == 0)
  1455. blk_plug_device(q);
  1456. break;
  1457. }
  1458. }
  1459. goto out;
  1460. not_ready:
  1461. spin_unlock_irq(shost->host_lock);
  1462. /*
  1463. * lock q, handle tag, requeue req, and decrement device_busy. We
  1464. * must return with queue_lock held.
  1465. *
  1466. * Decrementing device_busy without checking it is OK, as all such
  1467. * cases (host limits or settings) should run the queue at some
  1468. * later time.
  1469. */
  1470. spin_lock_irq(q->queue_lock);
  1471. blk_requeue_request(q, req);
  1472. sdev->device_busy--;
  1473. if(sdev->device_busy == 0)
  1474. blk_plug_device(q);
  1475. out:
  1476. /* must be careful here...if we trigger the ->remove() function
  1477. * we cannot be holding the q lock */
  1478. spin_unlock_irq(q->queue_lock);
  1479. put_device(&sdev->sdev_gendev);
  1480. spin_lock_irq(q->queue_lock);
  1481. }
  1482. u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
  1483. {
  1484. struct device *host_dev;
  1485. u64 bounce_limit = 0xffffffff;
  1486. if (shost->unchecked_isa_dma)
  1487. return BLK_BOUNCE_ISA;
  1488. /*
  1489. * Platforms with virtual-DMA translation
  1490. * hardware have no practical limit.
  1491. */
  1492. if (!PCI_DMA_BUS_IS_PHYS)
  1493. return BLK_BOUNCE_ANY;
  1494. host_dev = scsi_get_device(shost);
  1495. if (host_dev && host_dev->dma_mask)
  1496. bounce_limit = *host_dev->dma_mask;
  1497. return bounce_limit;
  1498. }
  1499. EXPORT_SYMBOL(scsi_calculate_bounce_limit);
  1500. struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
  1501. {
  1502. struct Scsi_Host *shost = sdev->host;
  1503. struct request_queue *q;
  1504. q = blk_init_queue(scsi_request_fn, NULL);
  1505. if (!q)
  1506. return NULL;
  1507. blk_queue_prep_rq(q, scsi_prep_fn);
  1508. blk_queue_max_hw_segments(q, shost->sg_tablesize);
  1509. blk_queue_max_phys_segments(q, SCSI_MAX_PHYS_SEGMENTS);
  1510. blk_queue_max_sectors(q, shost->max_sectors);
  1511. blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
  1512. blk_queue_segment_boundary(q, shost->dma_boundary);
  1513. blk_queue_issue_flush_fn(q, scsi_issue_flush_fn);
  1514. blk_queue_softirq_done(q, scsi_softirq_done);
  1515. if (!shost->use_clustering)
  1516. clear_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
  1517. return q;
  1518. }
  1519. void scsi_free_queue(struct request_queue *q)
  1520. {
  1521. blk_cleanup_queue(q);
  1522. }
  1523. /*
  1524. * Function: scsi_block_requests()
  1525. *
  1526. * Purpose: Utility function used by low-level drivers to prevent further
  1527. * commands from being queued to the device.
  1528. *
  1529. * Arguments: shost - Host in question
  1530. *
  1531. * Returns: Nothing
  1532. *
  1533. * Lock status: No locks are assumed held.
  1534. *
  1535. * Notes: There is no timer nor any other means by which the requests
  1536. * get unblocked other than the low-level driver calling
  1537. * scsi_unblock_requests().
  1538. */
  1539. void scsi_block_requests(struct Scsi_Host *shost)
  1540. {
  1541. shost->host_self_blocked = 1;
  1542. }
  1543. EXPORT_SYMBOL(scsi_block_requests);
  1544. /*
  1545. * Function: scsi_unblock_requests()
  1546. *
  1547. * Purpose: Utility function used by low-level drivers to allow further
  1548. * commands from being queued to the device.
  1549. *
  1550. * Arguments: shost - Host in question
  1551. *
  1552. * Returns: Nothing
  1553. *
  1554. * Lock status: No locks are assumed held.
  1555. *
  1556. * Notes: There is no timer nor any other means by which the requests
  1557. * get unblocked other than the low-level driver calling
  1558. * scsi_unblock_requests().
  1559. *
  1560. * This is done as an API function so that changes to the
  1561. * internals of the scsi mid-layer won't require wholesale
  1562. * changes to drivers that use this feature.
  1563. */
  1564. void scsi_unblock_requests(struct Scsi_Host *shost)
  1565. {
  1566. shost->host_self_blocked = 0;
  1567. scsi_run_host_queues(shost);
  1568. }
  1569. EXPORT_SYMBOL(scsi_unblock_requests);
  1570. int __init scsi_init_queue(void)
  1571. {
  1572. int i;
  1573. scsi_io_context_cache = kmem_cache_create("scsi_io_context",
  1574. sizeof(struct scsi_io_context),
  1575. 0, 0, NULL, NULL);
  1576. if (!scsi_io_context_cache) {
  1577. printk(KERN_ERR "SCSI: can't init scsi io context cache\n");
  1578. return -ENOMEM;
  1579. }
  1580. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1581. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1582. int size = sgp->size * sizeof(struct scatterlist);
  1583. sgp->slab = kmem_cache_create(sgp->name, size, 0,
  1584. SLAB_HWCACHE_ALIGN, NULL, NULL);
  1585. if (!sgp->slab) {
  1586. printk(KERN_ERR "SCSI: can't init sg slab %s\n",
  1587. sgp->name);
  1588. }
  1589. sgp->pool = mempool_create(SG_MEMPOOL_SIZE,
  1590. mempool_alloc_slab, mempool_free_slab,
  1591. sgp->slab);
  1592. if (!sgp->pool) {
  1593. printk(KERN_ERR "SCSI: can't init sg mempool %s\n",
  1594. sgp->name);
  1595. }
  1596. }
  1597. return 0;
  1598. }
  1599. void scsi_exit_queue(void)
  1600. {
  1601. int i;
  1602. kmem_cache_destroy(scsi_io_context_cache);
  1603. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1604. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1605. mempool_destroy(sgp->pool);
  1606. kmem_cache_destroy(sgp->slab);
  1607. }
  1608. }
  1609. /**
  1610. * scsi_mode_sense - issue a mode sense, falling back from 10 to
  1611. * six bytes if necessary.
  1612. * @sdev: SCSI device to be queried
  1613. * @dbd: set if mode sense will allow block descriptors to be returned
  1614. * @modepage: mode page being requested
  1615. * @buffer: request buffer (may not be smaller than eight bytes)
  1616. * @len: length of request buffer.
  1617. * @timeout: command timeout
  1618. * @retries: number of retries before failing
  1619. * @data: returns a structure abstracting the mode header data
  1620. * @sense: place to put sense data (or NULL if no sense to be collected).
  1621. * must be SCSI_SENSE_BUFFERSIZE big.
  1622. *
  1623. * Returns zero if unsuccessful, or the header offset (either 4
  1624. * or 8 depending on whether a six or ten byte command was
  1625. * issued) if successful.
  1626. **/
  1627. int
  1628. scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
  1629. unsigned char *buffer, int len, int timeout, int retries,
  1630. struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr) {
  1631. unsigned char cmd[12];
  1632. int use_10_for_ms;
  1633. int header_length;
  1634. int result;
  1635. struct scsi_sense_hdr my_sshdr;
  1636. memset(data, 0, sizeof(*data));
  1637. memset(&cmd[0], 0, 12);
  1638. cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
  1639. cmd[2] = modepage;
  1640. /* caller might not be interested in sense, but we need it */
  1641. if (!sshdr)
  1642. sshdr = &my_sshdr;
  1643. retry:
  1644. use_10_for_ms = sdev->use_10_for_ms;
  1645. if (use_10_for_ms) {
  1646. if (len < 8)
  1647. len = 8;
  1648. cmd[0] = MODE_SENSE_10;
  1649. cmd[8] = len;
  1650. header_length = 8;
  1651. } else {
  1652. if (len < 4)
  1653. len = 4;
  1654. cmd[0] = MODE_SENSE;
  1655. cmd[4] = len;
  1656. header_length = 4;
  1657. }
  1658. memset(buffer, 0, len);
  1659. result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
  1660. sshdr, timeout, retries);
  1661. /* This code looks awful: what it's doing is making sure an
  1662. * ILLEGAL REQUEST sense return identifies the actual command
  1663. * byte as the problem. MODE_SENSE commands can return
  1664. * ILLEGAL REQUEST if the code page isn't supported */
  1665. if (use_10_for_ms && !scsi_status_is_good(result) &&
  1666. (driver_byte(result) & DRIVER_SENSE)) {
  1667. if (scsi_sense_valid(sshdr)) {
  1668. if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
  1669. (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
  1670. /*
  1671. * Invalid command operation code
  1672. */
  1673. sdev->use_10_for_ms = 0;
  1674. goto retry;
  1675. }
  1676. }
  1677. }
  1678. if(scsi_status_is_good(result)) {
  1679. data->header_length = header_length;
  1680. if(use_10_for_ms) {
  1681. data->length = buffer[0]*256 + buffer[1] + 2;
  1682. data->medium_type = buffer[2];
  1683. data->device_specific = buffer[3];
  1684. data->longlba = buffer[4] & 0x01;
  1685. data->block_descriptor_length = buffer[6]*256
  1686. + buffer[7];
  1687. } else {
  1688. data->length = buffer[0] + 1;
  1689. data->medium_type = buffer[1];
  1690. data->device_specific = buffer[2];
  1691. data->block_descriptor_length = buffer[3];
  1692. }
  1693. }
  1694. return result;
  1695. }
  1696. EXPORT_SYMBOL(scsi_mode_sense);
  1697. int
  1698. scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries)
  1699. {
  1700. char cmd[] = {
  1701. TEST_UNIT_READY, 0, 0, 0, 0, 0,
  1702. };
  1703. struct scsi_sense_hdr sshdr;
  1704. int result;
  1705. result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, &sshdr,
  1706. timeout, retries);
  1707. if ((driver_byte(result) & DRIVER_SENSE) && sdev->removable) {
  1708. if ((scsi_sense_valid(&sshdr)) &&
  1709. ((sshdr.sense_key == UNIT_ATTENTION) ||
  1710. (sshdr.sense_key == NOT_READY))) {
  1711. sdev->changed = 1;
  1712. result = 0;
  1713. }
  1714. }
  1715. return result;
  1716. }
  1717. EXPORT_SYMBOL(scsi_test_unit_ready);
  1718. /**
  1719. * scsi_device_set_state - Take the given device through the device
  1720. * state model.
  1721. * @sdev: scsi device to change the state of.
  1722. * @state: state to change to.
  1723. *
  1724. * Returns zero if unsuccessful or an error if the requested
  1725. * transition is illegal.
  1726. **/
  1727. int
  1728. scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
  1729. {
  1730. enum scsi_device_state oldstate = sdev->sdev_state;
  1731. if (state == oldstate)
  1732. return 0;
  1733. switch (state) {
  1734. case SDEV_CREATED:
  1735. /* There are no legal states that come back to
  1736. * created. This is the manually initialised start
  1737. * state */
  1738. goto illegal;
  1739. case SDEV_RUNNING:
  1740. switch (oldstate) {
  1741. case SDEV_CREATED:
  1742. case SDEV_OFFLINE:
  1743. case SDEV_QUIESCE:
  1744. case SDEV_BLOCK:
  1745. break;
  1746. default:
  1747. goto illegal;
  1748. }
  1749. break;
  1750. case SDEV_QUIESCE:
  1751. switch (oldstate) {
  1752. case SDEV_RUNNING:
  1753. case SDEV_OFFLINE:
  1754. break;
  1755. default:
  1756. goto illegal;
  1757. }
  1758. break;
  1759. case SDEV_OFFLINE:
  1760. switch (oldstate) {
  1761. case SDEV_CREATED:
  1762. case SDEV_RUNNING:
  1763. case SDEV_QUIESCE:
  1764. case SDEV_BLOCK:
  1765. break;
  1766. default:
  1767. goto illegal;
  1768. }
  1769. break;
  1770. case SDEV_BLOCK:
  1771. switch (oldstate) {
  1772. case SDEV_CREATED:
  1773. case SDEV_RUNNING:
  1774. break;
  1775. default:
  1776. goto illegal;
  1777. }
  1778. break;
  1779. case SDEV_CANCEL:
  1780. switch (oldstate) {
  1781. case SDEV_CREATED:
  1782. case SDEV_RUNNING:
  1783. case SDEV_OFFLINE:
  1784. case SDEV_BLOCK:
  1785. break;
  1786. default:
  1787. goto illegal;
  1788. }
  1789. break;
  1790. case SDEV_DEL:
  1791. switch (oldstate) {
  1792. case SDEV_CANCEL:
  1793. break;
  1794. default:
  1795. goto illegal;
  1796. }
  1797. break;
  1798. }
  1799. sdev->sdev_state = state;
  1800. return 0;
  1801. illegal:
  1802. SCSI_LOG_ERROR_RECOVERY(1,
  1803. sdev_printk(KERN_ERR, sdev,
  1804. "Illegal state transition %s->%s\n",
  1805. scsi_device_state_name(oldstate),
  1806. scsi_device_state_name(state))
  1807. );
  1808. return -EINVAL;
  1809. }
  1810. EXPORT_SYMBOL(scsi_device_set_state);
  1811. /**
  1812. * scsi_device_quiesce - Block user issued commands.
  1813. * @sdev: scsi device to quiesce.
  1814. *
  1815. * This works by trying to transition to the SDEV_QUIESCE state
  1816. * (which must be a legal transition). When the device is in this
  1817. * state, only special requests will be accepted, all others will
  1818. * be deferred. Since special requests may also be requeued requests,
  1819. * a successful return doesn't guarantee the device will be
  1820. * totally quiescent.
  1821. *
  1822. * Must be called with user context, may sleep.
  1823. *
  1824. * Returns zero if unsuccessful or an error if not.
  1825. **/
  1826. int
  1827. scsi_device_quiesce(struct scsi_device *sdev)
  1828. {
  1829. int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
  1830. if (err)
  1831. return err;
  1832. scsi_run_queue(sdev->request_queue);
  1833. while (sdev->device_busy) {
  1834. msleep_interruptible(200);
  1835. scsi_run_queue(sdev->request_queue);
  1836. }
  1837. return 0;
  1838. }
  1839. EXPORT_SYMBOL(scsi_device_quiesce);
  1840. /**
  1841. * scsi_device_resume - Restart user issued commands to a quiesced device.
  1842. * @sdev: scsi device to resume.
  1843. *
  1844. * Moves the device from quiesced back to running and restarts the
  1845. * queues.
  1846. *
  1847. * Must be called with user context, may sleep.
  1848. **/
  1849. void
  1850. scsi_device_resume(struct scsi_device *sdev)
  1851. {
  1852. if(scsi_device_set_state(sdev, SDEV_RUNNING))
  1853. return;
  1854. scsi_run_queue(sdev->request_queue);
  1855. }
  1856. EXPORT_SYMBOL(scsi_device_resume);
  1857. static void
  1858. device_quiesce_fn(struct scsi_device *sdev, void *data)
  1859. {
  1860. scsi_device_quiesce(sdev);
  1861. }
  1862. void
  1863. scsi_target_quiesce(struct scsi_target *starget)
  1864. {
  1865. starget_for_each_device(starget, NULL, device_quiesce_fn);
  1866. }
  1867. EXPORT_SYMBOL(scsi_target_quiesce);
  1868. static void
  1869. device_resume_fn(struct scsi_device *sdev, void *data)
  1870. {
  1871. scsi_device_resume(sdev);
  1872. }
  1873. void
  1874. scsi_target_resume(struct scsi_target *starget)
  1875. {
  1876. starget_for_each_device(starget, NULL, device_resume_fn);
  1877. }
  1878. EXPORT_SYMBOL(scsi_target_resume);
  1879. /**
  1880. * scsi_internal_device_block - internal function to put a device
  1881. * temporarily into the SDEV_BLOCK state
  1882. * @sdev: device to block
  1883. *
  1884. * Block request made by scsi lld's to temporarily stop all
  1885. * scsi commands on the specified device. Called from interrupt
  1886. * or normal process context.
  1887. *
  1888. * Returns zero if successful or error if not
  1889. *
  1890. * Notes:
  1891. * This routine transitions the device to the SDEV_BLOCK state
  1892. * (which must be a legal transition). When the device is in this
  1893. * state, all commands are deferred until the scsi lld reenables
  1894. * the device with scsi_device_unblock or device_block_tmo fires.
  1895. * This routine assumes the host_lock is held on entry.
  1896. **/
  1897. int
  1898. scsi_internal_device_block(struct scsi_device *sdev)
  1899. {
  1900. request_queue_t *q = sdev->request_queue;
  1901. unsigned long flags;
  1902. int err = 0;
  1903. err = scsi_device_set_state(sdev, SDEV_BLOCK);
  1904. if (err)
  1905. return err;
  1906. /*
  1907. * The device has transitioned to SDEV_BLOCK. Stop the
  1908. * block layer from calling the midlayer with this device's
  1909. * request queue.
  1910. */
  1911. spin_lock_irqsave(q->queue_lock, flags);
  1912. blk_stop_queue(q);
  1913. spin_unlock_irqrestore(q->queue_lock, flags);
  1914. return 0;
  1915. }
  1916. EXPORT_SYMBOL_GPL(scsi_internal_device_block);
  1917. /**
  1918. * scsi_internal_device_unblock - resume a device after a block request
  1919. * @sdev: device to resume
  1920. *
  1921. * Called by scsi lld's or the midlayer to restart the device queue
  1922. * for the previously suspended scsi device. Called from interrupt or
  1923. * normal process context.
  1924. *
  1925. * Returns zero if successful or error if not.
  1926. *
  1927. * Notes:
  1928. * This routine transitions the device to the SDEV_RUNNING state
  1929. * (which must be a legal transition) allowing the midlayer to
  1930. * goose the queue for this device. This routine assumes the
  1931. * host_lock is held upon entry.
  1932. **/
  1933. int
  1934. scsi_internal_device_unblock(struct scsi_device *sdev)
  1935. {
  1936. request_queue_t *q = sdev->request_queue;
  1937. int err;
  1938. unsigned long flags;
  1939. /*
  1940. * Try to transition the scsi device to SDEV_RUNNING
  1941. * and goose the device queue if successful.
  1942. */
  1943. err = scsi_device_set_state(sdev, SDEV_RUNNING);
  1944. if (err)
  1945. return err;
  1946. spin_lock_irqsave(q->queue_lock, flags);
  1947. blk_start_queue(q);
  1948. spin_unlock_irqrestore(q->queue_lock, flags);
  1949. return 0;
  1950. }
  1951. EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
  1952. static void
  1953. device_block(struct scsi_device *sdev, void *data)
  1954. {
  1955. scsi_internal_device_block(sdev);
  1956. }
  1957. static int
  1958. target_block(struct device *dev, void *data)
  1959. {
  1960. if (scsi_is_target_device(dev))
  1961. starget_for_each_device(to_scsi_target(dev), NULL,
  1962. device_block);
  1963. return 0;
  1964. }
  1965. void
  1966. scsi_target_block(struct device *dev)
  1967. {
  1968. if (scsi_is_target_device(dev))
  1969. starget_for_each_device(to_scsi_target(dev), NULL,
  1970. device_block);
  1971. else
  1972. device_for_each_child(dev, NULL, target_block);
  1973. }
  1974. EXPORT_SYMBOL_GPL(scsi_target_block);
  1975. static void
  1976. device_unblock(struct scsi_device *sdev, void *data)
  1977. {
  1978. scsi_internal_device_unblock(sdev);
  1979. }
  1980. static int
  1981. target_unblock(struct device *dev, void *data)
  1982. {
  1983. if (scsi_is_target_device(dev))
  1984. starget_for_each_device(to_scsi_target(dev), NULL,
  1985. device_unblock);
  1986. return 0;
  1987. }
  1988. void
  1989. scsi_target_unblock(struct device *dev)
  1990. {
  1991. if (scsi_is_target_device(dev))
  1992. starget_for_each_device(to_scsi_target(dev), NULL,
  1993. device_unblock);
  1994. else
  1995. device_for_each_child(dev, NULL, target_unblock);
  1996. }
  1997. EXPORT_SYMBOL_GPL(scsi_target_unblock);