scsi_lib.c 68 KB

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