scsi_lib.c 63 KB

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