scsi_lib.c 95 KB

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