scsi_lib.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514
  1. /*
  2. * Copyright (C) 1999 Eric Youngdale
  3. * Copyright (C) 2014 Christoph Hellwig
  4. *
  5. * SCSI queueing library.
  6. * Initial versions: Eric Youngdale (eric@andante.org).
  7. * Based upon conversations with large numbers
  8. * of people at Linux Expo.
  9. */
  10. #include <linux/bio.h>
  11. #include <linux/bitops.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/completion.h>
  14. #include <linux/kernel.h>
  15. #include <linux/export.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 <linux/blk-mq.h>
  22. #include <linux/ratelimit.h>
  23. #include <asm/unaligned.h>
  24. #include <scsi/scsi.h>
  25. #include <scsi/scsi_cmnd.h>
  26. #include <scsi/scsi_dbg.h>
  27. #include <scsi/scsi_device.h>
  28. #include <scsi/scsi_driver.h>
  29. #include <scsi/scsi_eh.h>
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_transport.h> /* __scsi_init_queue() */
  32. #include <scsi/scsi_dh.h>
  33. #include <trace/events/scsi.h>
  34. #include "scsi_debugfs.h"
  35. #include "scsi_priv.h"
  36. #include "scsi_logging.h"
  37. static struct kmem_cache *scsi_sdb_cache;
  38. static struct kmem_cache *scsi_sense_cache;
  39. static struct kmem_cache *scsi_sense_isadma_cache;
  40. static DEFINE_MUTEX(scsi_sense_cache_mutex);
  41. static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd);
  42. static inline struct kmem_cache *
  43. scsi_select_sense_cache(bool unchecked_isa_dma)
  44. {
  45. return unchecked_isa_dma ? scsi_sense_isadma_cache : scsi_sense_cache;
  46. }
  47. static void scsi_free_sense_buffer(bool unchecked_isa_dma,
  48. unsigned char *sense_buffer)
  49. {
  50. kmem_cache_free(scsi_select_sense_cache(unchecked_isa_dma),
  51. sense_buffer);
  52. }
  53. static unsigned char *scsi_alloc_sense_buffer(bool unchecked_isa_dma,
  54. gfp_t gfp_mask, int numa_node)
  55. {
  56. return kmem_cache_alloc_node(scsi_select_sense_cache(unchecked_isa_dma),
  57. gfp_mask, numa_node);
  58. }
  59. int scsi_init_sense_cache(struct Scsi_Host *shost)
  60. {
  61. struct kmem_cache *cache;
  62. int ret = 0;
  63. cache = scsi_select_sense_cache(shost->unchecked_isa_dma);
  64. if (cache)
  65. return 0;
  66. mutex_lock(&scsi_sense_cache_mutex);
  67. if (shost->unchecked_isa_dma) {
  68. scsi_sense_isadma_cache =
  69. kmem_cache_create("scsi_sense_cache(DMA)",
  70. SCSI_SENSE_BUFFERSIZE, 0,
  71. SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA, NULL);
  72. if (!scsi_sense_isadma_cache)
  73. ret = -ENOMEM;
  74. } else {
  75. scsi_sense_cache =
  76. kmem_cache_create("scsi_sense_cache",
  77. SCSI_SENSE_BUFFERSIZE, 0, SLAB_HWCACHE_ALIGN, NULL);
  78. if (!scsi_sense_cache)
  79. ret = -ENOMEM;
  80. }
  81. mutex_unlock(&scsi_sense_cache_mutex);
  82. return ret;
  83. }
  84. /*
  85. * When to reinvoke queueing after a resource shortage. It's 3 msecs to
  86. * not change behaviour from the previous unplug mechanism, experimentation
  87. * may prove this needs changing.
  88. */
  89. #define SCSI_QUEUE_DELAY 3
  90. static void
  91. scsi_set_blocked(struct scsi_cmnd *cmd, int reason)
  92. {
  93. struct Scsi_Host *host = cmd->device->host;
  94. struct scsi_device *device = cmd->device;
  95. struct scsi_target *starget = scsi_target(device);
  96. /*
  97. * Set the appropriate busy bit for the device/host.
  98. *
  99. * If the host/device isn't busy, assume that something actually
  100. * completed, and that we should be able to queue a command now.
  101. *
  102. * Note that the prior mid-layer assumption that any host could
  103. * always queue at least one command is now broken. The mid-layer
  104. * will implement a user specifiable stall (see
  105. * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
  106. * if a command is requeued with no other commands outstanding
  107. * either for the device or for the host.
  108. */
  109. switch (reason) {
  110. case SCSI_MLQUEUE_HOST_BUSY:
  111. atomic_set(&host->host_blocked, host->max_host_blocked);
  112. break;
  113. case SCSI_MLQUEUE_DEVICE_BUSY:
  114. case SCSI_MLQUEUE_EH_RETRY:
  115. atomic_set(&device->device_blocked,
  116. device->max_device_blocked);
  117. break;
  118. case SCSI_MLQUEUE_TARGET_BUSY:
  119. atomic_set(&starget->target_blocked,
  120. starget->max_target_blocked);
  121. break;
  122. }
  123. }
  124. static void scsi_mq_requeue_cmd(struct scsi_cmnd *cmd)
  125. {
  126. struct scsi_device *sdev = cmd->device;
  127. if (cmd->request->rq_flags & RQF_DONTPREP) {
  128. cmd->request->rq_flags &= ~RQF_DONTPREP;
  129. scsi_mq_uninit_cmd(cmd);
  130. } else {
  131. WARN_ON_ONCE(true);
  132. }
  133. blk_mq_requeue_request(cmd->request, true);
  134. put_device(&sdev->sdev_gendev);
  135. }
  136. /**
  137. * __scsi_queue_insert - private queue insertion
  138. * @cmd: The SCSI command being requeued
  139. * @reason: The reason for the requeue
  140. * @unbusy: Whether the queue should be unbusied
  141. *
  142. * This is a private queue insertion. The public interface
  143. * scsi_queue_insert() always assumes the queue should be unbusied
  144. * because it's always called before the completion. This function is
  145. * for a requeue after completion, which should only occur in this
  146. * file.
  147. */
  148. static void __scsi_queue_insert(struct scsi_cmnd *cmd, int reason, int unbusy)
  149. {
  150. struct scsi_device *device = cmd->device;
  151. struct request_queue *q = device->request_queue;
  152. unsigned long flags;
  153. SCSI_LOG_MLQUEUE(1, scmd_printk(KERN_INFO, cmd,
  154. "Inserting command %p into mlqueue\n", cmd));
  155. scsi_set_blocked(cmd, reason);
  156. /*
  157. * Decrement the counters, since these commands are no longer
  158. * active on the host/device.
  159. */
  160. if (unbusy)
  161. scsi_device_unbusy(device);
  162. /*
  163. * Requeue this command. It will go before all other commands
  164. * that are already in the queue. Schedule requeue work under
  165. * lock such that the kblockd_schedule_work() call happens
  166. * before blk_cleanup_queue() finishes.
  167. */
  168. cmd->result = 0;
  169. if (q->mq_ops) {
  170. scsi_mq_requeue_cmd(cmd);
  171. return;
  172. }
  173. spin_lock_irqsave(q->queue_lock, flags);
  174. blk_requeue_request(q, cmd->request);
  175. kblockd_schedule_work(&device->requeue_work);
  176. spin_unlock_irqrestore(q->queue_lock, flags);
  177. }
  178. /*
  179. * Function: scsi_queue_insert()
  180. *
  181. * Purpose: Insert a command in the midlevel queue.
  182. *
  183. * Arguments: cmd - command that we are adding to queue.
  184. * reason - why we are inserting command to queue.
  185. *
  186. * Lock status: Assumed that lock is not held upon entry.
  187. *
  188. * Returns: Nothing.
  189. *
  190. * Notes: We do this for one of two cases. Either the host is busy
  191. * and it cannot accept any more commands for the time being,
  192. * or the device returned QUEUE_FULL and can accept no more
  193. * commands.
  194. * Notes: This could be called either from an interrupt context or a
  195. * normal process context.
  196. */
  197. void scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
  198. {
  199. __scsi_queue_insert(cmd, reason, 1);
  200. }
  201. /**
  202. * scsi_execute - insert request and wait for the result
  203. * @sdev: scsi device
  204. * @cmd: scsi command
  205. * @data_direction: data direction
  206. * @buffer: data buffer
  207. * @bufflen: len of buffer
  208. * @sense: optional sense buffer
  209. * @sshdr: optional decoded sense header
  210. * @timeout: request timeout in seconds
  211. * @retries: number of times to retry request
  212. * @flags: flags for ->cmd_flags
  213. * @rq_flags: flags for ->rq_flags
  214. * @resid: optional residual length
  215. *
  216. * Returns the scsi_cmnd result field if a command was executed, or a negative
  217. * Linux error code if we didn't get that far.
  218. */
  219. int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
  220. int data_direction, void *buffer, unsigned bufflen,
  221. unsigned char *sense, struct scsi_sense_hdr *sshdr,
  222. int timeout, int retries, u64 flags, req_flags_t rq_flags,
  223. int *resid)
  224. {
  225. struct request *req;
  226. struct scsi_request *rq;
  227. int ret = DRIVER_ERROR << 24;
  228. req = blk_get_request(sdev->request_queue,
  229. data_direction == DMA_TO_DEVICE ?
  230. REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, __GFP_RECLAIM);
  231. if (IS_ERR(req))
  232. return ret;
  233. rq = scsi_req(req);
  234. if (bufflen && blk_rq_map_kern(sdev->request_queue, req,
  235. buffer, bufflen, __GFP_RECLAIM))
  236. goto out;
  237. rq->cmd_len = COMMAND_SIZE(cmd[0]);
  238. memcpy(rq->cmd, cmd, rq->cmd_len);
  239. rq->retries = retries;
  240. req->timeout = timeout;
  241. req->cmd_flags |= flags;
  242. req->rq_flags |= rq_flags | RQF_QUIET | RQF_PREEMPT;
  243. /*
  244. * head injection *required* here otherwise quiesce won't work
  245. */
  246. blk_execute_rq(req->q, NULL, req, 1);
  247. /*
  248. * Some devices (USB mass-storage in particular) may transfer
  249. * garbage data together with a residue indicating that the data
  250. * is invalid. Prevent the garbage from being misinterpreted
  251. * and prevent security leaks by zeroing out the excess data.
  252. */
  253. if (unlikely(rq->resid_len > 0 && rq->resid_len <= bufflen))
  254. memset(buffer + (bufflen - rq->resid_len), 0, rq->resid_len);
  255. if (resid)
  256. *resid = rq->resid_len;
  257. if (sense && rq->sense_len)
  258. memcpy(sense, rq->sense, SCSI_SENSE_BUFFERSIZE);
  259. if (sshdr)
  260. scsi_normalize_sense(rq->sense, rq->sense_len, sshdr);
  261. ret = rq->result;
  262. out:
  263. blk_put_request(req);
  264. return ret;
  265. }
  266. EXPORT_SYMBOL(scsi_execute);
  267. /*
  268. * Function: scsi_init_cmd_errh()
  269. *
  270. * Purpose: Initialize cmd fields related to error handling.
  271. *
  272. * Arguments: cmd - command that is ready to be queued.
  273. *
  274. * Notes: This function has the job of initializing a number of
  275. * fields related to error handling. Typically this will
  276. * be called once for each command, as required.
  277. */
  278. static void scsi_init_cmd_errh(struct scsi_cmnd *cmd)
  279. {
  280. cmd->serial_number = 0;
  281. scsi_set_resid(cmd, 0);
  282. memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  283. if (cmd->cmd_len == 0)
  284. cmd->cmd_len = scsi_command_size(cmd->cmnd);
  285. }
  286. void scsi_device_unbusy(struct scsi_device *sdev)
  287. {
  288. struct Scsi_Host *shost = sdev->host;
  289. struct scsi_target *starget = scsi_target(sdev);
  290. unsigned long flags;
  291. atomic_dec(&shost->host_busy);
  292. if (starget->can_queue > 0)
  293. atomic_dec(&starget->target_busy);
  294. if (unlikely(scsi_host_in_recovery(shost) &&
  295. (shost->host_failed || shost->host_eh_scheduled))) {
  296. spin_lock_irqsave(shost->host_lock, flags);
  297. scsi_eh_wakeup(shost);
  298. spin_unlock_irqrestore(shost->host_lock, flags);
  299. }
  300. atomic_dec(&sdev->device_busy);
  301. }
  302. static void scsi_kick_queue(struct request_queue *q)
  303. {
  304. if (q->mq_ops)
  305. blk_mq_start_hw_queues(q);
  306. else
  307. blk_run_queue(q);
  308. }
  309. /*
  310. * Called for single_lun devices on IO completion. Clear starget_sdev_user,
  311. * and call blk_run_queue for all the scsi_devices on the target -
  312. * including current_sdev first.
  313. *
  314. * Called with *no* scsi locks held.
  315. */
  316. static void scsi_single_lun_run(struct scsi_device *current_sdev)
  317. {
  318. struct Scsi_Host *shost = current_sdev->host;
  319. struct scsi_device *sdev, *tmp;
  320. struct scsi_target *starget = scsi_target(current_sdev);
  321. unsigned long flags;
  322. spin_lock_irqsave(shost->host_lock, flags);
  323. starget->starget_sdev_user = NULL;
  324. spin_unlock_irqrestore(shost->host_lock, flags);
  325. /*
  326. * Call blk_run_queue for all LUNs on the target, starting with
  327. * current_sdev. We race with others (to set starget_sdev_user),
  328. * but in most cases, we will be first. Ideally, each LU on the
  329. * target would get some limited time or requests on the target.
  330. */
  331. scsi_kick_queue(current_sdev->request_queue);
  332. spin_lock_irqsave(shost->host_lock, flags);
  333. if (starget->starget_sdev_user)
  334. goto out;
  335. list_for_each_entry_safe(sdev, tmp, &starget->devices,
  336. same_target_siblings) {
  337. if (sdev == current_sdev)
  338. continue;
  339. if (scsi_device_get(sdev))
  340. continue;
  341. spin_unlock_irqrestore(shost->host_lock, flags);
  342. scsi_kick_queue(sdev->request_queue);
  343. spin_lock_irqsave(shost->host_lock, flags);
  344. scsi_device_put(sdev);
  345. }
  346. out:
  347. spin_unlock_irqrestore(shost->host_lock, flags);
  348. }
  349. static inline bool scsi_device_is_busy(struct scsi_device *sdev)
  350. {
  351. if (atomic_read(&sdev->device_busy) >= sdev->queue_depth)
  352. return true;
  353. if (atomic_read(&sdev->device_blocked) > 0)
  354. return true;
  355. return false;
  356. }
  357. static inline bool scsi_target_is_busy(struct scsi_target *starget)
  358. {
  359. if (starget->can_queue > 0) {
  360. if (atomic_read(&starget->target_busy) >= starget->can_queue)
  361. return true;
  362. if (atomic_read(&starget->target_blocked) > 0)
  363. return true;
  364. }
  365. return false;
  366. }
  367. static inline bool scsi_host_is_busy(struct Scsi_Host *shost)
  368. {
  369. if (shost->can_queue > 0 &&
  370. atomic_read(&shost->host_busy) >= shost->can_queue)
  371. return true;
  372. if (atomic_read(&shost->host_blocked) > 0)
  373. return true;
  374. if (shost->host_self_blocked)
  375. return true;
  376. return false;
  377. }
  378. static void scsi_starved_list_run(struct Scsi_Host *shost)
  379. {
  380. LIST_HEAD(starved_list);
  381. struct scsi_device *sdev;
  382. unsigned long flags;
  383. spin_lock_irqsave(shost->host_lock, flags);
  384. list_splice_init(&shost->starved_list, &starved_list);
  385. while (!list_empty(&starved_list)) {
  386. struct request_queue *slq;
  387. /*
  388. * As long as shost is accepting commands and we have
  389. * starved queues, call blk_run_queue. scsi_request_fn
  390. * drops the queue_lock and can add us back to the
  391. * starved_list.
  392. *
  393. * host_lock protects the starved_list and starved_entry.
  394. * scsi_request_fn must get the host_lock before checking
  395. * or modifying starved_list or starved_entry.
  396. */
  397. if (scsi_host_is_busy(shost))
  398. break;
  399. sdev = list_entry(starved_list.next,
  400. struct scsi_device, starved_entry);
  401. list_del_init(&sdev->starved_entry);
  402. if (scsi_target_is_busy(scsi_target(sdev))) {
  403. list_move_tail(&sdev->starved_entry,
  404. &shost->starved_list);
  405. continue;
  406. }
  407. /*
  408. * Once we drop the host lock, a racing scsi_remove_device()
  409. * call may remove the sdev from the starved list and destroy
  410. * it and the queue. Mitigate by taking a reference to the
  411. * queue and never touching the sdev again after we drop the
  412. * host lock. Note: if __scsi_remove_device() invokes
  413. * blk_cleanup_queue() before the queue is run from this
  414. * function then blk_run_queue() will return immediately since
  415. * blk_cleanup_queue() marks the queue with QUEUE_FLAG_DYING.
  416. */
  417. slq = sdev->request_queue;
  418. if (!blk_get_queue(slq))
  419. continue;
  420. spin_unlock_irqrestore(shost->host_lock, flags);
  421. scsi_kick_queue(slq);
  422. blk_put_queue(slq);
  423. spin_lock_irqsave(shost->host_lock, flags);
  424. }
  425. /* put any unprocessed entries back */
  426. list_splice(&starved_list, &shost->starved_list);
  427. spin_unlock_irqrestore(shost->host_lock, flags);
  428. }
  429. /*
  430. * Function: scsi_run_queue()
  431. *
  432. * Purpose: Select a proper request queue to serve next
  433. *
  434. * Arguments: q - last request's queue
  435. *
  436. * Returns: Nothing
  437. *
  438. * Notes: The previous command was completely finished, start
  439. * a new one if possible.
  440. */
  441. static void scsi_run_queue(struct request_queue *q)
  442. {
  443. struct scsi_device *sdev = q->queuedata;
  444. if (scsi_target(sdev)->single_lun)
  445. scsi_single_lun_run(sdev);
  446. if (!list_empty(&sdev->host->starved_list))
  447. scsi_starved_list_run(sdev->host);
  448. if (q->mq_ops)
  449. blk_mq_run_hw_queues(q, false);
  450. else
  451. blk_run_queue(q);
  452. }
  453. void scsi_requeue_run_queue(struct work_struct *work)
  454. {
  455. struct scsi_device *sdev;
  456. struct request_queue *q;
  457. sdev = container_of(work, struct scsi_device, requeue_work);
  458. q = sdev->request_queue;
  459. scsi_run_queue(q);
  460. }
  461. /*
  462. * Function: scsi_requeue_command()
  463. *
  464. * Purpose: Handle post-processing of completed commands.
  465. *
  466. * Arguments: q - queue to operate on
  467. * cmd - command that may need to be requeued.
  468. *
  469. * Returns: Nothing
  470. *
  471. * Notes: After command completion, there may be blocks left
  472. * over which weren't finished by the previous command
  473. * this can be for a number of reasons - the main one is
  474. * I/O errors in the middle of the request, in which case
  475. * we need to request the blocks that come after the bad
  476. * sector.
  477. * Notes: Upon return, cmd is a stale pointer.
  478. */
  479. static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
  480. {
  481. struct scsi_device *sdev = cmd->device;
  482. struct request *req = cmd->request;
  483. unsigned long flags;
  484. spin_lock_irqsave(q->queue_lock, flags);
  485. blk_unprep_request(req);
  486. req->special = NULL;
  487. scsi_put_command(cmd);
  488. blk_requeue_request(q, req);
  489. spin_unlock_irqrestore(q->queue_lock, flags);
  490. scsi_run_queue(q);
  491. put_device(&sdev->sdev_gendev);
  492. }
  493. void scsi_run_host_queues(struct Scsi_Host *shost)
  494. {
  495. struct scsi_device *sdev;
  496. shost_for_each_device(sdev, shost)
  497. scsi_run_queue(sdev->request_queue);
  498. }
  499. static void scsi_uninit_cmd(struct scsi_cmnd *cmd)
  500. {
  501. if (!blk_rq_is_passthrough(cmd->request)) {
  502. struct scsi_driver *drv = scsi_cmd_to_driver(cmd);
  503. if (drv->uninit_command)
  504. drv->uninit_command(cmd);
  505. }
  506. }
  507. static void scsi_mq_free_sgtables(struct scsi_cmnd *cmd)
  508. {
  509. struct scsi_data_buffer *sdb;
  510. if (cmd->sdb.table.nents)
  511. sg_free_table_chained(&cmd->sdb.table, true);
  512. if (cmd->request->next_rq) {
  513. sdb = cmd->request->next_rq->special;
  514. if (sdb)
  515. sg_free_table_chained(&sdb->table, true);
  516. }
  517. if (scsi_prot_sg_count(cmd))
  518. sg_free_table_chained(&cmd->prot_sdb->table, true);
  519. }
  520. static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd)
  521. {
  522. scsi_mq_free_sgtables(cmd);
  523. scsi_uninit_cmd(cmd);
  524. scsi_del_cmd_from_list(cmd);
  525. }
  526. /*
  527. * Function: scsi_release_buffers()
  528. *
  529. * Purpose: Free resources allocate for a scsi_command.
  530. *
  531. * Arguments: cmd - command that we are bailing.
  532. *
  533. * Lock status: Assumed that no lock is held upon entry.
  534. *
  535. * Returns: Nothing
  536. *
  537. * Notes: In the event that an upper level driver rejects a
  538. * command, we must release resources allocated during
  539. * the __init_io() function. Primarily this would involve
  540. * the scatter-gather table.
  541. */
  542. static void scsi_release_buffers(struct scsi_cmnd *cmd)
  543. {
  544. if (cmd->sdb.table.nents)
  545. sg_free_table_chained(&cmd->sdb.table, false);
  546. memset(&cmd->sdb, 0, sizeof(cmd->sdb));
  547. if (scsi_prot_sg_count(cmd))
  548. sg_free_table_chained(&cmd->prot_sdb->table, false);
  549. }
  550. static void scsi_release_bidi_buffers(struct scsi_cmnd *cmd)
  551. {
  552. struct scsi_data_buffer *bidi_sdb = cmd->request->next_rq->special;
  553. sg_free_table_chained(&bidi_sdb->table, false);
  554. kmem_cache_free(scsi_sdb_cache, bidi_sdb);
  555. cmd->request->next_rq->special = NULL;
  556. }
  557. static bool scsi_end_request(struct request *req, blk_status_t error,
  558. unsigned int bytes, unsigned int bidi_bytes)
  559. {
  560. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
  561. struct scsi_device *sdev = cmd->device;
  562. struct request_queue *q = sdev->request_queue;
  563. if (blk_update_request(req, error, bytes))
  564. return true;
  565. /* Bidi request must be completed as a whole */
  566. if (unlikely(bidi_bytes) &&
  567. blk_update_request(req->next_rq, error, bidi_bytes))
  568. return true;
  569. if (blk_queue_add_random(q))
  570. add_disk_randomness(req->rq_disk);
  571. if (!blk_rq_is_scsi(req)) {
  572. WARN_ON_ONCE(!(cmd->flags & SCMD_INITIALIZED));
  573. cmd->flags &= ~SCMD_INITIALIZED;
  574. }
  575. if (req->mq_ctx) {
  576. /*
  577. * In the MQ case the command gets freed by __blk_mq_end_request,
  578. * so we have to do all cleanup that depends on it earlier.
  579. *
  580. * We also can't kick the queues from irq context, so we
  581. * will have to defer it to a workqueue.
  582. */
  583. scsi_mq_uninit_cmd(cmd);
  584. __blk_mq_end_request(req, error);
  585. if (scsi_target(sdev)->single_lun ||
  586. !list_empty(&sdev->host->starved_list))
  587. kblockd_schedule_work(&sdev->requeue_work);
  588. else
  589. blk_mq_run_hw_queues(q, true);
  590. } else {
  591. unsigned long flags;
  592. if (bidi_bytes)
  593. scsi_release_bidi_buffers(cmd);
  594. scsi_release_buffers(cmd);
  595. scsi_put_command(cmd);
  596. spin_lock_irqsave(q->queue_lock, flags);
  597. blk_finish_request(req, error);
  598. spin_unlock_irqrestore(q->queue_lock, flags);
  599. scsi_run_queue(q);
  600. }
  601. put_device(&sdev->sdev_gendev);
  602. return false;
  603. }
  604. /**
  605. * __scsi_error_from_host_byte - translate SCSI error code into errno
  606. * @cmd: SCSI command (unused)
  607. * @result: scsi error code
  608. *
  609. * Translate SCSI error code into block errors.
  610. */
  611. static blk_status_t __scsi_error_from_host_byte(struct scsi_cmnd *cmd,
  612. int result)
  613. {
  614. switch (host_byte(result)) {
  615. case DID_TRANSPORT_FAILFAST:
  616. return BLK_STS_TRANSPORT;
  617. case DID_TARGET_FAILURE:
  618. set_host_byte(cmd, DID_OK);
  619. return BLK_STS_TARGET;
  620. case DID_NEXUS_FAILURE:
  621. return BLK_STS_NEXUS;
  622. case DID_ALLOC_FAILURE:
  623. set_host_byte(cmd, DID_OK);
  624. return BLK_STS_NOSPC;
  625. case DID_MEDIUM_ERROR:
  626. set_host_byte(cmd, DID_OK);
  627. return BLK_STS_MEDIUM;
  628. default:
  629. return BLK_STS_IOERR;
  630. }
  631. }
  632. /*
  633. * Function: scsi_io_completion()
  634. *
  635. * Purpose: Completion processing for block device I/O requests.
  636. *
  637. * Arguments: cmd - command that is finished.
  638. *
  639. * Lock status: Assumed that no lock is held upon entry.
  640. *
  641. * Returns: Nothing
  642. *
  643. * Notes: We will finish off the specified number of sectors. If we
  644. * are done, the command block will be released and the queue
  645. * function will be goosed. If we are not done then we have to
  646. * figure out what to do next:
  647. *
  648. * a) We can call scsi_requeue_command(). The request
  649. * will be unprepared and put back on the queue. Then
  650. * a new command will be created for it. This should
  651. * be used if we made forward progress, or if we want
  652. * to switch from READ(10) to READ(6) for example.
  653. *
  654. * b) We can call __scsi_queue_insert(). The request will
  655. * be put back on the queue and retried using the same
  656. * command as before, possibly after a delay.
  657. *
  658. * c) We can call scsi_end_request() with -EIO to fail
  659. * the remainder of the request.
  660. */
  661. void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
  662. {
  663. int result = cmd->result;
  664. struct request_queue *q = cmd->device->request_queue;
  665. struct request *req = cmd->request;
  666. blk_status_t error = BLK_STS_OK;
  667. struct scsi_sense_hdr sshdr;
  668. bool sense_valid = false;
  669. int sense_deferred = 0, level = 0;
  670. enum {ACTION_FAIL, ACTION_REPREP, ACTION_RETRY,
  671. ACTION_DELAYED_RETRY} action;
  672. unsigned long wait_for = (cmd->allowed + 1) * req->timeout;
  673. if (result) {
  674. sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
  675. if (sense_valid)
  676. sense_deferred = scsi_sense_is_deferred(&sshdr);
  677. }
  678. if (blk_rq_is_passthrough(req)) {
  679. if (result) {
  680. if (sense_valid) {
  681. /*
  682. * SG_IO wants current and deferred errors
  683. */
  684. scsi_req(req)->sense_len =
  685. min(8 + cmd->sense_buffer[7],
  686. SCSI_SENSE_BUFFERSIZE);
  687. }
  688. if (!sense_deferred)
  689. error = __scsi_error_from_host_byte(cmd, result);
  690. }
  691. /*
  692. * __scsi_error_from_host_byte may have reset the host_byte
  693. */
  694. scsi_req(req)->result = cmd->result;
  695. scsi_req(req)->resid_len = scsi_get_resid(cmd);
  696. if (scsi_bidi_cmnd(cmd)) {
  697. /*
  698. * Bidi commands Must be complete as a whole,
  699. * both sides at once.
  700. */
  701. scsi_req(req->next_rq)->resid_len = scsi_in(cmd)->resid;
  702. if (scsi_end_request(req, BLK_STS_OK, blk_rq_bytes(req),
  703. blk_rq_bytes(req->next_rq)))
  704. BUG();
  705. return;
  706. }
  707. } else if (blk_rq_bytes(req) == 0 && result && !sense_deferred) {
  708. /*
  709. * Flush commands do not transfers any data, and thus cannot use
  710. * good_bytes != blk_rq_bytes(req) as the signal for an error.
  711. * This sets the error explicitly for the problem case.
  712. */
  713. error = __scsi_error_from_host_byte(cmd, result);
  714. }
  715. /* no bidi support for !blk_rq_is_passthrough yet */
  716. BUG_ON(blk_bidi_rq(req));
  717. /*
  718. * Next deal with any sectors which we were able to correctly
  719. * handle.
  720. */
  721. SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, cmd,
  722. "%u sectors total, %d bytes done.\n",
  723. blk_rq_sectors(req), good_bytes));
  724. /*
  725. * Recovered errors need reporting, but they're always treated as
  726. * success, so fiddle the result code here. For passthrough requests
  727. * we already took a copy of the original into sreq->result which
  728. * is what gets returned to the user
  729. */
  730. if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) {
  731. /* if ATA PASS-THROUGH INFORMATION AVAILABLE skip
  732. * print since caller wants ATA registers. Only occurs on
  733. * SCSI ATA PASS_THROUGH commands when CK_COND=1
  734. */
  735. if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d))
  736. ;
  737. else if (!(req->rq_flags & RQF_QUIET))
  738. scsi_print_sense(cmd);
  739. result = 0;
  740. /* for passthrough error may be set */
  741. error = BLK_STS_OK;
  742. }
  743. /*
  744. * special case: failed zero length commands always need to
  745. * drop down into the retry code. Otherwise, if we finished
  746. * all bytes in the request we are done now.
  747. */
  748. if (!(blk_rq_bytes(req) == 0 && error) &&
  749. !scsi_end_request(req, error, good_bytes, 0))
  750. return;
  751. /*
  752. * Kill remainder if no retrys.
  753. */
  754. if (error && scsi_noretry_cmd(cmd)) {
  755. if (scsi_end_request(req, error, blk_rq_bytes(req), 0))
  756. BUG();
  757. return;
  758. }
  759. /*
  760. * If there had been no error, but we have leftover bytes in the
  761. * requeues just queue the command up again.
  762. */
  763. if (result == 0)
  764. goto requeue;
  765. error = __scsi_error_from_host_byte(cmd, result);
  766. if (host_byte(result) == DID_RESET) {
  767. /* Third party bus reset or reset for error recovery
  768. * reasons. Just retry the command and see what
  769. * happens.
  770. */
  771. action = ACTION_RETRY;
  772. } else if (sense_valid && !sense_deferred) {
  773. switch (sshdr.sense_key) {
  774. case UNIT_ATTENTION:
  775. if (cmd->device->removable) {
  776. /* Detected disc change. Set a bit
  777. * and quietly refuse further access.
  778. */
  779. cmd->device->changed = 1;
  780. action = ACTION_FAIL;
  781. } else {
  782. /* Must have been a power glitch, or a
  783. * bus reset. Could not have been a
  784. * media change, so we just retry the
  785. * command and see what happens.
  786. */
  787. action = ACTION_RETRY;
  788. }
  789. break;
  790. case ILLEGAL_REQUEST:
  791. /* If we had an ILLEGAL REQUEST returned, then
  792. * we may have performed an unsupported
  793. * command. The only thing this should be
  794. * would be a ten byte read where only a six
  795. * byte read was supported. Also, on a system
  796. * where READ CAPACITY failed, we may have
  797. * read past the end of the disk.
  798. */
  799. if ((cmd->device->use_10_for_rw &&
  800. sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
  801. (cmd->cmnd[0] == READ_10 ||
  802. cmd->cmnd[0] == WRITE_10)) {
  803. /* This will issue a new 6-byte command. */
  804. cmd->device->use_10_for_rw = 0;
  805. action = ACTION_REPREP;
  806. } else if (sshdr.asc == 0x10) /* DIX */ {
  807. action = ACTION_FAIL;
  808. error = BLK_STS_PROTECTION;
  809. /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
  810. } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) {
  811. action = ACTION_FAIL;
  812. error = BLK_STS_TARGET;
  813. } else
  814. action = ACTION_FAIL;
  815. break;
  816. case ABORTED_COMMAND:
  817. action = ACTION_FAIL;
  818. if (sshdr.asc == 0x10) /* DIF */
  819. error = BLK_STS_PROTECTION;
  820. break;
  821. case NOT_READY:
  822. /* If the device is in the process of becoming
  823. * ready, or has a temporary blockage, retry.
  824. */
  825. if (sshdr.asc == 0x04) {
  826. switch (sshdr.ascq) {
  827. case 0x01: /* becoming ready */
  828. case 0x04: /* format in progress */
  829. case 0x05: /* rebuild in progress */
  830. case 0x06: /* recalculation in progress */
  831. case 0x07: /* operation in progress */
  832. case 0x08: /* Long write in progress */
  833. case 0x09: /* self test in progress */
  834. case 0x14: /* space allocation in progress */
  835. action = ACTION_DELAYED_RETRY;
  836. break;
  837. default:
  838. action = ACTION_FAIL;
  839. break;
  840. }
  841. } else
  842. action = ACTION_FAIL;
  843. break;
  844. case VOLUME_OVERFLOW:
  845. /* See SSC3rXX or current. */
  846. action = ACTION_FAIL;
  847. break;
  848. default:
  849. action = ACTION_FAIL;
  850. break;
  851. }
  852. } else
  853. action = ACTION_FAIL;
  854. if (action != ACTION_FAIL &&
  855. time_before(cmd->jiffies_at_alloc + wait_for, jiffies))
  856. action = ACTION_FAIL;
  857. switch (action) {
  858. case ACTION_FAIL:
  859. /* Give up and fail the remainder of the request */
  860. if (!(req->rq_flags & RQF_QUIET)) {
  861. static DEFINE_RATELIMIT_STATE(_rs,
  862. DEFAULT_RATELIMIT_INTERVAL,
  863. DEFAULT_RATELIMIT_BURST);
  864. if (unlikely(scsi_logging_level))
  865. level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
  866. SCSI_LOG_MLCOMPLETE_BITS);
  867. /*
  868. * if logging is enabled the failure will be printed
  869. * in scsi_log_completion(), so avoid duplicate messages
  870. */
  871. if (!level && __ratelimit(&_rs)) {
  872. scsi_print_result(cmd, NULL, FAILED);
  873. if (driver_byte(result) & DRIVER_SENSE)
  874. scsi_print_sense(cmd);
  875. scsi_print_command(cmd);
  876. }
  877. }
  878. if (!scsi_end_request(req, error, blk_rq_err_bytes(req), 0))
  879. return;
  880. /*FALLTHRU*/
  881. case ACTION_REPREP:
  882. requeue:
  883. /* Unprep the request and put it back at the head of the queue.
  884. * A new command will be prepared and issued.
  885. */
  886. if (q->mq_ops) {
  887. scsi_mq_requeue_cmd(cmd);
  888. } else {
  889. scsi_release_buffers(cmd);
  890. scsi_requeue_command(q, cmd);
  891. }
  892. break;
  893. case ACTION_RETRY:
  894. /* Retry the same command immediately */
  895. __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, 0);
  896. break;
  897. case ACTION_DELAYED_RETRY:
  898. /* Retry the same command after a delay */
  899. __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, 0);
  900. break;
  901. }
  902. }
  903. static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb)
  904. {
  905. int count;
  906. /*
  907. * If sg table allocation fails, requeue request later.
  908. */
  909. if (unlikely(sg_alloc_table_chained(&sdb->table,
  910. blk_rq_nr_phys_segments(req), sdb->table.sgl)))
  911. return BLKPREP_DEFER;
  912. /*
  913. * Next, walk the list, and fill in the addresses and sizes of
  914. * each segment.
  915. */
  916. count = blk_rq_map_sg(req->q, req, sdb->table.sgl);
  917. BUG_ON(count > sdb->table.nents);
  918. sdb->table.nents = count;
  919. sdb->length = blk_rq_payload_bytes(req);
  920. return BLKPREP_OK;
  921. }
  922. /*
  923. * Function: scsi_init_io()
  924. *
  925. * Purpose: SCSI I/O initialize function.
  926. *
  927. * Arguments: cmd - Command descriptor we wish to initialize
  928. *
  929. * Returns: 0 on success
  930. * BLKPREP_DEFER if the failure is retryable
  931. * BLKPREP_KILL if the failure is fatal
  932. */
  933. int scsi_init_io(struct scsi_cmnd *cmd)
  934. {
  935. struct scsi_device *sdev = cmd->device;
  936. struct request *rq = cmd->request;
  937. bool is_mq = (rq->mq_ctx != NULL);
  938. int error = BLKPREP_KILL;
  939. if (WARN_ON_ONCE(!blk_rq_nr_phys_segments(rq)))
  940. goto err_exit;
  941. error = scsi_init_sgtable(rq, &cmd->sdb);
  942. if (error)
  943. goto err_exit;
  944. if (blk_bidi_rq(rq)) {
  945. if (!rq->q->mq_ops) {
  946. struct scsi_data_buffer *bidi_sdb =
  947. kmem_cache_zalloc(scsi_sdb_cache, GFP_ATOMIC);
  948. if (!bidi_sdb) {
  949. error = BLKPREP_DEFER;
  950. goto err_exit;
  951. }
  952. rq->next_rq->special = bidi_sdb;
  953. }
  954. error = scsi_init_sgtable(rq->next_rq, rq->next_rq->special);
  955. if (error)
  956. goto err_exit;
  957. }
  958. if (blk_integrity_rq(rq)) {
  959. struct scsi_data_buffer *prot_sdb = cmd->prot_sdb;
  960. int ivecs, count;
  961. if (prot_sdb == NULL) {
  962. /*
  963. * This can happen if someone (e.g. multipath)
  964. * queues a command to a device on an adapter
  965. * that does not support DIX.
  966. */
  967. WARN_ON_ONCE(1);
  968. error = BLKPREP_KILL;
  969. goto err_exit;
  970. }
  971. ivecs = blk_rq_count_integrity_sg(rq->q, rq->bio);
  972. if (sg_alloc_table_chained(&prot_sdb->table, ivecs,
  973. prot_sdb->table.sgl)) {
  974. error = BLKPREP_DEFER;
  975. goto err_exit;
  976. }
  977. count = blk_rq_map_integrity_sg(rq->q, rq->bio,
  978. prot_sdb->table.sgl);
  979. BUG_ON(unlikely(count > ivecs));
  980. BUG_ON(unlikely(count > queue_max_integrity_segments(rq->q)));
  981. cmd->prot_sdb = prot_sdb;
  982. cmd->prot_sdb->table.nents = count;
  983. }
  984. return BLKPREP_OK;
  985. err_exit:
  986. if (is_mq) {
  987. scsi_mq_free_sgtables(cmd);
  988. } else {
  989. scsi_release_buffers(cmd);
  990. cmd->request->special = NULL;
  991. scsi_put_command(cmd);
  992. put_device(&sdev->sdev_gendev);
  993. }
  994. return error;
  995. }
  996. EXPORT_SYMBOL(scsi_init_io);
  997. /**
  998. * scsi_initialize_rq - initialize struct scsi_cmnd partially
  999. * @rq: Request associated with the SCSI command to be initialized.
  1000. *
  1001. * This function initializes the members of struct scsi_cmnd that must be
  1002. * initialized before request processing starts and that won't be
  1003. * reinitialized if a SCSI command is requeued.
  1004. *
  1005. * Called from inside blk_get_request() for pass-through requests and from
  1006. * inside scsi_init_command() for filesystem requests.
  1007. */
  1008. void scsi_initialize_rq(struct request *rq)
  1009. {
  1010. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
  1011. scsi_req_init(&cmd->req);
  1012. cmd->jiffies_at_alloc = jiffies;
  1013. cmd->retries = 0;
  1014. }
  1015. EXPORT_SYMBOL(scsi_initialize_rq);
  1016. /* Add a command to the list used by the aacraid and dpt_i2o drivers */
  1017. void scsi_add_cmd_to_list(struct scsi_cmnd *cmd)
  1018. {
  1019. struct scsi_device *sdev = cmd->device;
  1020. struct Scsi_Host *shost = sdev->host;
  1021. unsigned long flags;
  1022. if (shost->use_cmd_list) {
  1023. spin_lock_irqsave(&sdev->list_lock, flags);
  1024. list_add_tail(&cmd->list, &sdev->cmd_list);
  1025. spin_unlock_irqrestore(&sdev->list_lock, flags);
  1026. }
  1027. }
  1028. /* Remove a command from the list used by the aacraid and dpt_i2o drivers */
  1029. void scsi_del_cmd_from_list(struct scsi_cmnd *cmd)
  1030. {
  1031. struct scsi_device *sdev = cmd->device;
  1032. struct Scsi_Host *shost = sdev->host;
  1033. unsigned long flags;
  1034. if (shost->use_cmd_list) {
  1035. spin_lock_irqsave(&sdev->list_lock, flags);
  1036. BUG_ON(list_empty(&cmd->list));
  1037. list_del_init(&cmd->list);
  1038. spin_unlock_irqrestore(&sdev->list_lock, flags);
  1039. }
  1040. }
  1041. /* Called after a request has been started. */
  1042. void scsi_init_command(struct scsi_device *dev, struct scsi_cmnd *cmd)
  1043. {
  1044. void *buf = cmd->sense_buffer;
  1045. void *prot = cmd->prot_sdb;
  1046. struct request *rq = blk_mq_rq_from_pdu(cmd);
  1047. unsigned int flags = cmd->flags & SCMD_PRESERVED_FLAGS;
  1048. unsigned long jiffies_at_alloc;
  1049. int retries;
  1050. if (!blk_rq_is_scsi(rq) && !(flags & SCMD_INITIALIZED)) {
  1051. flags |= SCMD_INITIALIZED;
  1052. scsi_initialize_rq(rq);
  1053. }
  1054. jiffies_at_alloc = cmd->jiffies_at_alloc;
  1055. retries = cmd->retries;
  1056. /* zero out the cmd, except for the embedded scsi_request */
  1057. memset((char *)cmd + sizeof(cmd->req), 0,
  1058. sizeof(*cmd) - sizeof(cmd->req) + dev->host->hostt->cmd_size);
  1059. cmd->device = dev;
  1060. cmd->sense_buffer = buf;
  1061. cmd->prot_sdb = prot;
  1062. cmd->flags = flags;
  1063. INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
  1064. cmd->jiffies_at_alloc = jiffies_at_alloc;
  1065. cmd->retries = retries;
  1066. scsi_add_cmd_to_list(cmd);
  1067. }
  1068. static int scsi_setup_scsi_cmnd(struct scsi_device *sdev, struct request *req)
  1069. {
  1070. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
  1071. /*
  1072. * Passthrough requests may transfer data, in which case they must
  1073. * a bio attached to them. Or they might contain a SCSI command
  1074. * that does not transfer data, in which case they may optionally
  1075. * submit a request without an attached bio.
  1076. */
  1077. if (req->bio) {
  1078. int ret = scsi_init_io(cmd);
  1079. if (unlikely(ret))
  1080. return ret;
  1081. } else {
  1082. BUG_ON(blk_rq_bytes(req));
  1083. memset(&cmd->sdb, 0, sizeof(cmd->sdb));
  1084. }
  1085. cmd->cmd_len = scsi_req(req)->cmd_len;
  1086. cmd->cmnd = scsi_req(req)->cmd;
  1087. cmd->transfersize = blk_rq_bytes(req);
  1088. cmd->allowed = scsi_req(req)->retries;
  1089. return BLKPREP_OK;
  1090. }
  1091. /*
  1092. * Setup a normal block command. These are simple request from filesystems
  1093. * that still need to be translated to SCSI CDBs from the ULD.
  1094. */
  1095. static int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req)
  1096. {
  1097. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
  1098. if (unlikely(sdev->handler && sdev->handler->prep_fn)) {
  1099. int ret = sdev->handler->prep_fn(sdev, req);
  1100. if (ret != BLKPREP_OK)
  1101. return ret;
  1102. }
  1103. cmd->cmnd = scsi_req(req)->cmd = scsi_req(req)->__cmd;
  1104. memset(cmd->cmnd, 0, BLK_MAX_CDB);
  1105. return scsi_cmd_to_driver(cmd)->init_command(cmd);
  1106. }
  1107. static int scsi_setup_cmnd(struct scsi_device *sdev, struct request *req)
  1108. {
  1109. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
  1110. if (!blk_rq_bytes(req))
  1111. cmd->sc_data_direction = DMA_NONE;
  1112. else if (rq_data_dir(req) == WRITE)
  1113. cmd->sc_data_direction = DMA_TO_DEVICE;
  1114. else
  1115. cmd->sc_data_direction = DMA_FROM_DEVICE;
  1116. if (blk_rq_is_scsi(req))
  1117. return scsi_setup_scsi_cmnd(sdev, req);
  1118. else
  1119. return scsi_setup_fs_cmnd(sdev, req);
  1120. }
  1121. static int
  1122. scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
  1123. {
  1124. int ret = BLKPREP_OK;
  1125. /*
  1126. * If the device is not in running state we will reject some
  1127. * or all commands.
  1128. */
  1129. if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
  1130. switch (sdev->sdev_state) {
  1131. case SDEV_OFFLINE:
  1132. case SDEV_TRANSPORT_OFFLINE:
  1133. /*
  1134. * If the device is offline we refuse to process any
  1135. * commands. The device must be brought online
  1136. * before trying any recovery commands.
  1137. */
  1138. sdev_printk(KERN_ERR, sdev,
  1139. "rejecting I/O to offline device\n");
  1140. ret = BLKPREP_KILL;
  1141. break;
  1142. case SDEV_DEL:
  1143. /*
  1144. * If the device is fully deleted, we refuse to
  1145. * process any commands as well.
  1146. */
  1147. sdev_printk(KERN_ERR, sdev,
  1148. "rejecting I/O to dead device\n");
  1149. ret = BLKPREP_KILL;
  1150. break;
  1151. case SDEV_BLOCK:
  1152. case SDEV_CREATED_BLOCK:
  1153. ret = BLKPREP_DEFER;
  1154. break;
  1155. case SDEV_QUIESCE:
  1156. /*
  1157. * If the devices is blocked we defer normal commands.
  1158. */
  1159. if (req && !(req->rq_flags & RQF_PREEMPT))
  1160. ret = BLKPREP_DEFER;
  1161. break;
  1162. default:
  1163. /*
  1164. * For any other not fully online state we only allow
  1165. * special commands. In particular any user initiated
  1166. * command is not allowed.
  1167. */
  1168. if (req && !(req->rq_flags & RQF_PREEMPT))
  1169. ret = BLKPREP_KILL;
  1170. break;
  1171. }
  1172. }
  1173. return ret;
  1174. }
  1175. static int
  1176. scsi_prep_return(struct request_queue *q, struct request *req, int ret)
  1177. {
  1178. struct scsi_device *sdev = q->queuedata;
  1179. switch (ret) {
  1180. case BLKPREP_KILL:
  1181. case BLKPREP_INVALID:
  1182. scsi_req(req)->result = DID_NO_CONNECT << 16;
  1183. /* release the command and kill it */
  1184. if (req->special) {
  1185. struct scsi_cmnd *cmd = req->special;
  1186. scsi_release_buffers(cmd);
  1187. scsi_put_command(cmd);
  1188. put_device(&sdev->sdev_gendev);
  1189. req->special = NULL;
  1190. }
  1191. break;
  1192. case BLKPREP_DEFER:
  1193. /*
  1194. * If we defer, the blk_peek_request() returns NULL, but the
  1195. * queue must be restarted, so we schedule a callback to happen
  1196. * shortly.
  1197. */
  1198. if (atomic_read(&sdev->device_busy) == 0)
  1199. blk_delay_queue(q, SCSI_QUEUE_DELAY);
  1200. break;
  1201. default:
  1202. req->rq_flags |= RQF_DONTPREP;
  1203. }
  1204. return ret;
  1205. }
  1206. static int scsi_prep_fn(struct request_queue *q, struct request *req)
  1207. {
  1208. struct scsi_device *sdev = q->queuedata;
  1209. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
  1210. int ret;
  1211. ret = scsi_prep_state_check(sdev, req);
  1212. if (ret != BLKPREP_OK)
  1213. goto out;
  1214. if (!req->special) {
  1215. /* Bail if we can't get a reference to the device */
  1216. if (unlikely(!get_device(&sdev->sdev_gendev))) {
  1217. ret = BLKPREP_DEFER;
  1218. goto out;
  1219. }
  1220. scsi_init_command(sdev, cmd);
  1221. req->special = cmd;
  1222. }
  1223. cmd->tag = req->tag;
  1224. cmd->request = req;
  1225. cmd->prot_op = SCSI_PROT_NORMAL;
  1226. ret = scsi_setup_cmnd(sdev, req);
  1227. out:
  1228. if (ret != BLKPREP_OK)
  1229. cmd->flags &= ~SCMD_INITIALIZED;
  1230. return scsi_prep_return(q, req, ret);
  1231. }
  1232. static void scsi_unprep_fn(struct request_queue *q, struct request *req)
  1233. {
  1234. scsi_uninit_cmd(blk_mq_rq_to_pdu(req));
  1235. }
  1236. /*
  1237. * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
  1238. * return 0.
  1239. *
  1240. * Called with the queue_lock held.
  1241. */
  1242. static inline int scsi_dev_queue_ready(struct request_queue *q,
  1243. struct scsi_device *sdev)
  1244. {
  1245. unsigned int busy;
  1246. busy = atomic_inc_return(&sdev->device_busy) - 1;
  1247. if (atomic_read(&sdev->device_blocked)) {
  1248. if (busy)
  1249. goto out_dec;
  1250. /*
  1251. * unblock after device_blocked iterates to zero
  1252. */
  1253. if (atomic_dec_return(&sdev->device_blocked) > 0) {
  1254. /*
  1255. * For the MQ case we take care of this in the caller.
  1256. */
  1257. if (!q->mq_ops)
  1258. blk_delay_queue(q, SCSI_QUEUE_DELAY);
  1259. goto out_dec;
  1260. }
  1261. SCSI_LOG_MLQUEUE(3, sdev_printk(KERN_INFO, sdev,
  1262. "unblocking device at zero depth\n"));
  1263. }
  1264. if (busy >= sdev->queue_depth)
  1265. goto out_dec;
  1266. return 1;
  1267. out_dec:
  1268. atomic_dec(&sdev->device_busy);
  1269. return 0;
  1270. }
  1271. /*
  1272. * scsi_target_queue_ready: checks if there we can send commands to target
  1273. * @sdev: scsi device on starget to check.
  1274. */
  1275. static inline int scsi_target_queue_ready(struct Scsi_Host *shost,
  1276. struct scsi_device *sdev)
  1277. {
  1278. struct scsi_target *starget = scsi_target(sdev);
  1279. unsigned int busy;
  1280. if (starget->single_lun) {
  1281. spin_lock_irq(shost->host_lock);
  1282. if (starget->starget_sdev_user &&
  1283. starget->starget_sdev_user != sdev) {
  1284. spin_unlock_irq(shost->host_lock);
  1285. return 0;
  1286. }
  1287. starget->starget_sdev_user = sdev;
  1288. spin_unlock_irq(shost->host_lock);
  1289. }
  1290. if (starget->can_queue <= 0)
  1291. return 1;
  1292. busy = atomic_inc_return(&starget->target_busy) - 1;
  1293. if (atomic_read(&starget->target_blocked) > 0) {
  1294. if (busy)
  1295. goto starved;
  1296. /*
  1297. * unblock after target_blocked iterates to zero
  1298. */
  1299. if (atomic_dec_return(&starget->target_blocked) > 0)
  1300. goto out_dec;
  1301. SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget,
  1302. "unblocking target at zero depth\n"));
  1303. }
  1304. if (busy >= starget->can_queue)
  1305. goto starved;
  1306. return 1;
  1307. starved:
  1308. spin_lock_irq(shost->host_lock);
  1309. list_move_tail(&sdev->starved_entry, &shost->starved_list);
  1310. spin_unlock_irq(shost->host_lock);
  1311. out_dec:
  1312. if (starget->can_queue > 0)
  1313. atomic_dec(&starget->target_busy);
  1314. return 0;
  1315. }
  1316. /*
  1317. * scsi_host_queue_ready: if we can send requests to shost, return 1 else
  1318. * return 0. We must end up running the queue again whenever 0 is
  1319. * returned, else IO can hang.
  1320. */
  1321. static inline int scsi_host_queue_ready(struct request_queue *q,
  1322. struct Scsi_Host *shost,
  1323. struct scsi_device *sdev)
  1324. {
  1325. unsigned int busy;
  1326. if (scsi_host_in_recovery(shost))
  1327. return 0;
  1328. busy = atomic_inc_return(&shost->host_busy) - 1;
  1329. if (atomic_read(&shost->host_blocked) > 0) {
  1330. if (busy)
  1331. goto starved;
  1332. /*
  1333. * unblock after host_blocked iterates to zero
  1334. */
  1335. if (atomic_dec_return(&shost->host_blocked) > 0)
  1336. goto out_dec;
  1337. SCSI_LOG_MLQUEUE(3,
  1338. shost_printk(KERN_INFO, shost,
  1339. "unblocking host at zero depth\n"));
  1340. }
  1341. if (shost->can_queue > 0 && busy >= shost->can_queue)
  1342. goto starved;
  1343. if (shost->host_self_blocked)
  1344. goto starved;
  1345. /* We're OK to process the command, so we can't be starved */
  1346. if (!list_empty(&sdev->starved_entry)) {
  1347. spin_lock_irq(shost->host_lock);
  1348. if (!list_empty(&sdev->starved_entry))
  1349. list_del_init(&sdev->starved_entry);
  1350. spin_unlock_irq(shost->host_lock);
  1351. }
  1352. return 1;
  1353. starved:
  1354. spin_lock_irq(shost->host_lock);
  1355. if (list_empty(&sdev->starved_entry))
  1356. list_add_tail(&sdev->starved_entry, &shost->starved_list);
  1357. spin_unlock_irq(shost->host_lock);
  1358. out_dec:
  1359. atomic_dec(&shost->host_busy);
  1360. return 0;
  1361. }
  1362. /*
  1363. * Busy state exporting function for request stacking drivers.
  1364. *
  1365. * For efficiency, no lock is taken to check the busy state of
  1366. * shost/starget/sdev, since the returned value is not guaranteed and
  1367. * may be changed after request stacking drivers call the function,
  1368. * regardless of taking lock or not.
  1369. *
  1370. * When scsi can't dispatch I/Os anymore and needs to kill I/Os scsi
  1371. * needs to return 'not busy'. Otherwise, request stacking drivers
  1372. * may hold requests forever.
  1373. */
  1374. static int scsi_lld_busy(struct request_queue *q)
  1375. {
  1376. struct scsi_device *sdev = q->queuedata;
  1377. struct Scsi_Host *shost;
  1378. if (blk_queue_dying(q))
  1379. return 0;
  1380. shost = sdev->host;
  1381. /*
  1382. * Ignore host/starget busy state.
  1383. * Since block layer does not have a concept of fairness across
  1384. * multiple queues, congestion of host/starget needs to be handled
  1385. * in SCSI layer.
  1386. */
  1387. if (scsi_host_in_recovery(shost) || scsi_device_is_busy(sdev))
  1388. return 1;
  1389. return 0;
  1390. }
  1391. /*
  1392. * Kill a request for a dead device
  1393. */
  1394. static void scsi_kill_request(struct request *req, struct request_queue *q)
  1395. {
  1396. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
  1397. struct scsi_device *sdev;
  1398. struct scsi_target *starget;
  1399. struct Scsi_Host *shost;
  1400. blk_start_request(req);
  1401. scmd_printk(KERN_INFO, cmd, "killing request\n");
  1402. sdev = cmd->device;
  1403. starget = scsi_target(sdev);
  1404. shost = sdev->host;
  1405. scsi_init_cmd_errh(cmd);
  1406. cmd->result = DID_NO_CONNECT << 16;
  1407. atomic_inc(&cmd->device->iorequest_cnt);
  1408. /*
  1409. * SCSI request completion path will do scsi_device_unbusy(),
  1410. * bump busy counts. To bump the counters, we need to dance
  1411. * with the locks as normal issue path does.
  1412. */
  1413. atomic_inc(&sdev->device_busy);
  1414. atomic_inc(&shost->host_busy);
  1415. if (starget->can_queue > 0)
  1416. atomic_inc(&starget->target_busy);
  1417. blk_complete_request(req);
  1418. }
  1419. static void scsi_softirq_done(struct request *rq)
  1420. {
  1421. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
  1422. unsigned long wait_for = (cmd->allowed + 1) * rq->timeout;
  1423. int disposition;
  1424. INIT_LIST_HEAD(&cmd->eh_entry);
  1425. atomic_inc(&cmd->device->iodone_cnt);
  1426. if (cmd->result)
  1427. atomic_inc(&cmd->device->ioerr_cnt);
  1428. disposition = scsi_decide_disposition(cmd);
  1429. if (disposition != SUCCESS &&
  1430. time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
  1431. sdev_printk(KERN_ERR, cmd->device,
  1432. "timing out command, waited %lus\n",
  1433. wait_for/HZ);
  1434. disposition = SUCCESS;
  1435. }
  1436. scsi_log_completion(cmd, disposition);
  1437. switch (disposition) {
  1438. case SUCCESS:
  1439. scsi_finish_command(cmd);
  1440. break;
  1441. case NEEDS_RETRY:
  1442. scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
  1443. break;
  1444. case ADD_TO_MLQUEUE:
  1445. scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
  1446. break;
  1447. default:
  1448. scsi_eh_scmd_add(cmd);
  1449. break;
  1450. }
  1451. }
  1452. /**
  1453. * scsi_dispatch_command - Dispatch a command to the low-level driver.
  1454. * @cmd: command block we are dispatching.
  1455. *
  1456. * Return: nonzero return request was rejected and device's queue needs to be
  1457. * plugged.
  1458. */
  1459. static int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
  1460. {
  1461. struct Scsi_Host *host = cmd->device->host;
  1462. int rtn = 0;
  1463. atomic_inc(&cmd->device->iorequest_cnt);
  1464. /* check if the device is still usable */
  1465. if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
  1466. /* in SDEV_DEL we error all commands. DID_NO_CONNECT
  1467. * returns an immediate error upwards, and signals
  1468. * that the device is no longer present */
  1469. cmd->result = DID_NO_CONNECT << 16;
  1470. goto done;
  1471. }
  1472. /* Check to see if the scsi lld made this device blocked. */
  1473. if (unlikely(scsi_device_blocked(cmd->device))) {
  1474. /*
  1475. * in blocked state, the command is just put back on
  1476. * the device queue. The suspend state has already
  1477. * blocked the queue so future requests should not
  1478. * occur until the device transitions out of the
  1479. * suspend state.
  1480. */
  1481. SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
  1482. "queuecommand : device blocked\n"));
  1483. return SCSI_MLQUEUE_DEVICE_BUSY;
  1484. }
  1485. /* Store the LUN value in cmnd, if needed. */
  1486. if (cmd->device->lun_in_cdb)
  1487. cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
  1488. (cmd->device->lun << 5 & 0xe0);
  1489. scsi_log_send(cmd);
  1490. /*
  1491. * Before we queue this command, check if the command
  1492. * length exceeds what the host adapter can handle.
  1493. */
  1494. if (cmd->cmd_len > cmd->device->host->max_cmd_len) {
  1495. SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
  1496. "queuecommand : command too long. "
  1497. "cdb_size=%d host->max_cmd_len=%d\n",
  1498. cmd->cmd_len, cmd->device->host->max_cmd_len));
  1499. cmd->result = (DID_ABORT << 16);
  1500. goto done;
  1501. }
  1502. if (unlikely(host->shost_state == SHOST_DEL)) {
  1503. cmd->result = (DID_NO_CONNECT << 16);
  1504. goto done;
  1505. }
  1506. trace_scsi_dispatch_cmd_start(cmd);
  1507. rtn = host->hostt->queuecommand(host, cmd);
  1508. if (rtn) {
  1509. trace_scsi_dispatch_cmd_error(cmd, rtn);
  1510. if (rtn != SCSI_MLQUEUE_DEVICE_BUSY &&
  1511. rtn != SCSI_MLQUEUE_TARGET_BUSY)
  1512. rtn = SCSI_MLQUEUE_HOST_BUSY;
  1513. SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
  1514. "queuecommand : request rejected\n"));
  1515. }
  1516. return rtn;
  1517. done:
  1518. cmd->scsi_done(cmd);
  1519. return 0;
  1520. }
  1521. /**
  1522. * scsi_done - Invoke completion on finished SCSI command.
  1523. * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
  1524. * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
  1525. *
  1526. * Description: This function is the mid-level's (SCSI Core) interrupt routine,
  1527. * which regains ownership of the SCSI command (de facto) from a LLDD, and
  1528. * calls blk_complete_request() for further processing.
  1529. *
  1530. * This function is interrupt context safe.
  1531. */
  1532. static void scsi_done(struct scsi_cmnd *cmd)
  1533. {
  1534. trace_scsi_dispatch_cmd_done(cmd);
  1535. blk_complete_request(cmd->request);
  1536. }
  1537. /*
  1538. * Function: scsi_request_fn()
  1539. *
  1540. * Purpose: Main strategy routine for SCSI.
  1541. *
  1542. * Arguments: q - Pointer to actual queue.
  1543. *
  1544. * Returns: Nothing
  1545. *
  1546. * Lock status: IO request lock assumed to be held when called.
  1547. */
  1548. static void scsi_request_fn(struct request_queue *q)
  1549. __releases(q->queue_lock)
  1550. __acquires(q->queue_lock)
  1551. {
  1552. struct scsi_device *sdev = q->queuedata;
  1553. struct Scsi_Host *shost;
  1554. struct scsi_cmnd *cmd;
  1555. struct request *req;
  1556. /*
  1557. * To start with, we keep looping until the queue is empty, or until
  1558. * the host is no longer able to accept any more requests.
  1559. */
  1560. shost = sdev->host;
  1561. for (;;) {
  1562. int rtn;
  1563. /*
  1564. * get next queueable request. We do this early to make sure
  1565. * that the request is fully prepared even if we cannot
  1566. * accept it.
  1567. */
  1568. req = blk_peek_request(q);
  1569. if (!req)
  1570. break;
  1571. if (unlikely(!scsi_device_online(sdev))) {
  1572. sdev_printk(KERN_ERR, sdev,
  1573. "rejecting I/O to offline device\n");
  1574. scsi_kill_request(req, q);
  1575. continue;
  1576. }
  1577. if (!scsi_dev_queue_ready(q, sdev))
  1578. break;
  1579. /*
  1580. * Remove the request from the request list.
  1581. */
  1582. if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
  1583. blk_start_request(req);
  1584. spin_unlock_irq(q->queue_lock);
  1585. cmd = blk_mq_rq_to_pdu(req);
  1586. if (cmd != req->special) {
  1587. printk(KERN_CRIT "impossible request in %s.\n"
  1588. "please mail a stack trace to "
  1589. "linux-scsi@vger.kernel.org\n",
  1590. __func__);
  1591. blk_dump_rq_flags(req, "foo");
  1592. BUG();
  1593. }
  1594. /*
  1595. * We hit this when the driver is using a host wide
  1596. * tag map. For device level tag maps the queue_depth check
  1597. * in the device ready fn would prevent us from trying
  1598. * to allocate a tag. Since the map is a shared host resource
  1599. * we add the dev to the starved list so it eventually gets
  1600. * a run when a tag is freed.
  1601. */
  1602. if (blk_queue_tagged(q) && !(req->rq_flags & RQF_QUEUED)) {
  1603. spin_lock_irq(shost->host_lock);
  1604. if (list_empty(&sdev->starved_entry))
  1605. list_add_tail(&sdev->starved_entry,
  1606. &shost->starved_list);
  1607. spin_unlock_irq(shost->host_lock);
  1608. goto not_ready;
  1609. }
  1610. if (!scsi_target_queue_ready(shost, sdev))
  1611. goto not_ready;
  1612. if (!scsi_host_queue_ready(q, shost, sdev))
  1613. goto host_not_ready;
  1614. if (sdev->simple_tags)
  1615. cmd->flags |= SCMD_TAGGED;
  1616. else
  1617. cmd->flags &= ~SCMD_TAGGED;
  1618. /*
  1619. * Finally, initialize any error handling parameters, and set up
  1620. * the timers for timeouts.
  1621. */
  1622. scsi_init_cmd_errh(cmd);
  1623. /*
  1624. * Dispatch the command to the low-level driver.
  1625. */
  1626. cmd->scsi_done = scsi_done;
  1627. rtn = scsi_dispatch_cmd(cmd);
  1628. if (rtn) {
  1629. scsi_queue_insert(cmd, rtn);
  1630. spin_lock_irq(q->queue_lock);
  1631. goto out_delay;
  1632. }
  1633. spin_lock_irq(q->queue_lock);
  1634. }
  1635. return;
  1636. host_not_ready:
  1637. if (scsi_target(sdev)->can_queue > 0)
  1638. atomic_dec(&scsi_target(sdev)->target_busy);
  1639. not_ready:
  1640. /*
  1641. * lock q, handle tag, requeue req, and decrement device_busy. We
  1642. * must return with queue_lock held.
  1643. *
  1644. * Decrementing device_busy without checking it is OK, as all such
  1645. * cases (host limits or settings) should run the queue at some
  1646. * later time.
  1647. */
  1648. spin_lock_irq(q->queue_lock);
  1649. blk_requeue_request(q, req);
  1650. atomic_dec(&sdev->device_busy);
  1651. out_delay:
  1652. if (!atomic_read(&sdev->device_busy) && !scsi_device_blocked(sdev))
  1653. blk_delay_queue(q, SCSI_QUEUE_DELAY);
  1654. }
  1655. static inline blk_status_t prep_to_mq(int ret)
  1656. {
  1657. switch (ret) {
  1658. case BLKPREP_OK:
  1659. return BLK_STS_OK;
  1660. case BLKPREP_DEFER:
  1661. return BLK_STS_RESOURCE;
  1662. default:
  1663. return BLK_STS_IOERR;
  1664. }
  1665. }
  1666. /* Size in bytes of the sg-list stored in the scsi-mq command-private data. */
  1667. static unsigned int scsi_mq_sgl_size(struct Scsi_Host *shost)
  1668. {
  1669. return min_t(unsigned int, shost->sg_tablesize, SG_CHUNK_SIZE) *
  1670. sizeof(struct scatterlist);
  1671. }
  1672. static int scsi_mq_prep_fn(struct request *req)
  1673. {
  1674. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
  1675. struct scsi_device *sdev = req->q->queuedata;
  1676. struct Scsi_Host *shost = sdev->host;
  1677. struct scatterlist *sg;
  1678. int ret;
  1679. scsi_init_command(sdev, cmd);
  1680. req->special = cmd;
  1681. cmd->request = req;
  1682. cmd->tag = req->tag;
  1683. cmd->prot_op = SCSI_PROT_NORMAL;
  1684. sg = (void *)cmd + sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
  1685. cmd->sdb.table.sgl = sg;
  1686. if (scsi_host_get_prot(shost)) {
  1687. memset(cmd->prot_sdb, 0, sizeof(struct scsi_data_buffer));
  1688. cmd->prot_sdb->table.sgl =
  1689. (struct scatterlist *)(cmd->prot_sdb + 1);
  1690. }
  1691. if (blk_bidi_rq(req)) {
  1692. struct request *next_rq = req->next_rq;
  1693. struct scsi_data_buffer *bidi_sdb = blk_mq_rq_to_pdu(next_rq);
  1694. memset(bidi_sdb, 0, sizeof(struct scsi_data_buffer));
  1695. bidi_sdb->table.sgl =
  1696. (struct scatterlist *)(bidi_sdb + 1);
  1697. next_rq->special = bidi_sdb;
  1698. }
  1699. blk_mq_start_request(req);
  1700. ret = scsi_setup_cmnd(sdev, req);
  1701. if (ret != BLK_STS_OK)
  1702. cmd->flags &= ~SCMD_INITIALIZED;
  1703. return ret;
  1704. }
  1705. static void scsi_mq_done(struct scsi_cmnd *cmd)
  1706. {
  1707. trace_scsi_dispatch_cmd_done(cmd);
  1708. blk_mq_complete_request(cmd->request);
  1709. }
  1710. static void scsi_mq_put_budget(struct blk_mq_hw_ctx *hctx)
  1711. {
  1712. struct request_queue *q = hctx->queue;
  1713. struct scsi_device *sdev = q->queuedata;
  1714. atomic_dec(&sdev->device_busy);
  1715. put_device(&sdev->sdev_gendev);
  1716. }
  1717. static bool scsi_mq_get_budget(struct blk_mq_hw_ctx *hctx)
  1718. {
  1719. struct request_queue *q = hctx->queue;
  1720. struct scsi_device *sdev = q->queuedata;
  1721. if (!get_device(&sdev->sdev_gendev))
  1722. goto out;
  1723. if (!scsi_dev_queue_ready(q, sdev))
  1724. goto out_put_device;
  1725. return true;
  1726. out_put_device:
  1727. put_device(&sdev->sdev_gendev);
  1728. out:
  1729. return false;
  1730. }
  1731. static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
  1732. const struct blk_mq_queue_data *bd)
  1733. {
  1734. struct request *req = bd->rq;
  1735. struct request_queue *q = req->q;
  1736. struct scsi_device *sdev = q->queuedata;
  1737. struct Scsi_Host *shost = sdev->host;
  1738. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
  1739. blk_status_t ret;
  1740. int reason;
  1741. ret = prep_to_mq(scsi_prep_state_check(sdev, req));
  1742. if (ret != BLK_STS_OK)
  1743. goto out_put_budget;
  1744. ret = BLK_STS_RESOURCE;
  1745. if (!scsi_target_queue_ready(shost, sdev))
  1746. goto out_put_budget;
  1747. if (!scsi_host_queue_ready(q, shost, sdev))
  1748. goto out_dec_target_busy;
  1749. if (!(req->rq_flags & RQF_DONTPREP)) {
  1750. ret = prep_to_mq(scsi_mq_prep_fn(req));
  1751. if (ret != BLK_STS_OK)
  1752. goto out_dec_host_busy;
  1753. req->rq_flags |= RQF_DONTPREP;
  1754. } else {
  1755. blk_mq_start_request(req);
  1756. }
  1757. if (sdev->simple_tags)
  1758. cmd->flags |= SCMD_TAGGED;
  1759. else
  1760. cmd->flags &= ~SCMD_TAGGED;
  1761. scsi_init_cmd_errh(cmd);
  1762. cmd->scsi_done = scsi_mq_done;
  1763. reason = scsi_dispatch_cmd(cmd);
  1764. if (reason) {
  1765. scsi_set_blocked(cmd, reason);
  1766. ret = BLK_STS_RESOURCE;
  1767. goto out_dec_host_busy;
  1768. }
  1769. return BLK_STS_OK;
  1770. out_dec_host_busy:
  1771. atomic_dec(&shost->host_busy);
  1772. out_dec_target_busy:
  1773. if (scsi_target(sdev)->can_queue > 0)
  1774. atomic_dec(&scsi_target(sdev)->target_busy);
  1775. out_put_budget:
  1776. scsi_mq_put_budget(hctx);
  1777. switch (ret) {
  1778. case BLK_STS_OK:
  1779. break;
  1780. case BLK_STS_RESOURCE:
  1781. if (atomic_read(&sdev->device_busy) == 0 &&
  1782. !scsi_device_blocked(sdev))
  1783. blk_mq_delay_run_hw_queue(hctx, SCSI_QUEUE_DELAY);
  1784. break;
  1785. default:
  1786. /*
  1787. * Make sure to release all allocated ressources when
  1788. * we hit an error, as we will never see this command
  1789. * again.
  1790. */
  1791. if (req->rq_flags & RQF_DONTPREP)
  1792. scsi_mq_uninit_cmd(cmd);
  1793. break;
  1794. }
  1795. return ret;
  1796. }
  1797. static enum blk_eh_timer_return scsi_timeout(struct request *req,
  1798. bool reserved)
  1799. {
  1800. if (reserved)
  1801. return BLK_EH_RESET_TIMER;
  1802. return scsi_times_out(req);
  1803. }
  1804. static int scsi_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
  1805. unsigned int hctx_idx, unsigned int numa_node)
  1806. {
  1807. struct Scsi_Host *shost = set->driver_data;
  1808. const bool unchecked_isa_dma = shost->unchecked_isa_dma;
  1809. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
  1810. struct scatterlist *sg;
  1811. if (unchecked_isa_dma)
  1812. cmd->flags |= SCMD_UNCHECKED_ISA_DMA;
  1813. cmd->sense_buffer = scsi_alloc_sense_buffer(unchecked_isa_dma,
  1814. GFP_KERNEL, numa_node);
  1815. if (!cmd->sense_buffer)
  1816. return -ENOMEM;
  1817. cmd->req.sense = cmd->sense_buffer;
  1818. if (scsi_host_get_prot(shost)) {
  1819. sg = (void *)cmd + sizeof(struct scsi_cmnd) +
  1820. shost->hostt->cmd_size;
  1821. cmd->prot_sdb = (void *)sg + scsi_mq_sgl_size(shost);
  1822. }
  1823. return 0;
  1824. }
  1825. static void scsi_mq_exit_request(struct blk_mq_tag_set *set, struct request *rq,
  1826. unsigned int hctx_idx)
  1827. {
  1828. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
  1829. scsi_free_sense_buffer(cmd->flags & SCMD_UNCHECKED_ISA_DMA,
  1830. cmd->sense_buffer);
  1831. }
  1832. static int scsi_map_queues(struct blk_mq_tag_set *set)
  1833. {
  1834. struct Scsi_Host *shost = container_of(set, struct Scsi_Host, tag_set);
  1835. if (shost->hostt->map_queues)
  1836. return shost->hostt->map_queues(shost);
  1837. return blk_mq_map_queues(set);
  1838. }
  1839. static u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
  1840. {
  1841. struct device *host_dev;
  1842. u64 bounce_limit = 0xffffffff;
  1843. if (shost->unchecked_isa_dma)
  1844. return BLK_BOUNCE_ISA;
  1845. /*
  1846. * Platforms with virtual-DMA translation
  1847. * hardware have no practical limit.
  1848. */
  1849. if (!PCI_DMA_BUS_IS_PHYS)
  1850. return BLK_BOUNCE_ANY;
  1851. host_dev = scsi_get_device(shost);
  1852. if (host_dev && host_dev->dma_mask)
  1853. bounce_limit = (u64)dma_max_pfn(host_dev) << PAGE_SHIFT;
  1854. return bounce_limit;
  1855. }
  1856. void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q)
  1857. {
  1858. struct device *dev = shost->dma_dev;
  1859. queue_flag_set_unlocked(QUEUE_FLAG_SCSI_PASSTHROUGH, q);
  1860. /*
  1861. * this limit is imposed by hardware restrictions
  1862. */
  1863. blk_queue_max_segments(q, min_t(unsigned short, shost->sg_tablesize,
  1864. SG_MAX_SEGMENTS));
  1865. if (scsi_host_prot_dma(shost)) {
  1866. shost->sg_prot_tablesize =
  1867. min_not_zero(shost->sg_prot_tablesize,
  1868. (unsigned short)SCSI_MAX_PROT_SG_SEGMENTS);
  1869. BUG_ON(shost->sg_prot_tablesize < shost->sg_tablesize);
  1870. blk_queue_max_integrity_segments(q, shost->sg_prot_tablesize);
  1871. }
  1872. blk_queue_max_hw_sectors(q, shost->max_sectors);
  1873. blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
  1874. blk_queue_segment_boundary(q, shost->dma_boundary);
  1875. dma_set_seg_boundary(dev, shost->dma_boundary);
  1876. blk_queue_max_segment_size(q, dma_get_max_seg_size(dev));
  1877. if (!shost->use_clustering)
  1878. q->limits.cluster = 0;
  1879. /*
  1880. * set a reasonable default alignment on word boundaries: the
  1881. * host and device may alter it using
  1882. * blk_queue_update_dma_alignment() later.
  1883. */
  1884. blk_queue_dma_alignment(q, 0x03);
  1885. }
  1886. EXPORT_SYMBOL_GPL(__scsi_init_queue);
  1887. static int scsi_old_init_rq(struct request_queue *q, struct request *rq,
  1888. gfp_t gfp)
  1889. {
  1890. struct Scsi_Host *shost = q->rq_alloc_data;
  1891. const bool unchecked_isa_dma = shost->unchecked_isa_dma;
  1892. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
  1893. memset(cmd, 0, sizeof(*cmd));
  1894. if (unchecked_isa_dma)
  1895. cmd->flags |= SCMD_UNCHECKED_ISA_DMA;
  1896. cmd->sense_buffer = scsi_alloc_sense_buffer(unchecked_isa_dma, gfp,
  1897. NUMA_NO_NODE);
  1898. if (!cmd->sense_buffer)
  1899. goto fail;
  1900. cmd->req.sense = cmd->sense_buffer;
  1901. if (scsi_host_get_prot(shost) >= SHOST_DIX_TYPE0_PROTECTION) {
  1902. cmd->prot_sdb = kmem_cache_zalloc(scsi_sdb_cache, gfp);
  1903. if (!cmd->prot_sdb)
  1904. goto fail_free_sense;
  1905. }
  1906. return 0;
  1907. fail_free_sense:
  1908. scsi_free_sense_buffer(unchecked_isa_dma, cmd->sense_buffer);
  1909. fail:
  1910. return -ENOMEM;
  1911. }
  1912. static void scsi_old_exit_rq(struct request_queue *q, struct request *rq)
  1913. {
  1914. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
  1915. if (cmd->prot_sdb)
  1916. kmem_cache_free(scsi_sdb_cache, cmd->prot_sdb);
  1917. scsi_free_sense_buffer(cmd->flags & SCMD_UNCHECKED_ISA_DMA,
  1918. cmd->sense_buffer);
  1919. }
  1920. struct request_queue *scsi_old_alloc_queue(struct scsi_device *sdev)
  1921. {
  1922. struct Scsi_Host *shost = sdev->host;
  1923. struct request_queue *q;
  1924. q = blk_alloc_queue_node(GFP_KERNEL, NUMA_NO_NODE);
  1925. if (!q)
  1926. return NULL;
  1927. q->cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
  1928. q->rq_alloc_data = shost;
  1929. q->request_fn = scsi_request_fn;
  1930. q->init_rq_fn = scsi_old_init_rq;
  1931. q->exit_rq_fn = scsi_old_exit_rq;
  1932. q->initialize_rq_fn = scsi_initialize_rq;
  1933. if (blk_init_allocated_queue(q) < 0) {
  1934. blk_cleanup_queue(q);
  1935. return NULL;
  1936. }
  1937. __scsi_init_queue(shost, q);
  1938. blk_queue_prep_rq(q, scsi_prep_fn);
  1939. blk_queue_unprep_rq(q, scsi_unprep_fn);
  1940. blk_queue_softirq_done(q, scsi_softirq_done);
  1941. blk_queue_rq_timed_out(q, scsi_times_out);
  1942. blk_queue_lld_busy(q, scsi_lld_busy);
  1943. return q;
  1944. }
  1945. static const struct blk_mq_ops scsi_mq_ops = {
  1946. .get_budget = scsi_mq_get_budget,
  1947. .put_budget = scsi_mq_put_budget,
  1948. .queue_rq = scsi_queue_rq,
  1949. .complete = scsi_softirq_done,
  1950. .timeout = scsi_timeout,
  1951. #ifdef CONFIG_BLK_DEBUG_FS
  1952. .show_rq = scsi_show_rq,
  1953. #endif
  1954. .init_request = scsi_mq_init_request,
  1955. .exit_request = scsi_mq_exit_request,
  1956. .initialize_rq_fn = scsi_initialize_rq,
  1957. .map_queues = scsi_map_queues,
  1958. };
  1959. struct request_queue *scsi_mq_alloc_queue(struct scsi_device *sdev)
  1960. {
  1961. sdev->request_queue = blk_mq_init_queue(&sdev->host->tag_set);
  1962. if (IS_ERR(sdev->request_queue))
  1963. return NULL;
  1964. sdev->request_queue->queuedata = sdev;
  1965. __scsi_init_queue(sdev->host, sdev->request_queue);
  1966. return sdev->request_queue;
  1967. }
  1968. int scsi_mq_setup_tags(struct Scsi_Host *shost)
  1969. {
  1970. unsigned int cmd_size, sgl_size;
  1971. sgl_size = scsi_mq_sgl_size(shost);
  1972. cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size + sgl_size;
  1973. if (scsi_host_get_prot(shost))
  1974. cmd_size += sizeof(struct scsi_data_buffer) + sgl_size;
  1975. memset(&shost->tag_set, 0, sizeof(shost->tag_set));
  1976. shost->tag_set.ops = &scsi_mq_ops;
  1977. shost->tag_set.nr_hw_queues = shost->nr_hw_queues ? : 1;
  1978. shost->tag_set.queue_depth = shost->can_queue;
  1979. shost->tag_set.cmd_size = cmd_size;
  1980. shost->tag_set.numa_node = NUMA_NO_NODE;
  1981. shost->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
  1982. shost->tag_set.flags |=
  1983. BLK_ALLOC_POLICY_TO_MQ_FLAG(shost->hostt->tag_alloc_policy);
  1984. shost->tag_set.driver_data = shost;
  1985. return blk_mq_alloc_tag_set(&shost->tag_set);
  1986. }
  1987. void scsi_mq_destroy_tags(struct Scsi_Host *shost)
  1988. {
  1989. blk_mq_free_tag_set(&shost->tag_set);
  1990. }
  1991. /**
  1992. * scsi_device_from_queue - return sdev associated with a request_queue
  1993. * @q: The request queue to return the sdev from
  1994. *
  1995. * Return the sdev associated with a request queue or NULL if the
  1996. * request_queue does not reference a SCSI device.
  1997. */
  1998. struct scsi_device *scsi_device_from_queue(struct request_queue *q)
  1999. {
  2000. struct scsi_device *sdev = NULL;
  2001. if (q->mq_ops) {
  2002. if (q->mq_ops == &scsi_mq_ops)
  2003. sdev = q->queuedata;
  2004. } else if (q->request_fn == scsi_request_fn)
  2005. sdev = q->queuedata;
  2006. if (!sdev || !get_device(&sdev->sdev_gendev))
  2007. sdev = NULL;
  2008. return sdev;
  2009. }
  2010. EXPORT_SYMBOL_GPL(scsi_device_from_queue);
  2011. /*
  2012. * Function: scsi_block_requests()
  2013. *
  2014. * Purpose: Utility function used by low-level drivers to prevent further
  2015. * commands from being queued to the device.
  2016. *
  2017. * Arguments: shost - Host in question
  2018. *
  2019. * Returns: Nothing
  2020. *
  2021. * Lock status: No locks are assumed held.
  2022. *
  2023. * Notes: There is no timer nor any other means by which the requests
  2024. * get unblocked other than the low-level driver calling
  2025. * scsi_unblock_requests().
  2026. */
  2027. void scsi_block_requests(struct Scsi_Host *shost)
  2028. {
  2029. shost->host_self_blocked = 1;
  2030. }
  2031. EXPORT_SYMBOL(scsi_block_requests);
  2032. /*
  2033. * Function: scsi_unblock_requests()
  2034. *
  2035. * Purpose: Utility function used by low-level drivers to allow further
  2036. * commands from being queued to the device.
  2037. *
  2038. * Arguments: shost - Host in question
  2039. *
  2040. * Returns: Nothing
  2041. *
  2042. * Lock status: No locks are assumed held.
  2043. *
  2044. * Notes: There is no timer nor any other means by which the requests
  2045. * get unblocked other than the low-level driver calling
  2046. * scsi_unblock_requests().
  2047. *
  2048. * This is done as an API function so that changes to the
  2049. * internals of the scsi mid-layer won't require wholesale
  2050. * changes to drivers that use this feature.
  2051. */
  2052. void scsi_unblock_requests(struct Scsi_Host *shost)
  2053. {
  2054. shost->host_self_blocked = 0;
  2055. scsi_run_host_queues(shost);
  2056. }
  2057. EXPORT_SYMBOL(scsi_unblock_requests);
  2058. int __init scsi_init_queue(void)
  2059. {
  2060. scsi_sdb_cache = kmem_cache_create("scsi_data_buffer",
  2061. sizeof(struct scsi_data_buffer),
  2062. 0, 0, NULL);
  2063. if (!scsi_sdb_cache) {
  2064. printk(KERN_ERR "SCSI: can't init scsi sdb cache\n");
  2065. return -ENOMEM;
  2066. }
  2067. return 0;
  2068. }
  2069. void scsi_exit_queue(void)
  2070. {
  2071. kmem_cache_destroy(scsi_sense_cache);
  2072. kmem_cache_destroy(scsi_sense_isadma_cache);
  2073. kmem_cache_destroy(scsi_sdb_cache);
  2074. }
  2075. /**
  2076. * scsi_mode_select - issue a mode select
  2077. * @sdev: SCSI device to be queried
  2078. * @pf: Page format bit (1 == standard, 0 == vendor specific)
  2079. * @sp: Save page bit (0 == don't save, 1 == save)
  2080. * @modepage: mode page being requested
  2081. * @buffer: request buffer (may not be smaller than eight bytes)
  2082. * @len: length of request buffer.
  2083. * @timeout: command timeout
  2084. * @retries: number of retries before failing
  2085. * @data: returns a structure abstracting the mode header data
  2086. * @sshdr: place to put sense data (or NULL if no sense to be collected).
  2087. * must be SCSI_SENSE_BUFFERSIZE big.
  2088. *
  2089. * Returns zero if successful; negative error number or scsi
  2090. * status on error
  2091. *
  2092. */
  2093. int
  2094. scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
  2095. unsigned char *buffer, int len, int timeout, int retries,
  2096. struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
  2097. {
  2098. unsigned char cmd[10];
  2099. unsigned char *real_buffer;
  2100. int ret;
  2101. memset(cmd, 0, sizeof(cmd));
  2102. cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
  2103. if (sdev->use_10_for_ms) {
  2104. if (len > 65535)
  2105. return -EINVAL;
  2106. real_buffer = kmalloc(8 + len, GFP_KERNEL);
  2107. if (!real_buffer)
  2108. return -ENOMEM;
  2109. memcpy(real_buffer + 8, buffer, len);
  2110. len += 8;
  2111. real_buffer[0] = 0;
  2112. real_buffer[1] = 0;
  2113. real_buffer[2] = data->medium_type;
  2114. real_buffer[3] = data->device_specific;
  2115. real_buffer[4] = data->longlba ? 0x01 : 0;
  2116. real_buffer[5] = 0;
  2117. real_buffer[6] = data->block_descriptor_length >> 8;
  2118. real_buffer[7] = data->block_descriptor_length;
  2119. cmd[0] = MODE_SELECT_10;
  2120. cmd[7] = len >> 8;
  2121. cmd[8] = len;
  2122. } else {
  2123. if (len > 255 || data->block_descriptor_length > 255 ||
  2124. data->longlba)
  2125. return -EINVAL;
  2126. real_buffer = kmalloc(4 + len, GFP_KERNEL);
  2127. if (!real_buffer)
  2128. return -ENOMEM;
  2129. memcpy(real_buffer + 4, buffer, len);
  2130. len += 4;
  2131. real_buffer[0] = 0;
  2132. real_buffer[1] = data->medium_type;
  2133. real_buffer[2] = data->device_specific;
  2134. real_buffer[3] = data->block_descriptor_length;
  2135. cmd[0] = MODE_SELECT;
  2136. cmd[4] = len;
  2137. }
  2138. ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
  2139. sshdr, timeout, retries, NULL);
  2140. kfree(real_buffer);
  2141. return ret;
  2142. }
  2143. EXPORT_SYMBOL_GPL(scsi_mode_select);
  2144. /**
  2145. * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
  2146. * @sdev: SCSI device to be queried
  2147. * @dbd: set if mode sense will allow block descriptors to be returned
  2148. * @modepage: mode page being requested
  2149. * @buffer: request buffer (may not be smaller than eight bytes)
  2150. * @len: length of request buffer.
  2151. * @timeout: command timeout
  2152. * @retries: number of retries before failing
  2153. * @data: returns a structure abstracting the mode header data
  2154. * @sshdr: place to put sense data (or NULL if no sense to be collected).
  2155. * must be SCSI_SENSE_BUFFERSIZE big.
  2156. *
  2157. * Returns zero if unsuccessful, or the header offset (either 4
  2158. * or 8 depending on whether a six or ten byte command was
  2159. * issued) if successful.
  2160. */
  2161. int
  2162. scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
  2163. unsigned char *buffer, int len, int timeout, int retries,
  2164. struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
  2165. {
  2166. unsigned char cmd[12];
  2167. int use_10_for_ms;
  2168. int header_length;
  2169. int result, retry_count = retries;
  2170. struct scsi_sense_hdr my_sshdr;
  2171. memset(data, 0, sizeof(*data));
  2172. memset(&cmd[0], 0, 12);
  2173. cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
  2174. cmd[2] = modepage;
  2175. /* caller might not be interested in sense, but we need it */
  2176. if (!sshdr)
  2177. sshdr = &my_sshdr;
  2178. retry:
  2179. use_10_for_ms = sdev->use_10_for_ms;
  2180. if (use_10_for_ms) {
  2181. if (len < 8)
  2182. len = 8;
  2183. cmd[0] = MODE_SENSE_10;
  2184. cmd[8] = len;
  2185. header_length = 8;
  2186. } else {
  2187. if (len < 4)
  2188. len = 4;
  2189. cmd[0] = MODE_SENSE;
  2190. cmd[4] = len;
  2191. header_length = 4;
  2192. }
  2193. memset(buffer, 0, len);
  2194. result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
  2195. sshdr, timeout, retries, NULL);
  2196. /* This code looks awful: what it's doing is making sure an
  2197. * ILLEGAL REQUEST sense return identifies the actual command
  2198. * byte as the problem. MODE_SENSE commands can return
  2199. * ILLEGAL REQUEST if the code page isn't supported */
  2200. if (use_10_for_ms && !scsi_status_is_good(result) &&
  2201. (driver_byte(result) & DRIVER_SENSE)) {
  2202. if (scsi_sense_valid(sshdr)) {
  2203. if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
  2204. (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
  2205. /*
  2206. * Invalid command operation code
  2207. */
  2208. sdev->use_10_for_ms = 0;
  2209. goto retry;
  2210. }
  2211. }
  2212. }
  2213. if(scsi_status_is_good(result)) {
  2214. if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
  2215. (modepage == 6 || modepage == 8))) {
  2216. /* Initio breakage? */
  2217. header_length = 0;
  2218. data->length = 13;
  2219. data->medium_type = 0;
  2220. data->device_specific = 0;
  2221. data->longlba = 0;
  2222. data->block_descriptor_length = 0;
  2223. } else if(use_10_for_ms) {
  2224. data->length = buffer[0]*256 + buffer[1] + 2;
  2225. data->medium_type = buffer[2];
  2226. data->device_specific = buffer[3];
  2227. data->longlba = buffer[4] & 0x01;
  2228. data->block_descriptor_length = buffer[6]*256
  2229. + buffer[7];
  2230. } else {
  2231. data->length = buffer[0] + 1;
  2232. data->medium_type = buffer[1];
  2233. data->device_specific = buffer[2];
  2234. data->block_descriptor_length = buffer[3];
  2235. }
  2236. data->header_length = header_length;
  2237. } else if ((status_byte(result) == CHECK_CONDITION) &&
  2238. scsi_sense_valid(sshdr) &&
  2239. sshdr->sense_key == UNIT_ATTENTION && retry_count) {
  2240. retry_count--;
  2241. goto retry;
  2242. }
  2243. return result;
  2244. }
  2245. EXPORT_SYMBOL(scsi_mode_sense);
  2246. /**
  2247. * scsi_test_unit_ready - test if unit is ready
  2248. * @sdev: scsi device to change the state of.
  2249. * @timeout: command timeout
  2250. * @retries: number of retries before failing
  2251. * @sshdr: outpout pointer for decoded sense information.
  2252. *
  2253. * Returns zero if unsuccessful or an error if TUR failed. For
  2254. * removable media, UNIT_ATTENTION sets ->changed flag.
  2255. **/
  2256. int
  2257. scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
  2258. struct scsi_sense_hdr *sshdr)
  2259. {
  2260. char cmd[] = {
  2261. TEST_UNIT_READY, 0, 0, 0, 0, 0,
  2262. };
  2263. int result;
  2264. /* try to eat the UNIT_ATTENTION if there are enough retries */
  2265. do {
  2266. result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
  2267. timeout, retries, NULL);
  2268. if (sdev->removable && scsi_sense_valid(sshdr) &&
  2269. sshdr->sense_key == UNIT_ATTENTION)
  2270. sdev->changed = 1;
  2271. } while (scsi_sense_valid(sshdr) &&
  2272. sshdr->sense_key == UNIT_ATTENTION && --retries);
  2273. return result;
  2274. }
  2275. EXPORT_SYMBOL(scsi_test_unit_ready);
  2276. /**
  2277. * scsi_device_set_state - Take the given device through the device state model.
  2278. * @sdev: scsi device to change the state of.
  2279. * @state: state to change to.
  2280. *
  2281. * Returns zero if successful or an error if the requested
  2282. * transition is illegal.
  2283. */
  2284. int
  2285. scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
  2286. {
  2287. enum scsi_device_state oldstate = sdev->sdev_state;
  2288. if (state == oldstate)
  2289. return 0;
  2290. switch (state) {
  2291. case SDEV_CREATED:
  2292. switch (oldstate) {
  2293. case SDEV_CREATED_BLOCK:
  2294. break;
  2295. default:
  2296. goto illegal;
  2297. }
  2298. break;
  2299. case SDEV_RUNNING:
  2300. switch (oldstate) {
  2301. case SDEV_CREATED:
  2302. case SDEV_OFFLINE:
  2303. case SDEV_TRANSPORT_OFFLINE:
  2304. case SDEV_QUIESCE:
  2305. case SDEV_BLOCK:
  2306. break;
  2307. default:
  2308. goto illegal;
  2309. }
  2310. break;
  2311. case SDEV_QUIESCE:
  2312. switch (oldstate) {
  2313. case SDEV_RUNNING:
  2314. case SDEV_OFFLINE:
  2315. case SDEV_TRANSPORT_OFFLINE:
  2316. break;
  2317. default:
  2318. goto illegal;
  2319. }
  2320. break;
  2321. case SDEV_OFFLINE:
  2322. case SDEV_TRANSPORT_OFFLINE:
  2323. switch (oldstate) {
  2324. case SDEV_CREATED:
  2325. case SDEV_RUNNING:
  2326. case SDEV_QUIESCE:
  2327. case SDEV_BLOCK:
  2328. break;
  2329. default:
  2330. goto illegal;
  2331. }
  2332. break;
  2333. case SDEV_BLOCK:
  2334. switch (oldstate) {
  2335. case SDEV_RUNNING:
  2336. case SDEV_CREATED_BLOCK:
  2337. break;
  2338. default:
  2339. goto illegal;
  2340. }
  2341. break;
  2342. case SDEV_CREATED_BLOCK:
  2343. switch (oldstate) {
  2344. case SDEV_CREATED:
  2345. break;
  2346. default:
  2347. goto illegal;
  2348. }
  2349. break;
  2350. case SDEV_CANCEL:
  2351. switch (oldstate) {
  2352. case SDEV_CREATED:
  2353. case SDEV_RUNNING:
  2354. case SDEV_QUIESCE:
  2355. case SDEV_OFFLINE:
  2356. case SDEV_TRANSPORT_OFFLINE:
  2357. break;
  2358. default:
  2359. goto illegal;
  2360. }
  2361. break;
  2362. case SDEV_DEL:
  2363. switch (oldstate) {
  2364. case SDEV_CREATED:
  2365. case SDEV_RUNNING:
  2366. case SDEV_OFFLINE:
  2367. case SDEV_TRANSPORT_OFFLINE:
  2368. case SDEV_CANCEL:
  2369. case SDEV_BLOCK:
  2370. case SDEV_CREATED_BLOCK:
  2371. break;
  2372. default:
  2373. goto illegal;
  2374. }
  2375. break;
  2376. }
  2377. sdev->sdev_state = state;
  2378. sysfs_notify(&sdev->sdev_gendev.kobj, NULL, "state");
  2379. return 0;
  2380. illegal:
  2381. SCSI_LOG_ERROR_RECOVERY(1,
  2382. sdev_printk(KERN_ERR, sdev,
  2383. "Illegal state transition %s->%s",
  2384. scsi_device_state_name(oldstate),
  2385. scsi_device_state_name(state))
  2386. );
  2387. return -EINVAL;
  2388. }
  2389. EXPORT_SYMBOL(scsi_device_set_state);
  2390. /**
  2391. * sdev_evt_emit - emit a single SCSI device uevent
  2392. * @sdev: associated SCSI device
  2393. * @evt: event to emit
  2394. *
  2395. * Send a single uevent (scsi_event) to the associated scsi_device.
  2396. */
  2397. static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
  2398. {
  2399. int idx = 0;
  2400. char *envp[3];
  2401. switch (evt->evt_type) {
  2402. case SDEV_EVT_MEDIA_CHANGE:
  2403. envp[idx++] = "SDEV_MEDIA_CHANGE=1";
  2404. break;
  2405. case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
  2406. scsi_rescan_device(&sdev->sdev_gendev);
  2407. envp[idx++] = "SDEV_UA=INQUIRY_DATA_HAS_CHANGED";
  2408. break;
  2409. case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
  2410. envp[idx++] = "SDEV_UA=CAPACITY_DATA_HAS_CHANGED";
  2411. break;
  2412. case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
  2413. envp[idx++] = "SDEV_UA=THIN_PROVISIONING_SOFT_THRESHOLD_REACHED";
  2414. break;
  2415. case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
  2416. envp[idx++] = "SDEV_UA=MODE_PARAMETERS_CHANGED";
  2417. break;
  2418. case SDEV_EVT_LUN_CHANGE_REPORTED:
  2419. envp[idx++] = "SDEV_UA=REPORTED_LUNS_DATA_HAS_CHANGED";
  2420. break;
  2421. case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
  2422. envp[idx++] = "SDEV_UA=ASYMMETRIC_ACCESS_STATE_CHANGED";
  2423. break;
  2424. default:
  2425. /* do nothing */
  2426. break;
  2427. }
  2428. envp[idx++] = NULL;
  2429. kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
  2430. }
  2431. /**
  2432. * sdev_evt_thread - send a uevent for each scsi event
  2433. * @work: work struct for scsi_device
  2434. *
  2435. * Dispatch queued events to their associated scsi_device kobjects
  2436. * as uevents.
  2437. */
  2438. void scsi_evt_thread(struct work_struct *work)
  2439. {
  2440. struct scsi_device *sdev;
  2441. enum scsi_device_event evt_type;
  2442. LIST_HEAD(event_list);
  2443. sdev = container_of(work, struct scsi_device, event_work);
  2444. for (evt_type = SDEV_EVT_FIRST; evt_type <= SDEV_EVT_LAST; evt_type++)
  2445. if (test_and_clear_bit(evt_type, sdev->pending_events))
  2446. sdev_evt_send_simple(sdev, evt_type, GFP_KERNEL);
  2447. while (1) {
  2448. struct scsi_event *evt;
  2449. struct list_head *this, *tmp;
  2450. unsigned long flags;
  2451. spin_lock_irqsave(&sdev->list_lock, flags);
  2452. list_splice_init(&sdev->event_list, &event_list);
  2453. spin_unlock_irqrestore(&sdev->list_lock, flags);
  2454. if (list_empty(&event_list))
  2455. break;
  2456. list_for_each_safe(this, tmp, &event_list) {
  2457. evt = list_entry(this, struct scsi_event, node);
  2458. list_del(&evt->node);
  2459. scsi_evt_emit(sdev, evt);
  2460. kfree(evt);
  2461. }
  2462. }
  2463. }
  2464. /**
  2465. * sdev_evt_send - send asserted event to uevent thread
  2466. * @sdev: scsi_device event occurred on
  2467. * @evt: event to send
  2468. *
  2469. * Assert scsi device event asynchronously.
  2470. */
  2471. void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
  2472. {
  2473. unsigned long flags;
  2474. #if 0
  2475. /* FIXME: currently this check eliminates all media change events
  2476. * for polled devices. Need to update to discriminate between AN
  2477. * and polled events */
  2478. if (!test_bit(evt->evt_type, sdev->supported_events)) {
  2479. kfree(evt);
  2480. return;
  2481. }
  2482. #endif
  2483. spin_lock_irqsave(&sdev->list_lock, flags);
  2484. list_add_tail(&evt->node, &sdev->event_list);
  2485. schedule_work(&sdev->event_work);
  2486. spin_unlock_irqrestore(&sdev->list_lock, flags);
  2487. }
  2488. EXPORT_SYMBOL_GPL(sdev_evt_send);
  2489. /**
  2490. * sdev_evt_alloc - allocate a new scsi event
  2491. * @evt_type: type of event to allocate
  2492. * @gfpflags: GFP flags for allocation
  2493. *
  2494. * Allocates and returns a new scsi_event.
  2495. */
  2496. struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
  2497. gfp_t gfpflags)
  2498. {
  2499. struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
  2500. if (!evt)
  2501. return NULL;
  2502. evt->evt_type = evt_type;
  2503. INIT_LIST_HEAD(&evt->node);
  2504. /* evt_type-specific initialization, if any */
  2505. switch (evt_type) {
  2506. case SDEV_EVT_MEDIA_CHANGE:
  2507. case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
  2508. case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
  2509. case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
  2510. case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
  2511. case SDEV_EVT_LUN_CHANGE_REPORTED:
  2512. case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
  2513. default:
  2514. /* do nothing */
  2515. break;
  2516. }
  2517. return evt;
  2518. }
  2519. EXPORT_SYMBOL_GPL(sdev_evt_alloc);
  2520. /**
  2521. * sdev_evt_send_simple - send asserted event to uevent thread
  2522. * @sdev: scsi_device event occurred on
  2523. * @evt_type: type of event to send
  2524. * @gfpflags: GFP flags for allocation
  2525. *
  2526. * Assert scsi device event asynchronously, given an event type.
  2527. */
  2528. void sdev_evt_send_simple(struct scsi_device *sdev,
  2529. enum scsi_device_event evt_type, gfp_t gfpflags)
  2530. {
  2531. struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
  2532. if (!evt) {
  2533. sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
  2534. evt_type);
  2535. return;
  2536. }
  2537. sdev_evt_send(sdev, evt);
  2538. }
  2539. EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
  2540. /**
  2541. * scsi_request_fn_active() - number of kernel threads inside scsi_request_fn()
  2542. * @sdev: SCSI device to count the number of scsi_request_fn() callers for.
  2543. */
  2544. static int scsi_request_fn_active(struct scsi_device *sdev)
  2545. {
  2546. struct request_queue *q = sdev->request_queue;
  2547. int request_fn_active;
  2548. WARN_ON_ONCE(sdev->host->use_blk_mq);
  2549. spin_lock_irq(q->queue_lock);
  2550. request_fn_active = q->request_fn_active;
  2551. spin_unlock_irq(q->queue_lock);
  2552. return request_fn_active;
  2553. }
  2554. /**
  2555. * scsi_wait_for_queuecommand() - wait for ongoing queuecommand() calls
  2556. * @sdev: SCSI device pointer.
  2557. *
  2558. * Wait until the ongoing shost->hostt->queuecommand() calls that are
  2559. * invoked from scsi_request_fn() have finished.
  2560. */
  2561. static void scsi_wait_for_queuecommand(struct scsi_device *sdev)
  2562. {
  2563. WARN_ON_ONCE(sdev->host->use_blk_mq);
  2564. while (scsi_request_fn_active(sdev))
  2565. msleep(20);
  2566. }
  2567. /**
  2568. * scsi_device_quiesce - Block user issued commands.
  2569. * @sdev: scsi device to quiesce.
  2570. *
  2571. * This works by trying to transition to the SDEV_QUIESCE state
  2572. * (which must be a legal transition). When the device is in this
  2573. * state, only special requests will be accepted, all others will
  2574. * be deferred. Since special requests may also be requeued requests,
  2575. * a successful return doesn't guarantee the device will be
  2576. * totally quiescent.
  2577. *
  2578. * Must be called with user context, may sleep.
  2579. *
  2580. * Returns zero if unsuccessful or an error if not.
  2581. */
  2582. int
  2583. scsi_device_quiesce(struct scsi_device *sdev)
  2584. {
  2585. int err;
  2586. mutex_lock(&sdev->state_mutex);
  2587. err = scsi_device_set_state(sdev, SDEV_QUIESCE);
  2588. mutex_unlock(&sdev->state_mutex);
  2589. if (err)
  2590. return err;
  2591. scsi_run_queue(sdev->request_queue);
  2592. while (atomic_read(&sdev->device_busy)) {
  2593. msleep_interruptible(200);
  2594. scsi_run_queue(sdev->request_queue);
  2595. }
  2596. return 0;
  2597. }
  2598. EXPORT_SYMBOL(scsi_device_quiesce);
  2599. /**
  2600. * scsi_device_resume - Restart user issued commands to a quiesced device.
  2601. * @sdev: scsi device to resume.
  2602. *
  2603. * Moves the device from quiesced back to running and restarts the
  2604. * queues.
  2605. *
  2606. * Must be called with user context, may sleep.
  2607. */
  2608. void scsi_device_resume(struct scsi_device *sdev)
  2609. {
  2610. /* check if the device state was mutated prior to resume, and if
  2611. * so assume the state is being managed elsewhere (for example
  2612. * device deleted during suspend)
  2613. */
  2614. mutex_lock(&sdev->state_mutex);
  2615. if (sdev->sdev_state == SDEV_QUIESCE &&
  2616. scsi_device_set_state(sdev, SDEV_RUNNING) == 0)
  2617. scsi_run_queue(sdev->request_queue);
  2618. mutex_unlock(&sdev->state_mutex);
  2619. }
  2620. EXPORT_SYMBOL(scsi_device_resume);
  2621. static void
  2622. device_quiesce_fn(struct scsi_device *sdev, void *data)
  2623. {
  2624. scsi_device_quiesce(sdev);
  2625. }
  2626. void
  2627. scsi_target_quiesce(struct scsi_target *starget)
  2628. {
  2629. starget_for_each_device(starget, NULL, device_quiesce_fn);
  2630. }
  2631. EXPORT_SYMBOL(scsi_target_quiesce);
  2632. static void
  2633. device_resume_fn(struct scsi_device *sdev, void *data)
  2634. {
  2635. scsi_device_resume(sdev);
  2636. }
  2637. void
  2638. scsi_target_resume(struct scsi_target *starget)
  2639. {
  2640. starget_for_each_device(starget, NULL, device_resume_fn);
  2641. }
  2642. EXPORT_SYMBOL(scsi_target_resume);
  2643. /**
  2644. * scsi_internal_device_block_nowait - try to transition to the SDEV_BLOCK state
  2645. * @sdev: device to block
  2646. *
  2647. * Pause SCSI command processing on the specified device. Does not sleep.
  2648. *
  2649. * Returns zero if successful or a negative error code upon failure.
  2650. *
  2651. * Notes:
  2652. * This routine transitions the device to the SDEV_BLOCK state (which must be
  2653. * a legal transition). When the device is in this state, command processing
  2654. * is paused until the device leaves the SDEV_BLOCK state. See also
  2655. * scsi_internal_device_unblock_nowait().
  2656. */
  2657. int scsi_internal_device_block_nowait(struct scsi_device *sdev)
  2658. {
  2659. struct request_queue *q = sdev->request_queue;
  2660. unsigned long flags;
  2661. int err = 0;
  2662. err = scsi_device_set_state(sdev, SDEV_BLOCK);
  2663. if (err) {
  2664. err = scsi_device_set_state(sdev, SDEV_CREATED_BLOCK);
  2665. if (err)
  2666. return err;
  2667. }
  2668. /*
  2669. * The device has transitioned to SDEV_BLOCK. Stop the
  2670. * block layer from calling the midlayer with this device's
  2671. * request queue.
  2672. */
  2673. if (q->mq_ops) {
  2674. blk_mq_quiesce_queue_nowait(q);
  2675. } else {
  2676. spin_lock_irqsave(q->queue_lock, flags);
  2677. blk_stop_queue(q);
  2678. spin_unlock_irqrestore(q->queue_lock, flags);
  2679. }
  2680. return 0;
  2681. }
  2682. EXPORT_SYMBOL_GPL(scsi_internal_device_block_nowait);
  2683. /**
  2684. * scsi_internal_device_block - try to transition to the SDEV_BLOCK state
  2685. * @sdev: device to block
  2686. *
  2687. * Pause SCSI command processing on the specified device and wait until all
  2688. * ongoing scsi_request_fn() / scsi_queue_rq() calls have finished. May sleep.
  2689. *
  2690. * Returns zero if successful or a negative error code upon failure.
  2691. *
  2692. * Note:
  2693. * This routine transitions the device to the SDEV_BLOCK state (which must be
  2694. * a legal transition). When the device is in this state, command processing
  2695. * is paused until the device leaves the SDEV_BLOCK state. See also
  2696. * scsi_internal_device_unblock().
  2697. *
  2698. * To do: avoid that scsi_send_eh_cmnd() calls queuecommand() after
  2699. * scsi_internal_device_block() has blocked a SCSI device and also
  2700. * remove the rport mutex lock and unlock calls from srp_queuecommand().
  2701. */
  2702. static int scsi_internal_device_block(struct scsi_device *sdev)
  2703. {
  2704. struct request_queue *q = sdev->request_queue;
  2705. int err;
  2706. mutex_lock(&sdev->state_mutex);
  2707. err = scsi_internal_device_block_nowait(sdev);
  2708. if (err == 0) {
  2709. if (q->mq_ops)
  2710. blk_mq_quiesce_queue(q);
  2711. else
  2712. scsi_wait_for_queuecommand(sdev);
  2713. }
  2714. mutex_unlock(&sdev->state_mutex);
  2715. return err;
  2716. }
  2717. void scsi_start_queue(struct scsi_device *sdev)
  2718. {
  2719. struct request_queue *q = sdev->request_queue;
  2720. unsigned long flags;
  2721. if (q->mq_ops) {
  2722. blk_mq_unquiesce_queue(q);
  2723. } else {
  2724. spin_lock_irqsave(q->queue_lock, flags);
  2725. blk_start_queue(q);
  2726. spin_unlock_irqrestore(q->queue_lock, flags);
  2727. }
  2728. }
  2729. /**
  2730. * scsi_internal_device_unblock_nowait - resume a device after a block request
  2731. * @sdev: device to resume
  2732. * @new_state: state to set the device to after unblocking
  2733. *
  2734. * Restart the device queue for a previously suspended SCSI device. Does not
  2735. * sleep.
  2736. *
  2737. * Returns zero if successful or a negative error code upon failure.
  2738. *
  2739. * Notes:
  2740. * This routine transitions the device to the SDEV_RUNNING state or to one of
  2741. * the offline states (which must be a legal transition) allowing the midlayer
  2742. * to goose the queue for this device.
  2743. */
  2744. int scsi_internal_device_unblock_nowait(struct scsi_device *sdev,
  2745. enum scsi_device_state new_state)
  2746. {
  2747. /*
  2748. * Try to transition the scsi device to SDEV_RUNNING or one of the
  2749. * offlined states and goose the device queue if successful.
  2750. */
  2751. switch (sdev->sdev_state) {
  2752. case SDEV_BLOCK:
  2753. case SDEV_TRANSPORT_OFFLINE:
  2754. sdev->sdev_state = new_state;
  2755. sysfs_notify(&sdev->sdev_gendev.kobj, NULL, "state");
  2756. break;
  2757. case SDEV_CREATED_BLOCK:
  2758. if (new_state == SDEV_TRANSPORT_OFFLINE ||
  2759. new_state == SDEV_OFFLINE)
  2760. sdev->sdev_state = new_state;
  2761. else
  2762. sdev->sdev_state = SDEV_CREATED;
  2763. sysfs_notify(&sdev->sdev_gendev.kobj, NULL, "state");
  2764. break;
  2765. case SDEV_CANCEL:
  2766. case SDEV_OFFLINE:
  2767. break;
  2768. default:
  2769. return -EINVAL;
  2770. }
  2771. scsi_start_queue(sdev);
  2772. return 0;
  2773. }
  2774. EXPORT_SYMBOL_GPL(scsi_internal_device_unblock_nowait);
  2775. /**
  2776. * scsi_internal_device_unblock - resume a device after a block request
  2777. * @sdev: device to resume
  2778. * @new_state: state to set the device to after unblocking
  2779. *
  2780. * Restart the device queue for a previously suspended SCSI device. May sleep.
  2781. *
  2782. * Returns zero if successful or a negative error code upon failure.
  2783. *
  2784. * Notes:
  2785. * This routine transitions the device to the SDEV_RUNNING state or to one of
  2786. * the offline states (which must be a legal transition) allowing the midlayer
  2787. * to goose the queue for this device.
  2788. */
  2789. static int scsi_internal_device_unblock(struct scsi_device *sdev,
  2790. enum scsi_device_state new_state)
  2791. {
  2792. int ret;
  2793. mutex_lock(&sdev->state_mutex);
  2794. ret = scsi_internal_device_unblock_nowait(sdev, new_state);
  2795. mutex_unlock(&sdev->state_mutex);
  2796. return ret;
  2797. }
  2798. static void
  2799. device_block(struct scsi_device *sdev, void *data)
  2800. {
  2801. scsi_internal_device_block(sdev);
  2802. }
  2803. static int
  2804. target_block(struct device *dev, void *data)
  2805. {
  2806. if (scsi_is_target_device(dev))
  2807. starget_for_each_device(to_scsi_target(dev), NULL,
  2808. device_block);
  2809. return 0;
  2810. }
  2811. void
  2812. scsi_target_block(struct device *dev)
  2813. {
  2814. if (scsi_is_target_device(dev))
  2815. starget_for_each_device(to_scsi_target(dev), NULL,
  2816. device_block);
  2817. else
  2818. device_for_each_child(dev, NULL, target_block);
  2819. }
  2820. EXPORT_SYMBOL_GPL(scsi_target_block);
  2821. static void
  2822. device_unblock(struct scsi_device *sdev, void *data)
  2823. {
  2824. scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data);
  2825. }
  2826. static int
  2827. target_unblock(struct device *dev, void *data)
  2828. {
  2829. if (scsi_is_target_device(dev))
  2830. starget_for_each_device(to_scsi_target(dev), data,
  2831. device_unblock);
  2832. return 0;
  2833. }
  2834. void
  2835. scsi_target_unblock(struct device *dev, enum scsi_device_state new_state)
  2836. {
  2837. if (scsi_is_target_device(dev))
  2838. starget_for_each_device(to_scsi_target(dev), &new_state,
  2839. device_unblock);
  2840. else
  2841. device_for_each_child(dev, &new_state, target_unblock);
  2842. }
  2843. EXPORT_SYMBOL_GPL(scsi_target_unblock);
  2844. /**
  2845. * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
  2846. * @sgl: scatter-gather list
  2847. * @sg_count: number of segments in sg
  2848. * @offset: offset in bytes into sg, on return offset into the mapped area
  2849. * @len: bytes to map, on return number of bytes mapped
  2850. *
  2851. * Returns virtual address of the start of the mapped page
  2852. */
  2853. void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
  2854. size_t *offset, size_t *len)
  2855. {
  2856. int i;
  2857. size_t sg_len = 0, len_complete = 0;
  2858. struct scatterlist *sg;
  2859. struct page *page;
  2860. WARN_ON(!irqs_disabled());
  2861. for_each_sg(sgl, sg, sg_count, i) {
  2862. len_complete = sg_len; /* Complete sg-entries */
  2863. sg_len += sg->length;
  2864. if (sg_len > *offset)
  2865. break;
  2866. }
  2867. if (unlikely(i == sg_count)) {
  2868. printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
  2869. "elements %d\n",
  2870. __func__, sg_len, *offset, sg_count);
  2871. WARN_ON(1);
  2872. return NULL;
  2873. }
  2874. /* Offset starting from the beginning of first page in this sg-entry */
  2875. *offset = *offset - len_complete + sg->offset;
  2876. /* Assumption: contiguous pages can be accessed as "page + i" */
  2877. page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
  2878. *offset &= ~PAGE_MASK;
  2879. /* Bytes in this sg-entry from *offset to the end of the page */
  2880. sg_len = PAGE_SIZE - *offset;
  2881. if (*len > sg_len)
  2882. *len = sg_len;
  2883. return kmap_atomic(page);
  2884. }
  2885. EXPORT_SYMBOL(scsi_kmap_atomic_sg);
  2886. /**
  2887. * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg
  2888. * @virt: virtual address to be unmapped
  2889. */
  2890. void scsi_kunmap_atomic_sg(void *virt)
  2891. {
  2892. kunmap_atomic(virt);
  2893. }
  2894. EXPORT_SYMBOL(scsi_kunmap_atomic_sg);
  2895. void sdev_disable_disk_events(struct scsi_device *sdev)
  2896. {
  2897. atomic_inc(&sdev->disk_events_disable_depth);
  2898. }
  2899. EXPORT_SYMBOL(sdev_disable_disk_events);
  2900. void sdev_enable_disk_events(struct scsi_device *sdev)
  2901. {
  2902. if (WARN_ON_ONCE(atomic_read(&sdev->disk_events_disable_depth) <= 0))
  2903. return;
  2904. atomic_dec(&sdev->disk_events_disable_depth);
  2905. }
  2906. EXPORT_SYMBOL(sdev_enable_disk_events);
  2907. /**
  2908. * scsi_vpd_lun_id - return a unique device identification
  2909. * @sdev: SCSI device
  2910. * @id: buffer for the identification
  2911. * @id_len: length of the buffer
  2912. *
  2913. * Copies a unique device identification into @id based
  2914. * on the information in the VPD page 0x83 of the device.
  2915. * The string will be formatted as a SCSI name string.
  2916. *
  2917. * Returns the length of the identification or error on failure.
  2918. * If the identifier is longer than the supplied buffer the actual
  2919. * identifier length is returned and the buffer is not zero-padded.
  2920. */
  2921. int scsi_vpd_lun_id(struct scsi_device *sdev, char *id, size_t id_len)
  2922. {
  2923. u8 cur_id_type = 0xff;
  2924. u8 cur_id_size = 0;
  2925. const unsigned char *d, *cur_id_str;
  2926. const struct scsi_vpd *vpd_pg83;
  2927. int id_size = -EINVAL;
  2928. rcu_read_lock();
  2929. vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
  2930. if (!vpd_pg83) {
  2931. rcu_read_unlock();
  2932. return -ENXIO;
  2933. }
  2934. /*
  2935. * Look for the correct descriptor.
  2936. * Order of preference for lun descriptor:
  2937. * - SCSI name string
  2938. * - NAA IEEE Registered Extended
  2939. * - EUI-64 based 16-byte
  2940. * - EUI-64 based 12-byte
  2941. * - NAA IEEE Registered
  2942. * - NAA IEEE Extended
  2943. * - T10 Vendor ID
  2944. * as longer descriptors reduce the likelyhood
  2945. * of identification clashes.
  2946. */
  2947. /* The id string must be at least 20 bytes + terminating NULL byte */
  2948. if (id_len < 21) {
  2949. rcu_read_unlock();
  2950. return -EINVAL;
  2951. }
  2952. memset(id, 0, id_len);
  2953. d = vpd_pg83->data + 4;
  2954. while (d < vpd_pg83->data + vpd_pg83->len) {
  2955. /* Skip designators not referring to the LUN */
  2956. if ((d[1] & 0x30) != 0x00)
  2957. goto next_desig;
  2958. switch (d[1] & 0xf) {
  2959. case 0x1:
  2960. /* T10 Vendor ID */
  2961. if (cur_id_size > d[3])
  2962. break;
  2963. /* Prefer anything */
  2964. if (cur_id_type > 0x01 && cur_id_type != 0xff)
  2965. break;
  2966. cur_id_size = d[3];
  2967. if (cur_id_size + 4 > id_len)
  2968. cur_id_size = id_len - 4;
  2969. cur_id_str = d + 4;
  2970. cur_id_type = d[1] & 0xf;
  2971. id_size = snprintf(id, id_len, "t10.%*pE",
  2972. cur_id_size, cur_id_str);
  2973. break;
  2974. case 0x2:
  2975. /* EUI-64 */
  2976. if (cur_id_size > d[3])
  2977. break;
  2978. /* Prefer NAA IEEE Registered Extended */
  2979. if (cur_id_type == 0x3 &&
  2980. cur_id_size == d[3])
  2981. break;
  2982. cur_id_size = d[3];
  2983. cur_id_str = d + 4;
  2984. cur_id_type = d[1] & 0xf;
  2985. switch (cur_id_size) {
  2986. case 8:
  2987. id_size = snprintf(id, id_len,
  2988. "eui.%8phN",
  2989. cur_id_str);
  2990. break;
  2991. case 12:
  2992. id_size = snprintf(id, id_len,
  2993. "eui.%12phN",
  2994. cur_id_str);
  2995. break;
  2996. case 16:
  2997. id_size = snprintf(id, id_len,
  2998. "eui.%16phN",
  2999. cur_id_str);
  3000. break;
  3001. default:
  3002. cur_id_size = 0;
  3003. break;
  3004. }
  3005. break;
  3006. case 0x3:
  3007. /* NAA */
  3008. if (cur_id_size > d[3])
  3009. break;
  3010. cur_id_size = d[3];
  3011. cur_id_str = d + 4;
  3012. cur_id_type = d[1] & 0xf;
  3013. switch (cur_id_size) {
  3014. case 8:
  3015. id_size = snprintf(id, id_len,
  3016. "naa.%8phN",
  3017. cur_id_str);
  3018. break;
  3019. case 16:
  3020. id_size = snprintf(id, id_len,
  3021. "naa.%16phN",
  3022. cur_id_str);
  3023. break;
  3024. default:
  3025. cur_id_size = 0;
  3026. break;
  3027. }
  3028. break;
  3029. case 0x8:
  3030. /* SCSI name string */
  3031. if (cur_id_size + 4 > d[3])
  3032. break;
  3033. /* Prefer others for truncated descriptor */
  3034. if (cur_id_size && d[3] > id_len)
  3035. break;
  3036. cur_id_size = id_size = d[3];
  3037. cur_id_str = d + 4;
  3038. cur_id_type = d[1] & 0xf;
  3039. if (cur_id_size >= id_len)
  3040. cur_id_size = id_len - 1;
  3041. memcpy(id, cur_id_str, cur_id_size);
  3042. /* Decrease priority for truncated descriptor */
  3043. if (cur_id_size != id_size)
  3044. cur_id_size = 6;
  3045. break;
  3046. default:
  3047. break;
  3048. }
  3049. next_desig:
  3050. d += d[3] + 4;
  3051. }
  3052. rcu_read_unlock();
  3053. return id_size;
  3054. }
  3055. EXPORT_SYMBOL(scsi_vpd_lun_id);
  3056. /*
  3057. * scsi_vpd_tpg_id - return a target port group identifier
  3058. * @sdev: SCSI device
  3059. *
  3060. * Returns the Target Port Group identifier from the information
  3061. * froom VPD page 0x83 of the device.
  3062. *
  3063. * Returns the identifier or error on failure.
  3064. */
  3065. int scsi_vpd_tpg_id(struct scsi_device *sdev, int *rel_id)
  3066. {
  3067. const unsigned char *d;
  3068. const struct scsi_vpd *vpd_pg83;
  3069. int group_id = -EAGAIN, rel_port = -1;
  3070. rcu_read_lock();
  3071. vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
  3072. if (!vpd_pg83) {
  3073. rcu_read_unlock();
  3074. return -ENXIO;
  3075. }
  3076. d = vpd_pg83->data + 4;
  3077. while (d < vpd_pg83->data + vpd_pg83->len) {
  3078. switch (d[1] & 0xf) {
  3079. case 0x4:
  3080. /* Relative target port */
  3081. rel_port = get_unaligned_be16(&d[6]);
  3082. break;
  3083. case 0x5:
  3084. /* Target port group */
  3085. group_id = get_unaligned_be16(&d[6]);
  3086. break;
  3087. default:
  3088. break;
  3089. }
  3090. d += d[3] + 4;
  3091. }
  3092. rcu_read_unlock();
  3093. if (group_id >= 0 && rel_id && rel_port != -1)
  3094. *rel_id = rel_port;
  3095. return group_id;
  3096. }
  3097. EXPORT_SYMBOL(scsi_vpd_tpg_id);