scsi_lib.c 57 KB

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