scsi_lib.c 68 KB

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