scsi_lib.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474
  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/bitops.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/completion.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mempool.h>
  15. #include <linux/slab.h>
  16. #include <linux/init.h>
  17. #include <linux/pci.h>
  18. #include <linux/delay.h>
  19. #include <linux/hardirq.h>
  20. #include <linux/scatterlist.h>
  21. #include <scsi/scsi.h>
  22. #include <scsi/scsi_cmnd.h>
  23. #include <scsi/scsi_dbg.h>
  24. #include <scsi/scsi_device.h>
  25. #include <scsi/scsi_driver.h>
  26. #include <scsi/scsi_eh.h>
  27. #include <scsi/scsi_host.h>
  28. #include "scsi_priv.h"
  29. #include "scsi_logging.h"
  30. #define SG_MEMPOOL_NR ARRAY_SIZE(scsi_sg_pools)
  31. #define SG_MEMPOOL_SIZE 2
  32. struct scsi_host_sg_pool {
  33. size_t size;
  34. char *name;
  35. struct kmem_cache *slab;
  36. mempool_t *pool;
  37. };
  38. #define SP(x) { x, "sgpool-" __stringify(x) }
  39. #if (SCSI_MAX_SG_SEGMENTS < 32)
  40. #error SCSI_MAX_SG_SEGMENTS is too small (must be 32 or greater)
  41. #endif
  42. static struct scsi_host_sg_pool scsi_sg_pools[] = {
  43. SP(8),
  44. SP(16),
  45. #if (SCSI_MAX_SG_SEGMENTS > 32)
  46. SP(32),
  47. #if (SCSI_MAX_SG_SEGMENTS > 64)
  48. SP(64),
  49. #if (SCSI_MAX_SG_SEGMENTS > 128)
  50. SP(128),
  51. #if (SCSI_MAX_SG_SEGMENTS > 256)
  52. #error SCSI_MAX_SG_SEGMENTS is too large (256 MAX)
  53. #endif
  54. #endif
  55. #endif
  56. #endif
  57. SP(SCSI_MAX_SG_SEGMENTS)
  58. };
  59. #undef SP
  60. static struct kmem_cache *scsi_bidi_sdb_cache;
  61. static void scsi_run_queue(struct request_queue *q);
  62. /*
  63. * Function: scsi_unprep_request()
  64. *
  65. * Purpose: Remove all preparation done for a request, including its
  66. * associated scsi_cmnd, so that it can be requeued.
  67. *
  68. * Arguments: req - request to unprepare
  69. *
  70. * Lock status: Assumed that no locks are held upon entry.
  71. *
  72. * Returns: Nothing.
  73. */
  74. static void scsi_unprep_request(struct request *req)
  75. {
  76. struct scsi_cmnd *cmd = req->special;
  77. req->cmd_flags &= ~REQ_DONTPREP;
  78. req->special = NULL;
  79. scsi_put_command(cmd);
  80. }
  81. /*
  82. * Function: scsi_queue_insert()
  83. *
  84. * Purpose: Insert a command in the midlevel queue.
  85. *
  86. * Arguments: cmd - command that we are adding to queue.
  87. * reason - why we are inserting command to queue.
  88. *
  89. * Lock status: Assumed that lock is not held upon entry.
  90. *
  91. * Returns: Nothing.
  92. *
  93. * Notes: We do this for one of two cases. Either the host is busy
  94. * and it cannot accept any more commands for the time being,
  95. * or the device returned QUEUE_FULL and can accept no more
  96. * commands.
  97. * Notes: This could be called either from an interrupt context or a
  98. * normal process context.
  99. */
  100. int scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
  101. {
  102. struct Scsi_Host *host = cmd->device->host;
  103. struct scsi_device *device = cmd->device;
  104. struct request_queue *q = device->request_queue;
  105. unsigned long flags;
  106. SCSI_LOG_MLQUEUE(1,
  107. printk("Inserting command %p into mlqueue\n", cmd));
  108. /*
  109. * Set the appropriate busy bit for the device/host.
  110. *
  111. * If the host/device isn't busy, assume that something actually
  112. * completed, and that we should be able to queue a command now.
  113. *
  114. * Note that the prior mid-layer assumption that any host could
  115. * always queue at least one command is now broken. The mid-layer
  116. * will implement a user specifiable stall (see
  117. * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
  118. * if a command is requeued with no other commands outstanding
  119. * either for the device or for the host.
  120. */
  121. if (reason == SCSI_MLQUEUE_HOST_BUSY)
  122. host->host_blocked = host->max_host_blocked;
  123. else if (reason == SCSI_MLQUEUE_DEVICE_BUSY)
  124. device->device_blocked = device->max_device_blocked;
  125. /*
  126. * Decrement the counters, since these commands are no longer
  127. * active on the host/device.
  128. */
  129. scsi_device_unbusy(device);
  130. /*
  131. * Requeue this command. It will go before all other commands
  132. * that are already in the queue.
  133. *
  134. * NOTE: there is magic here about the way the queue is plugged if
  135. * we have no outstanding commands.
  136. *
  137. * Although we *don't* plug the queue, we call the request
  138. * function. The SCSI request function detects the blocked condition
  139. * and plugs the queue appropriately.
  140. */
  141. spin_lock_irqsave(q->queue_lock, flags);
  142. blk_requeue_request(q, cmd->request);
  143. spin_unlock_irqrestore(q->queue_lock, flags);
  144. scsi_run_queue(q);
  145. return 0;
  146. }
  147. /**
  148. * scsi_execute - insert request and wait for the result
  149. * @sdev: scsi device
  150. * @cmd: scsi command
  151. * @data_direction: data direction
  152. * @buffer: data buffer
  153. * @bufflen: len of buffer
  154. * @sense: optional sense buffer
  155. * @timeout: request timeout in seconds
  156. * @retries: number of times to retry request
  157. * @flags: or into request flags;
  158. *
  159. * returns the req->errors value which is the scsi_cmnd result
  160. * field.
  161. */
  162. int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
  163. int data_direction, void *buffer, unsigned bufflen,
  164. unsigned char *sense, int timeout, int retries, int flags)
  165. {
  166. struct request *req;
  167. int write = (data_direction == DMA_TO_DEVICE);
  168. int ret = DRIVER_ERROR << 24;
  169. req = blk_get_request(sdev->request_queue, write, __GFP_WAIT);
  170. if (bufflen && blk_rq_map_kern(sdev->request_queue, req,
  171. buffer, bufflen, __GFP_WAIT))
  172. goto out;
  173. req->cmd_len = COMMAND_SIZE(cmd[0]);
  174. memcpy(req->cmd, cmd, req->cmd_len);
  175. req->sense = sense;
  176. req->sense_len = 0;
  177. req->retries = retries;
  178. req->timeout = timeout;
  179. req->cmd_type = REQ_TYPE_BLOCK_PC;
  180. req->cmd_flags |= flags | REQ_QUIET | REQ_PREEMPT;
  181. /*
  182. * head injection *required* here otherwise quiesce won't work
  183. */
  184. blk_execute_rq(req->q, NULL, req, 1);
  185. ret = req->errors;
  186. out:
  187. blk_put_request(req);
  188. return ret;
  189. }
  190. EXPORT_SYMBOL(scsi_execute);
  191. int scsi_execute_req(struct scsi_device *sdev, const unsigned char *cmd,
  192. int data_direction, void *buffer, unsigned bufflen,
  193. struct scsi_sense_hdr *sshdr, int timeout, int retries)
  194. {
  195. char *sense = NULL;
  196. int result;
  197. if (sshdr) {
  198. sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
  199. if (!sense)
  200. return DRIVER_ERROR << 24;
  201. }
  202. result = scsi_execute(sdev, cmd, data_direction, buffer, bufflen,
  203. sense, timeout, retries, 0);
  204. if (sshdr)
  205. scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, sshdr);
  206. kfree(sense);
  207. return result;
  208. }
  209. EXPORT_SYMBOL(scsi_execute_req);
  210. struct scsi_io_context {
  211. void *data;
  212. void (*done)(void *data, char *sense, int result, int resid);
  213. char sense[SCSI_SENSE_BUFFERSIZE];
  214. };
  215. static struct kmem_cache *scsi_io_context_cache;
  216. static void scsi_end_async(struct request *req, int uptodate)
  217. {
  218. struct scsi_io_context *sioc = req->end_io_data;
  219. if (sioc->done)
  220. sioc->done(sioc->data, sioc->sense, req->errors, req->data_len);
  221. kmem_cache_free(scsi_io_context_cache, sioc);
  222. __blk_put_request(req->q, req);
  223. }
  224. static int scsi_merge_bio(struct request *rq, struct bio *bio)
  225. {
  226. struct request_queue *q = rq->q;
  227. bio->bi_flags &= ~(1 << BIO_SEG_VALID);
  228. if (rq_data_dir(rq) == WRITE)
  229. bio->bi_rw |= (1 << BIO_RW);
  230. blk_queue_bounce(q, &bio);
  231. return blk_rq_append_bio(q, rq, bio);
  232. }
  233. static void scsi_bi_endio(struct bio *bio, int error)
  234. {
  235. bio_put(bio);
  236. }
  237. /**
  238. * scsi_req_map_sg - map a scatterlist into a request
  239. * @rq: request to fill
  240. * @sgl: scatterlist
  241. * @nsegs: number of elements
  242. * @bufflen: len of buffer
  243. * @gfp: memory allocation flags
  244. *
  245. * scsi_req_map_sg maps a scatterlist into a request so that the
  246. * request can be sent to the block layer. We do not trust the scatterlist
  247. * sent to use, as some ULDs use that struct to only organize the pages.
  248. */
  249. static int scsi_req_map_sg(struct request *rq, struct scatterlist *sgl,
  250. int nsegs, unsigned bufflen, gfp_t gfp)
  251. {
  252. struct request_queue *q = rq->q;
  253. int nr_pages = (bufflen + sgl[0].offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
  254. unsigned int data_len = bufflen, len, bytes, off;
  255. struct scatterlist *sg;
  256. struct page *page;
  257. struct bio *bio = NULL;
  258. int i, err, nr_vecs = 0;
  259. for_each_sg(sgl, sg, nsegs, i) {
  260. page = sg_page(sg);
  261. off = sg->offset;
  262. len = sg->length;
  263. data_len += len;
  264. while (len > 0 && data_len > 0) {
  265. /*
  266. * sg sends a scatterlist that is larger than
  267. * the data_len it wants transferred for certain
  268. * IO sizes
  269. */
  270. bytes = min_t(unsigned int, len, PAGE_SIZE - off);
  271. bytes = min(bytes, data_len);
  272. if (!bio) {
  273. nr_vecs = min_t(int, BIO_MAX_PAGES, nr_pages);
  274. nr_pages -= nr_vecs;
  275. bio = bio_alloc(gfp, nr_vecs);
  276. if (!bio) {
  277. err = -ENOMEM;
  278. goto free_bios;
  279. }
  280. bio->bi_end_io = scsi_bi_endio;
  281. }
  282. if (bio_add_pc_page(q, bio, page, bytes, off) !=
  283. bytes) {
  284. bio_put(bio);
  285. err = -EINVAL;
  286. goto free_bios;
  287. }
  288. if (bio->bi_vcnt >= nr_vecs) {
  289. err = scsi_merge_bio(rq, bio);
  290. if (err) {
  291. bio_endio(bio, 0);
  292. goto free_bios;
  293. }
  294. bio = NULL;
  295. }
  296. page++;
  297. len -= bytes;
  298. data_len -=bytes;
  299. off = 0;
  300. }
  301. }
  302. rq->buffer = rq->data = NULL;
  303. rq->data_len = bufflen;
  304. return 0;
  305. free_bios:
  306. while ((bio = rq->bio) != NULL) {
  307. rq->bio = bio->bi_next;
  308. /*
  309. * call endio instead of bio_put incase it was bounced
  310. */
  311. bio_endio(bio, 0);
  312. }
  313. return err;
  314. }
  315. /**
  316. * scsi_execute_async - insert request
  317. * @sdev: scsi device
  318. * @cmd: scsi command
  319. * @cmd_len: length of scsi cdb
  320. * @data_direction: DMA_TO_DEVICE, DMA_FROM_DEVICE, or DMA_NONE
  321. * @buffer: data buffer (this can be a kernel buffer or scatterlist)
  322. * @bufflen: len of buffer
  323. * @use_sg: if buffer is a scatterlist this is the number of elements
  324. * @timeout: request timeout in seconds
  325. * @retries: number of times to retry request
  326. * @privdata: data passed to done()
  327. * @done: callback function when done
  328. * @gfp: memory allocation flags
  329. */
  330. int scsi_execute_async(struct scsi_device *sdev, const unsigned char *cmd,
  331. int cmd_len, int data_direction, void *buffer, unsigned bufflen,
  332. int use_sg, int timeout, int retries, void *privdata,
  333. void (*done)(void *, char *, int, int), gfp_t gfp)
  334. {
  335. struct request *req;
  336. struct scsi_io_context *sioc;
  337. int err = 0;
  338. int write = (data_direction == DMA_TO_DEVICE);
  339. sioc = kmem_cache_zalloc(scsi_io_context_cache, gfp);
  340. if (!sioc)
  341. return DRIVER_ERROR << 24;
  342. req = blk_get_request(sdev->request_queue, write, gfp);
  343. if (!req)
  344. goto free_sense;
  345. req->cmd_type = REQ_TYPE_BLOCK_PC;
  346. req->cmd_flags |= REQ_QUIET;
  347. if (use_sg)
  348. err = scsi_req_map_sg(req, buffer, use_sg, bufflen, gfp);
  349. else if (bufflen)
  350. err = blk_rq_map_kern(req->q, req, buffer, bufflen, gfp);
  351. if (err)
  352. goto free_req;
  353. req->cmd_len = cmd_len;
  354. memset(req->cmd, 0, BLK_MAX_CDB); /* ATAPI hates garbage after CDB */
  355. memcpy(req->cmd, cmd, req->cmd_len);
  356. req->sense = sioc->sense;
  357. req->sense_len = 0;
  358. req->timeout = timeout;
  359. req->retries = retries;
  360. req->end_io_data = sioc;
  361. sioc->data = privdata;
  362. sioc->done = done;
  363. blk_execute_rq_nowait(req->q, NULL, req, 1, scsi_end_async);
  364. return 0;
  365. free_req:
  366. blk_put_request(req);
  367. free_sense:
  368. kmem_cache_free(scsi_io_context_cache, sioc);
  369. return DRIVER_ERROR << 24;
  370. }
  371. EXPORT_SYMBOL_GPL(scsi_execute_async);
  372. /*
  373. * Function: scsi_init_cmd_errh()
  374. *
  375. * Purpose: Initialize cmd fields related to error handling.
  376. *
  377. * Arguments: cmd - command that is ready to be queued.
  378. *
  379. * Notes: This function has the job of initializing a number of
  380. * fields related to error handling. Typically this will
  381. * be called once for each command, as required.
  382. */
  383. static void scsi_init_cmd_errh(struct scsi_cmnd *cmd)
  384. {
  385. cmd->serial_number = 0;
  386. scsi_set_resid(cmd, 0);
  387. memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  388. if (cmd->cmd_len == 0)
  389. cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]);
  390. }
  391. void scsi_device_unbusy(struct scsi_device *sdev)
  392. {
  393. struct Scsi_Host *shost = sdev->host;
  394. unsigned long flags;
  395. spin_lock_irqsave(shost->host_lock, flags);
  396. shost->host_busy--;
  397. if (unlikely(scsi_host_in_recovery(shost) &&
  398. (shost->host_failed || shost->host_eh_scheduled)))
  399. scsi_eh_wakeup(shost);
  400. spin_unlock(shost->host_lock);
  401. spin_lock(sdev->request_queue->queue_lock);
  402. sdev->device_busy--;
  403. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  404. }
  405. /*
  406. * Called for single_lun devices on IO completion. Clear starget_sdev_user,
  407. * and call blk_run_queue for all the scsi_devices on the target -
  408. * including current_sdev first.
  409. *
  410. * Called with *no* scsi locks held.
  411. */
  412. static void scsi_single_lun_run(struct scsi_device *current_sdev)
  413. {
  414. struct Scsi_Host *shost = current_sdev->host;
  415. struct scsi_device *sdev, *tmp;
  416. struct scsi_target *starget = scsi_target(current_sdev);
  417. unsigned long flags;
  418. spin_lock_irqsave(shost->host_lock, flags);
  419. starget->starget_sdev_user = NULL;
  420. spin_unlock_irqrestore(shost->host_lock, flags);
  421. /*
  422. * Call blk_run_queue for all LUNs on the target, starting with
  423. * current_sdev. We race with others (to set starget_sdev_user),
  424. * but in most cases, we will be first. Ideally, each LU on the
  425. * target would get some limited time or requests on the target.
  426. */
  427. blk_run_queue(current_sdev->request_queue);
  428. spin_lock_irqsave(shost->host_lock, flags);
  429. if (starget->starget_sdev_user)
  430. goto out;
  431. list_for_each_entry_safe(sdev, tmp, &starget->devices,
  432. same_target_siblings) {
  433. if (sdev == current_sdev)
  434. continue;
  435. if (scsi_device_get(sdev))
  436. continue;
  437. spin_unlock_irqrestore(shost->host_lock, flags);
  438. blk_run_queue(sdev->request_queue);
  439. spin_lock_irqsave(shost->host_lock, flags);
  440. scsi_device_put(sdev);
  441. }
  442. out:
  443. spin_unlock_irqrestore(shost->host_lock, flags);
  444. }
  445. /*
  446. * Function: scsi_run_queue()
  447. *
  448. * Purpose: Select a proper request queue to serve next
  449. *
  450. * Arguments: q - last request's queue
  451. *
  452. * Returns: Nothing
  453. *
  454. * Notes: The previous command was completely finished, start
  455. * a new one if possible.
  456. */
  457. static void scsi_run_queue(struct request_queue *q)
  458. {
  459. struct scsi_device *sdev = q->queuedata;
  460. struct Scsi_Host *shost = sdev->host;
  461. unsigned long flags;
  462. if (scsi_target(sdev)->single_lun)
  463. scsi_single_lun_run(sdev);
  464. spin_lock_irqsave(shost->host_lock, flags);
  465. while (!list_empty(&shost->starved_list) &&
  466. !shost->host_blocked && !shost->host_self_blocked &&
  467. !((shost->can_queue > 0) &&
  468. (shost->host_busy >= shost->can_queue))) {
  469. /*
  470. * As long as shost is accepting commands and we have
  471. * starved queues, call blk_run_queue. scsi_request_fn
  472. * drops the queue_lock and can add us back to the
  473. * starved_list.
  474. *
  475. * host_lock protects the starved_list and starved_entry.
  476. * scsi_request_fn must get the host_lock before checking
  477. * or modifying starved_list or starved_entry.
  478. */
  479. sdev = list_entry(shost->starved_list.next,
  480. struct scsi_device, starved_entry);
  481. list_del_init(&sdev->starved_entry);
  482. spin_unlock_irqrestore(shost->host_lock, flags);
  483. if (test_bit(QUEUE_FLAG_REENTER, &q->queue_flags) &&
  484. !test_and_set_bit(QUEUE_FLAG_REENTER,
  485. &sdev->request_queue->queue_flags)) {
  486. blk_run_queue(sdev->request_queue);
  487. clear_bit(QUEUE_FLAG_REENTER,
  488. &sdev->request_queue->queue_flags);
  489. } else
  490. blk_run_queue(sdev->request_queue);
  491. spin_lock_irqsave(shost->host_lock, flags);
  492. if (unlikely(!list_empty(&sdev->starved_entry)))
  493. /*
  494. * sdev lost a race, and was put back on the
  495. * starved list. This is unlikely but without this
  496. * in theory we could loop forever.
  497. */
  498. break;
  499. }
  500. spin_unlock_irqrestore(shost->host_lock, flags);
  501. blk_run_queue(q);
  502. }
  503. /*
  504. * Function: scsi_requeue_command()
  505. *
  506. * Purpose: Handle post-processing of completed commands.
  507. *
  508. * Arguments: q - queue to operate on
  509. * cmd - command that may need to be requeued.
  510. *
  511. * Returns: Nothing
  512. *
  513. * Notes: After command completion, there may be blocks left
  514. * over which weren't finished by the previous command
  515. * this can be for a number of reasons - the main one is
  516. * I/O errors in the middle of the request, in which case
  517. * we need to request the blocks that come after the bad
  518. * sector.
  519. * Notes: Upon return, cmd is a stale pointer.
  520. */
  521. static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
  522. {
  523. struct request *req = cmd->request;
  524. unsigned long flags;
  525. scsi_unprep_request(req);
  526. spin_lock_irqsave(q->queue_lock, flags);
  527. blk_requeue_request(q, req);
  528. spin_unlock_irqrestore(q->queue_lock, flags);
  529. scsi_run_queue(q);
  530. }
  531. void scsi_next_command(struct scsi_cmnd *cmd)
  532. {
  533. struct scsi_device *sdev = cmd->device;
  534. struct request_queue *q = sdev->request_queue;
  535. /* need to hold a reference on the device before we let go of the cmd */
  536. get_device(&sdev->sdev_gendev);
  537. scsi_put_command(cmd);
  538. scsi_run_queue(q);
  539. /* ok to remove device now */
  540. put_device(&sdev->sdev_gendev);
  541. }
  542. void scsi_run_host_queues(struct Scsi_Host *shost)
  543. {
  544. struct scsi_device *sdev;
  545. shost_for_each_device(sdev, shost)
  546. scsi_run_queue(sdev->request_queue);
  547. }
  548. /*
  549. * Function: scsi_end_request()
  550. *
  551. * Purpose: Post-processing of completed commands (usually invoked at end
  552. * of upper level post-processing and scsi_io_completion).
  553. *
  554. * Arguments: cmd - command that is complete.
  555. * error - 0 if I/O indicates success, < 0 for I/O error.
  556. * bytes - number of bytes of completed I/O
  557. * requeue - indicates whether we should requeue leftovers.
  558. *
  559. * Lock status: Assumed that lock is not held upon entry.
  560. *
  561. * Returns: cmd if requeue required, NULL otherwise.
  562. *
  563. * Notes: This is called for block device requests in order to
  564. * mark some number of sectors as complete.
  565. *
  566. * We are guaranteeing that the request queue will be goosed
  567. * at some point during this call.
  568. * Notes: If cmd was requeued, upon return it will be a stale pointer.
  569. */
  570. static struct scsi_cmnd *scsi_end_request(struct scsi_cmnd *cmd, int error,
  571. int bytes, int requeue)
  572. {
  573. struct request_queue *q = cmd->device->request_queue;
  574. struct request *req = cmd->request;
  575. /*
  576. * If there are blocks left over at the end, set up the command
  577. * to queue the remainder of them.
  578. */
  579. if (blk_end_request(req, error, bytes)) {
  580. int leftover = (req->hard_nr_sectors << 9);
  581. if (blk_pc_request(req))
  582. leftover = req->data_len;
  583. /* kill remainder if no retrys */
  584. if (error && blk_noretry_request(req))
  585. blk_end_request(req, error, leftover);
  586. else {
  587. if (requeue) {
  588. /*
  589. * Bleah. Leftovers again. Stick the
  590. * leftovers in the front of the
  591. * queue, and goose the queue again.
  592. */
  593. scsi_requeue_command(q, cmd);
  594. cmd = NULL;
  595. }
  596. return cmd;
  597. }
  598. }
  599. /*
  600. * This will goose the queue request function at the end, so we don't
  601. * need to worry about launching another command.
  602. */
  603. scsi_next_command(cmd);
  604. return NULL;
  605. }
  606. static inline unsigned int scsi_sgtable_index(unsigned short nents)
  607. {
  608. unsigned int index;
  609. BUG_ON(nents > SCSI_MAX_SG_SEGMENTS);
  610. if (nents <= 8)
  611. index = 0;
  612. else
  613. index = get_count_order(nents) - 3;
  614. return index;
  615. }
  616. static void scsi_sg_free(struct scatterlist *sgl, unsigned int nents)
  617. {
  618. struct scsi_host_sg_pool *sgp;
  619. sgp = scsi_sg_pools + scsi_sgtable_index(nents);
  620. mempool_free(sgl, sgp->pool);
  621. }
  622. static struct scatterlist *scsi_sg_alloc(unsigned int nents, gfp_t gfp_mask)
  623. {
  624. struct scsi_host_sg_pool *sgp;
  625. sgp = scsi_sg_pools + scsi_sgtable_index(nents);
  626. return mempool_alloc(sgp->pool, gfp_mask);
  627. }
  628. static int scsi_alloc_sgtable(struct scsi_data_buffer *sdb, int nents,
  629. gfp_t gfp_mask)
  630. {
  631. int ret;
  632. BUG_ON(!nents);
  633. ret = __sg_alloc_table(&sdb->table, nents, SCSI_MAX_SG_SEGMENTS,
  634. gfp_mask, scsi_sg_alloc);
  635. if (unlikely(ret))
  636. __sg_free_table(&sdb->table, SCSI_MAX_SG_SEGMENTS,
  637. scsi_sg_free);
  638. return ret;
  639. }
  640. static void scsi_free_sgtable(struct scsi_data_buffer *sdb)
  641. {
  642. __sg_free_table(&sdb->table, SCSI_MAX_SG_SEGMENTS, scsi_sg_free);
  643. }
  644. /*
  645. * Function: scsi_release_buffers()
  646. *
  647. * Purpose: Completion processing for block device I/O requests.
  648. *
  649. * Arguments: cmd - command that we are bailing.
  650. *
  651. * Lock status: Assumed that no lock is held upon entry.
  652. *
  653. * Returns: Nothing
  654. *
  655. * Notes: In the event that an upper level driver rejects a
  656. * command, we must release resources allocated during
  657. * the __init_io() function. Primarily this would involve
  658. * the scatter-gather table, and potentially any bounce
  659. * buffers.
  660. */
  661. void scsi_release_buffers(struct scsi_cmnd *cmd)
  662. {
  663. if (cmd->sdb.table.nents)
  664. scsi_free_sgtable(&cmd->sdb);
  665. memset(&cmd->sdb, 0, sizeof(cmd->sdb));
  666. if (scsi_bidi_cmnd(cmd)) {
  667. struct scsi_data_buffer *bidi_sdb =
  668. cmd->request->next_rq->special;
  669. scsi_free_sgtable(bidi_sdb);
  670. kmem_cache_free(scsi_bidi_sdb_cache, bidi_sdb);
  671. cmd->request->next_rq->special = NULL;
  672. }
  673. }
  674. EXPORT_SYMBOL(scsi_release_buffers);
  675. /*
  676. * Bidi commands Must be complete as a whole, both sides at once.
  677. * If part of the bytes were written and lld returned
  678. * scsi_in()->resid and/or scsi_out()->resid this information will be left
  679. * in req->data_len and req->next_rq->data_len. The upper-layer driver can
  680. * decide what to do with this information.
  681. */
  682. void scsi_end_bidi_request(struct scsi_cmnd *cmd)
  683. {
  684. struct request *req = cmd->request;
  685. unsigned int dlen = req->data_len;
  686. unsigned int next_dlen = req->next_rq->data_len;
  687. req->data_len = scsi_out(cmd)->resid;
  688. req->next_rq->data_len = scsi_in(cmd)->resid;
  689. /* The req and req->next_rq have not been completed */
  690. BUG_ON(blk_end_bidi_request(req, 0, dlen, next_dlen));
  691. scsi_release_buffers(cmd);
  692. /*
  693. * This will goose the queue request function at the end, so we don't
  694. * need to worry about launching another command.
  695. */
  696. scsi_next_command(cmd);
  697. }
  698. /*
  699. * Function: scsi_io_completion()
  700. *
  701. * Purpose: Completion processing for block device I/O requests.
  702. *
  703. * Arguments: cmd - command that is finished.
  704. *
  705. * Lock status: Assumed that no lock is held upon entry.
  706. *
  707. * Returns: Nothing
  708. *
  709. * Notes: This function is matched in terms of capabilities to
  710. * the function that created the scatter-gather list.
  711. * In other words, if there are no bounce buffers
  712. * (the normal case for most drivers), we don't need
  713. * the logic to deal with cleaning up afterwards.
  714. *
  715. * We must do one of several things here:
  716. *
  717. * a) Call scsi_end_request. This will finish off the
  718. * specified number of sectors. If we are done, the
  719. * command block will be released, and the queue
  720. * function will be goosed. If we are not done, then
  721. * scsi_end_request will directly goose the queue.
  722. *
  723. * b) We can just use scsi_requeue_command() here. This would
  724. * be used if we just wanted to retry, for example.
  725. */
  726. void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
  727. {
  728. int result = cmd->result;
  729. int this_count = scsi_bufflen(cmd);
  730. struct request_queue *q = cmd->device->request_queue;
  731. struct request *req = cmd->request;
  732. int clear_errors = 1;
  733. struct scsi_sense_hdr sshdr;
  734. int sense_valid = 0;
  735. int sense_deferred = 0;
  736. if (result) {
  737. sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
  738. if (sense_valid)
  739. sense_deferred = scsi_sense_is_deferred(&sshdr);
  740. }
  741. if (blk_pc_request(req)) { /* SG_IO ioctl from block level */
  742. req->errors = result;
  743. if (result) {
  744. clear_errors = 0;
  745. if (sense_valid && req->sense) {
  746. /*
  747. * SG_IO wants current and deferred errors
  748. */
  749. int len = 8 + cmd->sense_buffer[7];
  750. if (len > SCSI_SENSE_BUFFERSIZE)
  751. len = SCSI_SENSE_BUFFERSIZE;
  752. memcpy(req->sense, cmd->sense_buffer, len);
  753. req->sense_len = len;
  754. }
  755. }
  756. if (scsi_bidi_cmnd(cmd)) {
  757. /* will also release_buffers */
  758. scsi_end_bidi_request(cmd);
  759. return;
  760. }
  761. req->data_len = scsi_get_resid(cmd);
  762. }
  763. BUG_ON(blk_bidi_rq(req)); /* bidi not support for !blk_pc_request yet */
  764. scsi_release_buffers(cmd);
  765. /*
  766. * Next deal with any sectors which we were able to correctly
  767. * handle.
  768. */
  769. SCSI_LOG_HLCOMPLETE(1, printk("%ld sectors total, "
  770. "%d bytes done.\n",
  771. req->nr_sectors, good_bytes));
  772. if (clear_errors)
  773. req->errors = 0;
  774. /* A number of bytes were successfully read. If there
  775. * are leftovers and there is some kind of error
  776. * (result != 0), retry the rest.
  777. */
  778. if (scsi_end_request(cmd, 0, good_bytes, result == 0) == NULL)
  779. return;
  780. /* good_bytes = 0, or (inclusive) there were leftovers and
  781. * result = 0, so scsi_end_request couldn't retry.
  782. */
  783. if (sense_valid && !sense_deferred) {
  784. switch (sshdr.sense_key) {
  785. case UNIT_ATTENTION:
  786. if (cmd->device->removable) {
  787. /* Detected disc change. Set a bit
  788. * and quietly refuse further access.
  789. */
  790. cmd->device->changed = 1;
  791. scsi_end_request(cmd, -EIO, this_count, 1);
  792. return;
  793. } else {
  794. /* Must have been a power glitch, or a
  795. * bus reset. Could not have been a
  796. * media change, so we just retry the
  797. * request and see what happens.
  798. */
  799. scsi_requeue_command(q, cmd);
  800. return;
  801. }
  802. break;
  803. case ILLEGAL_REQUEST:
  804. /* If we had an ILLEGAL REQUEST returned, then
  805. * we may have performed an unsupported
  806. * command. The only thing this should be
  807. * would be a ten byte read where only a six
  808. * byte read was supported. Also, on a system
  809. * where READ CAPACITY failed, we may have
  810. * read past the end of the disk.
  811. */
  812. if ((cmd->device->use_10_for_rw &&
  813. sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
  814. (cmd->cmnd[0] == READ_10 ||
  815. cmd->cmnd[0] == WRITE_10)) {
  816. cmd->device->use_10_for_rw = 0;
  817. /* This will cause a retry with a
  818. * 6-byte command.
  819. */
  820. scsi_requeue_command(q, cmd);
  821. return;
  822. } else {
  823. scsi_end_request(cmd, -EIO, this_count, 1);
  824. return;
  825. }
  826. break;
  827. case NOT_READY:
  828. /* If the device is in the process of becoming
  829. * ready, or has a temporary blockage, retry.
  830. */
  831. if (sshdr.asc == 0x04) {
  832. switch (sshdr.ascq) {
  833. case 0x01: /* becoming ready */
  834. case 0x04: /* format in progress */
  835. case 0x05: /* rebuild in progress */
  836. case 0x06: /* recalculation in progress */
  837. case 0x07: /* operation in progress */
  838. case 0x08: /* Long write in progress */
  839. case 0x09: /* self test in progress */
  840. scsi_requeue_command(q, cmd);
  841. return;
  842. default:
  843. break;
  844. }
  845. }
  846. if (!(req->cmd_flags & REQ_QUIET))
  847. scsi_cmd_print_sense_hdr(cmd,
  848. "Device not ready",
  849. &sshdr);
  850. scsi_end_request(cmd, -EIO, this_count, 1);
  851. return;
  852. case VOLUME_OVERFLOW:
  853. if (!(req->cmd_flags & REQ_QUIET)) {
  854. scmd_printk(KERN_INFO, cmd,
  855. "Volume overflow, CDB: ");
  856. __scsi_print_command(cmd->cmnd);
  857. scsi_print_sense("", cmd);
  858. }
  859. /* See SSC3rXX or current. */
  860. scsi_end_request(cmd, -EIO, this_count, 1);
  861. return;
  862. default:
  863. break;
  864. }
  865. }
  866. if (host_byte(result) == DID_RESET) {
  867. /* Third party bus reset or reset for error recovery
  868. * reasons. Just retry the request and see what
  869. * happens.
  870. */
  871. scsi_requeue_command(q, cmd);
  872. return;
  873. }
  874. if (result) {
  875. if (!(req->cmd_flags & REQ_QUIET)) {
  876. scsi_print_result(cmd);
  877. if (driver_byte(result) & DRIVER_SENSE)
  878. scsi_print_sense("", cmd);
  879. }
  880. }
  881. scsi_end_request(cmd, -EIO, this_count, !result);
  882. }
  883. static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb,
  884. gfp_t gfp_mask)
  885. {
  886. int count;
  887. /*
  888. * If sg table allocation fails, requeue request later.
  889. */
  890. if (unlikely(scsi_alloc_sgtable(sdb, req->nr_phys_segments,
  891. gfp_mask))) {
  892. return BLKPREP_DEFER;
  893. }
  894. req->buffer = NULL;
  895. if (blk_pc_request(req))
  896. sdb->length = req->data_len;
  897. else
  898. sdb->length = req->nr_sectors << 9;
  899. /*
  900. * Next, walk the list, and fill in the addresses and sizes of
  901. * each segment.
  902. */
  903. count = blk_rq_map_sg(req->q, req, sdb->table.sgl);
  904. BUG_ON(count > sdb->table.nents);
  905. sdb->table.nents = count;
  906. return BLKPREP_OK;
  907. }
  908. /*
  909. * Function: scsi_init_io()
  910. *
  911. * Purpose: SCSI I/O initialize function.
  912. *
  913. * Arguments: cmd - Command descriptor we wish to initialize
  914. *
  915. * Returns: 0 on success
  916. * BLKPREP_DEFER if the failure is retryable
  917. * BLKPREP_KILL if the failure is fatal
  918. */
  919. int scsi_init_io(struct scsi_cmnd *cmd, gfp_t gfp_mask)
  920. {
  921. int error = scsi_init_sgtable(cmd->request, &cmd->sdb, gfp_mask);
  922. if (error)
  923. goto err_exit;
  924. if (blk_bidi_rq(cmd->request)) {
  925. struct scsi_data_buffer *bidi_sdb = kmem_cache_zalloc(
  926. scsi_bidi_sdb_cache, GFP_ATOMIC);
  927. if (!bidi_sdb) {
  928. error = BLKPREP_DEFER;
  929. goto err_exit;
  930. }
  931. cmd->request->next_rq->special = bidi_sdb;
  932. error = scsi_init_sgtable(cmd->request->next_rq, bidi_sdb,
  933. GFP_ATOMIC);
  934. if (error)
  935. goto err_exit;
  936. }
  937. return BLKPREP_OK ;
  938. err_exit:
  939. scsi_release_buffers(cmd);
  940. if (error == BLKPREP_KILL)
  941. scsi_put_command(cmd);
  942. else /* BLKPREP_DEFER */
  943. scsi_unprep_request(cmd->request);
  944. return error;
  945. }
  946. EXPORT_SYMBOL(scsi_init_io);
  947. static struct scsi_cmnd *scsi_get_cmd_from_req(struct scsi_device *sdev,
  948. struct request *req)
  949. {
  950. struct scsi_cmnd *cmd;
  951. if (!req->special) {
  952. cmd = scsi_get_command(sdev, GFP_ATOMIC);
  953. if (unlikely(!cmd))
  954. return NULL;
  955. req->special = cmd;
  956. } else {
  957. cmd = req->special;
  958. }
  959. /* pull a tag out of the request if we have one */
  960. cmd->tag = req->tag;
  961. cmd->request = req;
  962. return cmd;
  963. }
  964. int scsi_setup_blk_pc_cmnd(struct scsi_device *sdev, struct request *req)
  965. {
  966. struct scsi_cmnd *cmd;
  967. int ret = scsi_prep_state_check(sdev, req);
  968. if (ret != BLKPREP_OK)
  969. return ret;
  970. cmd = scsi_get_cmd_from_req(sdev, req);
  971. if (unlikely(!cmd))
  972. return BLKPREP_DEFER;
  973. /*
  974. * BLOCK_PC requests may transfer data, in which case they must
  975. * a bio attached to them. Or they might contain a SCSI command
  976. * that does not transfer data, in which case they may optionally
  977. * submit a request without an attached bio.
  978. */
  979. if (req->bio) {
  980. int ret;
  981. BUG_ON(!req->nr_phys_segments);
  982. ret = scsi_init_io(cmd, GFP_ATOMIC);
  983. if (unlikely(ret))
  984. return ret;
  985. } else {
  986. BUG_ON(req->data_len);
  987. BUG_ON(req->data);
  988. memset(&cmd->sdb, 0, sizeof(cmd->sdb));
  989. req->buffer = NULL;
  990. }
  991. BUILD_BUG_ON(sizeof(req->cmd) > sizeof(cmd->cmnd));
  992. memcpy(cmd->cmnd, req->cmd, sizeof(cmd->cmnd));
  993. cmd->cmd_len = req->cmd_len;
  994. if (!req->data_len)
  995. cmd->sc_data_direction = DMA_NONE;
  996. else if (rq_data_dir(req) == WRITE)
  997. cmd->sc_data_direction = DMA_TO_DEVICE;
  998. else
  999. cmd->sc_data_direction = DMA_FROM_DEVICE;
  1000. cmd->transfersize = req->data_len;
  1001. cmd->allowed = req->retries;
  1002. cmd->timeout_per_command = req->timeout;
  1003. return BLKPREP_OK;
  1004. }
  1005. EXPORT_SYMBOL(scsi_setup_blk_pc_cmnd);
  1006. /*
  1007. * Setup a REQ_TYPE_FS command. These are simple read/write request
  1008. * from filesystems that still need to be translated to SCSI CDBs from
  1009. * the ULD.
  1010. */
  1011. int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req)
  1012. {
  1013. struct scsi_cmnd *cmd;
  1014. int ret = scsi_prep_state_check(sdev, req);
  1015. if (ret != BLKPREP_OK)
  1016. return ret;
  1017. /*
  1018. * Filesystem requests must transfer data.
  1019. */
  1020. BUG_ON(!req->nr_phys_segments);
  1021. cmd = scsi_get_cmd_from_req(sdev, req);
  1022. if (unlikely(!cmd))
  1023. return BLKPREP_DEFER;
  1024. return scsi_init_io(cmd, GFP_ATOMIC);
  1025. }
  1026. EXPORT_SYMBOL(scsi_setup_fs_cmnd);
  1027. int scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
  1028. {
  1029. int ret = BLKPREP_OK;
  1030. /*
  1031. * If the device is not in running state we will reject some
  1032. * or all commands.
  1033. */
  1034. if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
  1035. switch (sdev->sdev_state) {
  1036. case SDEV_OFFLINE:
  1037. /*
  1038. * If the device is offline we refuse to process any
  1039. * commands. The device must be brought online
  1040. * before trying any recovery commands.
  1041. */
  1042. sdev_printk(KERN_ERR, sdev,
  1043. "rejecting I/O to offline device\n");
  1044. ret = BLKPREP_KILL;
  1045. break;
  1046. case SDEV_DEL:
  1047. /*
  1048. * If the device is fully deleted, we refuse to
  1049. * process any commands as well.
  1050. */
  1051. sdev_printk(KERN_ERR, sdev,
  1052. "rejecting I/O to dead device\n");
  1053. ret = BLKPREP_KILL;
  1054. break;
  1055. case SDEV_QUIESCE:
  1056. case SDEV_BLOCK:
  1057. /*
  1058. * If the devices is blocked we defer normal commands.
  1059. */
  1060. if (!(req->cmd_flags & REQ_PREEMPT))
  1061. ret = BLKPREP_DEFER;
  1062. break;
  1063. default:
  1064. /*
  1065. * For any other not fully online state we only allow
  1066. * special commands. In particular any user initiated
  1067. * command is not allowed.
  1068. */
  1069. if (!(req->cmd_flags & REQ_PREEMPT))
  1070. ret = BLKPREP_KILL;
  1071. break;
  1072. }
  1073. }
  1074. return ret;
  1075. }
  1076. EXPORT_SYMBOL(scsi_prep_state_check);
  1077. int scsi_prep_return(struct request_queue *q, struct request *req, int ret)
  1078. {
  1079. struct scsi_device *sdev = q->queuedata;
  1080. switch (ret) {
  1081. case BLKPREP_KILL:
  1082. req->errors = DID_NO_CONNECT << 16;
  1083. /* release the command and kill it */
  1084. if (req->special) {
  1085. struct scsi_cmnd *cmd = req->special;
  1086. scsi_release_buffers(cmd);
  1087. scsi_put_command(cmd);
  1088. req->special = NULL;
  1089. }
  1090. break;
  1091. case BLKPREP_DEFER:
  1092. /*
  1093. * If we defer, the elv_next_request() returns NULL, but the
  1094. * queue must be restarted, so we plug here if no returning
  1095. * command will automatically do that.
  1096. */
  1097. if (sdev->device_busy == 0)
  1098. blk_plug_device(q);
  1099. break;
  1100. default:
  1101. req->cmd_flags |= REQ_DONTPREP;
  1102. }
  1103. return ret;
  1104. }
  1105. EXPORT_SYMBOL(scsi_prep_return);
  1106. int scsi_prep_fn(struct request_queue *q, struct request *req)
  1107. {
  1108. struct scsi_device *sdev = q->queuedata;
  1109. int ret = BLKPREP_KILL;
  1110. if (req->cmd_type == REQ_TYPE_BLOCK_PC)
  1111. ret = scsi_setup_blk_pc_cmnd(sdev, req);
  1112. return scsi_prep_return(q, req, ret);
  1113. }
  1114. /*
  1115. * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
  1116. * return 0.
  1117. *
  1118. * Called with the queue_lock held.
  1119. */
  1120. static inline int scsi_dev_queue_ready(struct request_queue *q,
  1121. struct scsi_device *sdev)
  1122. {
  1123. if (sdev->device_busy >= sdev->queue_depth)
  1124. return 0;
  1125. if (sdev->device_busy == 0 && sdev->device_blocked) {
  1126. /*
  1127. * unblock after device_blocked iterates to zero
  1128. */
  1129. if (--sdev->device_blocked == 0) {
  1130. SCSI_LOG_MLQUEUE(3,
  1131. sdev_printk(KERN_INFO, sdev,
  1132. "unblocking device at zero depth\n"));
  1133. } else {
  1134. blk_plug_device(q);
  1135. return 0;
  1136. }
  1137. }
  1138. if (sdev->device_blocked)
  1139. return 0;
  1140. return 1;
  1141. }
  1142. /*
  1143. * scsi_host_queue_ready: if we can send requests to shost, return 1 else
  1144. * return 0. We must end up running the queue again whenever 0 is
  1145. * returned, else IO can hang.
  1146. *
  1147. * Called with host_lock held.
  1148. */
  1149. static inline int scsi_host_queue_ready(struct request_queue *q,
  1150. struct Scsi_Host *shost,
  1151. struct scsi_device *sdev)
  1152. {
  1153. if (scsi_host_in_recovery(shost))
  1154. return 0;
  1155. if (shost->host_busy == 0 && shost->host_blocked) {
  1156. /*
  1157. * unblock after host_blocked iterates to zero
  1158. */
  1159. if (--shost->host_blocked == 0) {
  1160. SCSI_LOG_MLQUEUE(3,
  1161. printk("scsi%d unblocking host at zero depth\n",
  1162. shost->host_no));
  1163. } else {
  1164. blk_plug_device(q);
  1165. return 0;
  1166. }
  1167. }
  1168. if ((shost->can_queue > 0 && shost->host_busy >= shost->can_queue) ||
  1169. shost->host_blocked || shost->host_self_blocked) {
  1170. if (list_empty(&sdev->starved_entry))
  1171. list_add_tail(&sdev->starved_entry, &shost->starved_list);
  1172. return 0;
  1173. }
  1174. /* We're OK to process the command, so we can't be starved */
  1175. if (!list_empty(&sdev->starved_entry))
  1176. list_del_init(&sdev->starved_entry);
  1177. return 1;
  1178. }
  1179. /*
  1180. * Kill a request for a dead device
  1181. */
  1182. static void scsi_kill_request(struct request *req, struct request_queue *q)
  1183. {
  1184. struct scsi_cmnd *cmd = req->special;
  1185. struct scsi_device *sdev = cmd->device;
  1186. struct Scsi_Host *shost = sdev->host;
  1187. blkdev_dequeue_request(req);
  1188. if (unlikely(cmd == NULL)) {
  1189. printk(KERN_CRIT "impossible request in %s.\n",
  1190. __FUNCTION__);
  1191. BUG();
  1192. }
  1193. scsi_init_cmd_errh(cmd);
  1194. cmd->result = DID_NO_CONNECT << 16;
  1195. atomic_inc(&cmd->device->iorequest_cnt);
  1196. /*
  1197. * SCSI request completion path will do scsi_device_unbusy(),
  1198. * bump busy counts. To bump the counters, we need to dance
  1199. * with the locks as normal issue path does.
  1200. */
  1201. sdev->device_busy++;
  1202. spin_unlock(sdev->request_queue->queue_lock);
  1203. spin_lock(shost->host_lock);
  1204. shost->host_busy++;
  1205. spin_unlock(shost->host_lock);
  1206. spin_lock(sdev->request_queue->queue_lock);
  1207. __scsi_done(cmd);
  1208. }
  1209. static void scsi_softirq_done(struct request *rq)
  1210. {
  1211. struct scsi_cmnd *cmd = rq->completion_data;
  1212. unsigned long wait_for = (cmd->allowed + 1) * cmd->timeout_per_command;
  1213. int disposition;
  1214. INIT_LIST_HEAD(&cmd->eh_entry);
  1215. disposition = scsi_decide_disposition(cmd);
  1216. if (disposition != SUCCESS &&
  1217. time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
  1218. sdev_printk(KERN_ERR, cmd->device,
  1219. "timing out command, waited %lus\n",
  1220. wait_for/HZ);
  1221. disposition = SUCCESS;
  1222. }
  1223. scsi_log_completion(cmd, disposition);
  1224. switch (disposition) {
  1225. case SUCCESS:
  1226. scsi_finish_command(cmd);
  1227. break;
  1228. case NEEDS_RETRY:
  1229. scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
  1230. break;
  1231. case ADD_TO_MLQUEUE:
  1232. scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
  1233. break;
  1234. default:
  1235. if (!scsi_eh_scmd_add(cmd, 0))
  1236. scsi_finish_command(cmd);
  1237. }
  1238. }
  1239. /*
  1240. * Function: scsi_request_fn()
  1241. *
  1242. * Purpose: Main strategy routine for SCSI.
  1243. *
  1244. * Arguments: q - Pointer to actual queue.
  1245. *
  1246. * Returns: Nothing
  1247. *
  1248. * Lock status: IO request lock assumed to be held when called.
  1249. */
  1250. static void scsi_request_fn(struct request_queue *q)
  1251. {
  1252. struct scsi_device *sdev = q->queuedata;
  1253. struct Scsi_Host *shost;
  1254. struct scsi_cmnd *cmd;
  1255. struct request *req;
  1256. if (!sdev) {
  1257. printk("scsi: killing requests for dead queue\n");
  1258. while ((req = elv_next_request(q)) != NULL)
  1259. scsi_kill_request(req, q);
  1260. return;
  1261. }
  1262. if(!get_device(&sdev->sdev_gendev))
  1263. /* We must be tearing the block queue down already */
  1264. return;
  1265. /*
  1266. * To start with, we keep looping until the queue is empty, or until
  1267. * the host is no longer able to accept any more requests.
  1268. */
  1269. shost = sdev->host;
  1270. while (!blk_queue_plugged(q)) {
  1271. int rtn;
  1272. /*
  1273. * get next queueable request. We do this early to make sure
  1274. * that the request is fully prepared even if we cannot
  1275. * accept it.
  1276. */
  1277. req = elv_next_request(q);
  1278. if (!req || !scsi_dev_queue_ready(q, sdev))
  1279. break;
  1280. if (unlikely(!scsi_device_online(sdev))) {
  1281. sdev_printk(KERN_ERR, sdev,
  1282. "rejecting I/O to offline device\n");
  1283. scsi_kill_request(req, q);
  1284. continue;
  1285. }
  1286. /*
  1287. * Remove the request from the request list.
  1288. */
  1289. if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
  1290. blkdev_dequeue_request(req);
  1291. sdev->device_busy++;
  1292. spin_unlock(q->queue_lock);
  1293. cmd = req->special;
  1294. if (unlikely(cmd == NULL)) {
  1295. printk(KERN_CRIT "impossible request in %s.\n"
  1296. "please mail a stack trace to "
  1297. "linux-scsi@vger.kernel.org\n",
  1298. __FUNCTION__);
  1299. blk_dump_rq_flags(req, "foo");
  1300. BUG();
  1301. }
  1302. spin_lock(shost->host_lock);
  1303. if (!scsi_host_queue_ready(q, shost, sdev))
  1304. goto not_ready;
  1305. if (scsi_target(sdev)->single_lun) {
  1306. if (scsi_target(sdev)->starget_sdev_user &&
  1307. scsi_target(sdev)->starget_sdev_user != sdev)
  1308. goto not_ready;
  1309. scsi_target(sdev)->starget_sdev_user = sdev;
  1310. }
  1311. shost->host_busy++;
  1312. /*
  1313. * XXX(hch): This is rather suboptimal, scsi_dispatch_cmd will
  1314. * take the lock again.
  1315. */
  1316. spin_unlock_irq(shost->host_lock);
  1317. /*
  1318. * Finally, initialize any error handling parameters, and set up
  1319. * the timers for timeouts.
  1320. */
  1321. scsi_init_cmd_errh(cmd);
  1322. /*
  1323. * Dispatch the command to the low-level driver.
  1324. */
  1325. rtn = scsi_dispatch_cmd(cmd);
  1326. spin_lock_irq(q->queue_lock);
  1327. if(rtn) {
  1328. /* we're refusing the command; because of
  1329. * the way locks get dropped, we need to
  1330. * check here if plugging is required */
  1331. if(sdev->device_busy == 0)
  1332. blk_plug_device(q);
  1333. break;
  1334. }
  1335. }
  1336. goto out;
  1337. not_ready:
  1338. spin_unlock_irq(shost->host_lock);
  1339. /*
  1340. * lock q, handle tag, requeue req, and decrement device_busy. We
  1341. * must return with queue_lock held.
  1342. *
  1343. * Decrementing device_busy without checking it is OK, as all such
  1344. * cases (host limits or settings) should run the queue at some
  1345. * later time.
  1346. */
  1347. spin_lock_irq(q->queue_lock);
  1348. blk_requeue_request(q, req);
  1349. sdev->device_busy--;
  1350. if(sdev->device_busy == 0)
  1351. blk_plug_device(q);
  1352. out:
  1353. /* must be careful here...if we trigger the ->remove() function
  1354. * we cannot be holding the q lock */
  1355. spin_unlock_irq(q->queue_lock);
  1356. put_device(&sdev->sdev_gendev);
  1357. spin_lock_irq(q->queue_lock);
  1358. }
  1359. u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
  1360. {
  1361. struct device *host_dev;
  1362. u64 bounce_limit = 0xffffffff;
  1363. if (shost->unchecked_isa_dma)
  1364. return BLK_BOUNCE_ISA;
  1365. /*
  1366. * Platforms with virtual-DMA translation
  1367. * hardware have no practical limit.
  1368. */
  1369. if (!PCI_DMA_BUS_IS_PHYS)
  1370. return BLK_BOUNCE_ANY;
  1371. host_dev = scsi_get_device(shost);
  1372. if (host_dev && host_dev->dma_mask)
  1373. bounce_limit = *host_dev->dma_mask;
  1374. return bounce_limit;
  1375. }
  1376. EXPORT_SYMBOL(scsi_calculate_bounce_limit);
  1377. struct request_queue *__scsi_alloc_queue(struct Scsi_Host *shost,
  1378. request_fn_proc *request_fn)
  1379. {
  1380. struct request_queue *q;
  1381. q = blk_init_queue(request_fn, NULL);
  1382. if (!q)
  1383. return NULL;
  1384. /*
  1385. * this limit is imposed by hardware restrictions
  1386. */
  1387. blk_queue_max_hw_segments(q, shost->sg_tablesize);
  1388. blk_queue_max_phys_segments(q, SCSI_MAX_SG_CHAIN_SEGMENTS);
  1389. blk_queue_max_sectors(q, shost->max_sectors);
  1390. blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
  1391. blk_queue_segment_boundary(q, shost->dma_boundary);
  1392. if (!shost->use_clustering)
  1393. clear_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
  1394. /*
  1395. * set a reasonable default alignment on word boundaries: the
  1396. * host and device may alter it using
  1397. * blk_queue_update_dma_alignment() later.
  1398. */
  1399. blk_queue_dma_alignment(q, 0x03);
  1400. return q;
  1401. }
  1402. EXPORT_SYMBOL(__scsi_alloc_queue);
  1403. struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
  1404. {
  1405. struct request_queue *q;
  1406. q = __scsi_alloc_queue(sdev->host, scsi_request_fn);
  1407. if (!q)
  1408. return NULL;
  1409. blk_queue_prep_rq(q, scsi_prep_fn);
  1410. blk_queue_softirq_done(q, scsi_softirq_done);
  1411. return q;
  1412. }
  1413. void scsi_free_queue(struct request_queue *q)
  1414. {
  1415. blk_cleanup_queue(q);
  1416. }
  1417. /*
  1418. * Function: scsi_block_requests()
  1419. *
  1420. * Purpose: Utility function used by low-level drivers to prevent further
  1421. * commands from being queued to the device.
  1422. *
  1423. * Arguments: shost - Host in question
  1424. *
  1425. * Returns: Nothing
  1426. *
  1427. * Lock status: No locks are assumed held.
  1428. *
  1429. * Notes: There is no timer nor any other means by which the requests
  1430. * get unblocked other than the low-level driver calling
  1431. * scsi_unblock_requests().
  1432. */
  1433. void scsi_block_requests(struct Scsi_Host *shost)
  1434. {
  1435. shost->host_self_blocked = 1;
  1436. }
  1437. EXPORT_SYMBOL(scsi_block_requests);
  1438. /*
  1439. * Function: scsi_unblock_requests()
  1440. *
  1441. * Purpose: Utility function used by low-level drivers to allow further
  1442. * commands from being queued to the device.
  1443. *
  1444. * Arguments: shost - Host in question
  1445. *
  1446. * Returns: Nothing
  1447. *
  1448. * Lock status: No locks are assumed held.
  1449. *
  1450. * Notes: There is no timer nor any other means by which the requests
  1451. * get unblocked other than the low-level driver calling
  1452. * scsi_unblock_requests().
  1453. *
  1454. * This is done as an API function so that changes to the
  1455. * internals of the scsi mid-layer won't require wholesale
  1456. * changes to drivers that use this feature.
  1457. */
  1458. void scsi_unblock_requests(struct Scsi_Host *shost)
  1459. {
  1460. shost->host_self_blocked = 0;
  1461. scsi_run_host_queues(shost);
  1462. }
  1463. EXPORT_SYMBOL(scsi_unblock_requests);
  1464. int __init scsi_init_queue(void)
  1465. {
  1466. int i;
  1467. scsi_io_context_cache = kmem_cache_create("scsi_io_context",
  1468. sizeof(struct scsi_io_context),
  1469. 0, 0, NULL);
  1470. if (!scsi_io_context_cache) {
  1471. printk(KERN_ERR "SCSI: can't init scsi io context cache\n");
  1472. return -ENOMEM;
  1473. }
  1474. scsi_bidi_sdb_cache = kmem_cache_create("scsi_bidi_sdb",
  1475. sizeof(struct scsi_data_buffer),
  1476. 0, 0, NULL);
  1477. if (!scsi_bidi_sdb_cache) {
  1478. printk(KERN_ERR "SCSI: can't init scsi bidi sdb cache\n");
  1479. return -ENOMEM;
  1480. }
  1481. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1482. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1483. int size = sgp->size * sizeof(struct scatterlist);
  1484. sgp->slab = kmem_cache_create(sgp->name, size, 0,
  1485. SLAB_HWCACHE_ALIGN, NULL);
  1486. if (!sgp->slab) {
  1487. printk(KERN_ERR "SCSI: can't init sg slab %s\n",
  1488. sgp->name);
  1489. }
  1490. sgp->pool = mempool_create_slab_pool(SG_MEMPOOL_SIZE,
  1491. sgp->slab);
  1492. if (!sgp->pool) {
  1493. printk(KERN_ERR "SCSI: can't init sg mempool %s\n",
  1494. sgp->name);
  1495. }
  1496. }
  1497. return 0;
  1498. }
  1499. void scsi_exit_queue(void)
  1500. {
  1501. int i;
  1502. kmem_cache_destroy(scsi_io_context_cache);
  1503. kmem_cache_destroy(scsi_bidi_sdb_cache);
  1504. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1505. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1506. mempool_destroy(sgp->pool);
  1507. kmem_cache_destroy(sgp->slab);
  1508. }
  1509. }
  1510. /**
  1511. * scsi_mode_select - issue a mode select
  1512. * @sdev: SCSI device to be queried
  1513. * @pf: Page format bit (1 == standard, 0 == vendor specific)
  1514. * @sp: Save page bit (0 == don't save, 1 == save)
  1515. * @modepage: mode page being requested
  1516. * @buffer: request buffer (may not be smaller than eight bytes)
  1517. * @len: length of request buffer.
  1518. * @timeout: command timeout
  1519. * @retries: number of retries before failing
  1520. * @data: returns a structure abstracting the mode header data
  1521. * @sshdr: place to put sense data (or NULL if no sense to be collected).
  1522. * must be SCSI_SENSE_BUFFERSIZE big.
  1523. *
  1524. * Returns zero if successful; negative error number or scsi
  1525. * status on error
  1526. *
  1527. */
  1528. int
  1529. scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
  1530. unsigned char *buffer, int len, int timeout, int retries,
  1531. struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
  1532. {
  1533. unsigned char cmd[10];
  1534. unsigned char *real_buffer;
  1535. int ret;
  1536. memset(cmd, 0, sizeof(cmd));
  1537. cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
  1538. if (sdev->use_10_for_ms) {
  1539. if (len > 65535)
  1540. return -EINVAL;
  1541. real_buffer = kmalloc(8 + len, GFP_KERNEL);
  1542. if (!real_buffer)
  1543. return -ENOMEM;
  1544. memcpy(real_buffer + 8, buffer, len);
  1545. len += 8;
  1546. real_buffer[0] = 0;
  1547. real_buffer[1] = 0;
  1548. real_buffer[2] = data->medium_type;
  1549. real_buffer[3] = data->device_specific;
  1550. real_buffer[4] = data->longlba ? 0x01 : 0;
  1551. real_buffer[5] = 0;
  1552. real_buffer[6] = data->block_descriptor_length >> 8;
  1553. real_buffer[7] = data->block_descriptor_length;
  1554. cmd[0] = MODE_SELECT_10;
  1555. cmd[7] = len >> 8;
  1556. cmd[8] = len;
  1557. } else {
  1558. if (len > 255 || data->block_descriptor_length > 255 ||
  1559. data->longlba)
  1560. return -EINVAL;
  1561. real_buffer = kmalloc(4 + len, GFP_KERNEL);
  1562. if (!real_buffer)
  1563. return -ENOMEM;
  1564. memcpy(real_buffer + 4, buffer, len);
  1565. len += 4;
  1566. real_buffer[0] = 0;
  1567. real_buffer[1] = data->medium_type;
  1568. real_buffer[2] = data->device_specific;
  1569. real_buffer[3] = data->block_descriptor_length;
  1570. cmd[0] = MODE_SELECT;
  1571. cmd[4] = len;
  1572. }
  1573. ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
  1574. sshdr, timeout, retries);
  1575. kfree(real_buffer);
  1576. return ret;
  1577. }
  1578. EXPORT_SYMBOL_GPL(scsi_mode_select);
  1579. /**
  1580. * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
  1581. * @sdev: SCSI device to be queried
  1582. * @dbd: set if mode sense will allow block descriptors to be returned
  1583. * @modepage: mode page being requested
  1584. * @buffer: request buffer (may not be smaller than eight bytes)
  1585. * @len: length of request buffer.
  1586. * @timeout: command timeout
  1587. * @retries: number of retries before failing
  1588. * @data: returns a structure abstracting the mode header data
  1589. * @sshdr: place to put sense data (or NULL if no sense to be collected).
  1590. * must be SCSI_SENSE_BUFFERSIZE big.
  1591. *
  1592. * Returns zero if unsuccessful, or the header offset (either 4
  1593. * or 8 depending on whether a six or ten byte command was
  1594. * issued) if successful.
  1595. */
  1596. int
  1597. scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
  1598. unsigned char *buffer, int len, int timeout, int retries,
  1599. struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
  1600. {
  1601. unsigned char cmd[12];
  1602. int use_10_for_ms;
  1603. int header_length;
  1604. int result;
  1605. struct scsi_sense_hdr my_sshdr;
  1606. memset(data, 0, sizeof(*data));
  1607. memset(&cmd[0], 0, 12);
  1608. cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
  1609. cmd[2] = modepage;
  1610. /* caller might not be interested in sense, but we need it */
  1611. if (!sshdr)
  1612. sshdr = &my_sshdr;
  1613. retry:
  1614. use_10_for_ms = sdev->use_10_for_ms;
  1615. if (use_10_for_ms) {
  1616. if (len < 8)
  1617. len = 8;
  1618. cmd[0] = MODE_SENSE_10;
  1619. cmd[8] = len;
  1620. header_length = 8;
  1621. } else {
  1622. if (len < 4)
  1623. len = 4;
  1624. cmd[0] = MODE_SENSE;
  1625. cmd[4] = len;
  1626. header_length = 4;
  1627. }
  1628. memset(buffer, 0, len);
  1629. result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
  1630. sshdr, timeout, retries);
  1631. /* This code looks awful: what it's doing is making sure an
  1632. * ILLEGAL REQUEST sense return identifies the actual command
  1633. * byte as the problem. MODE_SENSE commands can return
  1634. * ILLEGAL REQUEST if the code page isn't supported */
  1635. if (use_10_for_ms && !scsi_status_is_good(result) &&
  1636. (driver_byte(result) & DRIVER_SENSE)) {
  1637. if (scsi_sense_valid(sshdr)) {
  1638. if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
  1639. (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
  1640. /*
  1641. * Invalid command operation code
  1642. */
  1643. sdev->use_10_for_ms = 0;
  1644. goto retry;
  1645. }
  1646. }
  1647. }
  1648. if(scsi_status_is_good(result)) {
  1649. if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
  1650. (modepage == 6 || modepage == 8))) {
  1651. /* Initio breakage? */
  1652. header_length = 0;
  1653. data->length = 13;
  1654. data->medium_type = 0;
  1655. data->device_specific = 0;
  1656. data->longlba = 0;
  1657. data->block_descriptor_length = 0;
  1658. } else if(use_10_for_ms) {
  1659. data->length = buffer[0]*256 + buffer[1] + 2;
  1660. data->medium_type = buffer[2];
  1661. data->device_specific = buffer[3];
  1662. data->longlba = buffer[4] & 0x01;
  1663. data->block_descriptor_length = buffer[6]*256
  1664. + buffer[7];
  1665. } else {
  1666. data->length = buffer[0] + 1;
  1667. data->medium_type = buffer[1];
  1668. data->device_specific = buffer[2];
  1669. data->block_descriptor_length = buffer[3];
  1670. }
  1671. data->header_length = header_length;
  1672. }
  1673. return result;
  1674. }
  1675. EXPORT_SYMBOL(scsi_mode_sense);
  1676. /**
  1677. * scsi_test_unit_ready - test if unit is ready
  1678. * @sdev: scsi device to change the state of.
  1679. * @timeout: command timeout
  1680. * @retries: number of retries before failing
  1681. * @sshdr_external: Optional pointer to struct scsi_sense_hdr for
  1682. * returning sense. Make sure that this is cleared before passing
  1683. * in.
  1684. *
  1685. * Returns zero if unsuccessful or an error if TUR failed. For
  1686. * removable media, a return of NOT_READY or UNIT_ATTENTION is
  1687. * translated to success, with the ->changed flag updated.
  1688. **/
  1689. int
  1690. scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
  1691. struct scsi_sense_hdr *sshdr_external)
  1692. {
  1693. char cmd[] = {
  1694. TEST_UNIT_READY, 0, 0, 0, 0, 0,
  1695. };
  1696. struct scsi_sense_hdr *sshdr;
  1697. int result;
  1698. if (!sshdr_external)
  1699. sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
  1700. else
  1701. sshdr = sshdr_external;
  1702. /* try to eat the UNIT_ATTENTION if there are enough retries */
  1703. do {
  1704. result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
  1705. timeout, retries);
  1706. } while ((driver_byte(result) & DRIVER_SENSE) &&
  1707. sshdr && sshdr->sense_key == UNIT_ATTENTION &&
  1708. --retries);
  1709. if (!sshdr)
  1710. /* could not allocate sense buffer, so can't process it */
  1711. return result;
  1712. if ((driver_byte(result) & DRIVER_SENSE) && sdev->removable) {
  1713. if ((scsi_sense_valid(sshdr)) &&
  1714. ((sshdr->sense_key == UNIT_ATTENTION) ||
  1715. (sshdr->sense_key == NOT_READY))) {
  1716. sdev->changed = 1;
  1717. result = 0;
  1718. }
  1719. }
  1720. if (!sshdr_external)
  1721. kfree(sshdr);
  1722. return result;
  1723. }
  1724. EXPORT_SYMBOL(scsi_test_unit_ready);
  1725. /**
  1726. * scsi_device_set_state - Take the given device through the device state model.
  1727. * @sdev: scsi device to change the state of.
  1728. * @state: state to change to.
  1729. *
  1730. * Returns zero if unsuccessful or an error if the requested
  1731. * transition is illegal.
  1732. */
  1733. int
  1734. scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
  1735. {
  1736. enum scsi_device_state oldstate = sdev->sdev_state;
  1737. if (state == oldstate)
  1738. return 0;
  1739. switch (state) {
  1740. case SDEV_CREATED:
  1741. /* There are no legal states that come back to
  1742. * created. This is the manually initialised start
  1743. * state */
  1744. goto illegal;
  1745. case SDEV_RUNNING:
  1746. switch (oldstate) {
  1747. case SDEV_CREATED:
  1748. case SDEV_OFFLINE:
  1749. case SDEV_QUIESCE:
  1750. case SDEV_BLOCK:
  1751. break;
  1752. default:
  1753. goto illegal;
  1754. }
  1755. break;
  1756. case SDEV_QUIESCE:
  1757. switch (oldstate) {
  1758. case SDEV_RUNNING:
  1759. case SDEV_OFFLINE:
  1760. break;
  1761. default:
  1762. goto illegal;
  1763. }
  1764. break;
  1765. case SDEV_OFFLINE:
  1766. switch (oldstate) {
  1767. case SDEV_CREATED:
  1768. case SDEV_RUNNING:
  1769. case SDEV_QUIESCE:
  1770. case SDEV_BLOCK:
  1771. break;
  1772. default:
  1773. goto illegal;
  1774. }
  1775. break;
  1776. case SDEV_BLOCK:
  1777. switch (oldstate) {
  1778. case SDEV_CREATED:
  1779. case SDEV_RUNNING:
  1780. break;
  1781. default:
  1782. goto illegal;
  1783. }
  1784. break;
  1785. case SDEV_CANCEL:
  1786. switch (oldstate) {
  1787. case SDEV_CREATED:
  1788. case SDEV_RUNNING:
  1789. case SDEV_QUIESCE:
  1790. case SDEV_OFFLINE:
  1791. case SDEV_BLOCK:
  1792. break;
  1793. default:
  1794. goto illegal;
  1795. }
  1796. break;
  1797. case SDEV_DEL:
  1798. switch (oldstate) {
  1799. case SDEV_CREATED:
  1800. case SDEV_RUNNING:
  1801. case SDEV_OFFLINE:
  1802. case SDEV_CANCEL:
  1803. break;
  1804. default:
  1805. goto illegal;
  1806. }
  1807. break;
  1808. }
  1809. sdev->sdev_state = state;
  1810. return 0;
  1811. illegal:
  1812. SCSI_LOG_ERROR_RECOVERY(1,
  1813. sdev_printk(KERN_ERR, sdev,
  1814. "Illegal state transition %s->%s\n",
  1815. scsi_device_state_name(oldstate),
  1816. scsi_device_state_name(state))
  1817. );
  1818. return -EINVAL;
  1819. }
  1820. EXPORT_SYMBOL(scsi_device_set_state);
  1821. /**
  1822. * sdev_evt_emit - emit a single SCSI device uevent
  1823. * @sdev: associated SCSI device
  1824. * @evt: event to emit
  1825. *
  1826. * Send a single uevent (scsi_event) to the associated scsi_device.
  1827. */
  1828. static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
  1829. {
  1830. int idx = 0;
  1831. char *envp[3];
  1832. switch (evt->evt_type) {
  1833. case SDEV_EVT_MEDIA_CHANGE:
  1834. envp[idx++] = "SDEV_MEDIA_CHANGE=1";
  1835. break;
  1836. default:
  1837. /* do nothing */
  1838. break;
  1839. }
  1840. envp[idx++] = NULL;
  1841. kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
  1842. }
  1843. /**
  1844. * sdev_evt_thread - send a uevent for each scsi event
  1845. * @work: work struct for scsi_device
  1846. *
  1847. * Dispatch queued events to their associated scsi_device kobjects
  1848. * as uevents.
  1849. */
  1850. void scsi_evt_thread(struct work_struct *work)
  1851. {
  1852. struct scsi_device *sdev;
  1853. LIST_HEAD(event_list);
  1854. sdev = container_of(work, struct scsi_device, event_work);
  1855. while (1) {
  1856. struct scsi_event *evt;
  1857. struct list_head *this, *tmp;
  1858. unsigned long flags;
  1859. spin_lock_irqsave(&sdev->list_lock, flags);
  1860. list_splice_init(&sdev->event_list, &event_list);
  1861. spin_unlock_irqrestore(&sdev->list_lock, flags);
  1862. if (list_empty(&event_list))
  1863. break;
  1864. list_for_each_safe(this, tmp, &event_list) {
  1865. evt = list_entry(this, struct scsi_event, node);
  1866. list_del(&evt->node);
  1867. scsi_evt_emit(sdev, evt);
  1868. kfree(evt);
  1869. }
  1870. }
  1871. }
  1872. /**
  1873. * sdev_evt_send - send asserted event to uevent thread
  1874. * @sdev: scsi_device event occurred on
  1875. * @evt: event to send
  1876. *
  1877. * Assert scsi device event asynchronously.
  1878. */
  1879. void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
  1880. {
  1881. unsigned long flags;
  1882. if (!test_bit(evt->evt_type, sdev->supported_events)) {
  1883. kfree(evt);
  1884. return;
  1885. }
  1886. spin_lock_irqsave(&sdev->list_lock, flags);
  1887. list_add_tail(&evt->node, &sdev->event_list);
  1888. schedule_work(&sdev->event_work);
  1889. spin_unlock_irqrestore(&sdev->list_lock, flags);
  1890. }
  1891. EXPORT_SYMBOL_GPL(sdev_evt_send);
  1892. /**
  1893. * sdev_evt_alloc - allocate a new scsi event
  1894. * @evt_type: type of event to allocate
  1895. * @gfpflags: GFP flags for allocation
  1896. *
  1897. * Allocates and returns a new scsi_event.
  1898. */
  1899. struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
  1900. gfp_t gfpflags)
  1901. {
  1902. struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
  1903. if (!evt)
  1904. return NULL;
  1905. evt->evt_type = evt_type;
  1906. INIT_LIST_HEAD(&evt->node);
  1907. /* evt_type-specific initialization, if any */
  1908. switch (evt_type) {
  1909. case SDEV_EVT_MEDIA_CHANGE:
  1910. default:
  1911. /* do nothing */
  1912. break;
  1913. }
  1914. return evt;
  1915. }
  1916. EXPORT_SYMBOL_GPL(sdev_evt_alloc);
  1917. /**
  1918. * sdev_evt_send_simple - send asserted event to uevent thread
  1919. * @sdev: scsi_device event occurred on
  1920. * @evt_type: type of event to send
  1921. * @gfpflags: GFP flags for allocation
  1922. *
  1923. * Assert scsi device event asynchronously, given an event type.
  1924. */
  1925. void sdev_evt_send_simple(struct scsi_device *sdev,
  1926. enum scsi_device_event evt_type, gfp_t gfpflags)
  1927. {
  1928. struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
  1929. if (!evt) {
  1930. sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
  1931. evt_type);
  1932. return;
  1933. }
  1934. sdev_evt_send(sdev, evt);
  1935. }
  1936. EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
  1937. /**
  1938. * scsi_device_quiesce - Block user issued commands.
  1939. * @sdev: scsi device to quiesce.
  1940. *
  1941. * This works by trying to transition to the SDEV_QUIESCE state
  1942. * (which must be a legal transition). When the device is in this
  1943. * state, only special requests will be accepted, all others will
  1944. * be deferred. Since special requests may also be requeued requests,
  1945. * a successful return doesn't guarantee the device will be
  1946. * totally quiescent.
  1947. *
  1948. * Must be called with user context, may sleep.
  1949. *
  1950. * Returns zero if unsuccessful or an error if not.
  1951. */
  1952. int
  1953. scsi_device_quiesce(struct scsi_device *sdev)
  1954. {
  1955. int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
  1956. if (err)
  1957. return err;
  1958. scsi_run_queue(sdev->request_queue);
  1959. while (sdev->device_busy) {
  1960. msleep_interruptible(200);
  1961. scsi_run_queue(sdev->request_queue);
  1962. }
  1963. return 0;
  1964. }
  1965. EXPORT_SYMBOL(scsi_device_quiesce);
  1966. /**
  1967. * scsi_device_resume - Restart user issued commands to a quiesced device.
  1968. * @sdev: scsi device to resume.
  1969. *
  1970. * Moves the device from quiesced back to running and restarts the
  1971. * queues.
  1972. *
  1973. * Must be called with user context, may sleep.
  1974. */
  1975. void
  1976. scsi_device_resume(struct scsi_device *sdev)
  1977. {
  1978. if(scsi_device_set_state(sdev, SDEV_RUNNING))
  1979. return;
  1980. scsi_run_queue(sdev->request_queue);
  1981. }
  1982. EXPORT_SYMBOL(scsi_device_resume);
  1983. static void
  1984. device_quiesce_fn(struct scsi_device *sdev, void *data)
  1985. {
  1986. scsi_device_quiesce(sdev);
  1987. }
  1988. void
  1989. scsi_target_quiesce(struct scsi_target *starget)
  1990. {
  1991. starget_for_each_device(starget, NULL, device_quiesce_fn);
  1992. }
  1993. EXPORT_SYMBOL(scsi_target_quiesce);
  1994. static void
  1995. device_resume_fn(struct scsi_device *sdev, void *data)
  1996. {
  1997. scsi_device_resume(sdev);
  1998. }
  1999. void
  2000. scsi_target_resume(struct scsi_target *starget)
  2001. {
  2002. starget_for_each_device(starget, NULL, device_resume_fn);
  2003. }
  2004. EXPORT_SYMBOL(scsi_target_resume);
  2005. /**
  2006. * scsi_internal_device_block - internal function to put a device temporarily into the SDEV_BLOCK state
  2007. * @sdev: device to block
  2008. *
  2009. * Block request made by scsi lld's to temporarily stop all
  2010. * scsi commands on the specified device. Called from interrupt
  2011. * or normal process context.
  2012. *
  2013. * Returns zero if successful or error if not
  2014. *
  2015. * Notes:
  2016. * This routine transitions the device to the SDEV_BLOCK state
  2017. * (which must be a legal transition). When the device is in this
  2018. * state, all commands are deferred until the scsi lld reenables
  2019. * the device with scsi_device_unblock or device_block_tmo fires.
  2020. * This routine assumes the host_lock is held on entry.
  2021. */
  2022. int
  2023. scsi_internal_device_block(struct scsi_device *sdev)
  2024. {
  2025. struct request_queue *q = sdev->request_queue;
  2026. unsigned long flags;
  2027. int err = 0;
  2028. err = scsi_device_set_state(sdev, SDEV_BLOCK);
  2029. if (err)
  2030. return err;
  2031. /*
  2032. * The device has transitioned to SDEV_BLOCK. Stop the
  2033. * block layer from calling the midlayer with this device's
  2034. * request queue.
  2035. */
  2036. spin_lock_irqsave(q->queue_lock, flags);
  2037. blk_stop_queue(q);
  2038. spin_unlock_irqrestore(q->queue_lock, flags);
  2039. return 0;
  2040. }
  2041. EXPORT_SYMBOL_GPL(scsi_internal_device_block);
  2042. /**
  2043. * scsi_internal_device_unblock - resume a device after a block request
  2044. * @sdev: device to resume
  2045. *
  2046. * Called by scsi lld's or the midlayer to restart the device queue
  2047. * for the previously suspended scsi device. Called from interrupt or
  2048. * normal process context.
  2049. *
  2050. * Returns zero if successful or error if not.
  2051. *
  2052. * Notes:
  2053. * This routine transitions the device to the SDEV_RUNNING state
  2054. * (which must be a legal transition) allowing the midlayer to
  2055. * goose the queue for this device. This routine assumes the
  2056. * host_lock is held upon entry.
  2057. */
  2058. int
  2059. scsi_internal_device_unblock(struct scsi_device *sdev)
  2060. {
  2061. struct request_queue *q = sdev->request_queue;
  2062. int err;
  2063. unsigned long flags;
  2064. /*
  2065. * Try to transition the scsi device to SDEV_RUNNING
  2066. * and goose the device queue if successful.
  2067. */
  2068. err = scsi_device_set_state(sdev, SDEV_RUNNING);
  2069. if (err)
  2070. return err;
  2071. spin_lock_irqsave(q->queue_lock, flags);
  2072. blk_start_queue(q);
  2073. spin_unlock_irqrestore(q->queue_lock, flags);
  2074. return 0;
  2075. }
  2076. EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
  2077. static void
  2078. device_block(struct scsi_device *sdev, void *data)
  2079. {
  2080. scsi_internal_device_block(sdev);
  2081. }
  2082. static int
  2083. target_block(struct device *dev, void *data)
  2084. {
  2085. if (scsi_is_target_device(dev))
  2086. starget_for_each_device(to_scsi_target(dev), NULL,
  2087. device_block);
  2088. return 0;
  2089. }
  2090. void
  2091. scsi_target_block(struct device *dev)
  2092. {
  2093. if (scsi_is_target_device(dev))
  2094. starget_for_each_device(to_scsi_target(dev), NULL,
  2095. device_block);
  2096. else
  2097. device_for_each_child(dev, NULL, target_block);
  2098. }
  2099. EXPORT_SYMBOL_GPL(scsi_target_block);
  2100. static void
  2101. device_unblock(struct scsi_device *sdev, void *data)
  2102. {
  2103. scsi_internal_device_unblock(sdev);
  2104. }
  2105. static int
  2106. target_unblock(struct device *dev, void *data)
  2107. {
  2108. if (scsi_is_target_device(dev))
  2109. starget_for_each_device(to_scsi_target(dev), NULL,
  2110. device_unblock);
  2111. return 0;
  2112. }
  2113. void
  2114. scsi_target_unblock(struct device *dev)
  2115. {
  2116. if (scsi_is_target_device(dev))
  2117. starget_for_each_device(to_scsi_target(dev), NULL,
  2118. device_unblock);
  2119. else
  2120. device_for_each_child(dev, NULL, target_unblock);
  2121. }
  2122. EXPORT_SYMBOL_GPL(scsi_target_unblock);
  2123. /**
  2124. * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
  2125. * @sgl: scatter-gather list
  2126. * @sg_count: number of segments in sg
  2127. * @offset: offset in bytes into sg, on return offset into the mapped area
  2128. * @len: bytes to map, on return number of bytes mapped
  2129. *
  2130. * Returns virtual address of the start of the mapped page
  2131. */
  2132. void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
  2133. size_t *offset, size_t *len)
  2134. {
  2135. int i;
  2136. size_t sg_len = 0, len_complete = 0;
  2137. struct scatterlist *sg;
  2138. struct page *page;
  2139. WARN_ON(!irqs_disabled());
  2140. for_each_sg(sgl, sg, sg_count, i) {
  2141. len_complete = sg_len; /* Complete sg-entries */
  2142. sg_len += sg->length;
  2143. if (sg_len > *offset)
  2144. break;
  2145. }
  2146. if (unlikely(i == sg_count)) {
  2147. printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
  2148. "elements %d\n",
  2149. __FUNCTION__, sg_len, *offset, sg_count);
  2150. WARN_ON(1);
  2151. return NULL;
  2152. }
  2153. /* Offset starting from the beginning of first page in this sg-entry */
  2154. *offset = *offset - len_complete + sg->offset;
  2155. /* Assumption: contiguous pages can be accessed as "page + i" */
  2156. page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
  2157. *offset &= ~PAGE_MASK;
  2158. /* Bytes in this sg-entry from *offset to the end of the page */
  2159. sg_len = PAGE_SIZE - *offset;
  2160. if (*len > sg_len)
  2161. *len = sg_len;
  2162. return kmap_atomic(page, KM_BIO_SRC_IRQ);
  2163. }
  2164. EXPORT_SYMBOL(scsi_kmap_atomic_sg);
  2165. /**
  2166. * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg
  2167. * @virt: virtual address to be unmapped
  2168. */
  2169. void scsi_kunmap_atomic_sg(void *virt)
  2170. {
  2171. kunmap_atomic(virt, KM_BIO_SRC_IRQ);
  2172. }
  2173. EXPORT_SYMBOL(scsi_kunmap_atomic_sg);