scsi_lib.c 69 KB

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