scsi_lib.c 96 KB

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