scsi_lib.c 58 KB

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