scsi_lib.c 57 KB

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