scsi_lib.c 57 KB

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