scsi_lib.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704
  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 do one of several things here:
  758. *
  759. * a) Call scsi_end_request. This will finish off the
  760. * specified number of sectors. If we are done, the
  761. * command block will be released, and the queue
  762. * function will be goosed. If we are not done, then
  763. * scsi_end_request will directly goose the queue.
  764. *
  765. * b) We can just use scsi_requeue_command() here. This would
  766. * be used if we just wanted to retry, for example.
  767. */
  768. void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
  769. {
  770. int result = cmd->result;
  771. int this_count;
  772. struct request_queue *q = cmd->device->request_queue;
  773. struct request *req = cmd->request;
  774. int error = 0;
  775. struct scsi_sense_hdr sshdr;
  776. int sense_valid = 0;
  777. int sense_deferred = 0;
  778. if (result) {
  779. sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
  780. if (sense_valid)
  781. sense_deferred = scsi_sense_is_deferred(&sshdr);
  782. }
  783. if (blk_pc_request(req)) { /* SG_IO ioctl from block level */
  784. req->errors = result;
  785. if (result) {
  786. if (sense_valid && req->sense) {
  787. /*
  788. * SG_IO wants current and deferred errors
  789. */
  790. int len = 8 + cmd->sense_buffer[7];
  791. if (len > SCSI_SENSE_BUFFERSIZE)
  792. len = SCSI_SENSE_BUFFERSIZE;
  793. memcpy(req->sense, cmd->sense_buffer, len);
  794. req->sense_len = len;
  795. }
  796. if (!sense_deferred)
  797. error = -EIO;
  798. }
  799. if (scsi_bidi_cmnd(cmd)) {
  800. /* will also release_buffers */
  801. scsi_end_bidi_request(cmd);
  802. return;
  803. }
  804. req->data_len = scsi_get_resid(cmd);
  805. }
  806. BUG_ON(blk_bidi_rq(req)); /* bidi not support for !blk_pc_request yet */
  807. scsi_release_buffers(cmd);
  808. /*
  809. * Next deal with any sectors which we were able to correctly
  810. * handle.
  811. */
  812. SCSI_LOG_HLCOMPLETE(1, printk("%ld sectors total, "
  813. "%d bytes done.\n",
  814. req->nr_sectors, good_bytes));
  815. /* A number of bytes were successfully read. If there
  816. * are leftovers and there is some kind of error
  817. * (result != 0), retry the rest.
  818. */
  819. if (scsi_end_request(cmd, error, good_bytes, result == 0) == NULL)
  820. return;
  821. this_count = blk_rq_bytes(req);
  822. /* good_bytes = 0, or (inclusive) there were leftovers and
  823. * result = 0, so scsi_end_request couldn't retry.
  824. */
  825. if (sense_valid && !sense_deferred) {
  826. switch (sshdr.sense_key) {
  827. case UNIT_ATTENTION:
  828. if (cmd->device->removable) {
  829. /* Detected disc change. Set a bit
  830. * and quietly refuse further access.
  831. */
  832. cmd->device->changed = 1;
  833. scsi_end_request(cmd, -EIO, this_count, 1);
  834. return;
  835. } else {
  836. /* Must have been a power glitch, or a
  837. * bus reset. Could not have been a
  838. * media change, so we just retry the
  839. * request and see what happens.
  840. */
  841. scsi_requeue_command(q, cmd);
  842. return;
  843. }
  844. break;
  845. case ILLEGAL_REQUEST:
  846. /* If we had an ILLEGAL REQUEST returned, then
  847. * we may have performed an unsupported
  848. * command. The only thing this should be
  849. * would be a ten byte read where only a six
  850. * byte read was supported. Also, on a system
  851. * where READ CAPACITY failed, we may have
  852. * read past the end of the disk.
  853. */
  854. if ((cmd->device->use_10_for_rw &&
  855. sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
  856. (cmd->cmnd[0] == READ_10 ||
  857. cmd->cmnd[0] == WRITE_10)) {
  858. cmd->device->use_10_for_rw = 0;
  859. /* This will cause a retry with a
  860. * 6-byte command.
  861. */
  862. scsi_requeue_command(q, cmd);
  863. } else if (sshdr.asc == 0x10) /* DIX */
  864. scsi_end_request(cmd, -EIO, this_count, 0);
  865. else
  866. scsi_end_request(cmd, -EIO, this_count, 1);
  867. return;
  868. case ABORTED_COMMAND:
  869. if (sshdr.asc == 0x10) { /* DIF */
  870. scsi_end_request(cmd, -EIO, this_count, 0);
  871. return;
  872. }
  873. break;
  874. case NOT_READY:
  875. /* If the device is in the process of becoming
  876. * ready, or has a temporary blockage, retry.
  877. */
  878. if (sshdr.asc == 0x04) {
  879. switch (sshdr.ascq) {
  880. case 0x01: /* becoming ready */
  881. case 0x04: /* format in progress */
  882. case 0x05: /* rebuild in progress */
  883. case 0x06: /* recalculation in progress */
  884. case 0x07: /* operation in progress */
  885. case 0x08: /* Long write in progress */
  886. case 0x09: /* self test in progress */
  887. scsi_requeue_command(q, cmd);
  888. return;
  889. default:
  890. break;
  891. }
  892. }
  893. if (!(req->cmd_flags & REQ_QUIET))
  894. scsi_cmd_print_sense_hdr(cmd,
  895. "Device not ready",
  896. &sshdr);
  897. scsi_end_request(cmd, -EIO, this_count, 1);
  898. return;
  899. case VOLUME_OVERFLOW:
  900. if (!(req->cmd_flags & REQ_QUIET)) {
  901. scmd_printk(KERN_INFO, cmd,
  902. "Volume overflow, CDB: ");
  903. __scsi_print_command(cmd->cmnd);
  904. scsi_print_sense("", cmd);
  905. }
  906. /* See SSC3rXX or current. */
  907. scsi_end_request(cmd, -EIO, this_count, 1);
  908. return;
  909. default:
  910. break;
  911. }
  912. }
  913. if (host_byte(result) == DID_RESET) {
  914. /* Third party bus reset or reset for error recovery
  915. * reasons. Just retry the request and see what
  916. * happens.
  917. */
  918. scsi_requeue_command(q, cmd);
  919. return;
  920. }
  921. if (result) {
  922. if (!(req->cmd_flags & REQ_QUIET)) {
  923. scsi_print_result(cmd);
  924. if (driver_byte(result) & DRIVER_SENSE)
  925. scsi_print_sense("", cmd);
  926. }
  927. }
  928. scsi_end_request(cmd, -EIO, this_count, !result);
  929. }
  930. static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb,
  931. gfp_t gfp_mask)
  932. {
  933. int count;
  934. /*
  935. * If sg table allocation fails, requeue request later.
  936. */
  937. if (unlikely(scsi_alloc_sgtable(sdb, req->nr_phys_segments,
  938. gfp_mask))) {
  939. return BLKPREP_DEFER;
  940. }
  941. req->buffer = NULL;
  942. /*
  943. * Next, walk the list, and fill in the addresses and sizes of
  944. * each segment.
  945. */
  946. count = blk_rq_map_sg(req->q, req, sdb->table.sgl);
  947. BUG_ON(count > sdb->table.nents);
  948. sdb->table.nents = count;
  949. if (blk_pc_request(req))
  950. sdb->length = req->data_len;
  951. else
  952. sdb->length = req->nr_sectors << 9;
  953. return BLKPREP_OK;
  954. }
  955. /*
  956. * Function: scsi_init_io()
  957. *
  958. * Purpose: SCSI I/O initialize function.
  959. *
  960. * Arguments: cmd - Command descriptor we wish to initialize
  961. *
  962. * Returns: 0 on success
  963. * BLKPREP_DEFER if the failure is retryable
  964. * BLKPREP_KILL if the failure is fatal
  965. */
  966. int scsi_init_io(struct scsi_cmnd *cmd, gfp_t gfp_mask)
  967. {
  968. int error = scsi_init_sgtable(cmd->request, &cmd->sdb, gfp_mask);
  969. if (error)
  970. goto err_exit;
  971. if (blk_bidi_rq(cmd->request)) {
  972. struct scsi_data_buffer *bidi_sdb = kmem_cache_zalloc(
  973. scsi_sdb_cache, GFP_ATOMIC);
  974. if (!bidi_sdb) {
  975. error = BLKPREP_DEFER;
  976. goto err_exit;
  977. }
  978. cmd->request->next_rq->special = bidi_sdb;
  979. error = scsi_init_sgtable(cmd->request->next_rq, bidi_sdb,
  980. GFP_ATOMIC);
  981. if (error)
  982. goto err_exit;
  983. }
  984. if (blk_integrity_rq(cmd->request)) {
  985. struct scsi_data_buffer *prot_sdb = cmd->prot_sdb;
  986. int ivecs, count;
  987. BUG_ON(prot_sdb == NULL);
  988. ivecs = blk_rq_count_integrity_sg(cmd->request);
  989. if (scsi_alloc_sgtable(prot_sdb, ivecs, gfp_mask)) {
  990. error = BLKPREP_DEFER;
  991. goto err_exit;
  992. }
  993. count = blk_rq_map_integrity_sg(cmd->request,
  994. prot_sdb->table.sgl);
  995. BUG_ON(unlikely(count > ivecs));
  996. cmd->prot_sdb = prot_sdb;
  997. cmd->prot_sdb->table.nents = count;
  998. }
  999. return BLKPREP_OK ;
  1000. err_exit:
  1001. scsi_release_buffers(cmd);
  1002. if (error == BLKPREP_KILL)
  1003. scsi_put_command(cmd);
  1004. else /* BLKPREP_DEFER */
  1005. scsi_unprep_request(cmd->request);
  1006. return error;
  1007. }
  1008. EXPORT_SYMBOL(scsi_init_io);
  1009. static struct scsi_cmnd *scsi_get_cmd_from_req(struct scsi_device *sdev,
  1010. struct request *req)
  1011. {
  1012. struct scsi_cmnd *cmd;
  1013. if (!req->special) {
  1014. cmd = scsi_get_command(sdev, GFP_ATOMIC);
  1015. if (unlikely(!cmd))
  1016. return NULL;
  1017. req->special = cmd;
  1018. } else {
  1019. cmd = req->special;
  1020. }
  1021. /* pull a tag out of the request if we have one */
  1022. cmd->tag = req->tag;
  1023. cmd->request = req;
  1024. cmd->cmnd = req->cmd;
  1025. return cmd;
  1026. }
  1027. int scsi_setup_blk_pc_cmnd(struct scsi_device *sdev, struct request *req)
  1028. {
  1029. struct scsi_cmnd *cmd;
  1030. int ret = scsi_prep_state_check(sdev, req);
  1031. if (ret != BLKPREP_OK)
  1032. return ret;
  1033. cmd = scsi_get_cmd_from_req(sdev, req);
  1034. if (unlikely(!cmd))
  1035. return BLKPREP_DEFER;
  1036. /*
  1037. * BLOCK_PC requests may transfer data, in which case they must
  1038. * a bio attached to them. Or they might contain a SCSI command
  1039. * that does not transfer data, in which case they may optionally
  1040. * submit a request without an attached bio.
  1041. */
  1042. if (req->bio) {
  1043. int ret;
  1044. BUG_ON(!req->nr_phys_segments);
  1045. ret = scsi_init_io(cmd, GFP_ATOMIC);
  1046. if (unlikely(ret))
  1047. return ret;
  1048. } else {
  1049. BUG_ON(req->data_len);
  1050. BUG_ON(req->data);
  1051. memset(&cmd->sdb, 0, sizeof(cmd->sdb));
  1052. req->buffer = NULL;
  1053. }
  1054. cmd->cmd_len = req->cmd_len;
  1055. if (!req->data_len)
  1056. cmd->sc_data_direction = DMA_NONE;
  1057. else if (rq_data_dir(req) == WRITE)
  1058. cmd->sc_data_direction = DMA_TO_DEVICE;
  1059. else
  1060. cmd->sc_data_direction = DMA_FROM_DEVICE;
  1061. cmd->transfersize = req->data_len;
  1062. cmd->allowed = req->retries;
  1063. return BLKPREP_OK;
  1064. }
  1065. EXPORT_SYMBOL(scsi_setup_blk_pc_cmnd);
  1066. /*
  1067. * Setup a REQ_TYPE_FS command. These are simple read/write request
  1068. * from filesystems that still need to be translated to SCSI CDBs from
  1069. * the ULD.
  1070. */
  1071. int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req)
  1072. {
  1073. struct scsi_cmnd *cmd;
  1074. int ret = scsi_prep_state_check(sdev, req);
  1075. if (ret != BLKPREP_OK)
  1076. return ret;
  1077. if (unlikely(sdev->scsi_dh_data && sdev->scsi_dh_data->scsi_dh
  1078. && sdev->scsi_dh_data->scsi_dh->prep_fn)) {
  1079. ret = sdev->scsi_dh_data->scsi_dh->prep_fn(sdev, req);
  1080. if (ret != BLKPREP_OK)
  1081. return ret;
  1082. }
  1083. /*
  1084. * Filesystem requests must transfer data.
  1085. */
  1086. BUG_ON(!req->nr_phys_segments);
  1087. cmd = scsi_get_cmd_from_req(sdev, req);
  1088. if (unlikely(!cmd))
  1089. return BLKPREP_DEFER;
  1090. memset(cmd->cmnd, 0, BLK_MAX_CDB);
  1091. return scsi_init_io(cmd, GFP_ATOMIC);
  1092. }
  1093. EXPORT_SYMBOL(scsi_setup_fs_cmnd);
  1094. int scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
  1095. {
  1096. int ret = BLKPREP_OK;
  1097. /*
  1098. * If the device is not in running state we will reject some
  1099. * or all commands.
  1100. */
  1101. if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
  1102. switch (sdev->sdev_state) {
  1103. case SDEV_OFFLINE:
  1104. /*
  1105. * If the device is offline we refuse to process any
  1106. * commands. The device must be brought online
  1107. * before trying any recovery commands.
  1108. */
  1109. sdev_printk(KERN_ERR, sdev,
  1110. "rejecting I/O to offline device\n");
  1111. ret = BLKPREP_KILL;
  1112. break;
  1113. case SDEV_DEL:
  1114. /*
  1115. * If the device is fully deleted, we refuse to
  1116. * process any commands as well.
  1117. */
  1118. sdev_printk(KERN_ERR, sdev,
  1119. "rejecting I/O to dead device\n");
  1120. ret = BLKPREP_KILL;
  1121. break;
  1122. case SDEV_QUIESCE:
  1123. case SDEV_BLOCK:
  1124. case SDEV_CREATED_BLOCK:
  1125. /*
  1126. * If the devices is blocked we defer normal commands.
  1127. */
  1128. if (!(req->cmd_flags & REQ_PREEMPT))
  1129. ret = BLKPREP_DEFER;
  1130. break;
  1131. default:
  1132. /*
  1133. * For any other not fully online state we only allow
  1134. * special commands. In particular any user initiated
  1135. * command is not allowed.
  1136. */
  1137. if (!(req->cmd_flags & REQ_PREEMPT))
  1138. ret = BLKPREP_KILL;
  1139. break;
  1140. }
  1141. }
  1142. return ret;
  1143. }
  1144. EXPORT_SYMBOL(scsi_prep_state_check);
  1145. int scsi_prep_return(struct request_queue *q, struct request *req, int ret)
  1146. {
  1147. struct scsi_device *sdev = q->queuedata;
  1148. switch (ret) {
  1149. case BLKPREP_KILL:
  1150. req->errors = DID_NO_CONNECT << 16;
  1151. /* release the command and kill it */
  1152. if (req->special) {
  1153. struct scsi_cmnd *cmd = req->special;
  1154. scsi_release_buffers(cmd);
  1155. scsi_put_command(cmd);
  1156. req->special = NULL;
  1157. }
  1158. break;
  1159. case BLKPREP_DEFER:
  1160. /*
  1161. * If we defer, the elv_next_request() returns NULL, but the
  1162. * queue must be restarted, so we plug here if no returning
  1163. * command will automatically do that.
  1164. */
  1165. if (sdev->device_busy == 0)
  1166. blk_plug_device(q);
  1167. break;
  1168. default:
  1169. req->cmd_flags |= REQ_DONTPREP;
  1170. }
  1171. return ret;
  1172. }
  1173. EXPORT_SYMBOL(scsi_prep_return);
  1174. int scsi_prep_fn(struct request_queue *q, struct request *req)
  1175. {
  1176. struct scsi_device *sdev = q->queuedata;
  1177. int ret = BLKPREP_KILL;
  1178. if (req->cmd_type == REQ_TYPE_BLOCK_PC)
  1179. ret = scsi_setup_blk_pc_cmnd(sdev, req);
  1180. return scsi_prep_return(q, req, ret);
  1181. }
  1182. /*
  1183. * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
  1184. * return 0.
  1185. *
  1186. * Called with the queue_lock held.
  1187. */
  1188. static inline int scsi_dev_queue_ready(struct request_queue *q,
  1189. struct scsi_device *sdev)
  1190. {
  1191. if (sdev->device_busy == 0 && sdev->device_blocked) {
  1192. /*
  1193. * unblock after device_blocked iterates to zero
  1194. */
  1195. if (--sdev->device_blocked == 0) {
  1196. SCSI_LOG_MLQUEUE(3,
  1197. sdev_printk(KERN_INFO, sdev,
  1198. "unblocking device at zero depth\n"));
  1199. } else {
  1200. blk_plug_device(q);
  1201. return 0;
  1202. }
  1203. }
  1204. if (scsi_device_is_busy(sdev))
  1205. return 0;
  1206. return 1;
  1207. }
  1208. /*
  1209. * scsi_target_queue_ready: checks if there we can send commands to target
  1210. * @sdev: scsi device on starget to check.
  1211. *
  1212. * Called with the host lock held.
  1213. */
  1214. static inline int scsi_target_queue_ready(struct Scsi_Host *shost,
  1215. struct scsi_device *sdev)
  1216. {
  1217. struct scsi_target *starget = scsi_target(sdev);
  1218. if (starget->single_lun) {
  1219. if (starget->starget_sdev_user &&
  1220. starget->starget_sdev_user != sdev)
  1221. return 0;
  1222. starget->starget_sdev_user = sdev;
  1223. }
  1224. if (starget->target_busy == 0 && starget->target_blocked) {
  1225. /*
  1226. * unblock after target_blocked iterates to zero
  1227. */
  1228. if (--starget->target_blocked == 0) {
  1229. SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget,
  1230. "unblocking target at zero depth\n"));
  1231. } else {
  1232. blk_plug_device(sdev->request_queue);
  1233. return 0;
  1234. }
  1235. }
  1236. if (scsi_target_is_busy(starget)) {
  1237. if (list_empty(&sdev->starved_entry)) {
  1238. list_add_tail(&sdev->starved_entry,
  1239. &shost->starved_list);
  1240. return 0;
  1241. }
  1242. }
  1243. /* We're OK to process the command, so we can't be starved */
  1244. if (!list_empty(&sdev->starved_entry))
  1245. list_del_init(&sdev->starved_entry);
  1246. return 1;
  1247. }
  1248. /*
  1249. * scsi_host_queue_ready: if we can send requests to shost, return 1 else
  1250. * return 0. We must end up running the queue again whenever 0 is
  1251. * returned, else IO can hang.
  1252. *
  1253. * Called with host_lock held.
  1254. */
  1255. static inline int scsi_host_queue_ready(struct request_queue *q,
  1256. struct Scsi_Host *shost,
  1257. struct scsi_device *sdev)
  1258. {
  1259. if (scsi_host_in_recovery(shost))
  1260. return 0;
  1261. if (shost->host_busy == 0 && shost->host_blocked) {
  1262. /*
  1263. * unblock after host_blocked iterates to zero
  1264. */
  1265. if (--shost->host_blocked == 0) {
  1266. SCSI_LOG_MLQUEUE(3,
  1267. printk("scsi%d unblocking host at zero depth\n",
  1268. shost->host_no));
  1269. } else {
  1270. return 0;
  1271. }
  1272. }
  1273. if (scsi_host_is_busy(shost)) {
  1274. if (list_empty(&sdev->starved_entry))
  1275. list_add_tail(&sdev->starved_entry, &shost->starved_list);
  1276. return 0;
  1277. }
  1278. /* We're OK to process the command, so we can't be starved */
  1279. if (!list_empty(&sdev->starved_entry))
  1280. list_del_init(&sdev->starved_entry);
  1281. return 1;
  1282. }
  1283. /*
  1284. * Busy state exporting function for request stacking drivers.
  1285. *
  1286. * For efficiency, no lock is taken to check the busy state of
  1287. * shost/starget/sdev, since the returned value is not guaranteed and
  1288. * may be changed after request stacking drivers call the function,
  1289. * regardless of taking lock or not.
  1290. *
  1291. * When scsi can't dispatch I/Os anymore and needs to kill I/Os
  1292. * (e.g. !sdev), scsi needs to return 'not busy'.
  1293. * Otherwise, request stacking drivers may hold requests forever.
  1294. */
  1295. static int scsi_lld_busy(struct request_queue *q)
  1296. {
  1297. struct scsi_device *sdev = q->queuedata;
  1298. struct Scsi_Host *shost;
  1299. struct scsi_target *starget;
  1300. if (!sdev)
  1301. return 0;
  1302. shost = sdev->host;
  1303. starget = scsi_target(sdev);
  1304. if (scsi_host_in_recovery(shost) || scsi_host_is_busy(shost) ||
  1305. scsi_target_is_busy(starget) || scsi_device_is_busy(sdev))
  1306. return 1;
  1307. return 0;
  1308. }
  1309. /*
  1310. * Kill a request for a dead device
  1311. */
  1312. static void scsi_kill_request(struct request *req, struct request_queue *q)
  1313. {
  1314. struct scsi_cmnd *cmd = req->special;
  1315. struct scsi_device *sdev = cmd->device;
  1316. struct scsi_target *starget = scsi_target(sdev);
  1317. struct Scsi_Host *shost = sdev->host;
  1318. blkdev_dequeue_request(req);
  1319. if (unlikely(cmd == NULL)) {
  1320. printk(KERN_CRIT "impossible request in %s.\n",
  1321. __func__);
  1322. BUG();
  1323. }
  1324. scsi_init_cmd_errh(cmd);
  1325. cmd->result = DID_NO_CONNECT << 16;
  1326. atomic_inc(&cmd->device->iorequest_cnt);
  1327. /*
  1328. * SCSI request completion path will do scsi_device_unbusy(),
  1329. * bump busy counts. To bump the counters, we need to dance
  1330. * with the locks as normal issue path does.
  1331. */
  1332. sdev->device_busy++;
  1333. spin_unlock(sdev->request_queue->queue_lock);
  1334. spin_lock(shost->host_lock);
  1335. shost->host_busy++;
  1336. starget->target_busy++;
  1337. spin_unlock(shost->host_lock);
  1338. spin_lock(sdev->request_queue->queue_lock);
  1339. blk_complete_request(req);
  1340. }
  1341. static void scsi_softirq_done(struct request *rq)
  1342. {
  1343. struct scsi_cmnd *cmd = rq->special;
  1344. unsigned long wait_for = (cmd->allowed + 1) * rq->timeout;
  1345. int disposition;
  1346. INIT_LIST_HEAD(&cmd->eh_entry);
  1347. /*
  1348. * Set the serial numbers back to zero
  1349. */
  1350. cmd->serial_number = 0;
  1351. atomic_inc(&cmd->device->iodone_cnt);
  1352. if (cmd->result)
  1353. atomic_inc(&cmd->device->ioerr_cnt);
  1354. disposition = scsi_decide_disposition(cmd);
  1355. if (disposition != SUCCESS &&
  1356. time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
  1357. sdev_printk(KERN_ERR, cmd->device,
  1358. "timing out command, waited %lus\n",
  1359. wait_for/HZ);
  1360. disposition = SUCCESS;
  1361. }
  1362. scsi_log_completion(cmd, disposition);
  1363. switch (disposition) {
  1364. case SUCCESS:
  1365. scsi_finish_command(cmd);
  1366. break;
  1367. case NEEDS_RETRY:
  1368. scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
  1369. break;
  1370. case ADD_TO_MLQUEUE:
  1371. scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
  1372. break;
  1373. default:
  1374. if (!scsi_eh_scmd_add(cmd, 0))
  1375. scsi_finish_command(cmd);
  1376. }
  1377. }
  1378. /*
  1379. * Function: scsi_request_fn()
  1380. *
  1381. * Purpose: Main strategy routine for SCSI.
  1382. *
  1383. * Arguments: q - Pointer to actual queue.
  1384. *
  1385. * Returns: Nothing
  1386. *
  1387. * Lock status: IO request lock assumed to be held when called.
  1388. */
  1389. static void scsi_request_fn(struct request_queue *q)
  1390. {
  1391. struct scsi_device *sdev = q->queuedata;
  1392. struct Scsi_Host *shost;
  1393. struct scsi_cmnd *cmd;
  1394. struct request *req;
  1395. if (!sdev) {
  1396. printk("scsi: killing requests for dead queue\n");
  1397. while ((req = elv_next_request(q)) != NULL)
  1398. scsi_kill_request(req, q);
  1399. return;
  1400. }
  1401. if(!get_device(&sdev->sdev_gendev))
  1402. /* We must be tearing the block queue down already */
  1403. return;
  1404. /*
  1405. * To start with, we keep looping until the queue is empty, or until
  1406. * the host is no longer able to accept any more requests.
  1407. */
  1408. shost = sdev->host;
  1409. while (!blk_queue_plugged(q)) {
  1410. int rtn;
  1411. /*
  1412. * get next queueable request. We do this early to make sure
  1413. * that the request is fully prepared even if we cannot
  1414. * accept it.
  1415. */
  1416. req = elv_next_request(q);
  1417. if (!req || !scsi_dev_queue_ready(q, sdev))
  1418. break;
  1419. if (unlikely(!scsi_device_online(sdev))) {
  1420. sdev_printk(KERN_ERR, sdev,
  1421. "rejecting I/O to offline device\n");
  1422. scsi_kill_request(req, q);
  1423. continue;
  1424. }
  1425. /*
  1426. * Remove the request from the request list.
  1427. */
  1428. if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
  1429. blkdev_dequeue_request(req);
  1430. sdev->device_busy++;
  1431. spin_unlock(q->queue_lock);
  1432. cmd = req->special;
  1433. if (unlikely(cmd == NULL)) {
  1434. printk(KERN_CRIT "impossible request in %s.\n"
  1435. "please mail a stack trace to "
  1436. "linux-scsi@vger.kernel.org\n",
  1437. __func__);
  1438. blk_dump_rq_flags(req, "foo");
  1439. BUG();
  1440. }
  1441. spin_lock(shost->host_lock);
  1442. /*
  1443. * We hit this when the driver is using a host wide
  1444. * tag map. For device level tag maps the queue_depth check
  1445. * in the device ready fn would prevent us from trying
  1446. * to allocate a tag. Since the map is a shared host resource
  1447. * we add the dev to the starved list so it eventually gets
  1448. * a run when a tag is freed.
  1449. */
  1450. if (blk_queue_tagged(q) && !blk_rq_tagged(req)) {
  1451. if (list_empty(&sdev->starved_entry))
  1452. list_add_tail(&sdev->starved_entry,
  1453. &shost->starved_list);
  1454. goto not_ready;
  1455. }
  1456. if (!scsi_target_queue_ready(shost, sdev))
  1457. goto not_ready;
  1458. if (!scsi_host_queue_ready(q, shost, sdev))
  1459. goto not_ready;
  1460. scsi_target(sdev)->target_busy++;
  1461. shost->host_busy++;
  1462. /*
  1463. * XXX(hch): This is rather suboptimal, scsi_dispatch_cmd will
  1464. * take the lock again.
  1465. */
  1466. spin_unlock_irq(shost->host_lock);
  1467. /*
  1468. * Finally, initialize any error handling parameters, and set up
  1469. * the timers for timeouts.
  1470. */
  1471. scsi_init_cmd_errh(cmd);
  1472. /*
  1473. * Dispatch the command to the low-level driver.
  1474. */
  1475. rtn = scsi_dispatch_cmd(cmd);
  1476. spin_lock_irq(q->queue_lock);
  1477. if(rtn) {
  1478. /* we're refusing the command; because of
  1479. * the way locks get dropped, we need to
  1480. * check here if plugging is required */
  1481. if(sdev->device_busy == 0)
  1482. blk_plug_device(q);
  1483. break;
  1484. }
  1485. }
  1486. goto out;
  1487. not_ready:
  1488. spin_unlock_irq(shost->host_lock);
  1489. /*
  1490. * lock q, handle tag, requeue req, and decrement device_busy. We
  1491. * must return with queue_lock held.
  1492. *
  1493. * Decrementing device_busy without checking it is OK, as all such
  1494. * cases (host limits or settings) should run the queue at some
  1495. * later time.
  1496. */
  1497. spin_lock_irq(q->queue_lock);
  1498. blk_requeue_request(q, req);
  1499. sdev->device_busy--;
  1500. if(sdev->device_busy == 0)
  1501. blk_plug_device(q);
  1502. out:
  1503. /* must be careful here...if we trigger the ->remove() function
  1504. * we cannot be holding the q lock */
  1505. spin_unlock_irq(q->queue_lock);
  1506. put_device(&sdev->sdev_gendev);
  1507. spin_lock_irq(q->queue_lock);
  1508. }
  1509. u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
  1510. {
  1511. struct device *host_dev;
  1512. u64 bounce_limit = 0xffffffff;
  1513. if (shost->unchecked_isa_dma)
  1514. return BLK_BOUNCE_ISA;
  1515. /*
  1516. * Platforms with virtual-DMA translation
  1517. * hardware have no practical limit.
  1518. */
  1519. if (!PCI_DMA_BUS_IS_PHYS)
  1520. return BLK_BOUNCE_ANY;
  1521. host_dev = scsi_get_device(shost);
  1522. if (host_dev && host_dev->dma_mask)
  1523. bounce_limit = *host_dev->dma_mask;
  1524. return bounce_limit;
  1525. }
  1526. EXPORT_SYMBOL(scsi_calculate_bounce_limit);
  1527. struct request_queue *__scsi_alloc_queue(struct Scsi_Host *shost,
  1528. request_fn_proc *request_fn)
  1529. {
  1530. struct request_queue *q;
  1531. struct device *dev = shost->shost_gendev.parent;
  1532. q = blk_init_queue(request_fn, NULL);
  1533. if (!q)
  1534. return NULL;
  1535. /*
  1536. * this limit is imposed by hardware restrictions
  1537. */
  1538. blk_queue_max_hw_segments(q, shost->sg_tablesize);
  1539. blk_queue_max_phys_segments(q, SCSI_MAX_SG_CHAIN_SEGMENTS);
  1540. blk_queue_max_sectors(q, shost->max_sectors);
  1541. blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
  1542. blk_queue_segment_boundary(q, shost->dma_boundary);
  1543. dma_set_seg_boundary(dev, shost->dma_boundary);
  1544. blk_queue_max_segment_size(q, dma_get_max_seg_size(dev));
  1545. /* New queue, no concurrency on queue_flags */
  1546. if (!shost->use_clustering)
  1547. queue_flag_clear_unlocked(QUEUE_FLAG_CLUSTER, q);
  1548. /*
  1549. * set a reasonable default alignment on word boundaries: the
  1550. * host and device may alter it using
  1551. * blk_queue_update_dma_alignment() later.
  1552. */
  1553. blk_queue_dma_alignment(q, 0x03);
  1554. return q;
  1555. }
  1556. EXPORT_SYMBOL(__scsi_alloc_queue);
  1557. struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
  1558. {
  1559. struct request_queue *q;
  1560. q = __scsi_alloc_queue(sdev->host, scsi_request_fn);
  1561. if (!q)
  1562. return NULL;
  1563. blk_queue_prep_rq(q, scsi_prep_fn);
  1564. blk_queue_softirq_done(q, scsi_softirq_done);
  1565. blk_queue_rq_timed_out(q, scsi_times_out);
  1566. blk_queue_lld_busy(q, scsi_lld_busy);
  1567. return q;
  1568. }
  1569. void scsi_free_queue(struct request_queue *q)
  1570. {
  1571. blk_cleanup_queue(q);
  1572. }
  1573. /*
  1574. * Function: scsi_block_requests()
  1575. *
  1576. * Purpose: Utility function used by low-level drivers to prevent further
  1577. * commands from being queued to the device.
  1578. *
  1579. * Arguments: shost - Host in question
  1580. *
  1581. * Returns: Nothing
  1582. *
  1583. * Lock status: No locks are assumed held.
  1584. *
  1585. * Notes: There is no timer nor any other means by which the requests
  1586. * get unblocked other than the low-level driver calling
  1587. * scsi_unblock_requests().
  1588. */
  1589. void scsi_block_requests(struct Scsi_Host *shost)
  1590. {
  1591. shost->host_self_blocked = 1;
  1592. }
  1593. EXPORT_SYMBOL(scsi_block_requests);
  1594. /*
  1595. * Function: scsi_unblock_requests()
  1596. *
  1597. * Purpose: Utility function used by low-level drivers to allow further
  1598. * commands from being queued to the device.
  1599. *
  1600. * Arguments: shost - Host in question
  1601. *
  1602. * Returns: Nothing
  1603. *
  1604. * Lock status: No locks are assumed held.
  1605. *
  1606. * Notes: There is no timer nor any other means by which the requests
  1607. * get unblocked other than the low-level driver calling
  1608. * scsi_unblock_requests().
  1609. *
  1610. * This is done as an API function so that changes to the
  1611. * internals of the scsi mid-layer won't require wholesale
  1612. * changes to drivers that use this feature.
  1613. */
  1614. void scsi_unblock_requests(struct Scsi_Host *shost)
  1615. {
  1616. shost->host_self_blocked = 0;
  1617. scsi_run_host_queues(shost);
  1618. }
  1619. EXPORT_SYMBOL(scsi_unblock_requests);
  1620. int __init scsi_init_queue(void)
  1621. {
  1622. int i;
  1623. scsi_io_context_cache = kmem_cache_create("scsi_io_context",
  1624. sizeof(struct scsi_io_context),
  1625. 0, 0, NULL);
  1626. if (!scsi_io_context_cache) {
  1627. printk(KERN_ERR "SCSI: can't init scsi io context cache\n");
  1628. return -ENOMEM;
  1629. }
  1630. scsi_sdb_cache = kmem_cache_create("scsi_data_buffer",
  1631. sizeof(struct scsi_data_buffer),
  1632. 0, 0, NULL);
  1633. if (!scsi_sdb_cache) {
  1634. printk(KERN_ERR "SCSI: can't init scsi sdb cache\n");
  1635. goto cleanup_io_context;
  1636. }
  1637. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1638. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1639. int size = sgp->size * sizeof(struct scatterlist);
  1640. sgp->slab = kmem_cache_create(sgp->name, size, 0,
  1641. SLAB_HWCACHE_ALIGN, NULL);
  1642. if (!sgp->slab) {
  1643. printk(KERN_ERR "SCSI: can't init sg slab %s\n",
  1644. sgp->name);
  1645. goto cleanup_sdb;
  1646. }
  1647. sgp->pool = mempool_create_slab_pool(SG_MEMPOOL_SIZE,
  1648. sgp->slab);
  1649. if (!sgp->pool) {
  1650. printk(KERN_ERR "SCSI: can't init sg mempool %s\n",
  1651. sgp->name);
  1652. goto cleanup_sdb;
  1653. }
  1654. }
  1655. return 0;
  1656. cleanup_sdb:
  1657. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1658. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1659. if (sgp->pool)
  1660. mempool_destroy(sgp->pool);
  1661. if (sgp->slab)
  1662. kmem_cache_destroy(sgp->slab);
  1663. }
  1664. kmem_cache_destroy(scsi_sdb_cache);
  1665. cleanup_io_context:
  1666. kmem_cache_destroy(scsi_io_context_cache);
  1667. return -ENOMEM;
  1668. }
  1669. void scsi_exit_queue(void)
  1670. {
  1671. int i;
  1672. kmem_cache_destroy(scsi_io_context_cache);
  1673. kmem_cache_destroy(scsi_sdb_cache);
  1674. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1675. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1676. mempool_destroy(sgp->pool);
  1677. kmem_cache_destroy(sgp->slab);
  1678. }
  1679. }
  1680. /**
  1681. * scsi_mode_select - issue a mode select
  1682. * @sdev: SCSI device to be queried
  1683. * @pf: Page format bit (1 == standard, 0 == vendor specific)
  1684. * @sp: Save page bit (0 == don't save, 1 == save)
  1685. * @modepage: mode page being requested
  1686. * @buffer: request buffer (may not be smaller than eight bytes)
  1687. * @len: length of request buffer.
  1688. * @timeout: command timeout
  1689. * @retries: number of retries before failing
  1690. * @data: returns a structure abstracting the mode header data
  1691. * @sshdr: place to put sense data (or NULL if no sense to be collected).
  1692. * must be SCSI_SENSE_BUFFERSIZE big.
  1693. *
  1694. * Returns zero if successful; negative error number or scsi
  1695. * status on error
  1696. *
  1697. */
  1698. int
  1699. scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
  1700. unsigned char *buffer, int len, int timeout, int retries,
  1701. struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
  1702. {
  1703. unsigned char cmd[10];
  1704. unsigned char *real_buffer;
  1705. int ret;
  1706. memset(cmd, 0, sizeof(cmd));
  1707. cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
  1708. if (sdev->use_10_for_ms) {
  1709. if (len > 65535)
  1710. return -EINVAL;
  1711. real_buffer = kmalloc(8 + len, GFP_KERNEL);
  1712. if (!real_buffer)
  1713. return -ENOMEM;
  1714. memcpy(real_buffer + 8, buffer, len);
  1715. len += 8;
  1716. real_buffer[0] = 0;
  1717. real_buffer[1] = 0;
  1718. real_buffer[2] = data->medium_type;
  1719. real_buffer[3] = data->device_specific;
  1720. real_buffer[4] = data->longlba ? 0x01 : 0;
  1721. real_buffer[5] = 0;
  1722. real_buffer[6] = data->block_descriptor_length >> 8;
  1723. real_buffer[7] = data->block_descriptor_length;
  1724. cmd[0] = MODE_SELECT_10;
  1725. cmd[7] = len >> 8;
  1726. cmd[8] = len;
  1727. } else {
  1728. if (len > 255 || data->block_descriptor_length > 255 ||
  1729. data->longlba)
  1730. return -EINVAL;
  1731. real_buffer = kmalloc(4 + len, GFP_KERNEL);
  1732. if (!real_buffer)
  1733. return -ENOMEM;
  1734. memcpy(real_buffer + 4, buffer, len);
  1735. len += 4;
  1736. real_buffer[0] = 0;
  1737. real_buffer[1] = data->medium_type;
  1738. real_buffer[2] = data->device_specific;
  1739. real_buffer[3] = data->block_descriptor_length;
  1740. cmd[0] = MODE_SELECT;
  1741. cmd[4] = len;
  1742. }
  1743. ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
  1744. sshdr, timeout, retries);
  1745. kfree(real_buffer);
  1746. return ret;
  1747. }
  1748. EXPORT_SYMBOL_GPL(scsi_mode_select);
  1749. /**
  1750. * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
  1751. * @sdev: SCSI device to be queried
  1752. * @dbd: set if mode sense will allow block descriptors to be returned
  1753. * @modepage: mode page being requested
  1754. * @buffer: request buffer (may not be smaller than eight bytes)
  1755. * @len: length of request buffer.
  1756. * @timeout: command timeout
  1757. * @retries: number of retries before failing
  1758. * @data: returns a structure abstracting the mode header data
  1759. * @sshdr: place to put sense data (or NULL if no sense to be collected).
  1760. * must be SCSI_SENSE_BUFFERSIZE big.
  1761. *
  1762. * Returns zero if unsuccessful, or the header offset (either 4
  1763. * or 8 depending on whether a six or ten byte command was
  1764. * issued) if successful.
  1765. */
  1766. int
  1767. scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
  1768. unsigned char *buffer, int len, int timeout, int retries,
  1769. struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
  1770. {
  1771. unsigned char cmd[12];
  1772. int use_10_for_ms;
  1773. int header_length;
  1774. int result;
  1775. struct scsi_sense_hdr my_sshdr;
  1776. memset(data, 0, sizeof(*data));
  1777. memset(&cmd[0], 0, 12);
  1778. cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
  1779. cmd[2] = modepage;
  1780. /* caller might not be interested in sense, but we need it */
  1781. if (!sshdr)
  1782. sshdr = &my_sshdr;
  1783. retry:
  1784. use_10_for_ms = sdev->use_10_for_ms;
  1785. if (use_10_for_ms) {
  1786. if (len < 8)
  1787. len = 8;
  1788. cmd[0] = MODE_SENSE_10;
  1789. cmd[8] = len;
  1790. header_length = 8;
  1791. } else {
  1792. if (len < 4)
  1793. len = 4;
  1794. cmd[0] = MODE_SENSE;
  1795. cmd[4] = len;
  1796. header_length = 4;
  1797. }
  1798. memset(buffer, 0, len);
  1799. result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
  1800. sshdr, timeout, retries);
  1801. /* This code looks awful: what it's doing is making sure an
  1802. * ILLEGAL REQUEST sense return identifies the actual command
  1803. * byte as the problem. MODE_SENSE commands can return
  1804. * ILLEGAL REQUEST if the code page isn't supported */
  1805. if (use_10_for_ms && !scsi_status_is_good(result) &&
  1806. (driver_byte(result) & DRIVER_SENSE)) {
  1807. if (scsi_sense_valid(sshdr)) {
  1808. if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
  1809. (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
  1810. /*
  1811. * Invalid command operation code
  1812. */
  1813. sdev->use_10_for_ms = 0;
  1814. goto retry;
  1815. }
  1816. }
  1817. }
  1818. if(scsi_status_is_good(result)) {
  1819. if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
  1820. (modepage == 6 || modepage == 8))) {
  1821. /* Initio breakage? */
  1822. header_length = 0;
  1823. data->length = 13;
  1824. data->medium_type = 0;
  1825. data->device_specific = 0;
  1826. data->longlba = 0;
  1827. data->block_descriptor_length = 0;
  1828. } else if(use_10_for_ms) {
  1829. data->length = buffer[0]*256 + buffer[1] + 2;
  1830. data->medium_type = buffer[2];
  1831. data->device_specific = buffer[3];
  1832. data->longlba = buffer[4] & 0x01;
  1833. data->block_descriptor_length = buffer[6]*256
  1834. + buffer[7];
  1835. } else {
  1836. data->length = buffer[0] + 1;
  1837. data->medium_type = buffer[1];
  1838. data->device_specific = buffer[2];
  1839. data->block_descriptor_length = buffer[3];
  1840. }
  1841. data->header_length = header_length;
  1842. }
  1843. return result;
  1844. }
  1845. EXPORT_SYMBOL(scsi_mode_sense);
  1846. /**
  1847. * scsi_test_unit_ready - test if unit is ready
  1848. * @sdev: scsi device to change the state of.
  1849. * @timeout: command timeout
  1850. * @retries: number of retries before failing
  1851. * @sshdr_external: Optional pointer to struct scsi_sense_hdr for
  1852. * returning sense. Make sure that this is cleared before passing
  1853. * in.
  1854. *
  1855. * Returns zero if unsuccessful or an error if TUR failed. For
  1856. * removable media, a return of NOT_READY or UNIT_ATTENTION is
  1857. * translated to success, with the ->changed flag updated.
  1858. **/
  1859. int
  1860. scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
  1861. struct scsi_sense_hdr *sshdr_external)
  1862. {
  1863. char cmd[] = {
  1864. TEST_UNIT_READY, 0, 0, 0, 0, 0,
  1865. };
  1866. struct scsi_sense_hdr *sshdr;
  1867. int result;
  1868. if (!sshdr_external)
  1869. sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
  1870. else
  1871. sshdr = sshdr_external;
  1872. /* try to eat the UNIT_ATTENTION if there are enough retries */
  1873. do {
  1874. result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
  1875. timeout, retries);
  1876. if (sdev->removable && scsi_sense_valid(sshdr) &&
  1877. sshdr->sense_key == UNIT_ATTENTION)
  1878. sdev->changed = 1;
  1879. } while (scsi_sense_valid(sshdr) &&
  1880. sshdr->sense_key == UNIT_ATTENTION && --retries);
  1881. if (!sshdr)
  1882. /* could not allocate sense buffer, so can't process it */
  1883. return result;
  1884. if (sdev->removable && scsi_sense_valid(sshdr) &&
  1885. (sshdr->sense_key == UNIT_ATTENTION ||
  1886. sshdr->sense_key == NOT_READY)) {
  1887. sdev->changed = 1;
  1888. result = 0;
  1889. }
  1890. if (!sshdr_external)
  1891. kfree(sshdr);
  1892. return result;
  1893. }
  1894. EXPORT_SYMBOL(scsi_test_unit_ready);
  1895. /**
  1896. * scsi_device_set_state - Take the given device through the device state model.
  1897. * @sdev: scsi device to change the state of.
  1898. * @state: state to change to.
  1899. *
  1900. * Returns zero if unsuccessful or an error if the requested
  1901. * transition is illegal.
  1902. */
  1903. int
  1904. scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
  1905. {
  1906. enum scsi_device_state oldstate = sdev->sdev_state;
  1907. if (state == oldstate)
  1908. return 0;
  1909. switch (state) {
  1910. case SDEV_CREATED:
  1911. switch (oldstate) {
  1912. case SDEV_CREATED_BLOCK:
  1913. break;
  1914. default:
  1915. goto illegal;
  1916. }
  1917. break;
  1918. case SDEV_RUNNING:
  1919. switch (oldstate) {
  1920. case SDEV_CREATED:
  1921. case SDEV_OFFLINE:
  1922. case SDEV_QUIESCE:
  1923. case SDEV_BLOCK:
  1924. break;
  1925. default:
  1926. goto illegal;
  1927. }
  1928. break;
  1929. case SDEV_QUIESCE:
  1930. switch (oldstate) {
  1931. case SDEV_RUNNING:
  1932. case SDEV_OFFLINE:
  1933. break;
  1934. default:
  1935. goto illegal;
  1936. }
  1937. break;
  1938. case SDEV_OFFLINE:
  1939. switch (oldstate) {
  1940. case SDEV_CREATED:
  1941. case SDEV_RUNNING:
  1942. case SDEV_QUIESCE:
  1943. case SDEV_BLOCK:
  1944. break;
  1945. default:
  1946. goto illegal;
  1947. }
  1948. break;
  1949. case SDEV_BLOCK:
  1950. switch (oldstate) {
  1951. case SDEV_RUNNING:
  1952. case SDEV_CREATED_BLOCK:
  1953. break;
  1954. default:
  1955. goto illegal;
  1956. }
  1957. break;
  1958. case SDEV_CREATED_BLOCK:
  1959. switch (oldstate) {
  1960. case SDEV_CREATED:
  1961. break;
  1962. default:
  1963. goto illegal;
  1964. }
  1965. break;
  1966. case SDEV_CANCEL:
  1967. switch (oldstate) {
  1968. case SDEV_CREATED:
  1969. case SDEV_RUNNING:
  1970. case SDEV_QUIESCE:
  1971. case SDEV_OFFLINE:
  1972. case SDEV_BLOCK:
  1973. break;
  1974. default:
  1975. goto illegal;
  1976. }
  1977. break;
  1978. case SDEV_DEL:
  1979. switch (oldstate) {
  1980. case SDEV_CREATED:
  1981. case SDEV_RUNNING:
  1982. case SDEV_OFFLINE:
  1983. case SDEV_CANCEL:
  1984. break;
  1985. default:
  1986. goto illegal;
  1987. }
  1988. break;
  1989. }
  1990. sdev->sdev_state = state;
  1991. return 0;
  1992. illegal:
  1993. SCSI_LOG_ERROR_RECOVERY(1,
  1994. sdev_printk(KERN_ERR, sdev,
  1995. "Illegal state transition %s->%s\n",
  1996. scsi_device_state_name(oldstate),
  1997. scsi_device_state_name(state))
  1998. );
  1999. return -EINVAL;
  2000. }
  2001. EXPORT_SYMBOL(scsi_device_set_state);
  2002. /**
  2003. * sdev_evt_emit - emit a single SCSI device uevent
  2004. * @sdev: associated SCSI device
  2005. * @evt: event to emit
  2006. *
  2007. * Send a single uevent (scsi_event) to the associated scsi_device.
  2008. */
  2009. static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
  2010. {
  2011. int idx = 0;
  2012. char *envp[3];
  2013. switch (evt->evt_type) {
  2014. case SDEV_EVT_MEDIA_CHANGE:
  2015. envp[idx++] = "SDEV_MEDIA_CHANGE=1";
  2016. break;
  2017. default:
  2018. /* do nothing */
  2019. break;
  2020. }
  2021. envp[idx++] = NULL;
  2022. kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
  2023. }
  2024. /**
  2025. * sdev_evt_thread - send a uevent for each scsi event
  2026. * @work: work struct for scsi_device
  2027. *
  2028. * Dispatch queued events to their associated scsi_device kobjects
  2029. * as uevents.
  2030. */
  2031. void scsi_evt_thread(struct work_struct *work)
  2032. {
  2033. struct scsi_device *sdev;
  2034. LIST_HEAD(event_list);
  2035. sdev = container_of(work, struct scsi_device, event_work);
  2036. while (1) {
  2037. struct scsi_event *evt;
  2038. struct list_head *this, *tmp;
  2039. unsigned long flags;
  2040. spin_lock_irqsave(&sdev->list_lock, flags);
  2041. list_splice_init(&sdev->event_list, &event_list);
  2042. spin_unlock_irqrestore(&sdev->list_lock, flags);
  2043. if (list_empty(&event_list))
  2044. break;
  2045. list_for_each_safe(this, tmp, &event_list) {
  2046. evt = list_entry(this, struct scsi_event, node);
  2047. list_del(&evt->node);
  2048. scsi_evt_emit(sdev, evt);
  2049. kfree(evt);
  2050. }
  2051. }
  2052. }
  2053. /**
  2054. * sdev_evt_send - send asserted event to uevent thread
  2055. * @sdev: scsi_device event occurred on
  2056. * @evt: event to send
  2057. *
  2058. * Assert scsi device event asynchronously.
  2059. */
  2060. void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
  2061. {
  2062. unsigned long flags;
  2063. #if 0
  2064. /* FIXME: currently this check eliminates all media change events
  2065. * for polled devices. Need to update to discriminate between AN
  2066. * and polled events */
  2067. if (!test_bit(evt->evt_type, sdev->supported_events)) {
  2068. kfree(evt);
  2069. return;
  2070. }
  2071. #endif
  2072. spin_lock_irqsave(&sdev->list_lock, flags);
  2073. list_add_tail(&evt->node, &sdev->event_list);
  2074. schedule_work(&sdev->event_work);
  2075. spin_unlock_irqrestore(&sdev->list_lock, flags);
  2076. }
  2077. EXPORT_SYMBOL_GPL(sdev_evt_send);
  2078. /**
  2079. * sdev_evt_alloc - allocate a new scsi event
  2080. * @evt_type: type of event to allocate
  2081. * @gfpflags: GFP flags for allocation
  2082. *
  2083. * Allocates and returns a new scsi_event.
  2084. */
  2085. struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
  2086. gfp_t gfpflags)
  2087. {
  2088. struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
  2089. if (!evt)
  2090. return NULL;
  2091. evt->evt_type = evt_type;
  2092. INIT_LIST_HEAD(&evt->node);
  2093. /* evt_type-specific initialization, if any */
  2094. switch (evt_type) {
  2095. case SDEV_EVT_MEDIA_CHANGE:
  2096. default:
  2097. /* do nothing */
  2098. break;
  2099. }
  2100. return evt;
  2101. }
  2102. EXPORT_SYMBOL_GPL(sdev_evt_alloc);
  2103. /**
  2104. * sdev_evt_send_simple - send asserted event to uevent thread
  2105. * @sdev: scsi_device event occurred on
  2106. * @evt_type: type of event to send
  2107. * @gfpflags: GFP flags for allocation
  2108. *
  2109. * Assert scsi device event asynchronously, given an event type.
  2110. */
  2111. void sdev_evt_send_simple(struct scsi_device *sdev,
  2112. enum scsi_device_event evt_type, gfp_t gfpflags)
  2113. {
  2114. struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
  2115. if (!evt) {
  2116. sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
  2117. evt_type);
  2118. return;
  2119. }
  2120. sdev_evt_send(sdev, evt);
  2121. }
  2122. EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
  2123. /**
  2124. * scsi_device_quiesce - Block user issued commands.
  2125. * @sdev: scsi device to quiesce.
  2126. *
  2127. * This works by trying to transition to the SDEV_QUIESCE state
  2128. * (which must be a legal transition). When the device is in this
  2129. * state, only special requests will be accepted, all others will
  2130. * be deferred. Since special requests may also be requeued requests,
  2131. * a successful return doesn't guarantee the device will be
  2132. * totally quiescent.
  2133. *
  2134. * Must be called with user context, may sleep.
  2135. *
  2136. * Returns zero if unsuccessful or an error if not.
  2137. */
  2138. int
  2139. scsi_device_quiesce(struct scsi_device *sdev)
  2140. {
  2141. int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
  2142. if (err)
  2143. return err;
  2144. scsi_run_queue(sdev->request_queue);
  2145. while (sdev->device_busy) {
  2146. msleep_interruptible(200);
  2147. scsi_run_queue(sdev->request_queue);
  2148. }
  2149. return 0;
  2150. }
  2151. EXPORT_SYMBOL(scsi_device_quiesce);
  2152. /**
  2153. * scsi_device_resume - Restart user issued commands to a quiesced device.
  2154. * @sdev: scsi device to resume.
  2155. *
  2156. * Moves the device from quiesced back to running and restarts the
  2157. * queues.
  2158. *
  2159. * Must be called with user context, may sleep.
  2160. */
  2161. void
  2162. scsi_device_resume(struct scsi_device *sdev)
  2163. {
  2164. if(scsi_device_set_state(sdev, SDEV_RUNNING))
  2165. return;
  2166. scsi_run_queue(sdev->request_queue);
  2167. }
  2168. EXPORT_SYMBOL(scsi_device_resume);
  2169. static void
  2170. device_quiesce_fn(struct scsi_device *sdev, void *data)
  2171. {
  2172. scsi_device_quiesce(sdev);
  2173. }
  2174. void
  2175. scsi_target_quiesce(struct scsi_target *starget)
  2176. {
  2177. starget_for_each_device(starget, NULL, device_quiesce_fn);
  2178. }
  2179. EXPORT_SYMBOL(scsi_target_quiesce);
  2180. static void
  2181. device_resume_fn(struct scsi_device *sdev, void *data)
  2182. {
  2183. scsi_device_resume(sdev);
  2184. }
  2185. void
  2186. scsi_target_resume(struct scsi_target *starget)
  2187. {
  2188. starget_for_each_device(starget, NULL, device_resume_fn);
  2189. }
  2190. EXPORT_SYMBOL(scsi_target_resume);
  2191. /**
  2192. * scsi_internal_device_block - internal function to put a device temporarily into the SDEV_BLOCK state
  2193. * @sdev: device to block
  2194. *
  2195. * Block request made by scsi lld's to temporarily stop all
  2196. * scsi commands on the specified device. Called from interrupt
  2197. * or normal process context.
  2198. *
  2199. * Returns zero if successful or error if not
  2200. *
  2201. * Notes:
  2202. * This routine transitions the device to the SDEV_BLOCK state
  2203. * (which must be a legal transition). When the device is in this
  2204. * state, all commands are deferred until the scsi lld reenables
  2205. * the device with scsi_device_unblock or device_block_tmo fires.
  2206. * This routine assumes the host_lock is held on entry.
  2207. */
  2208. int
  2209. scsi_internal_device_block(struct scsi_device *sdev)
  2210. {
  2211. struct request_queue *q = sdev->request_queue;
  2212. unsigned long flags;
  2213. int err = 0;
  2214. err = scsi_device_set_state(sdev, SDEV_BLOCK);
  2215. if (err) {
  2216. err = scsi_device_set_state(sdev, SDEV_CREATED_BLOCK);
  2217. if (err)
  2218. return err;
  2219. }
  2220. /*
  2221. * The device has transitioned to SDEV_BLOCK. Stop the
  2222. * block layer from calling the midlayer with this device's
  2223. * request queue.
  2224. */
  2225. spin_lock_irqsave(q->queue_lock, flags);
  2226. blk_stop_queue(q);
  2227. spin_unlock_irqrestore(q->queue_lock, flags);
  2228. return 0;
  2229. }
  2230. EXPORT_SYMBOL_GPL(scsi_internal_device_block);
  2231. /**
  2232. * scsi_internal_device_unblock - resume a device after a block request
  2233. * @sdev: device to resume
  2234. *
  2235. * Called by scsi lld's or the midlayer to restart the device queue
  2236. * for the previously suspended scsi device. Called from interrupt or
  2237. * normal process context.
  2238. *
  2239. * Returns zero if successful or error if not.
  2240. *
  2241. * Notes:
  2242. * This routine transitions the device to the SDEV_RUNNING state
  2243. * (which must be a legal transition) allowing the midlayer to
  2244. * goose the queue for this device. This routine assumes the
  2245. * host_lock is held upon entry.
  2246. */
  2247. int
  2248. scsi_internal_device_unblock(struct scsi_device *sdev)
  2249. {
  2250. struct request_queue *q = sdev->request_queue;
  2251. int err;
  2252. unsigned long flags;
  2253. /*
  2254. * Try to transition the scsi device to SDEV_RUNNING
  2255. * and goose the device queue if successful.
  2256. */
  2257. err = scsi_device_set_state(sdev, SDEV_RUNNING);
  2258. if (err) {
  2259. err = scsi_device_set_state(sdev, SDEV_CREATED);
  2260. if (err)
  2261. return err;
  2262. }
  2263. spin_lock_irqsave(q->queue_lock, flags);
  2264. blk_start_queue(q);
  2265. spin_unlock_irqrestore(q->queue_lock, flags);
  2266. return 0;
  2267. }
  2268. EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
  2269. static void
  2270. device_block(struct scsi_device *sdev, void *data)
  2271. {
  2272. scsi_internal_device_block(sdev);
  2273. }
  2274. static int
  2275. target_block(struct device *dev, void *data)
  2276. {
  2277. if (scsi_is_target_device(dev))
  2278. starget_for_each_device(to_scsi_target(dev), NULL,
  2279. device_block);
  2280. return 0;
  2281. }
  2282. void
  2283. scsi_target_block(struct device *dev)
  2284. {
  2285. if (scsi_is_target_device(dev))
  2286. starget_for_each_device(to_scsi_target(dev), NULL,
  2287. device_block);
  2288. else
  2289. device_for_each_child(dev, NULL, target_block);
  2290. }
  2291. EXPORT_SYMBOL_GPL(scsi_target_block);
  2292. static void
  2293. device_unblock(struct scsi_device *sdev, void *data)
  2294. {
  2295. scsi_internal_device_unblock(sdev);
  2296. }
  2297. static int
  2298. target_unblock(struct device *dev, void *data)
  2299. {
  2300. if (scsi_is_target_device(dev))
  2301. starget_for_each_device(to_scsi_target(dev), NULL,
  2302. device_unblock);
  2303. return 0;
  2304. }
  2305. void
  2306. scsi_target_unblock(struct device *dev)
  2307. {
  2308. if (scsi_is_target_device(dev))
  2309. starget_for_each_device(to_scsi_target(dev), NULL,
  2310. device_unblock);
  2311. else
  2312. device_for_each_child(dev, NULL, target_unblock);
  2313. }
  2314. EXPORT_SYMBOL_GPL(scsi_target_unblock);
  2315. /**
  2316. * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
  2317. * @sgl: scatter-gather list
  2318. * @sg_count: number of segments in sg
  2319. * @offset: offset in bytes into sg, on return offset into the mapped area
  2320. * @len: bytes to map, on return number of bytes mapped
  2321. *
  2322. * Returns virtual address of the start of the mapped page
  2323. */
  2324. void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
  2325. size_t *offset, size_t *len)
  2326. {
  2327. int i;
  2328. size_t sg_len = 0, len_complete = 0;
  2329. struct scatterlist *sg;
  2330. struct page *page;
  2331. WARN_ON(!irqs_disabled());
  2332. for_each_sg(sgl, sg, sg_count, i) {
  2333. len_complete = sg_len; /* Complete sg-entries */
  2334. sg_len += sg->length;
  2335. if (sg_len > *offset)
  2336. break;
  2337. }
  2338. if (unlikely(i == sg_count)) {
  2339. printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
  2340. "elements %d\n",
  2341. __func__, sg_len, *offset, sg_count);
  2342. WARN_ON(1);
  2343. return NULL;
  2344. }
  2345. /* Offset starting from the beginning of first page in this sg-entry */
  2346. *offset = *offset - len_complete + sg->offset;
  2347. /* Assumption: contiguous pages can be accessed as "page + i" */
  2348. page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
  2349. *offset &= ~PAGE_MASK;
  2350. /* Bytes in this sg-entry from *offset to the end of the page */
  2351. sg_len = PAGE_SIZE - *offset;
  2352. if (*len > sg_len)
  2353. *len = sg_len;
  2354. return kmap_atomic(page, KM_BIO_SRC_IRQ);
  2355. }
  2356. EXPORT_SYMBOL(scsi_kmap_atomic_sg);
  2357. /**
  2358. * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg
  2359. * @virt: virtual address to be unmapped
  2360. */
  2361. void scsi_kunmap_atomic_sg(void *virt)
  2362. {
  2363. kunmap_atomic(virt, KM_BIO_SRC_IRQ);
  2364. }
  2365. EXPORT_SYMBOL(scsi_kunmap_atomic_sg);