scsi_lib.c 57 KB

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