scsi_lib.c 95 KB

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