scsi_lib.c 59 KB

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