scsi_lib.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523
  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_flags(sdev->request_queue,
  229. data_direction == DMA_TO_DEVICE ?
  230. REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, BLK_MQ_REQ_PREEMPT);
  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;
  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. return scsi_prep_return(q, req, ret);
  1229. }
  1230. static void scsi_unprep_fn(struct request_queue *q, struct request *req)
  1231. {
  1232. scsi_uninit_cmd(blk_mq_rq_to_pdu(req));
  1233. }
  1234. /*
  1235. * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
  1236. * return 0.
  1237. *
  1238. * Called with the queue_lock held.
  1239. */
  1240. static inline int scsi_dev_queue_ready(struct request_queue *q,
  1241. struct scsi_device *sdev)
  1242. {
  1243. unsigned int busy;
  1244. busy = atomic_inc_return(&sdev->device_busy) - 1;
  1245. if (atomic_read(&sdev->device_blocked)) {
  1246. if (busy)
  1247. goto out_dec;
  1248. /*
  1249. * unblock after device_blocked iterates to zero
  1250. */
  1251. if (atomic_dec_return(&sdev->device_blocked) > 0) {
  1252. /*
  1253. * For the MQ case we take care of this in the caller.
  1254. */
  1255. if (!q->mq_ops)
  1256. blk_delay_queue(q, SCSI_QUEUE_DELAY);
  1257. goto out_dec;
  1258. }
  1259. SCSI_LOG_MLQUEUE(3, sdev_printk(KERN_INFO, sdev,
  1260. "unblocking device at zero depth\n"));
  1261. }
  1262. if (busy >= sdev->queue_depth)
  1263. goto out_dec;
  1264. return 1;
  1265. out_dec:
  1266. atomic_dec(&sdev->device_busy);
  1267. return 0;
  1268. }
  1269. /*
  1270. * scsi_target_queue_ready: checks if there we can send commands to target
  1271. * @sdev: scsi device on starget to check.
  1272. */
  1273. static inline int scsi_target_queue_ready(struct Scsi_Host *shost,
  1274. struct scsi_device *sdev)
  1275. {
  1276. struct scsi_target *starget = scsi_target(sdev);
  1277. unsigned int busy;
  1278. if (starget->single_lun) {
  1279. spin_lock_irq(shost->host_lock);
  1280. if (starget->starget_sdev_user &&
  1281. starget->starget_sdev_user != sdev) {
  1282. spin_unlock_irq(shost->host_lock);
  1283. return 0;
  1284. }
  1285. starget->starget_sdev_user = sdev;
  1286. spin_unlock_irq(shost->host_lock);
  1287. }
  1288. if (starget->can_queue <= 0)
  1289. return 1;
  1290. busy = atomic_inc_return(&starget->target_busy) - 1;
  1291. if (atomic_read(&starget->target_blocked) > 0) {
  1292. if (busy)
  1293. goto starved;
  1294. /*
  1295. * unblock after target_blocked iterates to zero
  1296. */
  1297. if (atomic_dec_return(&starget->target_blocked) > 0)
  1298. goto out_dec;
  1299. SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget,
  1300. "unblocking target at zero depth\n"));
  1301. }
  1302. if (busy >= starget->can_queue)
  1303. goto starved;
  1304. return 1;
  1305. starved:
  1306. spin_lock_irq(shost->host_lock);
  1307. list_move_tail(&sdev->starved_entry, &shost->starved_list);
  1308. spin_unlock_irq(shost->host_lock);
  1309. out_dec:
  1310. if (starget->can_queue > 0)
  1311. atomic_dec(&starget->target_busy);
  1312. return 0;
  1313. }
  1314. /*
  1315. * scsi_host_queue_ready: if we can send requests to shost, return 1 else
  1316. * return 0. We must end up running the queue again whenever 0 is
  1317. * returned, else IO can hang.
  1318. */
  1319. static inline int scsi_host_queue_ready(struct request_queue *q,
  1320. struct Scsi_Host *shost,
  1321. struct scsi_device *sdev)
  1322. {
  1323. unsigned int busy;
  1324. if (scsi_host_in_recovery(shost))
  1325. return 0;
  1326. busy = atomic_inc_return(&shost->host_busy) - 1;
  1327. if (atomic_read(&shost->host_blocked) > 0) {
  1328. if (busy)
  1329. goto starved;
  1330. /*
  1331. * unblock after host_blocked iterates to zero
  1332. */
  1333. if (atomic_dec_return(&shost->host_blocked) > 0)
  1334. goto out_dec;
  1335. SCSI_LOG_MLQUEUE(3,
  1336. shost_printk(KERN_INFO, shost,
  1337. "unblocking host at zero depth\n"));
  1338. }
  1339. if (shost->can_queue > 0 && busy >= shost->can_queue)
  1340. goto starved;
  1341. if (shost->host_self_blocked)
  1342. goto starved;
  1343. /* We're OK to process the command, so we can't be starved */
  1344. if (!list_empty(&sdev->starved_entry)) {
  1345. spin_lock_irq(shost->host_lock);
  1346. if (!list_empty(&sdev->starved_entry))
  1347. list_del_init(&sdev->starved_entry);
  1348. spin_unlock_irq(shost->host_lock);
  1349. }
  1350. return 1;
  1351. starved:
  1352. spin_lock_irq(shost->host_lock);
  1353. if (list_empty(&sdev->starved_entry))
  1354. list_add_tail(&sdev->starved_entry, &shost->starved_list);
  1355. spin_unlock_irq(shost->host_lock);
  1356. out_dec:
  1357. atomic_dec(&shost->host_busy);
  1358. return 0;
  1359. }
  1360. /*
  1361. * Busy state exporting function for request stacking drivers.
  1362. *
  1363. * For efficiency, no lock is taken to check the busy state of
  1364. * shost/starget/sdev, since the returned value is not guaranteed and
  1365. * may be changed after request stacking drivers call the function,
  1366. * regardless of taking lock or not.
  1367. *
  1368. * When scsi can't dispatch I/Os anymore and needs to kill I/Os scsi
  1369. * needs to return 'not busy'. Otherwise, request stacking drivers
  1370. * may hold requests forever.
  1371. */
  1372. static int scsi_lld_busy(struct request_queue *q)
  1373. {
  1374. struct scsi_device *sdev = q->queuedata;
  1375. struct Scsi_Host *shost;
  1376. if (blk_queue_dying(q))
  1377. return 0;
  1378. shost = sdev->host;
  1379. /*
  1380. * Ignore host/starget busy state.
  1381. * Since block layer does not have a concept of fairness across
  1382. * multiple queues, congestion of host/starget needs to be handled
  1383. * in SCSI layer.
  1384. */
  1385. if (scsi_host_in_recovery(shost) || scsi_device_is_busy(sdev))
  1386. return 1;
  1387. return 0;
  1388. }
  1389. /*
  1390. * Kill a request for a dead device
  1391. */
  1392. static void scsi_kill_request(struct request *req, struct request_queue *q)
  1393. {
  1394. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
  1395. struct scsi_device *sdev;
  1396. struct scsi_target *starget;
  1397. struct Scsi_Host *shost;
  1398. blk_start_request(req);
  1399. scmd_printk(KERN_INFO, cmd, "killing request\n");
  1400. sdev = cmd->device;
  1401. starget = scsi_target(sdev);
  1402. shost = sdev->host;
  1403. scsi_init_cmd_errh(cmd);
  1404. cmd->result = DID_NO_CONNECT << 16;
  1405. atomic_inc(&cmd->device->iorequest_cnt);
  1406. /*
  1407. * SCSI request completion path will do scsi_device_unbusy(),
  1408. * bump busy counts. To bump the counters, we need to dance
  1409. * with the locks as normal issue path does.
  1410. */
  1411. atomic_inc(&sdev->device_busy);
  1412. atomic_inc(&shost->host_busy);
  1413. if (starget->can_queue > 0)
  1414. atomic_inc(&starget->target_busy);
  1415. blk_complete_request(req);
  1416. }
  1417. static void scsi_softirq_done(struct request *rq)
  1418. {
  1419. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
  1420. unsigned long wait_for = (cmd->allowed + 1) * rq->timeout;
  1421. int disposition;
  1422. INIT_LIST_HEAD(&cmd->eh_entry);
  1423. atomic_inc(&cmd->device->iodone_cnt);
  1424. if (cmd->result)
  1425. atomic_inc(&cmd->device->ioerr_cnt);
  1426. disposition = scsi_decide_disposition(cmd);
  1427. if (disposition != SUCCESS &&
  1428. time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
  1429. sdev_printk(KERN_ERR, cmd->device,
  1430. "timing out command, waited %lus\n",
  1431. wait_for/HZ);
  1432. disposition = SUCCESS;
  1433. }
  1434. scsi_log_completion(cmd, disposition);
  1435. switch (disposition) {
  1436. case SUCCESS:
  1437. scsi_finish_command(cmd);
  1438. break;
  1439. case NEEDS_RETRY:
  1440. scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
  1441. break;
  1442. case ADD_TO_MLQUEUE:
  1443. scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
  1444. break;
  1445. default:
  1446. scsi_eh_scmd_add(cmd);
  1447. break;
  1448. }
  1449. }
  1450. /**
  1451. * scsi_dispatch_command - Dispatch a command to the low-level driver.
  1452. * @cmd: command block we are dispatching.
  1453. *
  1454. * Return: nonzero return request was rejected and device's queue needs to be
  1455. * plugged.
  1456. */
  1457. static int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
  1458. {
  1459. struct Scsi_Host *host = cmd->device->host;
  1460. int rtn = 0;
  1461. atomic_inc(&cmd->device->iorequest_cnt);
  1462. /* check if the device is still usable */
  1463. if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
  1464. /* in SDEV_DEL we error all commands. DID_NO_CONNECT
  1465. * returns an immediate error upwards, and signals
  1466. * that the device is no longer present */
  1467. cmd->result = DID_NO_CONNECT << 16;
  1468. goto done;
  1469. }
  1470. /* Check to see if the scsi lld made this device blocked. */
  1471. if (unlikely(scsi_device_blocked(cmd->device))) {
  1472. /*
  1473. * in blocked state, the command is just put back on
  1474. * the device queue. The suspend state has already
  1475. * blocked the queue so future requests should not
  1476. * occur until the device transitions out of the
  1477. * suspend state.
  1478. */
  1479. SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
  1480. "queuecommand : device blocked\n"));
  1481. return SCSI_MLQUEUE_DEVICE_BUSY;
  1482. }
  1483. /* Store the LUN value in cmnd, if needed. */
  1484. if (cmd->device->lun_in_cdb)
  1485. cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
  1486. (cmd->device->lun << 5 & 0xe0);
  1487. scsi_log_send(cmd);
  1488. /*
  1489. * Before we queue this command, check if the command
  1490. * length exceeds what the host adapter can handle.
  1491. */
  1492. if (cmd->cmd_len > cmd->device->host->max_cmd_len) {
  1493. SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
  1494. "queuecommand : command too long. "
  1495. "cdb_size=%d host->max_cmd_len=%d\n",
  1496. cmd->cmd_len, cmd->device->host->max_cmd_len));
  1497. cmd->result = (DID_ABORT << 16);
  1498. goto done;
  1499. }
  1500. if (unlikely(host->shost_state == SHOST_DEL)) {
  1501. cmd->result = (DID_NO_CONNECT << 16);
  1502. goto done;
  1503. }
  1504. trace_scsi_dispatch_cmd_start(cmd);
  1505. rtn = host->hostt->queuecommand(host, cmd);
  1506. if (rtn) {
  1507. trace_scsi_dispatch_cmd_error(cmd, rtn);
  1508. if (rtn != SCSI_MLQUEUE_DEVICE_BUSY &&
  1509. rtn != SCSI_MLQUEUE_TARGET_BUSY)
  1510. rtn = SCSI_MLQUEUE_HOST_BUSY;
  1511. SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
  1512. "queuecommand : request rejected\n"));
  1513. }
  1514. return rtn;
  1515. done:
  1516. cmd->scsi_done(cmd);
  1517. return 0;
  1518. }
  1519. /**
  1520. * scsi_done - Invoke completion on finished SCSI command.
  1521. * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
  1522. * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
  1523. *
  1524. * Description: This function is the mid-level's (SCSI Core) interrupt routine,
  1525. * which regains ownership of the SCSI command (de facto) from a LLDD, and
  1526. * calls blk_complete_request() for further processing.
  1527. *
  1528. * This function is interrupt context safe.
  1529. */
  1530. static void scsi_done(struct scsi_cmnd *cmd)
  1531. {
  1532. trace_scsi_dispatch_cmd_done(cmd);
  1533. blk_complete_request(cmd->request);
  1534. }
  1535. /*
  1536. * Function: scsi_request_fn()
  1537. *
  1538. * Purpose: Main strategy routine for SCSI.
  1539. *
  1540. * Arguments: q - Pointer to actual queue.
  1541. *
  1542. * Returns: Nothing
  1543. *
  1544. * Lock status: IO request lock assumed to be held when called.
  1545. */
  1546. static void scsi_request_fn(struct request_queue *q)
  1547. __releases(q->queue_lock)
  1548. __acquires(q->queue_lock)
  1549. {
  1550. struct scsi_device *sdev = q->queuedata;
  1551. struct Scsi_Host *shost;
  1552. struct scsi_cmnd *cmd;
  1553. struct request *req;
  1554. /*
  1555. * To start with, we keep looping until the queue is empty, or until
  1556. * the host is no longer able to accept any more requests.
  1557. */
  1558. shost = sdev->host;
  1559. for (;;) {
  1560. int rtn;
  1561. /*
  1562. * get next queueable request. We do this early to make sure
  1563. * that the request is fully prepared even if we cannot
  1564. * accept it.
  1565. */
  1566. req = blk_peek_request(q);
  1567. if (!req)
  1568. break;
  1569. if (unlikely(!scsi_device_online(sdev))) {
  1570. sdev_printk(KERN_ERR, sdev,
  1571. "rejecting I/O to offline device\n");
  1572. scsi_kill_request(req, q);
  1573. continue;
  1574. }
  1575. if (!scsi_dev_queue_ready(q, sdev))
  1576. break;
  1577. /*
  1578. * Remove the request from the request list.
  1579. */
  1580. if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
  1581. blk_start_request(req);
  1582. spin_unlock_irq(q->queue_lock);
  1583. cmd = blk_mq_rq_to_pdu(req);
  1584. if (cmd != req->special) {
  1585. printk(KERN_CRIT "impossible request in %s.\n"
  1586. "please mail a stack trace to "
  1587. "linux-scsi@vger.kernel.org\n",
  1588. __func__);
  1589. blk_dump_rq_flags(req, "foo");
  1590. BUG();
  1591. }
  1592. /*
  1593. * We hit this when the driver is using a host wide
  1594. * tag map. For device level tag maps the queue_depth check
  1595. * in the device ready fn would prevent us from trying
  1596. * to allocate a tag. Since the map is a shared host resource
  1597. * we add the dev to the starved list so it eventually gets
  1598. * a run when a tag is freed.
  1599. */
  1600. if (blk_queue_tagged(q) && !(req->rq_flags & RQF_QUEUED)) {
  1601. spin_lock_irq(shost->host_lock);
  1602. if (list_empty(&sdev->starved_entry))
  1603. list_add_tail(&sdev->starved_entry,
  1604. &shost->starved_list);
  1605. spin_unlock_irq(shost->host_lock);
  1606. goto not_ready;
  1607. }
  1608. if (!scsi_target_queue_ready(shost, sdev))
  1609. goto not_ready;
  1610. if (!scsi_host_queue_ready(q, shost, sdev))
  1611. goto host_not_ready;
  1612. if (sdev->simple_tags)
  1613. cmd->flags |= SCMD_TAGGED;
  1614. else
  1615. cmd->flags &= ~SCMD_TAGGED;
  1616. /*
  1617. * Finally, initialize any error handling parameters, and set up
  1618. * the timers for timeouts.
  1619. */
  1620. scsi_init_cmd_errh(cmd);
  1621. /*
  1622. * Dispatch the command to the low-level driver.
  1623. */
  1624. cmd->scsi_done = scsi_done;
  1625. rtn = scsi_dispatch_cmd(cmd);
  1626. if (rtn) {
  1627. scsi_queue_insert(cmd, rtn);
  1628. spin_lock_irq(q->queue_lock);
  1629. goto out_delay;
  1630. }
  1631. spin_lock_irq(q->queue_lock);
  1632. }
  1633. return;
  1634. host_not_ready:
  1635. if (scsi_target(sdev)->can_queue > 0)
  1636. atomic_dec(&scsi_target(sdev)->target_busy);
  1637. not_ready:
  1638. /*
  1639. * lock q, handle tag, requeue req, and decrement device_busy. We
  1640. * must return with queue_lock held.
  1641. *
  1642. * Decrementing device_busy without checking it is OK, as all such
  1643. * cases (host limits or settings) should run the queue at some
  1644. * later time.
  1645. */
  1646. spin_lock_irq(q->queue_lock);
  1647. blk_requeue_request(q, req);
  1648. atomic_dec(&sdev->device_busy);
  1649. out_delay:
  1650. if (!atomic_read(&sdev->device_busy) && !scsi_device_blocked(sdev))
  1651. blk_delay_queue(q, SCSI_QUEUE_DELAY);
  1652. }
  1653. static inline blk_status_t prep_to_mq(int ret)
  1654. {
  1655. switch (ret) {
  1656. case BLKPREP_OK:
  1657. return BLK_STS_OK;
  1658. case BLKPREP_DEFER:
  1659. return BLK_STS_RESOURCE;
  1660. default:
  1661. return BLK_STS_IOERR;
  1662. }
  1663. }
  1664. /* Size in bytes of the sg-list stored in the scsi-mq command-private data. */
  1665. static unsigned int scsi_mq_sgl_size(struct Scsi_Host *shost)
  1666. {
  1667. return min_t(unsigned int, shost->sg_tablesize, SG_CHUNK_SIZE) *
  1668. sizeof(struct scatterlist);
  1669. }
  1670. static int scsi_mq_prep_fn(struct request *req)
  1671. {
  1672. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
  1673. struct scsi_device *sdev = req->q->queuedata;
  1674. struct Scsi_Host *shost = sdev->host;
  1675. struct scatterlist *sg;
  1676. scsi_init_command(sdev, cmd);
  1677. req->special = cmd;
  1678. cmd->request = req;
  1679. cmd->tag = req->tag;
  1680. cmd->prot_op = SCSI_PROT_NORMAL;
  1681. sg = (void *)cmd + sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
  1682. cmd->sdb.table.sgl = sg;
  1683. if (scsi_host_get_prot(shost)) {
  1684. memset(cmd->prot_sdb, 0, sizeof(struct scsi_data_buffer));
  1685. cmd->prot_sdb->table.sgl =
  1686. (struct scatterlist *)(cmd->prot_sdb + 1);
  1687. }
  1688. if (blk_bidi_rq(req)) {
  1689. struct request *next_rq = req->next_rq;
  1690. struct scsi_data_buffer *bidi_sdb = blk_mq_rq_to_pdu(next_rq);
  1691. memset(bidi_sdb, 0, sizeof(struct scsi_data_buffer));
  1692. bidi_sdb->table.sgl =
  1693. (struct scatterlist *)(bidi_sdb + 1);
  1694. next_rq->special = bidi_sdb;
  1695. }
  1696. blk_mq_start_request(req);
  1697. return scsi_setup_cmnd(sdev, req);
  1698. }
  1699. static void scsi_mq_done(struct scsi_cmnd *cmd)
  1700. {
  1701. trace_scsi_dispatch_cmd_done(cmd);
  1702. blk_mq_complete_request(cmd->request);
  1703. }
  1704. static void scsi_mq_put_budget(struct blk_mq_hw_ctx *hctx)
  1705. {
  1706. struct request_queue *q = hctx->queue;
  1707. struct scsi_device *sdev = q->queuedata;
  1708. atomic_dec(&sdev->device_busy);
  1709. put_device(&sdev->sdev_gendev);
  1710. }
  1711. static bool scsi_mq_get_budget(struct blk_mq_hw_ctx *hctx)
  1712. {
  1713. struct request_queue *q = hctx->queue;
  1714. struct scsi_device *sdev = q->queuedata;
  1715. if (!get_device(&sdev->sdev_gendev))
  1716. goto out;
  1717. if (!scsi_dev_queue_ready(q, sdev))
  1718. goto out_put_device;
  1719. return true;
  1720. out_put_device:
  1721. put_device(&sdev->sdev_gendev);
  1722. out:
  1723. return false;
  1724. }
  1725. static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
  1726. const struct blk_mq_queue_data *bd)
  1727. {
  1728. struct request *req = bd->rq;
  1729. struct request_queue *q = req->q;
  1730. struct scsi_device *sdev = q->queuedata;
  1731. struct Scsi_Host *shost = sdev->host;
  1732. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
  1733. blk_status_t ret;
  1734. int reason;
  1735. ret = prep_to_mq(scsi_prep_state_check(sdev, req));
  1736. if (ret != BLK_STS_OK)
  1737. goto out_put_budget;
  1738. ret = BLK_STS_RESOURCE;
  1739. if (!scsi_target_queue_ready(shost, sdev))
  1740. goto out_put_budget;
  1741. if (!scsi_host_queue_ready(q, shost, sdev))
  1742. goto out_dec_target_busy;
  1743. if (!(req->rq_flags & RQF_DONTPREP)) {
  1744. ret = prep_to_mq(scsi_mq_prep_fn(req));
  1745. if (ret != BLK_STS_OK)
  1746. goto out_dec_host_busy;
  1747. req->rq_flags |= RQF_DONTPREP;
  1748. } else {
  1749. blk_mq_start_request(req);
  1750. }
  1751. if (sdev->simple_tags)
  1752. cmd->flags |= SCMD_TAGGED;
  1753. else
  1754. cmd->flags &= ~SCMD_TAGGED;
  1755. scsi_init_cmd_errh(cmd);
  1756. cmd->scsi_done = scsi_mq_done;
  1757. reason = scsi_dispatch_cmd(cmd);
  1758. if (reason) {
  1759. scsi_set_blocked(cmd, reason);
  1760. ret = BLK_STS_RESOURCE;
  1761. goto out_dec_host_busy;
  1762. }
  1763. return BLK_STS_OK;
  1764. out_dec_host_busy:
  1765. atomic_dec(&shost->host_busy);
  1766. out_dec_target_busy:
  1767. if (scsi_target(sdev)->can_queue > 0)
  1768. atomic_dec(&scsi_target(sdev)->target_busy);
  1769. out_put_budget:
  1770. scsi_mq_put_budget(hctx);
  1771. switch (ret) {
  1772. case BLK_STS_OK:
  1773. break;
  1774. case BLK_STS_RESOURCE:
  1775. if (atomic_read(&sdev->device_busy) == 0 &&
  1776. !scsi_device_blocked(sdev))
  1777. blk_mq_delay_run_hw_queue(hctx, SCSI_QUEUE_DELAY);
  1778. break;
  1779. default:
  1780. /*
  1781. * Make sure to release all allocated ressources when
  1782. * we hit an error, as we will never see this command
  1783. * again.
  1784. */
  1785. if (req->rq_flags & RQF_DONTPREP)
  1786. scsi_mq_uninit_cmd(cmd);
  1787. break;
  1788. }
  1789. return ret;
  1790. }
  1791. static enum blk_eh_timer_return scsi_timeout(struct request *req,
  1792. bool reserved)
  1793. {
  1794. if (reserved)
  1795. return BLK_EH_RESET_TIMER;
  1796. return scsi_times_out(req);
  1797. }
  1798. static int scsi_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
  1799. unsigned int hctx_idx, unsigned int numa_node)
  1800. {
  1801. struct Scsi_Host *shost = set->driver_data;
  1802. const bool unchecked_isa_dma = shost->unchecked_isa_dma;
  1803. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
  1804. struct scatterlist *sg;
  1805. if (unchecked_isa_dma)
  1806. cmd->flags |= SCMD_UNCHECKED_ISA_DMA;
  1807. cmd->sense_buffer = scsi_alloc_sense_buffer(unchecked_isa_dma,
  1808. GFP_KERNEL, numa_node);
  1809. if (!cmd->sense_buffer)
  1810. return -ENOMEM;
  1811. cmd->req.sense = cmd->sense_buffer;
  1812. if (scsi_host_get_prot(shost)) {
  1813. sg = (void *)cmd + sizeof(struct scsi_cmnd) +
  1814. shost->hostt->cmd_size;
  1815. cmd->prot_sdb = (void *)sg + scsi_mq_sgl_size(shost);
  1816. }
  1817. return 0;
  1818. }
  1819. static void scsi_mq_exit_request(struct blk_mq_tag_set *set, struct request *rq,
  1820. unsigned int hctx_idx)
  1821. {
  1822. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
  1823. scsi_free_sense_buffer(cmd->flags & SCMD_UNCHECKED_ISA_DMA,
  1824. cmd->sense_buffer);
  1825. }
  1826. static int scsi_map_queues(struct blk_mq_tag_set *set)
  1827. {
  1828. struct Scsi_Host *shost = container_of(set, struct Scsi_Host, tag_set);
  1829. if (shost->hostt->map_queues)
  1830. return shost->hostt->map_queues(shost);
  1831. return blk_mq_map_queues(set);
  1832. }
  1833. static u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
  1834. {
  1835. struct device *host_dev;
  1836. u64 bounce_limit = 0xffffffff;
  1837. if (shost->unchecked_isa_dma)
  1838. return BLK_BOUNCE_ISA;
  1839. /*
  1840. * Platforms with virtual-DMA translation
  1841. * hardware have no practical limit.
  1842. */
  1843. if (!PCI_DMA_BUS_IS_PHYS)
  1844. return BLK_BOUNCE_ANY;
  1845. host_dev = scsi_get_device(shost);
  1846. if (host_dev && host_dev->dma_mask)
  1847. bounce_limit = (u64)dma_max_pfn(host_dev) << PAGE_SHIFT;
  1848. return bounce_limit;
  1849. }
  1850. void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q)
  1851. {
  1852. struct device *dev = shost->dma_dev;
  1853. queue_flag_set_unlocked(QUEUE_FLAG_SCSI_PASSTHROUGH, q);
  1854. /*
  1855. * this limit is imposed by hardware restrictions
  1856. */
  1857. blk_queue_max_segments(q, min_t(unsigned short, shost->sg_tablesize,
  1858. SG_MAX_SEGMENTS));
  1859. if (scsi_host_prot_dma(shost)) {
  1860. shost->sg_prot_tablesize =
  1861. min_not_zero(shost->sg_prot_tablesize,
  1862. (unsigned short)SCSI_MAX_PROT_SG_SEGMENTS);
  1863. BUG_ON(shost->sg_prot_tablesize < shost->sg_tablesize);
  1864. blk_queue_max_integrity_segments(q, shost->sg_prot_tablesize);
  1865. }
  1866. blk_queue_max_hw_sectors(q, shost->max_sectors);
  1867. blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
  1868. blk_queue_segment_boundary(q, shost->dma_boundary);
  1869. dma_set_seg_boundary(dev, shost->dma_boundary);
  1870. blk_queue_max_segment_size(q, dma_get_max_seg_size(dev));
  1871. if (!shost->use_clustering)
  1872. q->limits.cluster = 0;
  1873. /*
  1874. * set a reasonable default alignment on word boundaries: the
  1875. * host and device may alter it using
  1876. * blk_queue_update_dma_alignment() later.
  1877. */
  1878. blk_queue_dma_alignment(q, 0x03);
  1879. }
  1880. EXPORT_SYMBOL_GPL(__scsi_init_queue);
  1881. static int scsi_old_init_rq(struct request_queue *q, struct request *rq,
  1882. gfp_t gfp)
  1883. {
  1884. struct Scsi_Host *shost = q->rq_alloc_data;
  1885. const bool unchecked_isa_dma = shost->unchecked_isa_dma;
  1886. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
  1887. memset(cmd, 0, sizeof(*cmd));
  1888. if (unchecked_isa_dma)
  1889. cmd->flags |= SCMD_UNCHECKED_ISA_DMA;
  1890. cmd->sense_buffer = scsi_alloc_sense_buffer(unchecked_isa_dma, gfp,
  1891. NUMA_NO_NODE);
  1892. if (!cmd->sense_buffer)
  1893. goto fail;
  1894. cmd->req.sense = cmd->sense_buffer;
  1895. if (scsi_host_get_prot(shost) >= SHOST_DIX_TYPE0_PROTECTION) {
  1896. cmd->prot_sdb = kmem_cache_zalloc(scsi_sdb_cache, gfp);
  1897. if (!cmd->prot_sdb)
  1898. goto fail_free_sense;
  1899. }
  1900. return 0;
  1901. fail_free_sense:
  1902. scsi_free_sense_buffer(unchecked_isa_dma, cmd->sense_buffer);
  1903. fail:
  1904. return -ENOMEM;
  1905. }
  1906. static void scsi_old_exit_rq(struct request_queue *q, struct request *rq)
  1907. {
  1908. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
  1909. if (cmd->prot_sdb)
  1910. kmem_cache_free(scsi_sdb_cache, cmd->prot_sdb);
  1911. scsi_free_sense_buffer(cmd->flags & SCMD_UNCHECKED_ISA_DMA,
  1912. cmd->sense_buffer);
  1913. }
  1914. struct request_queue *scsi_old_alloc_queue(struct scsi_device *sdev)
  1915. {
  1916. struct Scsi_Host *shost = sdev->host;
  1917. struct request_queue *q;
  1918. q = blk_alloc_queue_node(GFP_KERNEL, NUMA_NO_NODE);
  1919. if (!q)
  1920. return NULL;
  1921. q->cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
  1922. q->rq_alloc_data = shost;
  1923. q->request_fn = scsi_request_fn;
  1924. q->init_rq_fn = scsi_old_init_rq;
  1925. q->exit_rq_fn = scsi_old_exit_rq;
  1926. q->initialize_rq_fn = scsi_initialize_rq;
  1927. if (blk_init_allocated_queue(q) < 0) {
  1928. blk_cleanup_queue(q);
  1929. return NULL;
  1930. }
  1931. __scsi_init_queue(shost, q);
  1932. blk_queue_prep_rq(q, scsi_prep_fn);
  1933. blk_queue_unprep_rq(q, scsi_unprep_fn);
  1934. blk_queue_softirq_done(q, scsi_softirq_done);
  1935. blk_queue_rq_timed_out(q, scsi_times_out);
  1936. blk_queue_lld_busy(q, scsi_lld_busy);
  1937. return q;
  1938. }
  1939. static const struct blk_mq_ops scsi_mq_ops = {
  1940. .get_budget = scsi_mq_get_budget,
  1941. .put_budget = scsi_mq_put_budget,
  1942. .queue_rq = scsi_queue_rq,
  1943. .complete = scsi_softirq_done,
  1944. .timeout = scsi_timeout,
  1945. #ifdef CONFIG_BLK_DEBUG_FS
  1946. .show_rq = scsi_show_rq,
  1947. #endif
  1948. .init_request = scsi_mq_init_request,
  1949. .exit_request = scsi_mq_exit_request,
  1950. .initialize_rq_fn = scsi_initialize_rq,
  1951. .map_queues = scsi_map_queues,
  1952. };
  1953. struct request_queue *scsi_mq_alloc_queue(struct scsi_device *sdev)
  1954. {
  1955. sdev->request_queue = blk_mq_init_queue(&sdev->host->tag_set);
  1956. if (IS_ERR(sdev->request_queue))
  1957. return NULL;
  1958. sdev->request_queue->queuedata = sdev;
  1959. __scsi_init_queue(sdev->host, sdev->request_queue);
  1960. return sdev->request_queue;
  1961. }
  1962. int scsi_mq_setup_tags(struct Scsi_Host *shost)
  1963. {
  1964. unsigned int cmd_size, sgl_size;
  1965. sgl_size = scsi_mq_sgl_size(shost);
  1966. cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size + sgl_size;
  1967. if (scsi_host_get_prot(shost))
  1968. cmd_size += sizeof(struct scsi_data_buffer) + sgl_size;
  1969. memset(&shost->tag_set, 0, sizeof(shost->tag_set));
  1970. shost->tag_set.ops = &scsi_mq_ops;
  1971. shost->tag_set.nr_hw_queues = shost->nr_hw_queues ? : 1;
  1972. shost->tag_set.queue_depth = shost->can_queue;
  1973. shost->tag_set.cmd_size = cmd_size;
  1974. shost->tag_set.numa_node = NUMA_NO_NODE;
  1975. shost->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
  1976. shost->tag_set.flags |=
  1977. BLK_ALLOC_POLICY_TO_MQ_FLAG(shost->hostt->tag_alloc_policy);
  1978. shost->tag_set.driver_data = shost;
  1979. return blk_mq_alloc_tag_set(&shost->tag_set);
  1980. }
  1981. void scsi_mq_destroy_tags(struct Scsi_Host *shost)
  1982. {
  1983. blk_mq_free_tag_set(&shost->tag_set);
  1984. }
  1985. /**
  1986. * scsi_device_from_queue - return sdev associated with a request_queue
  1987. * @q: The request queue to return the sdev from
  1988. *
  1989. * Return the sdev associated with a request queue or NULL if the
  1990. * request_queue does not reference a SCSI device.
  1991. */
  1992. struct scsi_device *scsi_device_from_queue(struct request_queue *q)
  1993. {
  1994. struct scsi_device *sdev = NULL;
  1995. if (q->mq_ops) {
  1996. if (q->mq_ops == &scsi_mq_ops)
  1997. sdev = q->queuedata;
  1998. } else if (q->request_fn == scsi_request_fn)
  1999. sdev = q->queuedata;
  2000. if (!sdev || !get_device(&sdev->sdev_gendev))
  2001. sdev = NULL;
  2002. return sdev;
  2003. }
  2004. EXPORT_SYMBOL_GPL(scsi_device_from_queue);
  2005. /*
  2006. * Function: scsi_block_requests()
  2007. *
  2008. * Purpose: Utility function used by low-level drivers to prevent further
  2009. * commands from being queued to the device.
  2010. *
  2011. * Arguments: shost - Host in question
  2012. *
  2013. * Returns: Nothing
  2014. *
  2015. * Lock status: No locks are assumed held.
  2016. *
  2017. * Notes: There is no timer nor any other means by which the requests
  2018. * get unblocked other than the low-level driver calling
  2019. * scsi_unblock_requests().
  2020. */
  2021. void scsi_block_requests(struct Scsi_Host *shost)
  2022. {
  2023. shost->host_self_blocked = 1;
  2024. }
  2025. EXPORT_SYMBOL(scsi_block_requests);
  2026. /*
  2027. * Function: scsi_unblock_requests()
  2028. *
  2029. * Purpose: Utility function used by low-level drivers to allow further
  2030. * commands from being queued to the device.
  2031. *
  2032. * Arguments: shost - Host in question
  2033. *
  2034. * Returns: Nothing
  2035. *
  2036. * Lock status: No locks are assumed held.
  2037. *
  2038. * Notes: There is no timer nor any other means by which the requests
  2039. * get unblocked other than the low-level driver calling
  2040. * scsi_unblock_requests().
  2041. *
  2042. * This is done as an API function so that changes to the
  2043. * internals of the scsi mid-layer won't require wholesale
  2044. * changes to drivers that use this feature.
  2045. */
  2046. void scsi_unblock_requests(struct Scsi_Host *shost)
  2047. {
  2048. shost->host_self_blocked = 0;
  2049. scsi_run_host_queues(shost);
  2050. }
  2051. EXPORT_SYMBOL(scsi_unblock_requests);
  2052. int __init scsi_init_queue(void)
  2053. {
  2054. scsi_sdb_cache = kmem_cache_create("scsi_data_buffer",
  2055. sizeof(struct scsi_data_buffer),
  2056. 0, 0, NULL);
  2057. if (!scsi_sdb_cache) {
  2058. printk(KERN_ERR "SCSI: can't init scsi sdb cache\n");
  2059. return -ENOMEM;
  2060. }
  2061. return 0;
  2062. }
  2063. void scsi_exit_queue(void)
  2064. {
  2065. kmem_cache_destroy(scsi_sense_cache);
  2066. kmem_cache_destroy(scsi_sense_isadma_cache);
  2067. kmem_cache_destroy(scsi_sdb_cache);
  2068. }
  2069. /**
  2070. * scsi_mode_select - issue a mode select
  2071. * @sdev: SCSI device to be queried
  2072. * @pf: Page format bit (1 == standard, 0 == vendor specific)
  2073. * @sp: Save page bit (0 == don't save, 1 == save)
  2074. * @modepage: mode page being requested
  2075. * @buffer: request buffer (may not be smaller than eight bytes)
  2076. * @len: length of request buffer.
  2077. * @timeout: command timeout
  2078. * @retries: number of retries before failing
  2079. * @data: returns a structure abstracting the mode header data
  2080. * @sshdr: place to put sense data (or NULL if no sense to be collected).
  2081. * must be SCSI_SENSE_BUFFERSIZE big.
  2082. *
  2083. * Returns zero if successful; negative error number or scsi
  2084. * status on error
  2085. *
  2086. */
  2087. int
  2088. scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
  2089. unsigned char *buffer, int len, int timeout, int retries,
  2090. struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
  2091. {
  2092. unsigned char cmd[10];
  2093. unsigned char *real_buffer;
  2094. int ret;
  2095. memset(cmd, 0, sizeof(cmd));
  2096. cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
  2097. if (sdev->use_10_for_ms) {
  2098. if (len > 65535)
  2099. return -EINVAL;
  2100. real_buffer = kmalloc(8 + len, GFP_KERNEL);
  2101. if (!real_buffer)
  2102. return -ENOMEM;
  2103. memcpy(real_buffer + 8, buffer, len);
  2104. len += 8;
  2105. real_buffer[0] = 0;
  2106. real_buffer[1] = 0;
  2107. real_buffer[2] = data->medium_type;
  2108. real_buffer[3] = data->device_specific;
  2109. real_buffer[4] = data->longlba ? 0x01 : 0;
  2110. real_buffer[5] = 0;
  2111. real_buffer[6] = data->block_descriptor_length >> 8;
  2112. real_buffer[7] = data->block_descriptor_length;
  2113. cmd[0] = MODE_SELECT_10;
  2114. cmd[7] = len >> 8;
  2115. cmd[8] = len;
  2116. } else {
  2117. if (len > 255 || data->block_descriptor_length > 255 ||
  2118. data->longlba)
  2119. return -EINVAL;
  2120. real_buffer = kmalloc(4 + len, GFP_KERNEL);
  2121. if (!real_buffer)
  2122. return -ENOMEM;
  2123. memcpy(real_buffer + 4, buffer, len);
  2124. len += 4;
  2125. real_buffer[0] = 0;
  2126. real_buffer[1] = data->medium_type;
  2127. real_buffer[2] = data->device_specific;
  2128. real_buffer[3] = data->block_descriptor_length;
  2129. cmd[0] = MODE_SELECT;
  2130. cmd[4] = len;
  2131. }
  2132. ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
  2133. sshdr, timeout, retries, NULL);
  2134. kfree(real_buffer);
  2135. return ret;
  2136. }
  2137. EXPORT_SYMBOL_GPL(scsi_mode_select);
  2138. /**
  2139. * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
  2140. * @sdev: SCSI device to be queried
  2141. * @dbd: set if mode sense will allow block descriptors to be returned
  2142. * @modepage: mode page being requested
  2143. * @buffer: request buffer (may not be smaller than eight bytes)
  2144. * @len: length of request buffer.
  2145. * @timeout: command timeout
  2146. * @retries: number of retries before failing
  2147. * @data: returns a structure abstracting the mode header data
  2148. * @sshdr: place to put sense data (or NULL if no sense to be collected).
  2149. * must be SCSI_SENSE_BUFFERSIZE big.
  2150. *
  2151. * Returns zero if unsuccessful, or the header offset (either 4
  2152. * or 8 depending on whether a six or ten byte command was
  2153. * issued) if successful.
  2154. */
  2155. int
  2156. scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
  2157. unsigned char *buffer, int len, int timeout, int retries,
  2158. struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
  2159. {
  2160. unsigned char cmd[12];
  2161. int use_10_for_ms;
  2162. int header_length;
  2163. int result, retry_count = retries;
  2164. struct scsi_sense_hdr my_sshdr;
  2165. memset(data, 0, sizeof(*data));
  2166. memset(&cmd[0], 0, 12);
  2167. cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
  2168. cmd[2] = modepage;
  2169. /* caller might not be interested in sense, but we need it */
  2170. if (!sshdr)
  2171. sshdr = &my_sshdr;
  2172. retry:
  2173. use_10_for_ms = sdev->use_10_for_ms;
  2174. if (use_10_for_ms) {
  2175. if (len < 8)
  2176. len = 8;
  2177. cmd[0] = MODE_SENSE_10;
  2178. cmd[8] = len;
  2179. header_length = 8;
  2180. } else {
  2181. if (len < 4)
  2182. len = 4;
  2183. cmd[0] = MODE_SENSE;
  2184. cmd[4] = len;
  2185. header_length = 4;
  2186. }
  2187. memset(buffer, 0, len);
  2188. result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
  2189. sshdr, timeout, retries, NULL);
  2190. /* This code looks awful: what it's doing is making sure an
  2191. * ILLEGAL REQUEST sense return identifies the actual command
  2192. * byte as the problem. MODE_SENSE commands can return
  2193. * ILLEGAL REQUEST if the code page isn't supported */
  2194. if (use_10_for_ms && !scsi_status_is_good(result) &&
  2195. (driver_byte(result) & DRIVER_SENSE)) {
  2196. if (scsi_sense_valid(sshdr)) {
  2197. if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
  2198. (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
  2199. /*
  2200. * Invalid command operation code
  2201. */
  2202. sdev->use_10_for_ms = 0;
  2203. goto retry;
  2204. }
  2205. }
  2206. }
  2207. if(scsi_status_is_good(result)) {
  2208. if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
  2209. (modepage == 6 || modepage == 8))) {
  2210. /* Initio breakage? */
  2211. header_length = 0;
  2212. data->length = 13;
  2213. data->medium_type = 0;
  2214. data->device_specific = 0;
  2215. data->longlba = 0;
  2216. data->block_descriptor_length = 0;
  2217. } else if(use_10_for_ms) {
  2218. data->length = buffer[0]*256 + buffer[1] + 2;
  2219. data->medium_type = buffer[2];
  2220. data->device_specific = buffer[3];
  2221. data->longlba = buffer[4] & 0x01;
  2222. data->block_descriptor_length = buffer[6]*256
  2223. + buffer[7];
  2224. } else {
  2225. data->length = buffer[0] + 1;
  2226. data->medium_type = buffer[1];
  2227. data->device_specific = buffer[2];
  2228. data->block_descriptor_length = buffer[3];
  2229. }
  2230. data->header_length = header_length;
  2231. } else if ((status_byte(result) == CHECK_CONDITION) &&
  2232. scsi_sense_valid(sshdr) &&
  2233. sshdr->sense_key == UNIT_ATTENTION && retry_count) {
  2234. retry_count--;
  2235. goto retry;
  2236. }
  2237. return result;
  2238. }
  2239. EXPORT_SYMBOL(scsi_mode_sense);
  2240. /**
  2241. * scsi_test_unit_ready - test if unit is ready
  2242. * @sdev: scsi device to change the state of.
  2243. * @timeout: command timeout
  2244. * @retries: number of retries before failing
  2245. * @sshdr: outpout pointer for decoded sense information.
  2246. *
  2247. * Returns zero if unsuccessful or an error if TUR failed. For
  2248. * removable media, UNIT_ATTENTION sets ->changed flag.
  2249. **/
  2250. int
  2251. scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
  2252. struct scsi_sense_hdr *sshdr)
  2253. {
  2254. char cmd[] = {
  2255. TEST_UNIT_READY, 0, 0, 0, 0, 0,
  2256. };
  2257. int result;
  2258. /* try to eat the UNIT_ATTENTION if there are enough retries */
  2259. do {
  2260. result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
  2261. timeout, retries, NULL);
  2262. if (sdev->removable && scsi_sense_valid(sshdr) &&
  2263. sshdr->sense_key == UNIT_ATTENTION)
  2264. sdev->changed = 1;
  2265. } while (scsi_sense_valid(sshdr) &&
  2266. sshdr->sense_key == UNIT_ATTENTION && --retries);
  2267. return result;
  2268. }
  2269. EXPORT_SYMBOL(scsi_test_unit_ready);
  2270. /**
  2271. * scsi_device_set_state - Take the given device through the device state model.
  2272. * @sdev: scsi device to change the state of.
  2273. * @state: state to change to.
  2274. *
  2275. * Returns zero if successful or an error if the requested
  2276. * transition is illegal.
  2277. */
  2278. int
  2279. scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
  2280. {
  2281. enum scsi_device_state oldstate = sdev->sdev_state;
  2282. if (state == oldstate)
  2283. return 0;
  2284. switch (state) {
  2285. case SDEV_CREATED:
  2286. switch (oldstate) {
  2287. case SDEV_CREATED_BLOCK:
  2288. break;
  2289. default:
  2290. goto illegal;
  2291. }
  2292. break;
  2293. case SDEV_RUNNING:
  2294. switch (oldstate) {
  2295. case SDEV_CREATED:
  2296. case SDEV_OFFLINE:
  2297. case SDEV_TRANSPORT_OFFLINE:
  2298. case SDEV_QUIESCE:
  2299. case SDEV_BLOCK:
  2300. break;
  2301. default:
  2302. goto illegal;
  2303. }
  2304. break;
  2305. case SDEV_QUIESCE:
  2306. switch (oldstate) {
  2307. case SDEV_RUNNING:
  2308. case SDEV_OFFLINE:
  2309. case SDEV_TRANSPORT_OFFLINE:
  2310. break;
  2311. default:
  2312. goto illegal;
  2313. }
  2314. break;
  2315. case SDEV_OFFLINE:
  2316. case SDEV_TRANSPORT_OFFLINE:
  2317. switch (oldstate) {
  2318. case SDEV_CREATED:
  2319. case SDEV_RUNNING:
  2320. case SDEV_QUIESCE:
  2321. case SDEV_BLOCK:
  2322. break;
  2323. default:
  2324. goto illegal;
  2325. }
  2326. break;
  2327. case SDEV_BLOCK:
  2328. switch (oldstate) {
  2329. case SDEV_RUNNING:
  2330. case SDEV_CREATED_BLOCK:
  2331. break;
  2332. default:
  2333. goto illegal;
  2334. }
  2335. break;
  2336. case SDEV_CREATED_BLOCK:
  2337. switch (oldstate) {
  2338. case SDEV_CREATED:
  2339. break;
  2340. default:
  2341. goto illegal;
  2342. }
  2343. break;
  2344. case SDEV_CANCEL:
  2345. switch (oldstate) {
  2346. case SDEV_CREATED:
  2347. case SDEV_RUNNING:
  2348. case SDEV_QUIESCE:
  2349. case SDEV_OFFLINE:
  2350. case SDEV_TRANSPORT_OFFLINE:
  2351. break;
  2352. default:
  2353. goto illegal;
  2354. }
  2355. break;
  2356. case SDEV_DEL:
  2357. switch (oldstate) {
  2358. case SDEV_CREATED:
  2359. case SDEV_RUNNING:
  2360. case SDEV_OFFLINE:
  2361. case SDEV_TRANSPORT_OFFLINE:
  2362. case SDEV_CANCEL:
  2363. case SDEV_BLOCK:
  2364. case SDEV_CREATED_BLOCK:
  2365. break;
  2366. default:
  2367. goto illegal;
  2368. }
  2369. break;
  2370. }
  2371. sdev->sdev_state = state;
  2372. return 0;
  2373. illegal:
  2374. SCSI_LOG_ERROR_RECOVERY(1,
  2375. sdev_printk(KERN_ERR, sdev,
  2376. "Illegal state transition %s->%s",
  2377. scsi_device_state_name(oldstate),
  2378. scsi_device_state_name(state))
  2379. );
  2380. return -EINVAL;
  2381. }
  2382. EXPORT_SYMBOL(scsi_device_set_state);
  2383. /**
  2384. * sdev_evt_emit - emit a single SCSI device uevent
  2385. * @sdev: associated SCSI device
  2386. * @evt: event to emit
  2387. *
  2388. * Send a single uevent (scsi_event) to the associated scsi_device.
  2389. */
  2390. static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
  2391. {
  2392. int idx = 0;
  2393. char *envp[3];
  2394. switch (evt->evt_type) {
  2395. case SDEV_EVT_MEDIA_CHANGE:
  2396. envp[idx++] = "SDEV_MEDIA_CHANGE=1";
  2397. break;
  2398. case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
  2399. scsi_rescan_device(&sdev->sdev_gendev);
  2400. envp[idx++] = "SDEV_UA=INQUIRY_DATA_HAS_CHANGED";
  2401. break;
  2402. case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
  2403. envp[idx++] = "SDEV_UA=CAPACITY_DATA_HAS_CHANGED";
  2404. break;
  2405. case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
  2406. envp[idx++] = "SDEV_UA=THIN_PROVISIONING_SOFT_THRESHOLD_REACHED";
  2407. break;
  2408. case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
  2409. envp[idx++] = "SDEV_UA=MODE_PARAMETERS_CHANGED";
  2410. break;
  2411. case SDEV_EVT_LUN_CHANGE_REPORTED:
  2412. envp[idx++] = "SDEV_UA=REPORTED_LUNS_DATA_HAS_CHANGED";
  2413. break;
  2414. case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
  2415. envp[idx++] = "SDEV_UA=ASYMMETRIC_ACCESS_STATE_CHANGED";
  2416. break;
  2417. default:
  2418. /* do nothing */
  2419. break;
  2420. }
  2421. envp[idx++] = NULL;
  2422. kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
  2423. }
  2424. /**
  2425. * sdev_evt_thread - send a uevent for each scsi event
  2426. * @work: work struct for scsi_device
  2427. *
  2428. * Dispatch queued events to their associated scsi_device kobjects
  2429. * as uevents.
  2430. */
  2431. void scsi_evt_thread(struct work_struct *work)
  2432. {
  2433. struct scsi_device *sdev;
  2434. enum scsi_device_event evt_type;
  2435. LIST_HEAD(event_list);
  2436. sdev = container_of(work, struct scsi_device, event_work);
  2437. for (evt_type = SDEV_EVT_FIRST; evt_type <= SDEV_EVT_LAST; evt_type++)
  2438. if (test_and_clear_bit(evt_type, sdev->pending_events))
  2439. sdev_evt_send_simple(sdev, evt_type, GFP_KERNEL);
  2440. while (1) {
  2441. struct scsi_event *evt;
  2442. struct list_head *this, *tmp;
  2443. unsigned long flags;
  2444. spin_lock_irqsave(&sdev->list_lock, flags);
  2445. list_splice_init(&sdev->event_list, &event_list);
  2446. spin_unlock_irqrestore(&sdev->list_lock, flags);
  2447. if (list_empty(&event_list))
  2448. break;
  2449. list_for_each_safe(this, tmp, &event_list) {
  2450. evt = list_entry(this, struct scsi_event, node);
  2451. list_del(&evt->node);
  2452. scsi_evt_emit(sdev, evt);
  2453. kfree(evt);
  2454. }
  2455. }
  2456. }
  2457. /**
  2458. * sdev_evt_send - send asserted event to uevent thread
  2459. * @sdev: scsi_device event occurred on
  2460. * @evt: event to send
  2461. *
  2462. * Assert scsi device event asynchronously.
  2463. */
  2464. void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
  2465. {
  2466. unsigned long flags;
  2467. #if 0
  2468. /* FIXME: currently this check eliminates all media change events
  2469. * for polled devices. Need to update to discriminate between AN
  2470. * and polled events */
  2471. if (!test_bit(evt->evt_type, sdev->supported_events)) {
  2472. kfree(evt);
  2473. return;
  2474. }
  2475. #endif
  2476. spin_lock_irqsave(&sdev->list_lock, flags);
  2477. list_add_tail(&evt->node, &sdev->event_list);
  2478. schedule_work(&sdev->event_work);
  2479. spin_unlock_irqrestore(&sdev->list_lock, flags);
  2480. }
  2481. EXPORT_SYMBOL_GPL(sdev_evt_send);
  2482. /**
  2483. * sdev_evt_alloc - allocate a new scsi event
  2484. * @evt_type: type of event to allocate
  2485. * @gfpflags: GFP flags for allocation
  2486. *
  2487. * Allocates and returns a new scsi_event.
  2488. */
  2489. struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
  2490. gfp_t gfpflags)
  2491. {
  2492. struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
  2493. if (!evt)
  2494. return NULL;
  2495. evt->evt_type = evt_type;
  2496. INIT_LIST_HEAD(&evt->node);
  2497. /* evt_type-specific initialization, if any */
  2498. switch (evt_type) {
  2499. case SDEV_EVT_MEDIA_CHANGE:
  2500. case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
  2501. case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
  2502. case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
  2503. case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
  2504. case SDEV_EVT_LUN_CHANGE_REPORTED:
  2505. case SDEV_EVT_ALUA_STATE_CHANGE_REPORTED:
  2506. default:
  2507. /* do nothing */
  2508. break;
  2509. }
  2510. return evt;
  2511. }
  2512. EXPORT_SYMBOL_GPL(sdev_evt_alloc);
  2513. /**
  2514. * sdev_evt_send_simple - send asserted event to uevent thread
  2515. * @sdev: scsi_device event occurred on
  2516. * @evt_type: type of event to send
  2517. * @gfpflags: GFP flags for allocation
  2518. *
  2519. * Assert scsi device event asynchronously, given an event type.
  2520. */
  2521. void sdev_evt_send_simple(struct scsi_device *sdev,
  2522. enum scsi_device_event evt_type, gfp_t gfpflags)
  2523. {
  2524. struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
  2525. if (!evt) {
  2526. sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
  2527. evt_type);
  2528. return;
  2529. }
  2530. sdev_evt_send(sdev, evt);
  2531. }
  2532. EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
  2533. /**
  2534. * scsi_request_fn_active() - number of kernel threads inside scsi_request_fn()
  2535. * @sdev: SCSI device to count the number of scsi_request_fn() callers for.
  2536. */
  2537. static int scsi_request_fn_active(struct scsi_device *sdev)
  2538. {
  2539. struct request_queue *q = sdev->request_queue;
  2540. int request_fn_active;
  2541. WARN_ON_ONCE(sdev->host->use_blk_mq);
  2542. spin_lock_irq(q->queue_lock);
  2543. request_fn_active = q->request_fn_active;
  2544. spin_unlock_irq(q->queue_lock);
  2545. return request_fn_active;
  2546. }
  2547. /**
  2548. * scsi_wait_for_queuecommand() - wait for ongoing queuecommand() calls
  2549. * @sdev: SCSI device pointer.
  2550. *
  2551. * Wait until the ongoing shost->hostt->queuecommand() calls that are
  2552. * invoked from scsi_request_fn() have finished.
  2553. */
  2554. static void scsi_wait_for_queuecommand(struct scsi_device *sdev)
  2555. {
  2556. WARN_ON_ONCE(sdev->host->use_blk_mq);
  2557. while (scsi_request_fn_active(sdev))
  2558. msleep(20);
  2559. }
  2560. /**
  2561. * scsi_device_quiesce - Block user issued commands.
  2562. * @sdev: scsi device to quiesce.
  2563. *
  2564. * This works by trying to transition to the SDEV_QUIESCE state
  2565. * (which must be a legal transition). When the device is in this
  2566. * state, only special requests will be accepted, all others will
  2567. * be deferred. Since special requests may also be requeued requests,
  2568. * a successful return doesn't guarantee the device will be
  2569. * totally quiescent.
  2570. *
  2571. * Must be called with user context, may sleep.
  2572. *
  2573. * Returns zero if unsuccessful or an error if not.
  2574. */
  2575. int
  2576. scsi_device_quiesce(struct scsi_device *sdev)
  2577. {
  2578. struct request_queue *q = sdev->request_queue;
  2579. int err;
  2580. /*
  2581. * It is allowed to call scsi_device_quiesce() multiple times from
  2582. * the same context but concurrent scsi_device_quiesce() calls are
  2583. * not allowed.
  2584. */
  2585. WARN_ON_ONCE(sdev->quiesced_by && sdev->quiesced_by != current);
  2586. blk_set_preempt_only(q);
  2587. blk_mq_freeze_queue(q);
  2588. /*
  2589. * Ensure that the effect of blk_set_preempt_only() will be visible
  2590. * for percpu_ref_tryget() callers that occur after the queue
  2591. * unfreeze even if the queue was already frozen before this function
  2592. * was called. See also https://lwn.net/Articles/573497/.
  2593. */
  2594. synchronize_rcu();
  2595. blk_mq_unfreeze_queue(q);
  2596. mutex_lock(&sdev->state_mutex);
  2597. err = scsi_device_set_state(sdev, SDEV_QUIESCE);
  2598. if (err == 0)
  2599. sdev->quiesced_by = current;
  2600. else
  2601. blk_clear_preempt_only(q);
  2602. mutex_unlock(&sdev->state_mutex);
  2603. return err;
  2604. }
  2605. EXPORT_SYMBOL(scsi_device_quiesce);
  2606. /**
  2607. * scsi_device_resume - Restart user issued commands to a quiesced device.
  2608. * @sdev: scsi device to resume.
  2609. *
  2610. * Moves the device from quiesced back to running and restarts the
  2611. * queues.
  2612. *
  2613. * Must be called with user context, may sleep.
  2614. */
  2615. void scsi_device_resume(struct scsi_device *sdev)
  2616. {
  2617. /* check if the device state was mutated prior to resume, and if
  2618. * so assume the state is being managed elsewhere (for example
  2619. * device deleted during suspend)
  2620. */
  2621. mutex_lock(&sdev->state_mutex);
  2622. WARN_ON_ONCE(!sdev->quiesced_by);
  2623. sdev->quiesced_by = NULL;
  2624. blk_clear_preempt_only(sdev->request_queue);
  2625. if (sdev->sdev_state == SDEV_QUIESCE)
  2626. scsi_device_set_state(sdev, SDEV_RUNNING);
  2627. mutex_unlock(&sdev->state_mutex);
  2628. }
  2629. EXPORT_SYMBOL(scsi_device_resume);
  2630. static void
  2631. device_quiesce_fn(struct scsi_device *sdev, void *data)
  2632. {
  2633. scsi_device_quiesce(sdev);
  2634. }
  2635. void
  2636. scsi_target_quiesce(struct scsi_target *starget)
  2637. {
  2638. starget_for_each_device(starget, NULL, device_quiesce_fn);
  2639. }
  2640. EXPORT_SYMBOL(scsi_target_quiesce);
  2641. static void
  2642. device_resume_fn(struct scsi_device *sdev, void *data)
  2643. {
  2644. scsi_device_resume(sdev);
  2645. }
  2646. void
  2647. scsi_target_resume(struct scsi_target *starget)
  2648. {
  2649. starget_for_each_device(starget, NULL, device_resume_fn);
  2650. }
  2651. EXPORT_SYMBOL(scsi_target_resume);
  2652. /**
  2653. * scsi_internal_device_block_nowait - try to transition to the SDEV_BLOCK state
  2654. * @sdev: device to block
  2655. *
  2656. * Pause SCSI command processing on the specified device. Does not sleep.
  2657. *
  2658. * Returns zero if successful or a negative error code upon failure.
  2659. *
  2660. * Notes:
  2661. * This routine transitions the device to the SDEV_BLOCK state (which must be
  2662. * a legal transition). When the device is in this state, command processing
  2663. * is paused until the device leaves the SDEV_BLOCK state. See also
  2664. * scsi_internal_device_unblock_nowait().
  2665. */
  2666. int scsi_internal_device_block_nowait(struct scsi_device *sdev)
  2667. {
  2668. struct request_queue *q = sdev->request_queue;
  2669. unsigned long flags;
  2670. int err = 0;
  2671. err = scsi_device_set_state(sdev, SDEV_BLOCK);
  2672. if (err) {
  2673. err = scsi_device_set_state(sdev, SDEV_CREATED_BLOCK);
  2674. if (err)
  2675. return err;
  2676. }
  2677. /*
  2678. * The device has transitioned to SDEV_BLOCK. Stop the
  2679. * block layer from calling the midlayer with this device's
  2680. * request queue.
  2681. */
  2682. if (q->mq_ops) {
  2683. blk_mq_quiesce_queue_nowait(q);
  2684. } else {
  2685. spin_lock_irqsave(q->queue_lock, flags);
  2686. blk_stop_queue(q);
  2687. spin_unlock_irqrestore(q->queue_lock, flags);
  2688. }
  2689. return 0;
  2690. }
  2691. EXPORT_SYMBOL_GPL(scsi_internal_device_block_nowait);
  2692. /**
  2693. * scsi_internal_device_block - try to transition to the SDEV_BLOCK state
  2694. * @sdev: device to block
  2695. *
  2696. * Pause SCSI command processing on the specified device and wait until all
  2697. * ongoing scsi_request_fn() / scsi_queue_rq() calls have finished. May sleep.
  2698. *
  2699. * Returns zero if successful or a negative error code upon failure.
  2700. *
  2701. * Note:
  2702. * This routine transitions the device to the SDEV_BLOCK state (which must be
  2703. * a legal transition). When the device is in this state, command processing
  2704. * is paused until the device leaves the SDEV_BLOCK state. See also
  2705. * scsi_internal_device_unblock().
  2706. *
  2707. * To do: avoid that scsi_send_eh_cmnd() calls queuecommand() after
  2708. * scsi_internal_device_block() has blocked a SCSI device and also
  2709. * remove the rport mutex lock and unlock calls from srp_queuecommand().
  2710. */
  2711. static int scsi_internal_device_block(struct scsi_device *sdev)
  2712. {
  2713. struct request_queue *q = sdev->request_queue;
  2714. int err;
  2715. mutex_lock(&sdev->state_mutex);
  2716. err = scsi_internal_device_block_nowait(sdev);
  2717. if (err == 0) {
  2718. if (q->mq_ops)
  2719. blk_mq_quiesce_queue(q);
  2720. else
  2721. scsi_wait_for_queuecommand(sdev);
  2722. }
  2723. mutex_unlock(&sdev->state_mutex);
  2724. return err;
  2725. }
  2726. void scsi_start_queue(struct scsi_device *sdev)
  2727. {
  2728. struct request_queue *q = sdev->request_queue;
  2729. unsigned long flags;
  2730. if (q->mq_ops) {
  2731. blk_mq_unquiesce_queue(q);
  2732. } else {
  2733. spin_lock_irqsave(q->queue_lock, flags);
  2734. blk_start_queue(q);
  2735. spin_unlock_irqrestore(q->queue_lock, flags);
  2736. }
  2737. }
  2738. /**
  2739. * scsi_internal_device_unblock_nowait - resume a device after a block request
  2740. * @sdev: device to resume
  2741. * @new_state: state to set the device to after unblocking
  2742. *
  2743. * Restart the device queue for a previously suspended SCSI device. Does not
  2744. * sleep.
  2745. *
  2746. * Returns zero if successful or a negative error code upon failure.
  2747. *
  2748. * Notes:
  2749. * This routine transitions the device to the SDEV_RUNNING state or to one of
  2750. * the offline states (which must be a legal transition) allowing the midlayer
  2751. * to goose the queue for this device.
  2752. */
  2753. int scsi_internal_device_unblock_nowait(struct scsi_device *sdev,
  2754. enum scsi_device_state new_state)
  2755. {
  2756. /*
  2757. * Try to transition the scsi device to SDEV_RUNNING or one of the
  2758. * offlined states and goose the device queue if successful.
  2759. */
  2760. switch (sdev->sdev_state) {
  2761. case SDEV_BLOCK:
  2762. case SDEV_TRANSPORT_OFFLINE:
  2763. sdev->sdev_state = new_state;
  2764. break;
  2765. case SDEV_CREATED_BLOCK:
  2766. if (new_state == SDEV_TRANSPORT_OFFLINE ||
  2767. new_state == SDEV_OFFLINE)
  2768. sdev->sdev_state = new_state;
  2769. else
  2770. sdev->sdev_state = SDEV_CREATED;
  2771. break;
  2772. case SDEV_CANCEL:
  2773. case SDEV_OFFLINE:
  2774. break;
  2775. default:
  2776. return -EINVAL;
  2777. }
  2778. scsi_start_queue(sdev);
  2779. return 0;
  2780. }
  2781. EXPORT_SYMBOL_GPL(scsi_internal_device_unblock_nowait);
  2782. /**
  2783. * scsi_internal_device_unblock - resume a device after a block request
  2784. * @sdev: device to resume
  2785. * @new_state: state to set the device to after unblocking
  2786. *
  2787. * Restart the device queue for a previously suspended SCSI device. May sleep.
  2788. *
  2789. * Returns zero if successful or a negative error code upon failure.
  2790. *
  2791. * Notes:
  2792. * This routine transitions the device to the SDEV_RUNNING state or to one of
  2793. * the offline states (which must be a legal transition) allowing the midlayer
  2794. * to goose the queue for this device.
  2795. */
  2796. static int scsi_internal_device_unblock(struct scsi_device *sdev,
  2797. enum scsi_device_state new_state)
  2798. {
  2799. int ret;
  2800. mutex_lock(&sdev->state_mutex);
  2801. ret = scsi_internal_device_unblock_nowait(sdev, new_state);
  2802. mutex_unlock(&sdev->state_mutex);
  2803. return ret;
  2804. }
  2805. static void
  2806. device_block(struct scsi_device *sdev, void *data)
  2807. {
  2808. scsi_internal_device_block(sdev);
  2809. }
  2810. static int
  2811. target_block(struct device *dev, void *data)
  2812. {
  2813. if (scsi_is_target_device(dev))
  2814. starget_for_each_device(to_scsi_target(dev), NULL,
  2815. device_block);
  2816. return 0;
  2817. }
  2818. void
  2819. scsi_target_block(struct device *dev)
  2820. {
  2821. if (scsi_is_target_device(dev))
  2822. starget_for_each_device(to_scsi_target(dev), NULL,
  2823. device_block);
  2824. else
  2825. device_for_each_child(dev, NULL, target_block);
  2826. }
  2827. EXPORT_SYMBOL_GPL(scsi_target_block);
  2828. static void
  2829. device_unblock(struct scsi_device *sdev, void *data)
  2830. {
  2831. scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data);
  2832. }
  2833. static int
  2834. target_unblock(struct device *dev, void *data)
  2835. {
  2836. if (scsi_is_target_device(dev))
  2837. starget_for_each_device(to_scsi_target(dev), data,
  2838. device_unblock);
  2839. return 0;
  2840. }
  2841. void
  2842. scsi_target_unblock(struct device *dev, enum scsi_device_state new_state)
  2843. {
  2844. if (scsi_is_target_device(dev))
  2845. starget_for_each_device(to_scsi_target(dev), &new_state,
  2846. device_unblock);
  2847. else
  2848. device_for_each_child(dev, &new_state, target_unblock);
  2849. }
  2850. EXPORT_SYMBOL_GPL(scsi_target_unblock);
  2851. /**
  2852. * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
  2853. * @sgl: scatter-gather list
  2854. * @sg_count: number of segments in sg
  2855. * @offset: offset in bytes into sg, on return offset into the mapped area
  2856. * @len: bytes to map, on return number of bytes mapped
  2857. *
  2858. * Returns virtual address of the start of the mapped page
  2859. */
  2860. void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
  2861. size_t *offset, size_t *len)
  2862. {
  2863. int i;
  2864. size_t sg_len = 0, len_complete = 0;
  2865. struct scatterlist *sg;
  2866. struct page *page;
  2867. WARN_ON(!irqs_disabled());
  2868. for_each_sg(sgl, sg, sg_count, i) {
  2869. len_complete = sg_len; /* Complete sg-entries */
  2870. sg_len += sg->length;
  2871. if (sg_len > *offset)
  2872. break;
  2873. }
  2874. if (unlikely(i == sg_count)) {
  2875. printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
  2876. "elements %d\n",
  2877. __func__, sg_len, *offset, sg_count);
  2878. WARN_ON(1);
  2879. return NULL;
  2880. }
  2881. /* Offset starting from the beginning of first page in this sg-entry */
  2882. *offset = *offset - len_complete + sg->offset;
  2883. /* Assumption: contiguous pages can be accessed as "page + i" */
  2884. page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
  2885. *offset &= ~PAGE_MASK;
  2886. /* Bytes in this sg-entry from *offset to the end of the page */
  2887. sg_len = PAGE_SIZE - *offset;
  2888. if (*len > sg_len)
  2889. *len = sg_len;
  2890. return kmap_atomic(page);
  2891. }
  2892. EXPORT_SYMBOL(scsi_kmap_atomic_sg);
  2893. /**
  2894. * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg
  2895. * @virt: virtual address to be unmapped
  2896. */
  2897. void scsi_kunmap_atomic_sg(void *virt)
  2898. {
  2899. kunmap_atomic(virt);
  2900. }
  2901. EXPORT_SYMBOL(scsi_kunmap_atomic_sg);
  2902. void sdev_disable_disk_events(struct scsi_device *sdev)
  2903. {
  2904. atomic_inc(&sdev->disk_events_disable_depth);
  2905. }
  2906. EXPORT_SYMBOL(sdev_disable_disk_events);
  2907. void sdev_enable_disk_events(struct scsi_device *sdev)
  2908. {
  2909. if (WARN_ON_ONCE(atomic_read(&sdev->disk_events_disable_depth) <= 0))
  2910. return;
  2911. atomic_dec(&sdev->disk_events_disable_depth);
  2912. }
  2913. EXPORT_SYMBOL(sdev_enable_disk_events);
  2914. /**
  2915. * scsi_vpd_lun_id - return a unique device identification
  2916. * @sdev: SCSI device
  2917. * @id: buffer for the identification
  2918. * @id_len: length of the buffer
  2919. *
  2920. * Copies a unique device identification into @id based
  2921. * on the information in the VPD page 0x83 of the device.
  2922. * The string will be formatted as a SCSI name string.
  2923. *
  2924. * Returns the length of the identification or error on failure.
  2925. * If the identifier is longer than the supplied buffer the actual
  2926. * identifier length is returned and the buffer is not zero-padded.
  2927. */
  2928. int scsi_vpd_lun_id(struct scsi_device *sdev, char *id, size_t id_len)
  2929. {
  2930. u8 cur_id_type = 0xff;
  2931. u8 cur_id_size = 0;
  2932. const unsigned char *d, *cur_id_str;
  2933. const struct scsi_vpd *vpd_pg83;
  2934. int id_size = -EINVAL;
  2935. rcu_read_lock();
  2936. vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
  2937. if (!vpd_pg83) {
  2938. rcu_read_unlock();
  2939. return -ENXIO;
  2940. }
  2941. /*
  2942. * Look for the correct descriptor.
  2943. * Order of preference for lun descriptor:
  2944. * - SCSI name string
  2945. * - NAA IEEE Registered Extended
  2946. * - EUI-64 based 16-byte
  2947. * - EUI-64 based 12-byte
  2948. * - NAA IEEE Registered
  2949. * - NAA IEEE Extended
  2950. * - T10 Vendor ID
  2951. * as longer descriptors reduce the likelyhood
  2952. * of identification clashes.
  2953. */
  2954. /* The id string must be at least 20 bytes + terminating NULL byte */
  2955. if (id_len < 21) {
  2956. rcu_read_unlock();
  2957. return -EINVAL;
  2958. }
  2959. memset(id, 0, id_len);
  2960. d = vpd_pg83->data + 4;
  2961. while (d < vpd_pg83->data + vpd_pg83->len) {
  2962. /* Skip designators not referring to the LUN */
  2963. if ((d[1] & 0x30) != 0x00)
  2964. goto next_desig;
  2965. switch (d[1] & 0xf) {
  2966. case 0x1:
  2967. /* T10 Vendor ID */
  2968. if (cur_id_size > d[3])
  2969. break;
  2970. /* Prefer anything */
  2971. if (cur_id_type > 0x01 && cur_id_type != 0xff)
  2972. break;
  2973. cur_id_size = d[3];
  2974. if (cur_id_size + 4 > id_len)
  2975. cur_id_size = id_len - 4;
  2976. cur_id_str = d + 4;
  2977. cur_id_type = d[1] & 0xf;
  2978. id_size = snprintf(id, id_len, "t10.%*pE",
  2979. cur_id_size, cur_id_str);
  2980. break;
  2981. case 0x2:
  2982. /* EUI-64 */
  2983. if (cur_id_size > d[3])
  2984. break;
  2985. /* Prefer NAA IEEE Registered Extended */
  2986. if (cur_id_type == 0x3 &&
  2987. cur_id_size == d[3])
  2988. break;
  2989. cur_id_size = d[3];
  2990. cur_id_str = d + 4;
  2991. cur_id_type = d[1] & 0xf;
  2992. switch (cur_id_size) {
  2993. case 8:
  2994. id_size = snprintf(id, id_len,
  2995. "eui.%8phN",
  2996. cur_id_str);
  2997. break;
  2998. case 12:
  2999. id_size = snprintf(id, id_len,
  3000. "eui.%12phN",
  3001. cur_id_str);
  3002. break;
  3003. case 16:
  3004. id_size = snprintf(id, id_len,
  3005. "eui.%16phN",
  3006. cur_id_str);
  3007. break;
  3008. default:
  3009. cur_id_size = 0;
  3010. break;
  3011. }
  3012. break;
  3013. case 0x3:
  3014. /* NAA */
  3015. if (cur_id_size > d[3])
  3016. break;
  3017. cur_id_size = d[3];
  3018. cur_id_str = d + 4;
  3019. cur_id_type = d[1] & 0xf;
  3020. switch (cur_id_size) {
  3021. case 8:
  3022. id_size = snprintf(id, id_len,
  3023. "naa.%8phN",
  3024. cur_id_str);
  3025. break;
  3026. case 16:
  3027. id_size = snprintf(id, id_len,
  3028. "naa.%16phN",
  3029. cur_id_str);
  3030. break;
  3031. default:
  3032. cur_id_size = 0;
  3033. break;
  3034. }
  3035. break;
  3036. case 0x8:
  3037. /* SCSI name string */
  3038. if (cur_id_size + 4 > d[3])
  3039. break;
  3040. /* Prefer others for truncated descriptor */
  3041. if (cur_id_size && d[3] > id_len)
  3042. break;
  3043. cur_id_size = id_size = d[3];
  3044. cur_id_str = d + 4;
  3045. cur_id_type = d[1] & 0xf;
  3046. if (cur_id_size >= id_len)
  3047. cur_id_size = id_len - 1;
  3048. memcpy(id, cur_id_str, cur_id_size);
  3049. /* Decrease priority for truncated descriptor */
  3050. if (cur_id_size != id_size)
  3051. cur_id_size = 6;
  3052. break;
  3053. default:
  3054. break;
  3055. }
  3056. next_desig:
  3057. d += d[3] + 4;
  3058. }
  3059. rcu_read_unlock();
  3060. return id_size;
  3061. }
  3062. EXPORT_SYMBOL(scsi_vpd_lun_id);
  3063. /*
  3064. * scsi_vpd_tpg_id - return a target port group identifier
  3065. * @sdev: SCSI device
  3066. *
  3067. * Returns the Target Port Group identifier from the information
  3068. * froom VPD page 0x83 of the device.
  3069. *
  3070. * Returns the identifier or error on failure.
  3071. */
  3072. int scsi_vpd_tpg_id(struct scsi_device *sdev, int *rel_id)
  3073. {
  3074. const unsigned char *d;
  3075. const struct scsi_vpd *vpd_pg83;
  3076. int group_id = -EAGAIN, rel_port = -1;
  3077. rcu_read_lock();
  3078. vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
  3079. if (!vpd_pg83) {
  3080. rcu_read_unlock();
  3081. return -ENXIO;
  3082. }
  3083. d = vpd_pg83->data + 4;
  3084. while (d < vpd_pg83->data + vpd_pg83->len) {
  3085. switch (d[1] & 0xf) {
  3086. case 0x4:
  3087. /* Relative target port */
  3088. rel_port = get_unaligned_be16(&d[6]);
  3089. break;
  3090. case 0x5:
  3091. /* Target port group */
  3092. group_id = get_unaligned_be16(&d[6]);
  3093. break;
  3094. default:
  3095. break;
  3096. }
  3097. d += d[3] + 4;
  3098. }
  3099. rcu_read_unlock();
  3100. if (group_id >= 0 && rel_id && rel_port != -1)
  3101. *rel_id = rel_port;
  3102. return group_id;
  3103. }
  3104. EXPORT_SYMBOL(scsi_vpd_tpg_id);