target_core_transport.c 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393
  1. /*******************************************************************************
  2. * Filename: target_core_transport.c
  3. *
  4. * This file contains the Generic Target Engine Core.
  5. *
  6. * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
  7. * Copyright (c) 2005, 2006, 2007 SBE, Inc.
  8. * Copyright (c) 2007-2010 Rising Tide Systems
  9. * Copyright (c) 2008-2010 Linux-iSCSI.org
  10. *
  11. * Nicholas A. Bellinger <nab@kernel.org>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  26. *
  27. ******************************************************************************/
  28. #include <linux/net.h>
  29. #include <linux/delay.h>
  30. #include <linux/string.h>
  31. #include <linux/timer.h>
  32. #include <linux/slab.h>
  33. #include <linux/blkdev.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/kthread.h>
  36. #include <linux/in.h>
  37. #include <linux/cdrom.h>
  38. #include <linux/module.h>
  39. #include <linux/ratelimit.h>
  40. #include <asm/unaligned.h>
  41. #include <net/sock.h>
  42. #include <net/tcp.h>
  43. #include <scsi/scsi.h>
  44. #include <scsi/scsi_cmnd.h>
  45. #include <scsi/scsi_tcq.h>
  46. #include <target/target_core_base.h>
  47. #include <target/target_core_backend.h>
  48. #include <target/target_core_fabric.h>
  49. #include <target/target_core_configfs.h>
  50. #include "target_core_internal.h"
  51. #include "target_core_alua.h"
  52. #include "target_core_pr.h"
  53. #include "target_core_ua.h"
  54. static int sub_api_initialized;
  55. static struct workqueue_struct *target_completion_wq;
  56. static struct kmem_cache *se_sess_cache;
  57. struct kmem_cache *se_ua_cache;
  58. struct kmem_cache *t10_pr_reg_cache;
  59. struct kmem_cache *t10_alua_lu_gp_cache;
  60. struct kmem_cache *t10_alua_lu_gp_mem_cache;
  61. struct kmem_cache *t10_alua_tg_pt_gp_cache;
  62. struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
  63. static int transport_generic_write_pending(struct se_cmd *);
  64. static int transport_processing_thread(void *param);
  65. static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *);
  66. static void transport_complete_task_attr(struct se_cmd *cmd);
  67. static void transport_handle_queue_full(struct se_cmd *cmd,
  68. struct se_device *dev);
  69. static int transport_generic_get_mem(struct se_cmd *cmd);
  70. static void transport_put_cmd(struct se_cmd *cmd);
  71. static void transport_remove_cmd_from_queue(struct se_cmd *cmd);
  72. static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
  73. static void target_complete_ok_work(struct work_struct *work);
  74. int init_se_kmem_caches(void)
  75. {
  76. se_sess_cache = kmem_cache_create("se_sess_cache",
  77. sizeof(struct se_session), __alignof__(struct se_session),
  78. 0, NULL);
  79. if (!se_sess_cache) {
  80. pr_err("kmem_cache_create() for struct se_session"
  81. " failed\n");
  82. goto out;
  83. }
  84. se_ua_cache = kmem_cache_create("se_ua_cache",
  85. sizeof(struct se_ua), __alignof__(struct se_ua),
  86. 0, NULL);
  87. if (!se_ua_cache) {
  88. pr_err("kmem_cache_create() for struct se_ua failed\n");
  89. goto out_free_sess_cache;
  90. }
  91. t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
  92. sizeof(struct t10_pr_registration),
  93. __alignof__(struct t10_pr_registration), 0, NULL);
  94. if (!t10_pr_reg_cache) {
  95. pr_err("kmem_cache_create() for struct t10_pr_registration"
  96. " failed\n");
  97. goto out_free_ua_cache;
  98. }
  99. t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
  100. sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
  101. 0, NULL);
  102. if (!t10_alua_lu_gp_cache) {
  103. pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
  104. " failed\n");
  105. goto out_free_pr_reg_cache;
  106. }
  107. t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
  108. sizeof(struct t10_alua_lu_gp_member),
  109. __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
  110. if (!t10_alua_lu_gp_mem_cache) {
  111. pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
  112. "cache failed\n");
  113. goto out_free_lu_gp_cache;
  114. }
  115. t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
  116. sizeof(struct t10_alua_tg_pt_gp),
  117. __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
  118. if (!t10_alua_tg_pt_gp_cache) {
  119. pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
  120. "cache failed\n");
  121. goto out_free_lu_gp_mem_cache;
  122. }
  123. t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
  124. "t10_alua_tg_pt_gp_mem_cache",
  125. sizeof(struct t10_alua_tg_pt_gp_member),
  126. __alignof__(struct t10_alua_tg_pt_gp_member),
  127. 0, NULL);
  128. if (!t10_alua_tg_pt_gp_mem_cache) {
  129. pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
  130. "mem_t failed\n");
  131. goto out_free_tg_pt_gp_cache;
  132. }
  133. target_completion_wq = alloc_workqueue("target_completion",
  134. WQ_MEM_RECLAIM, 0);
  135. if (!target_completion_wq)
  136. goto out_free_tg_pt_gp_mem_cache;
  137. return 0;
  138. out_free_tg_pt_gp_mem_cache:
  139. kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
  140. out_free_tg_pt_gp_cache:
  141. kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
  142. out_free_lu_gp_mem_cache:
  143. kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
  144. out_free_lu_gp_cache:
  145. kmem_cache_destroy(t10_alua_lu_gp_cache);
  146. out_free_pr_reg_cache:
  147. kmem_cache_destroy(t10_pr_reg_cache);
  148. out_free_ua_cache:
  149. kmem_cache_destroy(se_ua_cache);
  150. out_free_sess_cache:
  151. kmem_cache_destroy(se_sess_cache);
  152. out:
  153. return -ENOMEM;
  154. }
  155. void release_se_kmem_caches(void)
  156. {
  157. destroy_workqueue(target_completion_wq);
  158. kmem_cache_destroy(se_sess_cache);
  159. kmem_cache_destroy(se_ua_cache);
  160. kmem_cache_destroy(t10_pr_reg_cache);
  161. kmem_cache_destroy(t10_alua_lu_gp_cache);
  162. kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
  163. kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
  164. kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
  165. }
  166. /* This code ensures unique mib indexes are handed out. */
  167. static DEFINE_SPINLOCK(scsi_mib_index_lock);
  168. static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
  169. /*
  170. * Allocate a new row index for the entry type specified
  171. */
  172. u32 scsi_get_new_index(scsi_index_t type)
  173. {
  174. u32 new_index;
  175. BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
  176. spin_lock(&scsi_mib_index_lock);
  177. new_index = ++scsi_mib_index[type];
  178. spin_unlock(&scsi_mib_index_lock);
  179. return new_index;
  180. }
  181. static void transport_init_queue_obj(struct se_queue_obj *qobj)
  182. {
  183. atomic_set(&qobj->queue_cnt, 0);
  184. INIT_LIST_HEAD(&qobj->qobj_list);
  185. init_waitqueue_head(&qobj->thread_wq);
  186. spin_lock_init(&qobj->cmd_queue_lock);
  187. }
  188. void transport_subsystem_check_init(void)
  189. {
  190. int ret;
  191. if (sub_api_initialized)
  192. return;
  193. ret = request_module("target_core_iblock");
  194. if (ret != 0)
  195. pr_err("Unable to load target_core_iblock\n");
  196. ret = request_module("target_core_file");
  197. if (ret != 0)
  198. pr_err("Unable to load target_core_file\n");
  199. ret = request_module("target_core_pscsi");
  200. if (ret != 0)
  201. pr_err("Unable to load target_core_pscsi\n");
  202. ret = request_module("target_core_stgt");
  203. if (ret != 0)
  204. pr_err("Unable to load target_core_stgt\n");
  205. sub_api_initialized = 1;
  206. return;
  207. }
  208. struct se_session *transport_init_session(void)
  209. {
  210. struct se_session *se_sess;
  211. se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
  212. if (!se_sess) {
  213. pr_err("Unable to allocate struct se_session from"
  214. " se_sess_cache\n");
  215. return ERR_PTR(-ENOMEM);
  216. }
  217. INIT_LIST_HEAD(&se_sess->sess_list);
  218. INIT_LIST_HEAD(&se_sess->sess_acl_list);
  219. INIT_LIST_HEAD(&se_sess->sess_cmd_list);
  220. INIT_LIST_HEAD(&se_sess->sess_wait_list);
  221. spin_lock_init(&se_sess->sess_cmd_lock);
  222. kref_init(&se_sess->sess_kref);
  223. return se_sess;
  224. }
  225. EXPORT_SYMBOL(transport_init_session);
  226. /*
  227. * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
  228. */
  229. void __transport_register_session(
  230. struct se_portal_group *se_tpg,
  231. struct se_node_acl *se_nacl,
  232. struct se_session *se_sess,
  233. void *fabric_sess_ptr)
  234. {
  235. unsigned char buf[PR_REG_ISID_LEN];
  236. se_sess->se_tpg = se_tpg;
  237. se_sess->fabric_sess_ptr = fabric_sess_ptr;
  238. /*
  239. * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
  240. *
  241. * Only set for struct se_session's that will actually be moving I/O.
  242. * eg: *NOT* discovery sessions.
  243. */
  244. if (se_nacl) {
  245. /*
  246. * If the fabric module supports an ISID based TransportID,
  247. * save this value in binary from the fabric I_T Nexus now.
  248. */
  249. if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
  250. memset(&buf[0], 0, PR_REG_ISID_LEN);
  251. se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
  252. &buf[0], PR_REG_ISID_LEN);
  253. se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
  254. }
  255. kref_get(&se_nacl->acl_kref);
  256. spin_lock_irq(&se_nacl->nacl_sess_lock);
  257. /*
  258. * The se_nacl->nacl_sess pointer will be set to the
  259. * last active I_T Nexus for each struct se_node_acl.
  260. */
  261. se_nacl->nacl_sess = se_sess;
  262. list_add_tail(&se_sess->sess_acl_list,
  263. &se_nacl->acl_sess_list);
  264. spin_unlock_irq(&se_nacl->nacl_sess_lock);
  265. }
  266. list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
  267. pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
  268. se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
  269. }
  270. EXPORT_SYMBOL(__transport_register_session);
  271. void transport_register_session(
  272. struct se_portal_group *se_tpg,
  273. struct se_node_acl *se_nacl,
  274. struct se_session *se_sess,
  275. void *fabric_sess_ptr)
  276. {
  277. unsigned long flags;
  278. spin_lock_irqsave(&se_tpg->session_lock, flags);
  279. __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
  280. spin_unlock_irqrestore(&se_tpg->session_lock, flags);
  281. }
  282. EXPORT_SYMBOL(transport_register_session);
  283. static void target_release_session(struct kref *kref)
  284. {
  285. struct se_session *se_sess = container_of(kref,
  286. struct se_session, sess_kref);
  287. struct se_portal_group *se_tpg = se_sess->se_tpg;
  288. se_tpg->se_tpg_tfo->close_session(se_sess);
  289. }
  290. void target_get_session(struct se_session *se_sess)
  291. {
  292. kref_get(&se_sess->sess_kref);
  293. }
  294. EXPORT_SYMBOL(target_get_session);
  295. int target_put_session(struct se_session *se_sess)
  296. {
  297. return kref_put(&se_sess->sess_kref, target_release_session);
  298. }
  299. EXPORT_SYMBOL(target_put_session);
  300. static void target_complete_nacl(struct kref *kref)
  301. {
  302. struct se_node_acl *nacl = container_of(kref,
  303. struct se_node_acl, acl_kref);
  304. complete(&nacl->acl_free_comp);
  305. }
  306. void target_put_nacl(struct se_node_acl *nacl)
  307. {
  308. kref_put(&nacl->acl_kref, target_complete_nacl);
  309. }
  310. void transport_deregister_session_configfs(struct se_session *se_sess)
  311. {
  312. struct se_node_acl *se_nacl;
  313. unsigned long flags;
  314. /*
  315. * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
  316. */
  317. se_nacl = se_sess->se_node_acl;
  318. if (se_nacl) {
  319. spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
  320. if (se_nacl->acl_stop == 0)
  321. list_del(&se_sess->sess_acl_list);
  322. /*
  323. * If the session list is empty, then clear the pointer.
  324. * Otherwise, set the struct se_session pointer from the tail
  325. * element of the per struct se_node_acl active session list.
  326. */
  327. if (list_empty(&se_nacl->acl_sess_list))
  328. se_nacl->nacl_sess = NULL;
  329. else {
  330. se_nacl->nacl_sess = container_of(
  331. se_nacl->acl_sess_list.prev,
  332. struct se_session, sess_acl_list);
  333. }
  334. spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
  335. }
  336. }
  337. EXPORT_SYMBOL(transport_deregister_session_configfs);
  338. void transport_free_session(struct se_session *se_sess)
  339. {
  340. kmem_cache_free(se_sess_cache, se_sess);
  341. }
  342. EXPORT_SYMBOL(transport_free_session);
  343. void transport_deregister_session(struct se_session *se_sess)
  344. {
  345. struct se_portal_group *se_tpg = se_sess->se_tpg;
  346. struct target_core_fabric_ops *se_tfo;
  347. struct se_node_acl *se_nacl;
  348. unsigned long flags;
  349. bool comp_nacl = true;
  350. if (!se_tpg) {
  351. transport_free_session(se_sess);
  352. return;
  353. }
  354. se_tfo = se_tpg->se_tpg_tfo;
  355. spin_lock_irqsave(&se_tpg->session_lock, flags);
  356. list_del(&se_sess->sess_list);
  357. se_sess->se_tpg = NULL;
  358. se_sess->fabric_sess_ptr = NULL;
  359. spin_unlock_irqrestore(&se_tpg->session_lock, flags);
  360. /*
  361. * Determine if we need to do extra work for this initiator node's
  362. * struct se_node_acl if it had been previously dynamically generated.
  363. */
  364. se_nacl = se_sess->se_node_acl;
  365. spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
  366. if (se_nacl && se_nacl->dynamic_node_acl) {
  367. if (!se_tfo->tpg_check_demo_mode_cache(se_tpg)) {
  368. list_del(&se_nacl->acl_list);
  369. se_tpg->num_node_acls--;
  370. spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
  371. core_tpg_wait_for_nacl_pr_ref(se_nacl);
  372. core_free_device_list_for_node(se_nacl, se_tpg);
  373. se_tfo->tpg_release_fabric_acl(se_tpg, se_nacl);
  374. comp_nacl = false;
  375. spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
  376. }
  377. }
  378. spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
  379. pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
  380. se_tpg->se_tpg_tfo->get_fabric_name());
  381. /*
  382. * If last kref is dropping now for an explict NodeACL, awake sleeping
  383. * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
  384. * removal context.
  385. */
  386. if (se_nacl && comp_nacl == true)
  387. target_put_nacl(se_nacl);
  388. transport_free_session(se_sess);
  389. }
  390. EXPORT_SYMBOL(transport_deregister_session);
  391. /*
  392. * Called with cmd->t_state_lock held.
  393. */
  394. static void target_remove_from_state_list(struct se_cmd *cmd)
  395. {
  396. struct se_device *dev = cmd->se_dev;
  397. unsigned long flags;
  398. if (!dev)
  399. return;
  400. if (cmd->transport_state & CMD_T_BUSY)
  401. return;
  402. spin_lock_irqsave(&dev->execute_task_lock, flags);
  403. if (cmd->state_active) {
  404. list_del(&cmd->state_list);
  405. atomic_dec(&cmd->t_task_cdbs_ex_left);
  406. cmd->state_active = false;
  407. }
  408. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  409. }
  410. /* transport_cmd_check_stop():
  411. *
  412. * 'transport_off = 1' determines if CMD_T_ACTIVE should be cleared.
  413. * 'transport_off = 2' determines if task_dev_state should be removed.
  414. *
  415. * A non-zero u8 t_state sets cmd->t_state.
  416. * Returns 1 when command is stopped, else 0.
  417. */
  418. static int transport_cmd_check_stop(
  419. struct se_cmd *cmd,
  420. int transport_off,
  421. u8 t_state)
  422. {
  423. unsigned long flags;
  424. spin_lock_irqsave(&cmd->t_state_lock, flags);
  425. /*
  426. * Determine if IOCTL context caller in requesting the stopping of this
  427. * command for LUN shutdown purposes.
  428. */
  429. if (cmd->transport_state & CMD_T_LUN_STOP) {
  430. pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
  431. __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
  432. cmd->transport_state &= ~CMD_T_ACTIVE;
  433. if (transport_off == 2)
  434. target_remove_from_state_list(cmd);
  435. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  436. complete(&cmd->transport_lun_stop_comp);
  437. return 1;
  438. }
  439. /*
  440. * Determine if frontend context caller is requesting the stopping of
  441. * this command for frontend exceptions.
  442. */
  443. if (cmd->transport_state & CMD_T_STOP) {
  444. pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
  445. __func__, __LINE__,
  446. cmd->se_tfo->get_task_tag(cmd));
  447. if (transport_off == 2)
  448. target_remove_from_state_list(cmd);
  449. /*
  450. * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
  451. * to FE.
  452. */
  453. if (transport_off == 2)
  454. cmd->se_lun = NULL;
  455. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  456. complete(&cmd->t_transport_stop_comp);
  457. return 1;
  458. }
  459. if (transport_off) {
  460. cmd->transport_state &= ~CMD_T_ACTIVE;
  461. if (transport_off == 2) {
  462. target_remove_from_state_list(cmd);
  463. /*
  464. * Clear struct se_cmd->se_lun before the transport_off == 2
  465. * handoff to fabric module.
  466. */
  467. cmd->se_lun = NULL;
  468. /*
  469. * Some fabric modules like tcm_loop can release
  470. * their internally allocated I/O reference now and
  471. * struct se_cmd now.
  472. *
  473. * Fabric modules are expected to return '1' here if the
  474. * se_cmd being passed is released at this point,
  475. * or zero if not being released.
  476. */
  477. if (cmd->se_tfo->check_stop_free != NULL) {
  478. spin_unlock_irqrestore(
  479. &cmd->t_state_lock, flags);
  480. return cmd->se_tfo->check_stop_free(cmd);
  481. }
  482. }
  483. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  484. return 0;
  485. } else if (t_state)
  486. cmd->t_state = t_state;
  487. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  488. return 0;
  489. }
  490. static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
  491. {
  492. return transport_cmd_check_stop(cmd, 2, 0);
  493. }
  494. static void transport_lun_remove_cmd(struct se_cmd *cmd)
  495. {
  496. struct se_lun *lun = cmd->se_lun;
  497. unsigned long flags;
  498. if (!lun)
  499. return;
  500. spin_lock_irqsave(&cmd->t_state_lock, flags);
  501. if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
  502. cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
  503. target_remove_from_state_list(cmd);
  504. }
  505. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  506. spin_lock_irqsave(&lun->lun_cmd_lock, flags);
  507. if (!list_empty(&cmd->se_lun_node))
  508. list_del_init(&cmd->se_lun_node);
  509. spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
  510. }
  511. void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
  512. {
  513. if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
  514. transport_lun_remove_cmd(cmd);
  515. if (transport_cmd_check_stop_to_fabric(cmd))
  516. return;
  517. if (remove) {
  518. transport_remove_cmd_from_queue(cmd);
  519. transport_put_cmd(cmd);
  520. }
  521. }
  522. static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
  523. bool at_head)
  524. {
  525. struct se_device *dev = cmd->se_dev;
  526. struct se_queue_obj *qobj = &dev->dev_queue_obj;
  527. unsigned long flags;
  528. if (t_state) {
  529. spin_lock_irqsave(&cmd->t_state_lock, flags);
  530. cmd->t_state = t_state;
  531. cmd->transport_state |= CMD_T_ACTIVE;
  532. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  533. }
  534. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  535. /* If the cmd is already on the list, remove it before we add it */
  536. if (!list_empty(&cmd->se_queue_node))
  537. list_del(&cmd->se_queue_node);
  538. else
  539. atomic_inc(&qobj->queue_cnt);
  540. if (at_head)
  541. list_add(&cmd->se_queue_node, &qobj->qobj_list);
  542. else
  543. list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
  544. cmd->transport_state |= CMD_T_QUEUED;
  545. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  546. wake_up_interruptible(&qobj->thread_wq);
  547. }
  548. static struct se_cmd *
  549. transport_get_cmd_from_queue(struct se_queue_obj *qobj)
  550. {
  551. struct se_cmd *cmd;
  552. unsigned long flags;
  553. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  554. if (list_empty(&qobj->qobj_list)) {
  555. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  556. return NULL;
  557. }
  558. cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
  559. cmd->transport_state &= ~CMD_T_QUEUED;
  560. list_del_init(&cmd->se_queue_node);
  561. atomic_dec(&qobj->queue_cnt);
  562. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  563. return cmd;
  564. }
  565. static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
  566. {
  567. struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
  568. unsigned long flags;
  569. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  570. if (!(cmd->transport_state & CMD_T_QUEUED)) {
  571. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  572. return;
  573. }
  574. cmd->transport_state &= ~CMD_T_QUEUED;
  575. atomic_dec(&qobj->queue_cnt);
  576. list_del_init(&cmd->se_queue_node);
  577. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  578. }
  579. static void target_complete_failure_work(struct work_struct *work)
  580. {
  581. struct se_cmd *cmd = container_of(work, struct se_cmd, work);
  582. transport_generic_request_failure(cmd);
  583. }
  584. void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status)
  585. {
  586. struct se_device *dev = cmd->se_dev;
  587. int success = scsi_status == GOOD;
  588. unsigned long flags;
  589. cmd->scsi_status = scsi_status;
  590. spin_lock_irqsave(&cmd->t_state_lock, flags);
  591. cmd->transport_state &= ~CMD_T_BUSY;
  592. if (dev && dev->transport->transport_complete) {
  593. if (dev->transport->transport_complete(cmd,
  594. cmd->t_data_sg) != 0) {
  595. cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
  596. success = 1;
  597. }
  598. }
  599. /*
  600. * See if we are waiting to complete for an exception condition.
  601. */
  602. if (cmd->transport_state & CMD_T_REQUEST_STOP) {
  603. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  604. complete(&cmd->task_stop_comp);
  605. return;
  606. }
  607. if (!success)
  608. cmd->transport_state |= CMD_T_FAILED;
  609. /*
  610. * Check for case where an explict ABORT_TASK has been received
  611. * and transport_wait_for_tasks() will be waiting for completion..
  612. */
  613. if (cmd->transport_state & CMD_T_ABORTED &&
  614. cmd->transport_state & CMD_T_STOP) {
  615. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  616. complete(&cmd->t_transport_stop_comp);
  617. return;
  618. } else if (cmd->transport_state & CMD_T_FAILED) {
  619. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  620. INIT_WORK(&cmd->work, target_complete_failure_work);
  621. } else {
  622. INIT_WORK(&cmd->work, target_complete_ok_work);
  623. }
  624. cmd->t_state = TRANSPORT_COMPLETE;
  625. cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
  626. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  627. queue_work(target_completion_wq, &cmd->work);
  628. }
  629. EXPORT_SYMBOL(target_complete_cmd);
  630. static void target_add_to_state_list(struct se_cmd *cmd)
  631. {
  632. struct se_device *dev = cmd->se_dev;
  633. unsigned long flags;
  634. spin_lock_irqsave(&dev->execute_task_lock, flags);
  635. if (!cmd->state_active) {
  636. list_add_tail(&cmd->state_list, &dev->state_list);
  637. cmd->state_active = true;
  638. }
  639. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  640. }
  641. static void __target_add_to_execute_list(struct se_cmd *cmd)
  642. {
  643. struct se_device *dev = cmd->se_dev;
  644. bool head_of_queue = false;
  645. if (!list_empty(&cmd->execute_list))
  646. return;
  647. if (dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED &&
  648. cmd->sam_task_attr == MSG_HEAD_TAG)
  649. head_of_queue = true;
  650. if (head_of_queue)
  651. list_add(&cmd->execute_list, &dev->execute_list);
  652. else
  653. list_add_tail(&cmd->execute_list, &dev->execute_list);
  654. atomic_inc(&dev->execute_tasks);
  655. if (cmd->state_active)
  656. return;
  657. if (head_of_queue)
  658. list_add(&cmd->state_list, &dev->state_list);
  659. else
  660. list_add_tail(&cmd->state_list, &dev->state_list);
  661. cmd->state_active = true;
  662. }
  663. static void target_add_to_execute_list(struct se_cmd *cmd)
  664. {
  665. unsigned long flags;
  666. struct se_device *dev = cmd->se_dev;
  667. spin_lock_irqsave(&dev->execute_task_lock, flags);
  668. __target_add_to_execute_list(cmd);
  669. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  670. }
  671. void __target_remove_from_execute_list(struct se_cmd *cmd)
  672. {
  673. list_del_init(&cmd->execute_list);
  674. atomic_dec(&cmd->se_dev->execute_tasks);
  675. }
  676. static void target_remove_from_execute_list(struct se_cmd *cmd)
  677. {
  678. struct se_device *dev = cmd->se_dev;
  679. unsigned long flags;
  680. if (WARN_ON(list_empty(&cmd->execute_list)))
  681. return;
  682. spin_lock_irqsave(&dev->execute_task_lock, flags);
  683. __target_remove_from_execute_list(cmd);
  684. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  685. }
  686. /*
  687. * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
  688. */
  689. static void target_qf_do_work(struct work_struct *work)
  690. {
  691. struct se_device *dev = container_of(work, struct se_device,
  692. qf_work_queue);
  693. LIST_HEAD(qf_cmd_list);
  694. struct se_cmd *cmd, *cmd_tmp;
  695. spin_lock_irq(&dev->qf_cmd_lock);
  696. list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
  697. spin_unlock_irq(&dev->qf_cmd_lock);
  698. list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
  699. list_del(&cmd->se_qf_node);
  700. atomic_dec(&dev->dev_qf_count);
  701. smp_mb__after_atomic_dec();
  702. pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
  703. " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
  704. (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
  705. (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
  706. : "UNKNOWN");
  707. transport_add_cmd_to_queue(cmd, cmd->t_state, true);
  708. }
  709. }
  710. unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
  711. {
  712. switch (cmd->data_direction) {
  713. case DMA_NONE:
  714. return "NONE";
  715. case DMA_FROM_DEVICE:
  716. return "READ";
  717. case DMA_TO_DEVICE:
  718. return "WRITE";
  719. case DMA_BIDIRECTIONAL:
  720. return "BIDI";
  721. default:
  722. break;
  723. }
  724. return "UNKNOWN";
  725. }
  726. void transport_dump_dev_state(
  727. struct se_device *dev,
  728. char *b,
  729. int *bl)
  730. {
  731. *bl += sprintf(b + *bl, "Status: ");
  732. switch (dev->dev_status) {
  733. case TRANSPORT_DEVICE_ACTIVATED:
  734. *bl += sprintf(b + *bl, "ACTIVATED");
  735. break;
  736. case TRANSPORT_DEVICE_DEACTIVATED:
  737. *bl += sprintf(b + *bl, "DEACTIVATED");
  738. break;
  739. case TRANSPORT_DEVICE_SHUTDOWN:
  740. *bl += sprintf(b + *bl, "SHUTDOWN");
  741. break;
  742. case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
  743. case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
  744. *bl += sprintf(b + *bl, "OFFLINE");
  745. break;
  746. default:
  747. *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
  748. break;
  749. }
  750. *bl += sprintf(b + *bl, " Execute/Max Queue Depth: %d/%d",
  751. atomic_read(&dev->execute_tasks), dev->queue_depth);
  752. *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
  753. dev->se_sub_dev->se_dev_attrib.block_size, dev->se_sub_dev->se_dev_attrib.max_sectors);
  754. *bl += sprintf(b + *bl, " ");
  755. }
  756. void transport_dump_vpd_proto_id(
  757. struct t10_vpd *vpd,
  758. unsigned char *p_buf,
  759. int p_buf_len)
  760. {
  761. unsigned char buf[VPD_TMP_BUF_SIZE];
  762. int len;
  763. memset(buf, 0, VPD_TMP_BUF_SIZE);
  764. len = sprintf(buf, "T10 VPD Protocol Identifier: ");
  765. switch (vpd->protocol_identifier) {
  766. case 0x00:
  767. sprintf(buf+len, "Fibre Channel\n");
  768. break;
  769. case 0x10:
  770. sprintf(buf+len, "Parallel SCSI\n");
  771. break;
  772. case 0x20:
  773. sprintf(buf+len, "SSA\n");
  774. break;
  775. case 0x30:
  776. sprintf(buf+len, "IEEE 1394\n");
  777. break;
  778. case 0x40:
  779. sprintf(buf+len, "SCSI Remote Direct Memory Access"
  780. " Protocol\n");
  781. break;
  782. case 0x50:
  783. sprintf(buf+len, "Internet SCSI (iSCSI)\n");
  784. break;
  785. case 0x60:
  786. sprintf(buf+len, "SAS Serial SCSI Protocol\n");
  787. break;
  788. case 0x70:
  789. sprintf(buf+len, "Automation/Drive Interface Transport"
  790. " Protocol\n");
  791. break;
  792. case 0x80:
  793. sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
  794. break;
  795. default:
  796. sprintf(buf+len, "Unknown 0x%02x\n",
  797. vpd->protocol_identifier);
  798. break;
  799. }
  800. if (p_buf)
  801. strncpy(p_buf, buf, p_buf_len);
  802. else
  803. pr_debug("%s", buf);
  804. }
  805. void
  806. transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
  807. {
  808. /*
  809. * Check if the Protocol Identifier Valid (PIV) bit is set..
  810. *
  811. * from spc3r23.pdf section 7.5.1
  812. */
  813. if (page_83[1] & 0x80) {
  814. vpd->protocol_identifier = (page_83[0] & 0xf0);
  815. vpd->protocol_identifier_set = 1;
  816. transport_dump_vpd_proto_id(vpd, NULL, 0);
  817. }
  818. }
  819. EXPORT_SYMBOL(transport_set_vpd_proto_id);
  820. int transport_dump_vpd_assoc(
  821. struct t10_vpd *vpd,
  822. unsigned char *p_buf,
  823. int p_buf_len)
  824. {
  825. unsigned char buf[VPD_TMP_BUF_SIZE];
  826. int ret = 0;
  827. int len;
  828. memset(buf, 0, VPD_TMP_BUF_SIZE);
  829. len = sprintf(buf, "T10 VPD Identifier Association: ");
  830. switch (vpd->association) {
  831. case 0x00:
  832. sprintf(buf+len, "addressed logical unit\n");
  833. break;
  834. case 0x10:
  835. sprintf(buf+len, "target port\n");
  836. break;
  837. case 0x20:
  838. sprintf(buf+len, "SCSI target device\n");
  839. break;
  840. default:
  841. sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
  842. ret = -EINVAL;
  843. break;
  844. }
  845. if (p_buf)
  846. strncpy(p_buf, buf, p_buf_len);
  847. else
  848. pr_debug("%s", buf);
  849. return ret;
  850. }
  851. int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
  852. {
  853. /*
  854. * The VPD identification association..
  855. *
  856. * from spc3r23.pdf Section 7.6.3.1 Table 297
  857. */
  858. vpd->association = (page_83[1] & 0x30);
  859. return transport_dump_vpd_assoc(vpd, NULL, 0);
  860. }
  861. EXPORT_SYMBOL(transport_set_vpd_assoc);
  862. int transport_dump_vpd_ident_type(
  863. struct t10_vpd *vpd,
  864. unsigned char *p_buf,
  865. int p_buf_len)
  866. {
  867. unsigned char buf[VPD_TMP_BUF_SIZE];
  868. int ret = 0;
  869. int len;
  870. memset(buf, 0, VPD_TMP_BUF_SIZE);
  871. len = sprintf(buf, "T10 VPD Identifier Type: ");
  872. switch (vpd->device_identifier_type) {
  873. case 0x00:
  874. sprintf(buf+len, "Vendor specific\n");
  875. break;
  876. case 0x01:
  877. sprintf(buf+len, "T10 Vendor ID based\n");
  878. break;
  879. case 0x02:
  880. sprintf(buf+len, "EUI-64 based\n");
  881. break;
  882. case 0x03:
  883. sprintf(buf+len, "NAA\n");
  884. break;
  885. case 0x04:
  886. sprintf(buf+len, "Relative target port identifier\n");
  887. break;
  888. case 0x08:
  889. sprintf(buf+len, "SCSI name string\n");
  890. break;
  891. default:
  892. sprintf(buf+len, "Unsupported: 0x%02x\n",
  893. vpd->device_identifier_type);
  894. ret = -EINVAL;
  895. break;
  896. }
  897. if (p_buf) {
  898. if (p_buf_len < strlen(buf)+1)
  899. return -EINVAL;
  900. strncpy(p_buf, buf, p_buf_len);
  901. } else {
  902. pr_debug("%s", buf);
  903. }
  904. return ret;
  905. }
  906. int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
  907. {
  908. /*
  909. * The VPD identifier type..
  910. *
  911. * from spc3r23.pdf Section 7.6.3.1 Table 298
  912. */
  913. vpd->device_identifier_type = (page_83[1] & 0x0f);
  914. return transport_dump_vpd_ident_type(vpd, NULL, 0);
  915. }
  916. EXPORT_SYMBOL(transport_set_vpd_ident_type);
  917. int transport_dump_vpd_ident(
  918. struct t10_vpd *vpd,
  919. unsigned char *p_buf,
  920. int p_buf_len)
  921. {
  922. unsigned char buf[VPD_TMP_BUF_SIZE];
  923. int ret = 0;
  924. memset(buf, 0, VPD_TMP_BUF_SIZE);
  925. switch (vpd->device_identifier_code_set) {
  926. case 0x01: /* Binary */
  927. sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
  928. &vpd->device_identifier[0]);
  929. break;
  930. case 0x02: /* ASCII */
  931. sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
  932. &vpd->device_identifier[0]);
  933. break;
  934. case 0x03: /* UTF-8 */
  935. sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
  936. &vpd->device_identifier[0]);
  937. break;
  938. default:
  939. sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
  940. " 0x%02x", vpd->device_identifier_code_set);
  941. ret = -EINVAL;
  942. break;
  943. }
  944. if (p_buf)
  945. strncpy(p_buf, buf, p_buf_len);
  946. else
  947. pr_debug("%s", buf);
  948. return ret;
  949. }
  950. int
  951. transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
  952. {
  953. static const char hex_str[] = "0123456789abcdef";
  954. int j = 0, i = 4; /* offset to start of the identifer */
  955. /*
  956. * The VPD Code Set (encoding)
  957. *
  958. * from spc3r23.pdf Section 7.6.3.1 Table 296
  959. */
  960. vpd->device_identifier_code_set = (page_83[0] & 0x0f);
  961. switch (vpd->device_identifier_code_set) {
  962. case 0x01: /* Binary */
  963. vpd->device_identifier[j++] =
  964. hex_str[vpd->device_identifier_type];
  965. while (i < (4 + page_83[3])) {
  966. vpd->device_identifier[j++] =
  967. hex_str[(page_83[i] & 0xf0) >> 4];
  968. vpd->device_identifier[j++] =
  969. hex_str[page_83[i] & 0x0f];
  970. i++;
  971. }
  972. break;
  973. case 0x02: /* ASCII */
  974. case 0x03: /* UTF-8 */
  975. while (i < (4 + page_83[3]))
  976. vpd->device_identifier[j++] = page_83[i++];
  977. break;
  978. default:
  979. break;
  980. }
  981. return transport_dump_vpd_ident(vpd, NULL, 0);
  982. }
  983. EXPORT_SYMBOL(transport_set_vpd_ident);
  984. static void core_setup_task_attr_emulation(struct se_device *dev)
  985. {
  986. /*
  987. * If this device is from Target_Core_Mod/pSCSI, disable the
  988. * SAM Task Attribute emulation.
  989. *
  990. * This is currently not available in upsream Linux/SCSI Target
  991. * mode code, and is assumed to be disabled while using TCM/pSCSI.
  992. */
  993. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  994. dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
  995. return;
  996. }
  997. dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
  998. pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
  999. " device\n", dev->transport->name,
  1000. dev->transport->get_device_rev(dev));
  1001. }
  1002. static void scsi_dump_inquiry(struct se_device *dev)
  1003. {
  1004. struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
  1005. char buf[17];
  1006. int i, device_type;
  1007. /*
  1008. * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
  1009. */
  1010. for (i = 0; i < 8; i++)
  1011. if (wwn->vendor[i] >= 0x20)
  1012. buf[i] = wwn->vendor[i];
  1013. else
  1014. buf[i] = ' ';
  1015. buf[i] = '\0';
  1016. pr_debug(" Vendor: %s\n", buf);
  1017. for (i = 0; i < 16; i++)
  1018. if (wwn->model[i] >= 0x20)
  1019. buf[i] = wwn->model[i];
  1020. else
  1021. buf[i] = ' ';
  1022. buf[i] = '\0';
  1023. pr_debug(" Model: %s\n", buf);
  1024. for (i = 0; i < 4; i++)
  1025. if (wwn->revision[i] >= 0x20)
  1026. buf[i] = wwn->revision[i];
  1027. else
  1028. buf[i] = ' ';
  1029. buf[i] = '\0';
  1030. pr_debug(" Revision: %s\n", buf);
  1031. device_type = dev->transport->get_device_type(dev);
  1032. pr_debug(" Type: %s ", scsi_device_type(device_type));
  1033. pr_debug(" ANSI SCSI revision: %02x\n",
  1034. dev->transport->get_device_rev(dev));
  1035. }
  1036. struct se_device *transport_add_device_to_core_hba(
  1037. struct se_hba *hba,
  1038. struct se_subsystem_api *transport,
  1039. struct se_subsystem_dev *se_dev,
  1040. u32 device_flags,
  1041. void *transport_dev,
  1042. struct se_dev_limits *dev_limits,
  1043. const char *inquiry_prod,
  1044. const char *inquiry_rev)
  1045. {
  1046. int force_pt;
  1047. struct se_device *dev;
  1048. dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
  1049. if (!dev) {
  1050. pr_err("Unable to allocate memory for se_dev_t\n");
  1051. return NULL;
  1052. }
  1053. transport_init_queue_obj(&dev->dev_queue_obj);
  1054. dev->dev_flags = device_flags;
  1055. dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
  1056. dev->dev_ptr = transport_dev;
  1057. dev->se_hba = hba;
  1058. dev->se_sub_dev = se_dev;
  1059. dev->transport = transport;
  1060. INIT_LIST_HEAD(&dev->dev_list);
  1061. INIT_LIST_HEAD(&dev->dev_sep_list);
  1062. INIT_LIST_HEAD(&dev->dev_tmr_list);
  1063. INIT_LIST_HEAD(&dev->execute_list);
  1064. INIT_LIST_HEAD(&dev->delayed_cmd_list);
  1065. INIT_LIST_HEAD(&dev->state_list);
  1066. INIT_LIST_HEAD(&dev->qf_cmd_list);
  1067. spin_lock_init(&dev->execute_task_lock);
  1068. spin_lock_init(&dev->delayed_cmd_lock);
  1069. spin_lock_init(&dev->dev_reservation_lock);
  1070. spin_lock_init(&dev->dev_status_lock);
  1071. spin_lock_init(&dev->se_port_lock);
  1072. spin_lock_init(&dev->se_tmr_lock);
  1073. spin_lock_init(&dev->qf_cmd_lock);
  1074. atomic_set(&dev->dev_ordered_id, 0);
  1075. se_dev_set_default_attribs(dev, dev_limits);
  1076. dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
  1077. dev->creation_time = get_jiffies_64();
  1078. spin_lock_init(&dev->stats_lock);
  1079. spin_lock(&hba->device_lock);
  1080. list_add_tail(&dev->dev_list, &hba->hba_dev_list);
  1081. hba->dev_count++;
  1082. spin_unlock(&hba->device_lock);
  1083. /*
  1084. * Setup the SAM Task Attribute emulation for struct se_device
  1085. */
  1086. core_setup_task_attr_emulation(dev);
  1087. /*
  1088. * Force PR and ALUA passthrough emulation with internal object use.
  1089. */
  1090. force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
  1091. /*
  1092. * Setup the Reservations infrastructure for struct se_device
  1093. */
  1094. core_setup_reservations(dev, force_pt);
  1095. /*
  1096. * Setup the Asymmetric Logical Unit Assignment for struct se_device
  1097. */
  1098. if (core_setup_alua(dev, force_pt) < 0)
  1099. goto out;
  1100. /*
  1101. * Startup the struct se_device processing thread
  1102. */
  1103. dev->process_thread = kthread_run(transport_processing_thread, dev,
  1104. "LIO_%s", dev->transport->name);
  1105. if (IS_ERR(dev->process_thread)) {
  1106. pr_err("Unable to create kthread: LIO_%s\n",
  1107. dev->transport->name);
  1108. goto out;
  1109. }
  1110. /*
  1111. * Setup work_queue for QUEUE_FULL
  1112. */
  1113. INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
  1114. /*
  1115. * Preload the initial INQUIRY const values if we are doing
  1116. * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
  1117. * passthrough because this is being provided by the backend LLD.
  1118. * This is required so that transport_get_inquiry() copies these
  1119. * originals once back into DEV_T10_WWN(dev) for the virtual device
  1120. * setup.
  1121. */
  1122. if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
  1123. if (!inquiry_prod || !inquiry_rev) {
  1124. pr_err("All non TCM/pSCSI plugins require"
  1125. " INQUIRY consts\n");
  1126. goto out;
  1127. }
  1128. strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
  1129. strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
  1130. strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
  1131. }
  1132. scsi_dump_inquiry(dev);
  1133. return dev;
  1134. out:
  1135. kthread_stop(dev->process_thread);
  1136. spin_lock(&hba->device_lock);
  1137. list_del(&dev->dev_list);
  1138. hba->dev_count--;
  1139. spin_unlock(&hba->device_lock);
  1140. se_release_vpd_for_dev(dev);
  1141. kfree(dev);
  1142. return NULL;
  1143. }
  1144. EXPORT_SYMBOL(transport_add_device_to_core_hba);
  1145. /* transport_generic_prepare_cdb():
  1146. *
  1147. * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
  1148. * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
  1149. * The point of this is since we are mapping iSCSI LUNs to
  1150. * SCSI Target IDs having a non-zero LUN in the CDB will throw the
  1151. * devices and HBAs for a loop.
  1152. */
  1153. static inline void transport_generic_prepare_cdb(
  1154. unsigned char *cdb)
  1155. {
  1156. switch (cdb[0]) {
  1157. case READ_10: /* SBC - RDProtect */
  1158. case READ_12: /* SBC - RDProtect */
  1159. case READ_16: /* SBC - RDProtect */
  1160. case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
  1161. case VERIFY: /* SBC - VRProtect */
  1162. case VERIFY_16: /* SBC - VRProtect */
  1163. case WRITE_VERIFY: /* SBC - VRProtect */
  1164. case WRITE_VERIFY_12: /* SBC - VRProtect */
  1165. break;
  1166. default:
  1167. cdb[1] &= 0x1f; /* clear logical unit number */
  1168. break;
  1169. }
  1170. }
  1171. static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
  1172. /*
  1173. * Used by fabric modules containing a local struct se_cmd within their
  1174. * fabric dependent per I/O descriptor.
  1175. */
  1176. void transport_init_se_cmd(
  1177. struct se_cmd *cmd,
  1178. struct target_core_fabric_ops *tfo,
  1179. struct se_session *se_sess,
  1180. u32 data_length,
  1181. int data_direction,
  1182. int task_attr,
  1183. unsigned char *sense_buffer)
  1184. {
  1185. INIT_LIST_HEAD(&cmd->se_lun_node);
  1186. INIT_LIST_HEAD(&cmd->se_delayed_node);
  1187. INIT_LIST_HEAD(&cmd->se_qf_node);
  1188. INIT_LIST_HEAD(&cmd->se_queue_node);
  1189. INIT_LIST_HEAD(&cmd->se_cmd_list);
  1190. INIT_LIST_HEAD(&cmd->execute_list);
  1191. INIT_LIST_HEAD(&cmd->state_list);
  1192. init_completion(&cmd->transport_lun_fe_stop_comp);
  1193. init_completion(&cmd->transport_lun_stop_comp);
  1194. init_completion(&cmd->t_transport_stop_comp);
  1195. init_completion(&cmd->cmd_wait_comp);
  1196. init_completion(&cmd->task_stop_comp);
  1197. spin_lock_init(&cmd->t_state_lock);
  1198. cmd->transport_state = CMD_T_DEV_ACTIVE;
  1199. cmd->se_tfo = tfo;
  1200. cmd->se_sess = se_sess;
  1201. cmd->data_length = data_length;
  1202. cmd->data_direction = data_direction;
  1203. cmd->sam_task_attr = task_attr;
  1204. cmd->sense_buffer = sense_buffer;
  1205. cmd->state_active = false;
  1206. }
  1207. EXPORT_SYMBOL(transport_init_se_cmd);
  1208. static int transport_check_alloc_task_attr(struct se_cmd *cmd)
  1209. {
  1210. /*
  1211. * Check if SAM Task Attribute emulation is enabled for this
  1212. * struct se_device storage object
  1213. */
  1214. if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  1215. return 0;
  1216. if (cmd->sam_task_attr == MSG_ACA_TAG) {
  1217. pr_debug("SAM Task Attribute ACA"
  1218. " emulation is not supported\n");
  1219. return -EINVAL;
  1220. }
  1221. /*
  1222. * Used to determine when ORDERED commands should go from
  1223. * Dormant to Active status.
  1224. */
  1225. cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
  1226. smp_mb__after_atomic_inc();
  1227. pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
  1228. cmd->se_ordered_id, cmd->sam_task_attr,
  1229. cmd->se_dev->transport->name);
  1230. return 0;
  1231. }
  1232. /* target_setup_cmd_from_cdb():
  1233. *
  1234. * Called from fabric RX Thread.
  1235. */
  1236. int target_setup_cmd_from_cdb(
  1237. struct se_cmd *cmd,
  1238. unsigned char *cdb)
  1239. {
  1240. int ret;
  1241. transport_generic_prepare_cdb(cdb);
  1242. /*
  1243. * Ensure that the received CDB is less than the max (252 + 8) bytes
  1244. * for VARIABLE_LENGTH_CMD
  1245. */
  1246. if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
  1247. pr_err("Received SCSI CDB with command_size: %d that"
  1248. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  1249. scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
  1250. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1251. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1252. return -EINVAL;
  1253. }
  1254. /*
  1255. * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
  1256. * allocate the additional extended CDB buffer now.. Otherwise
  1257. * setup the pointer from __t_task_cdb to t_task_cdb.
  1258. */
  1259. if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
  1260. cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
  1261. GFP_KERNEL);
  1262. if (!cmd->t_task_cdb) {
  1263. pr_err("Unable to allocate cmd->t_task_cdb"
  1264. " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
  1265. scsi_command_size(cdb),
  1266. (unsigned long)sizeof(cmd->__t_task_cdb));
  1267. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1268. cmd->scsi_sense_reason =
  1269. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1270. return -ENOMEM;
  1271. }
  1272. } else
  1273. cmd->t_task_cdb = &cmd->__t_task_cdb[0];
  1274. /*
  1275. * Copy the original CDB into cmd->
  1276. */
  1277. memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
  1278. /*
  1279. * Setup the received CDB based on SCSI defined opcodes and
  1280. * perform unit attention, persistent reservations and ALUA
  1281. * checks for virtual device backends. The cmd->t_task_cdb
  1282. * pointer is expected to be setup before we reach this point.
  1283. */
  1284. ret = transport_generic_cmd_sequencer(cmd, cdb);
  1285. if (ret < 0)
  1286. return ret;
  1287. /*
  1288. * Check for SAM Task Attribute Emulation
  1289. */
  1290. if (transport_check_alloc_task_attr(cmd) < 0) {
  1291. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1292. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1293. return -EINVAL;
  1294. }
  1295. spin_lock(&cmd->se_lun->lun_sep_lock);
  1296. if (cmd->se_lun->lun_sep)
  1297. cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
  1298. spin_unlock(&cmd->se_lun->lun_sep_lock);
  1299. return 0;
  1300. }
  1301. EXPORT_SYMBOL(target_setup_cmd_from_cdb);
  1302. /*
  1303. * Used by fabric module frontends to queue tasks directly.
  1304. * Many only be used from process context only
  1305. */
  1306. int transport_handle_cdb_direct(
  1307. struct se_cmd *cmd)
  1308. {
  1309. int ret;
  1310. if (!cmd->se_lun) {
  1311. dump_stack();
  1312. pr_err("cmd->se_lun is NULL\n");
  1313. return -EINVAL;
  1314. }
  1315. if (in_interrupt()) {
  1316. dump_stack();
  1317. pr_err("transport_generic_handle_cdb cannot be called"
  1318. " from interrupt context\n");
  1319. return -EINVAL;
  1320. }
  1321. /*
  1322. * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE following
  1323. * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
  1324. * in existing usage to ensure that outstanding descriptors are handled
  1325. * correctly during shutdown via transport_wait_for_tasks()
  1326. *
  1327. * Also, we don't take cmd->t_state_lock here as we only expect
  1328. * this to be called for initial descriptor submission.
  1329. */
  1330. cmd->t_state = TRANSPORT_NEW_CMD;
  1331. cmd->transport_state |= CMD_T_ACTIVE;
  1332. /*
  1333. * transport_generic_new_cmd() is already handling QUEUE_FULL,
  1334. * so follow TRANSPORT_NEW_CMD processing thread context usage
  1335. * and call transport_generic_request_failure() if necessary..
  1336. */
  1337. ret = transport_generic_new_cmd(cmd);
  1338. if (ret < 0)
  1339. transport_generic_request_failure(cmd);
  1340. return 0;
  1341. }
  1342. EXPORT_SYMBOL(transport_handle_cdb_direct);
  1343. /**
  1344. * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
  1345. *
  1346. * @se_cmd: command descriptor to submit
  1347. * @se_sess: associated se_sess for endpoint
  1348. * @cdb: pointer to SCSI CDB
  1349. * @sense: pointer to SCSI sense buffer
  1350. * @unpacked_lun: unpacked LUN to reference for struct se_lun
  1351. * @data_length: fabric expected data transfer length
  1352. * @task_addr: SAM task attribute
  1353. * @data_dir: DMA data direction
  1354. * @flags: flags for command submission from target_sc_flags_tables
  1355. *
  1356. * This may only be called from process context, and also currently
  1357. * assumes internal allocation of fabric payload buffer by target-core.
  1358. **/
  1359. void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
  1360. unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
  1361. u32 data_length, int task_attr, int data_dir, int flags)
  1362. {
  1363. struct se_portal_group *se_tpg;
  1364. int rc;
  1365. se_tpg = se_sess->se_tpg;
  1366. BUG_ON(!se_tpg);
  1367. BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
  1368. BUG_ON(in_interrupt());
  1369. /*
  1370. * Initialize se_cmd for target operation. From this point
  1371. * exceptions are handled by sending exception status via
  1372. * target_core_fabric_ops->queue_status() callback
  1373. */
  1374. transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
  1375. data_length, data_dir, task_attr, sense);
  1376. if (flags & TARGET_SCF_UNKNOWN_SIZE)
  1377. se_cmd->unknown_data_length = 1;
  1378. /*
  1379. * Obtain struct se_cmd->cmd_kref reference and add new cmd to
  1380. * se_sess->sess_cmd_list. A second kref_get here is necessary
  1381. * for fabrics using TARGET_SCF_ACK_KREF that expect a second
  1382. * kref_put() to happen during fabric packet acknowledgement.
  1383. */
  1384. target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
  1385. /*
  1386. * Signal bidirectional data payloads to target-core
  1387. */
  1388. if (flags & TARGET_SCF_BIDI_OP)
  1389. se_cmd->se_cmd_flags |= SCF_BIDI;
  1390. /*
  1391. * Locate se_lun pointer and attach it to struct se_cmd
  1392. */
  1393. if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
  1394. transport_send_check_condition_and_sense(se_cmd,
  1395. se_cmd->scsi_sense_reason, 0);
  1396. target_put_sess_cmd(se_sess, se_cmd);
  1397. return;
  1398. }
  1399. /*
  1400. * Sanitize CDBs via transport_generic_cmd_sequencer() and
  1401. * allocate the necessary tasks to complete the received CDB+data
  1402. */
  1403. rc = target_setup_cmd_from_cdb(se_cmd, cdb);
  1404. if (rc != 0) {
  1405. transport_generic_request_failure(se_cmd);
  1406. return;
  1407. }
  1408. /*
  1409. * Check if we need to delay processing because of ALUA
  1410. * Active/NonOptimized primary access state..
  1411. */
  1412. core_alua_check_nonop_delay(se_cmd);
  1413. /*
  1414. * Dispatch se_cmd descriptor to se_lun->lun_se_dev backend
  1415. * for immediate execution of READs, otherwise wait for
  1416. * transport_generic_handle_data() to be called for WRITEs
  1417. * when fabric has filled the incoming buffer.
  1418. */
  1419. transport_handle_cdb_direct(se_cmd);
  1420. return;
  1421. }
  1422. EXPORT_SYMBOL(target_submit_cmd);
  1423. static void target_complete_tmr_failure(struct work_struct *work)
  1424. {
  1425. struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
  1426. se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
  1427. se_cmd->se_tfo->queue_tm_rsp(se_cmd);
  1428. transport_generic_free_cmd(se_cmd, 0);
  1429. }
  1430. /**
  1431. * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
  1432. * for TMR CDBs
  1433. *
  1434. * @se_cmd: command descriptor to submit
  1435. * @se_sess: associated se_sess for endpoint
  1436. * @sense: pointer to SCSI sense buffer
  1437. * @unpacked_lun: unpacked LUN to reference for struct se_lun
  1438. * @fabric_context: fabric context for TMR req
  1439. * @tm_type: Type of TM request
  1440. * @gfp: gfp type for caller
  1441. * @tag: referenced task tag for TMR_ABORT_TASK
  1442. * @flags: submit cmd flags
  1443. *
  1444. * Callable from all contexts.
  1445. **/
  1446. int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
  1447. unsigned char *sense, u32 unpacked_lun,
  1448. void *fabric_tmr_ptr, unsigned char tm_type,
  1449. gfp_t gfp, unsigned int tag, int flags)
  1450. {
  1451. struct se_portal_group *se_tpg;
  1452. int ret;
  1453. se_tpg = se_sess->se_tpg;
  1454. BUG_ON(!se_tpg);
  1455. transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
  1456. 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
  1457. /*
  1458. * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
  1459. * allocation failure.
  1460. */
  1461. ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
  1462. if (ret < 0)
  1463. return -ENOMEM;
  1464. if (tm_type == TMR_ABORT_TASK)
  1465. se_cmd->se_tmr_req->ref_task_tag = tag;
  1466. /* See target_submit_cmd for commentary */
  1467. target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
  1468. ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
  1469. if (ret) {
  1470. /*
  1471. * For callback during failure handling, push this work off
  1472. * to process context with TMR_LUN_DOES_NOT_EXIST status.
  1473. */
  1474. INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
  1475. schedule_work(&se_cmd->work);
  1476. return 0;
  1477. }
  1478. transport_generic_handle_tmr(se_cmd);
  1479. return 0;
  1480. }
  1481. EXPORT_SYMBOL(target_submit_tmr);
  1482. /*
  1483. * Used by fabric module frontends defining a TFO->new_cmd_map() caller
  1484. * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
  1485. * complete setup in TCM process context w/ TFO->new_cmd_map().
  1486. */
  1487. int transport_generic_handle_cdb_map(
  1488. struct se_cmd *cmd)
  1489. {
  1490. if (!cmd->se_lun) {
  1491. dump_stack();
  1492. pr_err("cmd->se_lun is NULL\n");
  1493. return -EINVAL;
  1494. }
  1495. transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
  1496. return 0;
  1497. }
  1498. EXPORT_SYMBOL(transport_generic_handle_cdb_map);
  1499. /* transport_generic_handle_data():
  1500. *
  1501. *
  1502. */
  1503. int transport_generic_handle_data(
  1504. struct se_cmd *cmd)
  1505. {
  1506. /*
  1507. * For the software fabric case, then we assume the nexus is being
  1508. * failed/shutdown when signals are pending from the kthread context
  1509. * caller, so we return a failure. For the HW target mode case running
  1510. * in interrupt code, the signal_pending() check is skipped.
  1511. */
  1512. if (!in_interrupt() && signal_pending(current))
  1513. return -EPERM;
  1514. /*
  1515. * If the received CDB has aleady been ABORTED by the generic
  1516. * target engine, we now call transport_check_aborted_status()
  1517. * to queue any delated TASK_ABORTED status for the received CDB to the
  1518. * fabric module as we are expecting no further incoming DATA OUT
  1519. * sequences at this point.
  1520. */
  1521. if (transport_check_aborted_status(cmd, 1) != 0)
  1522. return 0;
  1523. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
  1524. return 0;
  1525. }
  1526. EXPORT_SYMBOL(transport_generic_handle_data);
  1527. /* transport_generic_handle_tmr():
  1528. *
  1529. *
  1530. */
  1531. int transport_generic_handle_tmr(
  1532. struct se_cmd *cmd)
  1533. {
  1534. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
  1535. return 0;
  1536. }
  1537. EXPORT_SYMBOL(transport_generic_handle_tmr);
  1538. /*
  1539. * If the cmd is active, request it to be stopped and sleep until it
  1540. * has completed.
  1541. */
  1542. bool target_stop_cmd(struct se_cmd *cmd, unsigned long *flags)
  1543. {
  1544. bool was_active = false;
  1545. if (cmd->transport_state & CMD_T_BUSY) {
  1546. cmd->transport_state |= CMD_T_REQUEST_STOP;
  1547. spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
  1548. pr_debug("cmd %p waiting to complete\n", cmd);
  1549. wait_for_completion(&cmd->task_stop_comp);
  1550. pr_debug("cmd %p stopped successfully\n", cmd);
  1551. spin_lock_irqsave(&cmd->t_state_lock, *flags);
  1552. cmd->transport_state &= ~CMD_T_REQUEST_STOP;
  1553. cmd->transport_state &= ~CMD_T_BUSY;
  1554. was_active = true;
  1555. }
  1556. return was_active;
  1557. }
  1558. /*
  1559. * Handle SAM-esque emulation for generic transport request failures.
  1560. */
  1561. void transport_generic_request_failure(struct se_cmd *cmd)
  1562. {
  1563. int ret = 0;
  1564. pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
  1565. " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
  1566. cmd->t_task_cdb[0]);
  1567. pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
  1568. cmd->se_tfo->get_cmd_state(cmd),
  1569. cmd->t_state, cmd->scsi_sense_reason);
  1570. pr_debug("-----[ t_task_cdbs_ex_left: %d --"
  1571. " CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
  1572. atomic_read(&cmd->t_task_cdbs_ex_left),
  1573. (cmd->transport_state & CMD_T_ACTIVE) != 0,
  1574. (cmd->transport_state & CMD_T_STOP) != 0,
  1575. (cmd->transport_state & CMD_T_SENT) != 0);
  1576. /*
  1577. * For SAM Task Attribute emulation for failed struct se_cmd
  1578. */
  1579. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  1580. transport_complete_task_attr(cmd);
  1581. switch (cmd->scsi_sense_reason) {
  1582. case TCM_NON_EXISTENT_LUN:
  1583. case TCM_UNSUPPORTED_SCSI_OPCODE:
  1584. case TCM_INVALID_CDB_FIELD:
  1585. case TCM_INVALID_PARAMETER_LIST:
  1586. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  1587. case TCM_UNKNOWN_MODE_PAGE:
  1588. case TCM_WRITE_PROTECTED:
  1589. case TCM_CHECK_CONDITION_ABORT_CMD:
  1590. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  1591. case TCM_CHECK_CONDITION_NOT_READY:
  1592. break;
  1593. case TCM_RESERVATION_CONFLICT:
  1594. /*
  1595. * No SENSE Data payload for this case, set SCSI Status
  1596. * and queue the response to $FABRIC_MOD.
  1597. *
  1598. * Uses linux/include/scsi/scsi.h SAM status codes defs
  1599. */
  1600. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  1601. /*
  1602. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  1603. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  1604. * CONFLICT STATUS.
  1605. *
  1606. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  1607. */
  1608. if (cmd->se_sess &&
  1609. cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
  1610. core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
  1611. cmd->orig_fe_lun, 0x2C,
  1612. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  1613. ret = cmd->se_tfo->queue_status(cmd);
  1614. if (ret == -EAGAIN || ret == -ENOMEM)
  1615. goto queue_full;
  1616. goto check_stop;
  1617. default:
  1618. pr_err("Unknown transport error for CDB 0x%02x: %d\n",
  1619. cmd->t_task_cdb[0], cmd->scsi_sense_reason);
  1620. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1621. break;
  1622. }
  1623. /*
  1624. * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
  1625. * make the call to transport_send_check_condition_and_sense()
  1626. * directly. Otherwise expect the fabric to make the call to
  1627. * transport_send_check_condition_and_sense() after handling
  1628. * possible unsoliticied write data payloads.
  1629. */
  1630. ret = transport_send_check_condition_and_sense(cmd,
  1631. cmd->scsi_sense_reason, 0);
  1632. if (ret == -EAGAIN || ret == -ENOMEM)
  1633. goto queue_full;
  1634. check_stop:
  1635. transport_lun_remove_cmd(cmd);
  1636. if (!transport_cmd_check_stop_to_fabric(cmd))
  1637. ;
  1638. return;
  1639. queue_full:
  1640. cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
  1641. transport_handle_queue_full(cmd, cmd->se_dev);
  1642. }
  1643. EXPORT_SYMBOL(transport_generic_request_failure);
  1644. static inline u32 transport_lba_21(unsigned char *cdb)
  1645. {
  1646. return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
  1647. }
  1648. static inline u32 transport_lba_32(unsigned char *cdb)
  1649. {
  1650. return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1651. }
  1652. static inline unsigned long long transport_lba_64(unsigned char *cdb)
  1653. {
  1654. unsigned int __v1, __v2;
  1655. __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1656. __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1657. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  1658. }
  1659. /*
  1660. * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
  1661. */
  1662. static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
  1663. {
  1664. unsigned int __v1, __v2;
  1665. __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
  1666. __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
  1667. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  1668. }
  1669. static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
  1670. {
  1671. unsigned long flags;
  1672. spin_lock_irqsave(&se_cmd->t_state_lock, flags);
  1673. se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
  1674. spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
  1675. }
  1676. /*
  1677. * Called from Fabric Module context from transport_execute_tasks()
  1678. *
  1679. * The return of this function determins if the tasks from struct se_cmd
  1680. * get added to the execution queue in transport_execute_tasks(),
  1681. * or are added to the delayed or ordered lists here.
  1682. */
  1683. static inline int transport_execute_task_attr(struct se_cmd *cmd)
  1684. {
  1685. if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  1686. return 1;
  1687. /*
  1688. * Check for the existence of HEAD_OF_QUEUE, and if true return 1
  1689. * to allow the passed struct se_cmd list of tasks to the front of the list.
  1690. */
  1691. if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  1692. pr_debug("Added HEAD_OF_QUEUE for CDB:"
  1693. " 0x%02x, se_ordered_id: %u\n",
  1694. cmd->t_task_cdb[0],
  1695. cmd->se_ordered_id);
  1696. return 1;
  1697. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  1698. atomic_inc(&cmd->se_dev->dev_ordered_sync);
  1699. smp_mb__after_atomic_inc();
  1700. pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
  1701. " list, se_ordered_id: %u\n",
  1702. cmd->t_task_cdb[0],
  1703. cmd->se_ordered_id);
  1704. /*
  1705. * Add ORDERED command to tail of execution queue if
  1706. * no other older commands exist that need to be
  1707. * completed first.
  1708. */
  1709. if (!atomic_read(&cmd->se_dev->simple_cmds))
  1710. return 1;
  1711. } else {
  1712. /*
  1713. * For SIMPLE and UNTAGGED Task Attribute commands
  1714. */
  1715. atomic_inc(&cmd->se_dev->simple_cmds);
  1716. smp_mb__after_atomic_inc();
  1717. }
  1718. /*
  1719. * Otherwise if one or more outstanding ORDERED task attribute exist,
  1720. * add the dormant task(s) built for the passed struct se_cmd to the
  1721. * execution queue and become in Active state for this struct se_device.
  1722. */
  1723. if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
  1724. /*
  1725. * Otherwise, add cmd w/ tasks to delayed cmd queue that
  1726. * will be drained upon completion of HEAD_OF_QUEUE task.
  1727. */
  1728. spin_lock(&cmd->se_dev->delayed_cmd_lock);
  1729. cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
  1730. list_add_tail(&cmd->se_delayed_node,
  1731. &cmd->se_dev->delayed_cmd_list);
  1732. spin_unlock(&cmd->se_dev->delayed_cmd_lock);
  1733. pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
  1734. " delayed CMD list, se_ordered_id: %u\n",
  1735. cmd->t_task_cdb[0], cmd->sam_task_attr,
  1736. cmd->se_ordered_id);
  1737. /*
  1738. * Return zero to let transport_execute_tasks() know
  1739. * not to add the delayed tasks to the execution list.
  1740. */
  1741. return 0;
  1742. }
  1743. /*
  1744. * Otherwise, no ORDERED task attributes exist..
  1745. */
  1746. return 1;
  1747. }
  1748. /*
  1749. * Called from fabric module context in transport_generic_new_cmd() and
  1750. * transport_generic_process_write()
  1751. */
  1752. static int transport_execute_tasks(struct se_cmd *cmd)
  1753. {
  1754. int add_tasks;
  1755. struct se_device *se_dev = cmd->se_dev;
  1756. /*
  1757. * Call transport_cmd_check_stop() to see if a fabric exception
  1758. * has occurred that prevents execution.
  1759. */
  1760. if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
  1761. /*
  1762. * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
  1763. * attribute for the tasks of the received struct se_cmd CDB
  1764. */
  1765. add_tasks = transport_execute_task_attr(cmd);
  1766. if (!add_tasks)
  1767. goto execute_tasks;
  1768. __transport_execute_tasks(se_dev, cmd);
  1769. return 0;
  1770. }
  1771. execute_tasks:
  1772. __transport_execute_tasks(se_dev, NULL);
  1773. return 0;
  1774. }
  1775. static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *new_cmd)
  1776. {
  1777. int error;
  1778. struct se_cmd *cmd = NULL;
  1779. unsigned long flags;
  1780. check_depth:
  1781. spin_lock_irq(&dev->execute_task_lock);
  1782. if (new_cmd != NULL)
  1783. __target_add_to_execute_list(new_cmd);
  1784. if (list_empty(&dev->execute_list)) {
  1785. spin_unlock_irq(&dev->execute_task_lock);
  1786. return 0;
  1787. }
  1788. cmd = list_first_entry(&dev->execute_list, struct se_cmd, execute_list);
  1789. __target_remove_from_execute_list(cmd);
  1790. spin_unlock_irq(&dev->execute_task_lock);
  1791. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1792. cmd->transport_state |= CMD_T_BUSY;
  1793. cmd->transport_state |= CMD_T_SENT;
  1794. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1795. if (cmd->execute_cmd)
  1796. error = cmd->execute_cmd(cmd);
  1797. else {
  1798. error = dev->transport->execute_cmd(cmd, cmd->t_data_sg,
  1799. cmd->t_data_nents, cmd->data_direction);
  1800. }
  1801. if (error != 0) {
  1802. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1803. cmd->transport_state &= ~CMD_T_BUSY;
  1804. cmd->transport_state &= ~CMD_T_SENT;
  1805. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1806. transport_generic_request_failure(cmd);
  1807. }
  1808. new_cmd = NULL;
  1809. goto check_depth;
  1810. return 0;
  1811. }
  1812. static inline u32 transport_get_sectors_6(
  1813. unsigned char *cdb,
  1814. struct se_cmd *cmd,
  1815. int *ret)
  1816. {
  1817. struct se_device *dev = cmd->se_dev;
  1818. /*
  1819. * Assume TYPE_DISK for non struct se_device objects.
  1820. * Use 8-bit sector value.
  1821. */
  1822. if (!dev)
  1823. goto type_disk;
  1824. /*
  1825. * Use 24-bit allocation length for TYPE_TAPE.
  1826. */
  1827. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  1828. return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
  1829. /*
  1830. * Everything else assume TYPE_DISK Sector CDB location.
  1831. * Use 8-bit sector value. SBC-3 says:
  1832. *
  1833. * A TRANSFER LENGTH field set to zero specifies that 256
  1834. * logical blocks shall be written. Any other value
  1835. * specifies the number of logical blocks that shall be
  1836. * written.
  1837. */
  1838. type_disk:
  1839. return cdb[4] ? : 256;
  1840. }
  1841. static inline u32 transport_get_sectors_10(
  1842. unsigned char *cdb,
  1843. struct se_cmd *cmd,
  1844. int *ret)
  1845. {
  1846. struct se_device *dev = cmd->se_dev;
  1847. /*
  1848. * Assume TYPE_DISK for non struct se_device objects.
  1849. * Use 16-bit sector value.
  1850. */
  1851. if (!dev)
  1852. goto type_disk;
  1853. /*
  1854. * XXX_10 is not defined in SSC, throw an exception
  1855. */
  1856. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  1857. *ret = -EINVAL;
  1858. return 0;
  1859. }
  1860. /*
  1861. * Everything else assume TYPE_DISK Sector CDB location.
  1862. * Use 16-bit sector value.
  1863. */
  1864. type_disk:
  1865. return (u32)(cdb[7] << 8) + cdb[8];
  1866. }
  1867. static inline u32 transport_get_sectors_12(
  1868. unsigned char *cdb,
  1869. struct se_cmd *cmd,
  1870. int *ret)
  1871. {
  1872. struct se_device *dev = cmd->se_dev;
  1873. /*
  1874. * Assume TYPE_DISK for non struct se_device objects.
  1875. * Use 32-bit sector value.
  1876. */
  1877. if (!dev)
  1878. goto type_disk;
  1879. /*
  1880. * XXX_12 is not defined in SSC, throw an exception
  1881. */
  1882. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  1883. *ret = -EINVAL;
  1884. return 0;
  1885. }
  1886. /*
  1887. * Everything else assume TYPE_DISK Sector CDB location.
  1888. * Use 32-bit sector value.
  1889. */
  1890. type_disk:
  1891. return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
  1892. }
  1893. static inline u32 transport_get_sectors_16(
  1894. unsigned char *cdb,
  1895. struct se_cmd *cmd,
  1896. int *ret)
  1897. {
  1898. struct se_device *dev = cmd->se_dev;
  1899. /*
  1900. * Assume TYPE_DISK for non struct se_device objects.
  1901. * Use 32-bit sector value.
  1902. */
  1903. if (!dev)
  1904. goto type_disk;
  1905. /*
  1906. * Use 24-bit allocation length for TYPE_TAPE.
  1907. */
  1908. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  1909. return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
  1910. type_disk:
  1911. return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
  1912. (cdb[12] << 8) + cdb[13];
  1913. }
  1914. /*
  1915. * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
  1916. */
  1917. static inline u32 transport_get_sectors_32(
  1918. unsigned char *cdb,
  1919. struct se_cmd *cmd,
  1920. int *ret)
  1921. {
  1922. /*
  1923. * Assume TYPE_DISK for non struct se_device objects.
  1924. * Use 32-bit sector value.
  1925. */
  1926. return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
  1927. (cdb[30] << 8) + cdb[31];
  1928. }
  1929. static inline u32 transport_get_size(
  1930. u32 sectors,
  1931. unsigned char *cdb,
  1932. struct se_cmd *cmd)
  1933. {
  1934. struct se_device *dev = cmd->se_dev;
  1935. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  1936. if (cdb[1] & 1) { /* sectors */
  1937. return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
  1938. } else /* bytes */
  1939. return sectors;
  1940. }
  1941. pr_debug("Returning block_size: %u, sectors: %u == %u for"
  1942. " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size,
  1943. sectors, dev->se_sub_dev->se_dev_attrib.block_size * sectors,
  1944. dev->transport->name);
  1945. return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
  1946. }
  1947. static void transport_xor_callback(struct se_cmd *cmd)
  1948. {
  1949. unsigned char *buf, *addr;
  1950. struct scatterlist *sg;
  1951. unsigned int offset;
  1952. int i;
  1953. int count;
  1954. /*
  1955. * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
  1956. *
  1957. * 1) read the specified logical block(s);
  1958. * 2) transfer logical blocks from the data-out buffer;
  1959. * 3) XOR the logical blocks transferred from the data-out buffer with
  1960. * the logical blocks read, storing the resulting XOR data in a buffer;
  1961. * 4) if the DISABLE WRITE bit is set to zero, then write the logical
  1962. * blocks transferred from the data-out buffer; and
  1963. * 5) transfer the resulting XOR data to the data-in buffer.
  1964. */
  1965. buf = kmalloc(cmd->data_length, GFP_KERNEL);
  1966. if (!buf) {
  1967. pr_err("Unable to allocate xor_callback buf\n");
  1968. return;
  1969. }
  1970. /*
  1971. * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
  1972. * into the locally allocated *buf
  1973. */
  1974. sg_copy_to_buffer(cmd->t_data_sg,
  1975. cmd->t_data_nents,
  1976. buf,
  1977. cmd->data_length);
  1978. /*
  1979. * Now perform the XOR against the BIDI read memory located at
  1980. * cmd->t_mem_bidi_list
  1981. */
  1982. offset = 0;
  1983. for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
  1984. addr = kmap_atomic(sg_page(sg));
  1985. if (!addr)
  1986. goto out;
  1987. for (i = 0; i < sg->length; i++)
  1988. *(addr + sg->offset + i) ^= *(buf + offset + i);
  1989. offset += sg->length;
  1990. kunmap_atomic(addr);
  1991. }
  1992. out:
  1993. kfree(buf);
  1994. }
  1995. /*
  1996. * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
  1997. */
  1998. static int transport_get_sense_data(struct se_cmd *cmd)
  1999. {
  2000. unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
  2001. struct se_device *dev = cmd->se_dev;
  2002. unsigned long flags;
  2003. u32 offset = 0;
  2004. WARN_ON(!cmd->se_lun);
  2005. if (!dev)
  2006. return 0;
  2007. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2008. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  2009. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2010. return 0;
  2011. }
  2012. if (!(cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
  2013. goto out;
  2014. if (!dev->transport->get_sense_buffer) {
  2015. pr_err("dev->transport->get_sense_buffer is NULL\n");
  2016. goto out;
  2017. }
  2018. sense_buffer = dev->transport->get_sense_buffer(cmd);
  2019. if (!sense_buffer) {
  2020. pr_err("ITT 0x%08x cmd %p: Unable to locate"
  2021. " sense buffer for task with sense\n",
  2022. cmd->se_tfo->get_task_tag(cmd), cmd);
  2023. goto out;
  2024. }
  2025. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2026. offset = cmd->se_tfo->set_fabric_sense_len(cmd, TRANSPORT_SENSE_BUFFER);
  2027. memcpy(&buffer[offset], sense_buffer, TRANSPORT_SENSE_BUFFER);
  2028. /* Automatically padded */
  2029. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
  2030. pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x and sense\n",
  2031. dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status);
  2032. return 0;
  2033. out:
  2034. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2035. return -1;
  2036. }
  2037. static inline long long transport_dev_end_lba(struct se_device *dev)
  2038. {
  2039. return dev->transport->get_blocks(dev) + 1;
  2040. }
  2041. static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
  2042. {
  2043. struct se_device *dev = cmd->se_dev;
  2044. u32 sectors;
  2045. if (dev->transport->get_device_type(dev) != TYPE_DISK)
  2046. return 0;
  2047. sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
  2048. if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
  2049. pr_err("LBA: %llu Sectors: %u exceeds"
  2050. " transport_dev_end_lba(): %llu\n",
  2051. cmd->t_task_lba, sectors,
  2052. transport_dev_end_lba(dev));
  2053. return -EINVAL;
  2054. }
  2055. return 0;
  2056. }
  2057. static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
  2058. {
  2059. /*
  2060. * Determine if the received WRITE_SAME is used to for direct
  2061. * passthrough into Linux/SCSI with struct request via TCM/pSCSI
  2062. * or we are signaling the use of internal WRITE_SAME + UNMAP=1
  2063. * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
  2064. */
  2065. int passthrough = (dev->transport->transport_type ==
  2066. TRANSPORT_PLUGIN_PHBA_PDEV);
  2067. if (!passthrough) {
  2068. if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
  2069. pr_err("WRITE_SAME PBDATA and LBDATA"
  2070. " bits not supported for Block Discard"
  2071. " Emulation\n");
  2072. return -ENOSYS;
  2073. }
  2074. /*
  2075. * Currently for the emulated case we only accept
  2076. * tpws with the UNMAP=1 bit set.
  2077. */
  2078. if (!(flags[0] & 0x08)) {
  2079. pr_err("WRITE_SAME w/o UNMAP bit not"
  2080. " supported for Block Discard Emulation\n");
  2081. return -ENOSYS;
  2082. }
  2083. }
  2084. return 0;
  2085. }
  2086. /* transport_generic_cmd_sequencer():
  2087. *
  2088. * Generic Command Sequencer that should work for most DAS transport
  2089. * drivers.
  2090. *
  2091. * Called from target_setup_cmd_from_cdb() in the $FABRIC_MOD
  2092. * RX Thread.
  2093. *
  2094. * FIXME: Need to support other SCSI OPCODES where as well.
  2095. */
  2096. static int transport_generic_cmd_sequencer(
  2097. struct se_cmd *cmd,
  2098. unsigned char *cdb)
  2099. {
  2100. struct se_device *dev = cmd->se_dev;
  2101. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  2102. int ret = 0, sector_ret = 0, passthrough;
  2103. u32 sectors = 0, size = 0, pr_reg_type = 0;
  2104. u16 service_action;
  2105. u8 alua_ascq = 0;
  2106. /*
  2107. * Check for an existing UNIT ATTENTION condition
  2108. */
  2109. if (core_scsi3_ua_check(cmd, cdb) < 0) {
  2110. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2111. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
  2112. return -EINVAL;
  2113. }
  2114. /*
  2115. * Check status of Asymmetric Logical Unit Assignment port
  2116. */
  2117. ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
  2118. if (ret != 0) {
  2119. /*
  2120. * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
  2121. * The ALUA additional sense code qualifier (ASCQ) is determined
  2122. * by the ALUA primary or secondary access state..
  2123. */
  2124. if (ret > 0) {
  2125. pr_debug("[%s]: ALUA TG Port not available,"
  2126. " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
  2127. cmd->se_tfo->get_fabric_name(), alua_ascq);
  2128. transport_set_sense_codes(cmd, 0x04, alua_ascq);
  2129. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2130. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
  2131. return -EINVAL;
  2132. }
  2133. goto out_invalid_cdb_field;
  2134. }
  2135. /*
  2136. * Check status for SPC-3 Persistent Reservations
  2137. */
  2138. if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
  2139. if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
  2140. cmd, cdb, pr_reg_type) != 0) {
  2141. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2142. cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
  2143. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  2144. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2145. return -EBUSY;
  2146. }
  2147. /*
  2148. * This means the CDB is allowed for the SCSI Initiator port
  2149. * when said port is *NOT* holding the legacy SPC-2 or
  2150. * SPC-3 Persistent Reservation.
  2151. */
  2152. }
  2153. /*
  2154. * If we operate in passthrough mode we skip most CDB emulation and
  2155. * instead hand the commands down to the physical SCSI device.
  2156. */
  2157. passthrough =
  2158. (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
  2159. switch (cdb[0]) {
  2160. case READ_6:
  2161. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2162. if (sector_ret)
  2163. goto out_unsupported_cdb;
  2164. size = transport_get_size(sectors, cdb, cmd);
  2165. cmd->t_task_lba = transport_lba_21(cdb);
  2166. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2167. break;
  2168. case READ_10:
  2169. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2170. if (sector_ret)
  2171. goto out_unsupported_cdb;
  2172. size = transport_get_size(sectors, cdb, cmd);
  2173. cmd->t_task_lba = transport_lba_32(cdb);
  2174. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2175. break;
  2176. case READ_12:
  2177. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2178. if (sector_ret)
  2179. goto out_unsupported_cdb;
  2180. size = transport_get_size(sectors, cdb, cmd);
  2181. cmd->t_task_lba = transport_lba_32(cdb);
  2182. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2183. break;
  2184. case READ_16:
  2185. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2186. if (sector_ret)
  2187. goto out_unsupported_cdb;
  2188. size = transport_get_size(sectors, cdb, cmd);
  2189. cmd->t_task_lba = transport_lba_64(cdb);
  2190. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2191. break;
  2192. case WRITE_6:
  2193. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2194. if (sector_ret)
  2195. goto out_unsupported_cdb;
  2196. size = transport_get_size(sectors, cdb, cmd);
  2197. cmd->t_task_lba = transport_lba_21(cdb);
  2198. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2199. break;
  2200. case WRITE_10:
  2201. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2202. if (sector_ret)
  2203. goto out_unsupported_cdb;
  2204. size = transport_get_size(sectors, cdb, cmd);
  2205. cmd->t_task_lba = transport_lba_32(cdb);
  2206. if (cdb[1] & 0x8)
  2207. cmd->se_cmd_flags |= SCF_FUA;
  2208. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2209. break;
  2210. case WRITE_12:
  2211. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2212. if (sector_ret)
  2213. goto out_unsupported_cdb;
  2214. size = transport_get_size(sectors, cdb, cmd);
  2215. cmd->t_task_lba = transport_lba_32(cdb);
  2216. if (cdb[1] & 0x8)
  2217. cmd->se_cmd_flags |= SCF_FUA;
  2218. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2219. break;
  2220. case WRITE_16:
  2221. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2222. if (sector_ret)
  2223. goto out_unsupported_cdb;
  2224. size = transport_get_size(sectors, cdb, cmd);
  2225. cmd->t_task_lba = transport_lba_64(cdb);
  2226. if (cdb[1] & 0x8)
  2227. cmd->se_cmd_flags |= SCF_FUA;
  2228. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2229. break;
  2230. case XDWRITEREAD_10:
  2231. if ((cmd->data_direction != DMA_TO_DEVICE) ||
  2232. !(cmd->se_cmd_flags & SCF_BIDI))
  2233. goto out_invalid_cdb_field;
  2234. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2235. if (sector_ret)
  2236. goto out_unsupported_cdb;
  2237. size = transport_get_size(sectors, cdb, cmd);
  2238. cmd->t_task_lba = transport_lba_32(cdb);
  2239. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2240. /*
  2241. * Do now allow BIDI commands for passthrough mode.
  2242. */
  2243. if (passthrough)
  2244. goto out_unsupported_cdb;
  2245. /*
  2246. * Setup BIDI XOR callback to be run after I/O completion.
  2247. */
  2248. cmd->transport_complete_callback = &transport_xor_callback;
  2249. if (cdb[1] & 0x8)
  2250. cmd->se_cmd_flags |= SCF_FUA;
  2251. break;
  2252. case VARIABLE_LENGTH_CMD:
  2253. service_action = get_unaligned_be16(&cdb[8]);
  2254. switch (service_action) {
  2255. case XDWRITEREAD_32:
  2256. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2257. if (sector_ret)
  2258. goto out_unsupported_cdb;
  2259. size = transport_get_size(sectors, cdb, cmd);
  2260. /*
  2261. * Use WRITE_32 and READ_32 opcodes for the emulated
  2262. * XDWRITE_READ_32 logic.
  2263. */
  2264. cmd->t_task_lba = transport_lba_64_ext(cdb);
  2265. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2266. /*
  2267. * Do now allow BIDI commands for passthrough mode.
  2268. */
  2269. if (passthrough)
  2270. goto out_unsupported_cdb;
  2271. /*
  2272. * Setup BIDI XOR callback to be run during after I/O
  2273. * completion.
  2274. */
  2275. cmd->transport_complete_callback = &transport_xor_callback;
  2276. if (cdb[1] & 0x8)
  2277. cmd->se_cmd_flags |= SCF_FUA;
  2278. break;
  2279. case WRITE_SAME_32:
  2280. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2281. if (sector_ret)
  2282. goto out_unsupported_cdb;
  2283. if (sectors)
  2284. size = transport_get_size(1, cdb, cmd);
  2285. else {
  2286. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
  2287. " supported\n");
  2288. goto out_invalid_cdb_field;
  2289. }
  2290. cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
  2291. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2292. if (target_check_write_same_discard(&cdb[10], dev) < 0)
  2293. goto out_unsupported_cdb;
  2294. if (!passthrough)
  2295. cmd->execute_cmd = target_emulate_write_same;
  2296. break;
  2297. default:
  2298. pr_err("VARIABLE_LENGTH_CMD service action"
  2299. " 0x%04x not supported\n", service_action);
  2300. goto out_unsupported_cdb;
  2301. }
  2302. break;
  2303. case MAINTENANCE_IN:
  2304. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2305. /* MAINTENANCE_IN from SCC-2 */
  2306. /*
  2307. * Check for emulated MI_REPORT_TARGET_PGS.
  2308. */
  2309. if (cdb[1] == MI_REPORT_TARGET_PGS &&
  2310. su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
  2311. cmd->execute_cmd =
  2312. target_emulate_report_target_port_groups;
  2313. }
  2314. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2315. (cdb[8] << 8) | cdb[9];
  2316. } else {
  2317. /* GPCMD_SEND_KEY from multi media commands */
  2318. size = (cdb[8] << 8) + cdb[9];
  2319. }
  2320. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2321. break;
  2322. case MODE_SELECT:
  2323. size = cdb[4];
  2324. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2325. break;
  2326. case MODE_SELECT_10:
  2327. size = (cdb[7] << 8) + cdb[8];
  2328. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2329. break;
  2330. case MODE_SENSE:
  2331. size = cdb[4];
  2332. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2333. if (!passthrough)
  2334. cmd->execute_cmd = target_emulate_modesense;
  2335. break;
  2336. case MODE_SENSE_10:
  2337. size = (cdb[7] << 8) + cdb[8];
  2338. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2339. if (!passthrough)
  2340. cmd->execute_cmd = target_emulate_modesense;
  2341. break;
  2342. case GPCMD_READ_BUFFER_CAPACITY:
  2343. case GPCMD_SEND_OPC:
  2344. case LOG_SELECT:
  2345. case LOG_SENSE:
  2346. size = (cdb[7] << 8) + cdb[8];
  2347. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2348. break;
  2349. case READ_BLOCK_LIMITS:
  2350. size = READ_BLOCK_LEN;
  2351. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2352. break;
  2353. case GPCMD_GET_CONFIGURATION:
  2354. case GPCMD_READ_FORMAT_CAPACITIES:
  2355. case GPCMD_READ_DISC_INFO:
  2356. case GPCMD_READ_TRACK_RZONE_INFO:
  2357. size = (cdb[7] << 8) + cdb[8];
  2358. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2359. break;
  2360. case PERSISTENT_RESERVE_IN:
  2361. if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
  2362. cmd->execute_cmd = target_scsi3_emulate_pr_in;
  2363. size = (cdb[7] << 8) + cdb[8];
  2364. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2365. break;
  2366. case PERSISTENT_RESERVE_OUT:
  2367. if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
  2368. cmd->execute_cmd = target_scsi3_emulate_pr_out;
  2369. size = (cdb[7] << 8) + cdb[8];
  2370. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2371. break;
  2372. case GPCMD_MECHANISM_STATUS:
  2373. case GPCMD_READ_DVD_STRUCTURE:
  2374. size = (cdb[8] << 8) + cdb[9];
  2375. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2376. break;
  2377. case READ_POSITION:
  2378. size = READ_POSITION_LEN;
  2379. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2380. break;
  2381. case MAINTENANCE_OUT:
  2382. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2383. /* MAINTENANCE_OUT from SCC-2
  2384. *
  2385. * Check for emulated MO_SET_TARGET_PGS.
  2386. */
  2387. if (cdb[1] == MO_SET_TARGET_PGS &&
  2388. su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
  2389. cmd->execute_cmd =
  2390. target_emulate_set_target_port_groups;
  2391. }
  2392. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2393. (cdb[8] << 8) | cdb[9];
  2394. } else {
  2395. /* GPCMD_REPORT_KEY from multi media commands */
  2396. size = (cdb[8] << 8) + cdb[9];
  2397. }
  2398. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2399. break;
  2400. case INQUIRY:
  2401. size = (cdb[3] << 8) + cdb[4];
  2402. /*
  2403. * Do implict HEAD_OF_QUEUE processing for INQUIRY.
  2404. * See spc4r17 section 5.3
  2405. */
  2406. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2407. cmd->sam_task_attr = MSG_HEAD_TAG;
  2408. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2409. if (!passthrough)
  2410. cmd->execute_cmd = target_emulate_inquiry;
  2411. break;
  2412. case READ_BUFFER:
  2413. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2414. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2415. break;
  2416. case READ_CAPACITY:
  2417. size = READ_CAP_LEN;
  2418. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2419. if (!passthrough)
  2420. cmd->execute_cmd = target_emulate_readcapacity;
  2421. break;
  2422. case READ_MEDIA_SERIAL_NUMBER:
  2423. case SECURITY_PROTOCOL_IN:
  2424. case SECURITY_PROTOCOL_OUT:
  2425. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  2426. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2427. break;
  2428. case SERVICE_ACTION_IN:
  2429. switch (cmd->t_task_cdb[1] & 0x1f) {
  2430. case SAI_READ_CAPACITY_16:
  2431. if (!passthrough)
  2432. cmd->execute_cmd =
  2433. target_emulate_readcapacity_16;
  2434. break;
  2435. default:
  2436. if (passthrough)
  2437. break;
  2438. pr_err("Unsupported SA: 0x%02x\n",
  2439. cmd->t_task_cdb[1] & 0x1f);
  2440. goto out_invalid_cdb_field;
  2441. }
  2442. /*FALLTHROUGH*/
  2443. case ACCESS_CONTROL_IN:
  2444. case ACCESS_CONTROL_OUT:
  2445. case EXTENDED_COPY:
  2446. case READ_ATTRIBUTE:
  2447. case RECEIVE_COPY_RESULTS:
  2448. case WRITE_ATTRIBUTE:
  2449. size = (cdb[10] << 24) | (cdb[11] << 16) |
  2450. (cdb[12] << 8) | cdb[13];
  2451. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2452. break;
  2453. case RECEIVE_DIAGNOSTIC:
  2454. case SEND_DIAGNOSTIC:
  2455. size = (cdb[3] << 8) | cdb[4];
  2456. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2457. break;
  2458. /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
  2459. #if 0
  2460. case GPCMD_READ_CD:
  2461. sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2462. size = (2336 * sectors);
  2463. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2464. break;
  2465. #endif
  2466. case READ_TOC:
  2467. size = cdb[8];
  2468. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2469. break;
  2470. case REQUEST_SENSE:
  2471. size = cdb[4];
  2472. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2473. if (!passthrough)
  2474. cmd->execute_cmd = target_emulate_request_sense;
  2475. break;
  2476. case READ_ELEMENT_STATUS:
  2477. size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
  2478. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2479. break;
  2480. case WRITE_BUFFER:
  2481. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2482. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2483. break;
  2484. case RESERVE:
  2485. case RESERVE_10:
  2486. /*
  2487. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  2488. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  2489. */
  2490. if (cdb[0] == RESERVE_10)
  2491. size = (cdb[7] << 8) | cdb[8];
  2492. else
  2493. size = cmd->data_length;
  2494. /*
  2495. * Setup the legacy emulated handler for SPC-2 and
  2496. * >= SPC-3 compatible reservation handling (CRH=1)
  2497. * Otherwise, we assume the underlying SCSI logic is
  2498. * is running in SPC_PASSTHROUGH, and wants reservations
  2499. * emulation disabled.
  2500. */
  2501. if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
  2502. cmd->execute_cmd = target_scsi2_reservation_reserve;
  2503. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2504. break;
  2505. case RELEASE:
  2506. case RELEASE_10:
  2507. /*
  2508. * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
  2509. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  2510. */
  2511. if (cdb[0] == RELEASE_10)
  2512. size = (cdb[7] << 8) | cdb[8];
  2513. else
  2514. size = cmd->data_length;
  2515. if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
  2516. cmd->execute_cmd = target_scsi2_reservation_release;
  2517. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2518. break;
  2519. case SYNCHRONIZE_CACHE:
  2520. case SYNCHRONIZE_CACHE_16:
  2521. /*
  2522. * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
  2523. */
  2524. if (cdb[0] == SYNCHRONIZE_CACHE) {
  2525. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2526. cmd->t_task_lba = transport_lba_32(cdb);
  2527. } else {
  2528. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2529. cmd->t_task_lba = transport_lba_64(cdb);
  2530. }
  2531. if (sector_ret)
  2532. goto out_unsupported_cdb;
  2533. size = transport_get_size(sectors, cdb, cmd);
  2534. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2535. if (passthrough)
  2536. break;
  2537. /*
  2538. * Check to ensure that LBA + Range does not exceed past end of
  2539. * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
  2540. */
  2541. if ((cmd->t_task_lba != 0) || (sectors != 0)) {
  2542. if (transport_cmd_get_valid_sectors(cmd) < 0)
  2543. goto out_invalid_cdb_field;
  2544. }
  2545. cmd->execute_cmd = target_emulate_synchronize_cache;
  2546. break;
  2547. case UNMAP:
  2548. size = get_unaligned_be16(&cdb[7]);
  2549. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2550. if (!passthrough)
  2551. cmd->execute_cmd = target_emulate_unmap;
  2552. break;
  2553. case WRITE_SAME_16:
  2554. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2555. if (sector_ret)
  2556. goto out_unsupported_cdb;
  2557. if (sectors)
  2558. size = transport_get_size(1, cdb, cmd);
  2559. else {
  2560. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
  2561. goto out_invalid_cdb_field;
  2562. }
  2563. cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
  2564. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2565. if (target_check_write_same_discard(&cdb[1], dev) < 0)
  2566. goto out_unsupported_cdb;
  2567. if (!passthrough)
  2568. cmd->execute_cmd = target_emulate_write_same;
  2569. break;
  2570. case WRITE_SAME:
  2571. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2572. if (sector_ret)
  2573. goto out_unsupported_cdb;
  2574. if (sectors)
  2575. size = transport_get_size(1, cdb, cmd);
  2576. else {
  2577. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
  2578. goto out_invalid_cdb_field;
  2579. }
  2580. cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
  2581. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2582. /*
  2583. * Follow sbcr26 with WRITE_SAME (10) and check for the existence
  2584. * of byte 1 bit 3 UNMAP instead of original reserved field
  2585. */
  2586. if (target_check_write_same_discard(&cdb[1], dev) < 0)
  2587. goto out_unsupported_cdb;
  2588. if (!passthrough)
  2589. cmd->execute_cmd = target_emulate_write_same;
  2590. break;
  2591. case ALLOW_MEDIUM_REMOVAL:
  2592. case ERASE:
  2593. case REZERO_UNIT:
  2594. case SEEK_10:
  2595. case SPACE:
  2596. case START_STOP:
  2597. case TEST_UNIT_READY:
  2598. case VERIFY:
  2599. case WRITE_FILEMARKS:
  2600. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2601. if (!passthrough)
  2602. cmd->execute_cmd = target_emulate_noop;
  2603. break;
  2604. case GPCMD_CLOSE_TRACK:
  2605. case INITIALIZE_ELEMENT_STATUS:
  2606. case GPCMD_LOAD_UNLOAD:
  2607. case GPCMD_SET_SPEED:
  2608. case MOVE_MEDIUM:
  2609. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2610. break;
  2611. case REPORT_LUNS:
  2612. cmd->execute_cmd = target_report_luns;
  2613. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  2614. /*
  2615. * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
  2616. * See spc4r17 section 5.3
  2617. */
  2618. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2619. cmd->sam_task_attr = MSG_HEAD_TAG;
  2620. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2621. break;
  2622. case GET_EVENT_STATUS_NOTIFICATION:
  2623. size = (cdb[7] << 8) | cdb[8];
  2624. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2625. break;
  2626. default:
  2627. pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
  2628. " 0x%02x, sending CHECK_CONDITION.\n",
  2629. cmd->se_tfo->get_fabric_name(), cdb[0]);
  2630. goto out_unsupported_cdb;
  2631. }
  2632. if (cmd->unknown_data_length)
  2633. cmd->data_length = size;
  2634. if (size != cmd->data_length) {
  2635. pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
  2636. " %u does not match SCSI CDB Length: %u for SAM Opcode:"
  2637. " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
  2638. cmd->data_length, size, cdb[0]);
  2639. cmd->cmd_spdtl = size;
  2640. if (cmd->data_direction == DMA_TO_DEVICE) {
  2641. pr_err("Rejecting underflow/overflow"
  2642. " WRITE data\n");
  2643. goto out_invalid_cdb_field;
  2644. }
  2645. /*
  2646. * Reject READ_* or WRITE_* with overflow/underflow for
  2647. * type SCF_SCSI_DATA_SG_IO_CDB.
  2648. */
  2649. if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
  2650. pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
  2651. " CDB on non 512-byte sector setup subsystem"
  2652. " plugin: %s\n", dev->transport->name);
  2653. /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
  2654. goto out_invalid_cdb_field;
  2655. }
  2656. if (size > cmd->data_length) {
  2657. cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
  2658. cmd->residual_count = (size - cmd->data_length);
  2659. } else {
  2660. cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  2661. cmd->residual_count = (cmd->data_length - size);
  2662. }
  2663. cmd->data_length = size;
  2664. }
  2665. if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB &&
  2666. (sectors > dev->se_sub_dev->se_dev_attrib.fabric_max_sectors ||
  2667. sectors > dev->se_sub_dev->se_dev_attrib.max_sectors)) {
  2668. printk_ratelimited(KERN_ERR "SCSI OP %02xh with too big sectors %u\n",
  2669. cdb[0], sectors);
  2670. goto out_invalid_cdb_field;
  2671. }
  2672. /* reject any command that we don't have a handler for */
  2673. if (!(passthrough || cmd->execute_cmd ||
  2674. (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
  2675. goto out_unsupported_cdb;
  2676. transport_set_supported_SAM_opcode(cmd);
  2677. return ret;
  2678. out_unsupported_cdb:
  2679. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2680. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  2681. return -EINVAL;
  2682. out_invalid_cdb_field:
  2683. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2684. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  2685. return -EINVAL;
  2686. }
  2687. /*
  2688. * Called from I/O completion to determine which dormant/delayed
  2689. * and ordered cmds need to have their tasks added to the execution queue.
  2690. */
  2691. static void transport_complete_task_attr(struct se_cmd *cmd)
  2692. {
  2693. struct se_device *dev = cmd->se_dev;
  2694. struct se_cmd *cmd_p, *cmd_tmp;
  2695. int new_active_tasks = 0;
  2696. if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
  2697. atomic_dec(&dev->simple_cmds);
  2698. smp_mb__after_atomic_dec();
  2699. dev->dev_cur_ordered_id++;
  2700. pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
  2701. " SIMPLE: %u\n", dev->dev_cur_ordered_id,
  2702. cmd->se_ordered_id);
  2703. } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  2704. dev->dev_cur_ordered_id++;
  2705. pr_debug("Incremented dev_cur_ordered_id: %u for"
  2706. " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
  2707. cmd->se_ordered_id);
  2708. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  2709. atomic_dec(&dev->dev_ordered_sync);
  2710. smp_mb__after_atomic_dec();
  2711. dev->dev_cur_ordered_id++;
  2712. pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
  2713. " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
  2714. }
  2715. /*
  2716. * Process all commands up to the last received
  2717. * ORDERED task attribute which requires another blocking
  2718. * boundary
  2719. */
  2720. spin_lock(&dev->delayed_cmd_lock);
  2721. list_for_each_entry_safe(cmd_p, cmd_tmp,
  2722. &dev->delayed_cmd_list, se_delayed_node) {
  2723. list_del(&cmd_p->se_delayed_node);
  2724. spin_unlock(&dev->delayed_cmd_lock);
  2725. pr_debug("Calling add_tasks() for"
  2726. " cmd_p: 0x%02x Task Attr: 0x%02x"
  2727. " Dormant -> Active, se_ordered_id: %u\n",
  2728. cmd_p->t_task_cdb[0],
  2729. cmd_p->sam_task_attr, cmd_p->se_ordered_id);
  2730. target_add_to_execute_list(cmd_p);
  2731. new_active_tasks++;
  2732. spin_lock(&dev->delayed_cmd_lock);
  2733. if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
  2734. break;
  2735. }
  2736. spin_unlock(&dev->delayed_cmd_lock);
  2737. /*
  2738. * If new tasks have become active, wake up the transport thread
  2739. * to do the processing of the Active tasks.
  2740. */
  2741. if (new_active_tasks != 0)
  2742. wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
  2743. }
  2744. static void transport_complete_qf(struct se_cmd *cmd)
  2745. {
  2746. int ret = 0;
  2747. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2748. transport_complete_task_attr(cmd);
  2749. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  2750. ret = cmd->se_tfo->queue_status(cmd);
  2751. if (ret)
  2752. goto out;
  2753. }
  2754. switch (cmd->data_direction) {
  2755. case DMA_FROM_DEVICE:
  2756. ret = cmd->se_tfo->queue_data_in(cmd);
  2757. break;
  2758. case DMA_TO_DEVICE:
  2759. if (cmd->t_bidi_data_sg) {
  2760. ret = cmd->se_tfo->queue_data_in(cmd);
  2761. if (ret < 0)
  2762. break;
  2763. }
  2764. /* Fall through for DMA_TO_DEVICE */
  2765. case DMA_NONE:
  2766. ret = cmd->se_tfo->queue_status(cmd);
  2767. break;
  2768. default:
  2769. break;
  2770. }
  2771. out:
  2772. if (ret < 0) {
  2773. transport_handle_queue_full(cmd, cmd->se_dev);
  2774. return;
  2775. }
  2776. transport_lun_remove_cmd(cmd);
  2777. transport_cmd_check_stop_to_fabric(cmd);
  2778. }
  2779. static void transport_handle_queue_full(
  2780. struct se_cmd *cmd,
  2781. struct se_device *dev)
  2782. {
  2783. spin_lock_irq(&dev->qf_cmd_lock);
  2784. list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
  2785. atomic_inc(&dev->dev_qf_count);
  2786. smp_mb__after_atomic_inc();
  2787. spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
  2788. schedule_work(&cmd->se_dev->qf_work_queue);
  2789. }
  2790. static void target_complete_ok_work(struct work_struct *work)
  2791. {
  2792. struct se_cmd *cmd = container_of(work, struct se_cmd, work);
  2793. int reason = 0, ret;
  2794. /*
  2795. * Check if we need to move delayed/dormant tasks from cmds on the
  2796. * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
  2797. * Attribute.
  2798. */
  2799. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2800. transport_complete_task_attr(cmd);
  2801. /*
  2802. * Check to schedule QUEUE_FULL work, or execute an existing
  2803. * cmd->transport_qf_callback()
  2804. */
  2805. if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
  2806. schedule_work(&cmd->se_dev->qf_work_queue);
  2807. /*
  2808. * Check if we need to retrieve a sense buffer from
  2809. * the struct se_cmd in question.
  2810. */
  2811. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  2812. if (transport_get_sense_data(cmd) < 0)
  2813. reason = TCM_NON_EXISTENT_LUN;
  2814. if (cmd->scsi_status) {
  2815. ret = transport_send_check_condition_and_sense(
  2816. cmd, reason, 1);
  2817. if (ret == -EAGAIN || ret == -ENOMEM)
  2818. goto queue_full;
  2819. transport_lun_remove_cmd(cmd);
  2820. transport_cmd_check_stop_to_fabric(cmd);
  2821. return;
  2822. }
  2823. }
  2824. /*
  2825. * Check for a callback, used by amongst other things
  2826. * XDWRITE_READ_10 emulation.
  2827. */
  2828. if (cmd->transport_complete_callback)
  2829. cmd->transport_complete_callback(cmd);
  2830. switch (cmd->data_direction) {
  2831. case DMA_FROM_DEVICE:
  2832. spin_lock(&cmd->se_lun->lun_sep_lock);
  2833. if (cmd->se_lun->lun_sep) {
  2834. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  2835. cmd->data_length;
  2836. }
  2837. spin_unlock(&cmd->se_lun->lun_sep_lock);
  2838. ret = cmd->se_tfo->queue_data_in(cmd);
  2839. if (ret == -EAGAIN || ret == -ENOMEM)
  2840. goto queue_full;
  2841. break;
  2842. case DMA_TO_DEVICE:
  2843. spin_lock(&cmd->se_lun->lun_sep_lock);
  2844. if (cmd->se_lun->lun_sep) {
  2845. cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
  2846. cmd->data_length;
  2847. }
  2848. spin_unlock(&cmd->se_lun->lun_sep_lock);
  2849. /*
  2850. * Check if we need to send READ payload for BIDI-COMMAND
  2851. */
  2852. if (cmd->t_bidi_data_sg) {
  2853. spin_lock(&cmd->se_lun->lun_sep_lock);
  2854. if (cmd->se_lun->lun_sep) {
  2855. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  2856. cmd->data_length;
  2857. }
  2858. spin_unlock(&cmd->se_lun->lun_sep_lock);
  2859. ret = cmd->se_tfo->queue_data_in(cmd);
  2860. if (ret == -EAGAIN || ret == -ENOMEM)
  2861. goto queue_full;
  2862. break;
  2863. }
  2864. /* Fall through for DMA_TO_DEVICE */
  2865. case DMA_NONE:
  2866. ret = cmd->se_tfo->queue_status(cmd);
  2867. if (ret == -EAGAIN || ret == -ENOMEM)
  2868. goto queue_full;
  2869. break;
  2870. default:
  2871. break;
  2872. }
  2873. transport_lun_remove_cmd(cmd);
  2874. transport_cmd_check_stop_to_fabric(cmd);
  2875. return;
  2876. queue_full:
  2877. pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
  2878. " data_direction: %d\n", cmd, cmd->data_direction);
  2879. cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
  2880. transport_handle_queue_full(cmd, cmd->se_dev);
  2881. }
  2882. static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
  2883. {
  2884. struct scatterlist *sg;
  2885. int count;
  2886. for_each_sg(sgl, sg, nents, count)
  2887. __free_page(sg_page(sg));
  2888. kfree(sgl);
  2889. }
  2890. static inline void transport_free_pages(struct se_cmd *cmd)
  2891. {
  2892. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
  2893. return;
  2894. transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
  2895. cmd->t_data_sg = NULL;
  2896. cmd->t_data_nents = 0;
  2897. transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
  2898. cmd->t_bidi_data_sg = NULL;
  2899. cmd->t_bidi_data_nents = 0;
  2900. }
  2901. /**
  2902. * transport_release_cmd - free a command
  2903. * @cmd: command to free
  2904. *
  2905. * This routine unconditionally frees a command, and reference counting
  2906. * or list removal must be done in the caller.
  2907. */
  2908. static void transport_release_cmd(struct se_cmd *cmd)
  2909. {
  2910. BUG_ON(!cmd->se_tfo);
  2911. if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
  2912. core_tmr_release_req(cmd->se_tmr_req);
  2913. if (cmd->t_task_cdb != cmd->__t_task_cdb)
  2914. kfree(cmd->t_task_cdb);
  2915. /*
  2916. * If this cmd has been setup with target_get_sess_cmd(), drop
  2917. * the kref and call ->release_cmd() in kref callback.
  2918. */
  2919. if (cmd->check_release != 0) {
  2920. target_put_sess_cmd(cmd->se_sess, cmd);
  2921. return;
  2922. }
  2923. cmd->se_tfo->release_cmd(cmd);
  2924. }
  2925. /**
  2926. * transport_put_cmd - release a reference to a command
  2927. * @cmd: command to release
  2928. *
  2929. * This routine releases our reference to the command and frees it if possible.
  2930. */
  2931. static void transport_put_cmd(struct se_cmd *cmd)
  2932. {
  2933. unsigned long flags;
  2934. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2935. if (atomic_read(&cmd->t_fe_count)) {
  2936. if (!atomic_dec_and_test(&cmd->t_fe_count))
  2937. goto out_busy;
  2938. }
  2939. if (atomic_read(&cmd->t_se_count)) {
  2940. if (!atomic_dec_and_test(&cmd->t_se_count))
  2941. goto out_busy;
  2942. }
  2943. if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
  2944. cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
  2945. target_remove_from_state_list(cmd);
  2946. }
  2947. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2948. transport_free_pages(cmd);
  2949. transport_release_cmd(cmd);
  2950. return;
  2951. out_busy:
  2952. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2953. }
  2954. /*
  2955. * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
  2956. * allocating in the core.
  2957. * @cmd: Associated se_cmd descriptor
  2958. * @mem: SGL style memory for TCM WRITE / READ
  2959. * @sg_mem_num: Number of SGL elements
  2960. * @mem_bidi_in: SGL style memory for TCM BIDI READ
  2961. * @sg_mem_bidi_num: Number of BIDI READ SGL elements
  2962. *
  2963. * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
  2964. * of parameters.
  2965. */
  2966. int transport_generic_map_mem_to_cmd(
  2967. struct se_cmd *cmd,
  2968. struct scatterlist *sgl,
  2969. u32 sgl_count,
  2970. struct scatterlist *sgl_bidi,
  2971. u32 sgl_bidi_count)
  2972. {
  2973. if (!sgl || !sgl_count)
  2974. return 0;
  2975. if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
  2976. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
  2977. /*
  2978. * Reject SCSI data overflow with map_mem_to_cmd() as incoming
  2979. * scatterlists already have been set to follow what the fabric
  2980. * passes for the original expected data transfer length.
  2981. */
  2982. if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
  2983. pr_warn("Rejecting SCSI DATA overflow for fabric using"
  2984. " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
  2985. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2986. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  2987. return -EINVAL;
  2988. }
  2989. cmd->t_data_sg = sgl;
  2990. cmd->t_data_nents = sgl_count;
  2991. if (sgl_bidi && sgl_bidi_count) {
  2992. cmd->t_bidi_data_sg = sgl_bidi;
  2993. cmd->t_bidi_data_nents = sgl_bidi_count;
  2994. }
  2995. cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  2996. }
  2997. return 0;
  2998. }
  2999. EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
  3000. void *transport_kmap_data_sg(struct se_cmd *cmd)
  3001. {
  3002. struct scatterlist *sg = cmd->t_data_sg;
  3003. struct page **pages;
  3004. int i;
  3005. BUG_ON(!sg);
  3006. /*
  3007. * We need to take into account a possible offset here for fabrics like
  3008. * tcm_loop who may be using a contig buffer from the SCSI midlayer for
  3009. * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
  3010. */
  3011. if (!cmd->t_data_nents)
  3012. return NULL;
  3013. else if (cmd->t_data_nents == 1)
  3014. return kmap(sg_page(sg)) + sg->offset;
  3015. /* >1 page. use vmap */
  3016. pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
  3017. if (!pages)
  3018. return NULL;
  3019. /* convert sg[] to pages[] */
  3020. for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
  3021. pages[i] = sg_page(sg);
  3022. }
  3023. cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
  3024. kfree(pages);
  3025. if (!cmd->t_data_vmap)
  3026. return NULL;
  3027. return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
  3028. }
  3029. EXPORT_SYMBOL(transport_kmap_data_sg);
  3030. void transport_kunmap_data_sg(struct se_cmd *cmd)
  3031. {
  3032. if (!cmd->t_data_nents) {
  3033. return;
  3034. } else if (cmd->t_data_nents == 1) {
  3035. kunmap(sg_page(cmd->t_data_sg));
  3036. return;
  3037. }
  3038. vunmap(cmd->t_data_vmap);
  3039. cmd->t_data_vmap = NULL;
  3040. }
  3041. EXPORT_SYMBOL(transport_kunmap_data_sg);
  3042. static int
  3043. transport_generic_get_mem(struct se_cmd *cmd)
  3044. {
  3045. u32 length = cmd->data_length;
  3046. unsigned int nents;
  3047. struct page *page;
  3048. gfp_t zero_flag;
  3049. int i = 0;
  3050. nents = DIV_ROUND_UP(length, PAGE_SIZE);
  3051. cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
  3052. if (!cmd->t_data_sg)
  3053. return -ENOMEM;
  3054. cmd->t_data_nents = nents;
  3055. sg_init_table(cmd->t_data_sg, nents);
  3056. zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB ? 0 : __GFP_ZERO;
  3057. while (length) {
  3058. u32 page_len = min_t(u32, length, PAGE_SIZE);
  3059. page = alloc_page(GFP_KERNEL | zero_flag);
  3060. if (!page)
  3061. goto out;
  3062. sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
  3063. length -= page_len;
  3064. i++;
  3065. }
  3066. return 0;
  3067. out:
  3068. while (i >= 0) {
  3069. __free_page(sg_page(&cmd->t_data_sg[i]));
  3070. i--;
  3071. }
  3072. kfree(cmd->t_data_sg);
  3073. cmd->t_data_sg = NULL;
  3074. return -ENOMEM;
  3075. }
  3076. /*
  3077. * Allocate any required resources to execute the command. For writes we
  3078. * might not have the payload yet, so notify the fabric via a call to
  3079. * ->write_pending instead. Otherwise place it on the execution queue.
  3080. */
  3081. int transport_generic_new_cmd(struct se_cmd *cmd)
  3082. {
  3083. struct se_device *dev = cmd->se_dev;
  3084. int ret = 0;
  3085. /*
  3086. * Determine is the TCM fabric module has already allocated physical
  3087. * memory, and is directly calling transport_generic_map_mem_to_cmd()
  3088. * beforehand.
  3089. */
  3090. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
  3091. cmd->data_length) {
  3092. ret = transport_generic_get_mem(cmd);
  3093. if (ret < 0)
  3094. goto out_fail;
  3095. }
  3096. /* Workaround for handling zero-length control CDBs */
  3097. if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
  3098. !cmd->data_length) {
  3099. spin_lock_irq(&cmd->t_state_lock);
  3100. cmd->t_state = TRANSPORT_COMPLETE;
  3101. cmd->transport_state |= CMD_T_ACTIVE;
  3102. spin_unlock_irq(&cmd->t_state_lock);
  3103. if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
  3104. u8 ua_asc = 0, ua_ascq = 0;
  3105. core_scsi3_ua_clear_for_request_sense(cmd,
  3106. &ua_asc, &ua_ascq);
  3107. }
  3108. INIT_WORK(&cmd->work, target_complete_ok_work);
  3109. queue_work(target_completion_wq, &cmd->work);
  3110. return 0;
  3111. }
  3112. if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
  3113. struct se_dev_attrib *attr = &dev->se_sub_dev->se_dev_attrib;
  3114. if (transport_cmd_get_valid_sectors(cmd) < 0)
  3115. return -EINVAL;
  3116. BUG_ON(cmd->data_length % attr->block_size);
  3117. BUG_ON(DIV_ROUND_UP(cmd->data_length, attr->block_size) >
  3118. attr->max_sectors);
  3119. }
  3120. atomic_inc(&cmd->t_fe_count);
  3121. atomic_inc(&cmd->t_se_count);
  3122. atomic_set(&cmd->t_task_cdbs_ex_left, 1);
  3123. /*
  3124. * For WRITEs, let the fabric know its buffer is ready.
  3125. *
  3126. * The command will be added to the execution queue after its write
  3127. * data has arrived.
  3128. */
  3129. if (cmd->data_direction == DMA_TO_DEVICE) {
  3130. target_add_to_state_list(cmd);
  3131. return transport_generic_write_pending(cmd);
  3132. }
  3133. /*
  3134. * Everything else but a WRITE, add the command to the execution queue.
  3135. */
  3136. transport_execute_tasks(cmd);
  3137. return 0;
  3138. out_fail:
  3139. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3140. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3141. return -EINVAL;
  3142. }
  3143. EXPORT_SYMBOL(transport_generic_new_cmd);
  3144. /* transport_generic_process_write():
  3145. *
  3146. *
  3147. */
  3148. void transport_generic_process_write(struct se_cmd *cmd)
  3149. {
  3150. transport_execute_tasks(cmd);
  3151. }
  3152. EXPORT_SYMBOL(transport_generic_process_write);
  3153. static void transport_write_pending_qf(struct se_cmd *cmd)
  3154. {
  3155. int ret;
  3156. ret = cmd->se_tfo->write_pending(cmd);
  3157. if (ret == -EAGAIN || ret == -ENOMEM) {
  3158. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
  3159. cmd);
  3160. transport_handle_queue_full(cmd, cmd->se_dev);
  3161. }
  3162. }
  3163. static int transport_generic_write_pending(struct se_cmd *cmd)
  3164. {
  3165. unsigned long flags;
  3166. int ret;
  3167. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3168. cmd->t_state = TRANSPORT_WRITE_PENDING;
  3169. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3170. /*
  3171. * Clear the se_cmd for WRITE_PENDING status in order to set
  3172. * CMD_T_ACTIVE so that transport_generic_handle_data can be called
  3173. * from HW target mode interrupt code. This is safe to be called
  3174. * with transport_off=1 before the cmd->se_tfo->write_pending
  3175. * because the se_cmd->se_lun pointer is not being cleared.
  3176. */
  3177. transport_cmd_check_stop(cmd, 1, 0);
  3178. /*
  3179. * Call the fabric write_pending function here to let the
  3180. * frontend know that WRITE buffers are ready.
  3181. */
  3182. ret = cmd->se_tfo->write_pending(cmd);
  3183. if (ret == -EAGAIN || ret == -ENOMEM)
  3184. goto queue_full;
  3185. else if (ret < 0)
  3186. return ret;
  3187. return 1;
  3188. queue_full:
  3189. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
  3190. cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
  3191. transport_handle_queue_full(cmd, cmd->se_dev);
  3192. return 0;
  3193. }
  3194. void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
  3195. {
  3196. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
  3197. if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
  3198. transport_wait_for_tasks(cmd);
  3199. transport_release_cmd(cmd);
  3200. } else {
  3201. if (wait_for_tasks)
  3202. transport_wait_for_tasks(cmd);
  3203. core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
  3204. if (cmd->se_lun)
  3205. transport_lun_remove_cmd(cmd);
  3206. transport_put_cmd(cmd);
  3207. }
  3208. }
  3209. EXPORT_SYMBOL(transport_generic_free_cmd);
  3210. /* target_get_sess_cmd - Add command to active ->sess_cmd_list
  3211. * @se_sess: session to reference
  3212. * @se_cmd: command descriptor to add
  3213. * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
  3214. */
  3215. void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
  3216. bool ack_kref)
  3217. {
  3218. unsigned long flags;
  3219. kref_init(&se_cmd->cmd_kref);
  3220. /*
  3221. * Add a second kref if the fabric caller is expecting to handle
  3222. * fabric acknowledgement that requires two target_put_sess_cmd()
  3223. * invocations before se_cmd descriptor release.
  3224. */
  3225. if (ack_kref == true) {
  3226. kref_get(&se_cmd->cmd_kref);
  3227. se_cmd->se_cmd_flags |= SCF_ACK_KREF;
  3228. }
  3229. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3230. list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
  3231. se_cmd->check_release = 1;
  3232. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3233. }
  3234. EXPORT_SYMBOL(target_get_sess_cmd);
  3235. static void target_release_cmd_kref(struct kref *kref)
  3236. {
  3237. struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
  3238. struct se_session *se_sess = se_cmd->se_sess;
  3239. unsigned long flags;
  3240. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3241. if (list_empty(&se_cmd->se_cmd_list)) {
  3242. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3243. se_cmd->se_tfo->release_cmd(se_cmd);
  3244. return;
  3245. }
  3246. if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
  3247. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3248. complete(&se_cmd->cmd_wait_comp);
  3249. return;
  3250. }
  3251. list_del(&se_cmd->se_cmd_list);
  3252. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3253. se_cmd->se_tfo->release_cmd(se_cmd);
  3254. }
  3255. /* target_put_sess_cmd - Check for active I/O shutdown via kref_put
  3256. * @se_sess: session to reference
  3257. * @se_cmd: command descriptor to drop
  3258. */
  3259. int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
  3260. {
  3261. return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
  3262. }
  3263. EXPORT_SYMBOL(target_put_sess_cmd);
  3264. /* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
  3265. * @se_sess: session to split
  3266. */
  3267. void target_splice_sess_cmd_list(struct se_session *se_sess)
  3268. {
  3269. struct se_cmd *se_cmd;
  3270. unsigned long flags;
  3271. WARN_ON(!list_empty(&se_sess->sess_wait_list));
  3272. INIT_LIST_HEAD(&se_sess->sess_wait_list);
  3273. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3274. se_sess->sess_tearing_down = 1;
  3275. list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
  3276. list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
  3277. se_cmd->cmd_wait_set = 1;
  3278. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3279. }
  3280. EXPORT_SYMBOL(target_splice_sess_cmd_list);
  3281. /* target_wait_for_sess_cmds - Wait for outstanding descriptors
  3282. * @se_sess: session to wait for active I/O
  3283. * @wait_for_tasks: Make extra transport_wait_for_tasks call
  3284. */
  3285. void target_wait_for_sess_cmds(
  3286. struct se_session *se_sess,
  3287. int wait_for_tasks)
  3288. {
  3289. struct se_cmd *se_cmd, *tmp_cmd;
  3290. bool rc = false;
  3291. list_for_each_entry_safe(se_cmd, tmp_cmd,
  3292. &se_sess->sess_wait_list, se_cmd_list) {
  3293. list_del(&se_cmd->se_cmd_list);
  3294. pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
  3295. " %d\n", se_cmd, se_cmd->t_state,
  3296. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3297. if (wait_for_tasks) {
  3298. pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
  3299. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3300. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3301. rc = transport_wait_for_tasks(se_cmd);
  3302. pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
  3303. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3304. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3305. }
  3306. if (!rc) {
  3307. wait_for_completion(&se_cmd->cmd_wait_comp);
  3308. pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
  3309. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3310. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3311. }
  3312. se_cmd->se_tfo->release_cmd(se_cmd);
  3313. }
  3314. }
  3315. EXPORT_SYMBOL(target_wait_for_sess_cmds);
  3316. /* transport_lun_wait_for_tasks():
  3317. *
  3318. * Called from ConfigFS context to stop the passed struct se_cmd to allow
  3319. * an struct se_lun to be successfully shutdown.
  3320. */
  3321. static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
  3322. {
  3323. unsigned long flags;
  3324. int ret = 0;
  3325. /*
  3326. * If the frontend has already requested this struct se_cmd to
  3327. * be stopped, we can safely ignore this struct se_cmd.
  3328. */
  3329. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3330. if (cmd->transport_state & CMD_T_STOP) {
  3331. cmd->transport_state &= ~CMD_T_LUN_STOP;
  3332. pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
  3333. cmd->se_tfo->get_task_tag(cmd));
  3334. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3335. transport_cmd_check_stop(cmd, 1, 0);
  3336. return -EPERM;
  3337. }
  3338. cmd->transport_state |= CMD_T_LUN_FE_STOP;
  3339. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3340. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  3341. // XXX: audit task_flags checks.
  3342. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3343. if ((cmd->transport_state & CMD_T_BUSY) &&
  3344. (cmd->transport_state & CMD_T_SENT)) {
  3345. if (!target_stop_cmd(cmd, &flags))
  3346. ret++;
  3347. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3348. } else {
  3349. spin_unlock_irqrestore(&cmd->t_state_lock,
  3350. flags);
  3351. target_remove_from_execute_list(cmd);
  3352. }
  3353. pr_debug("ConfigFS: cmd: %p stop tasks ret:"
  3354. " %d\n", cmd, ret);
  3355. if (!ret) {
  3356. pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
  3357. cmd->se_tfo->get_task_tag(cmd));
  3358. wait_for_completion(&cmd->transport_lun_stop_comp);
  3359. pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
  3360. cmd->se_tfo->get_task_tag(cmd));
  3361. }
  3362. transport_remove_cmd_from_queue(cmd);
  3363. return 0;
  3364. }
  3365. static void __transport_clear_lun_from_sessions(struct se_lun *lun)
  3366. {
  3367. struct se_cmd *cmd = NULL;
  3368. unsigned long lun_flags, cmd_flags;
  3369. /*
  3370. * Do exception processing and return CHECK_CONDITION status to the
  3371. * Initiator Port.
  3372. */
  3373. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3374. while (!list_empty(&lun->lun_cmd_list)) {
  3375. cmd = list_first_entry(&lun->lun_cmd_list,
  3376. struct se_cmd, se_lun_node);
  3377. list_del_init(&cmd->se_lun_node);
  3378. /*
  3379. * This will notify iscsi_target_transport.c:
  3380. * transport_cmd_check_stop() that a LUN shutdown is in
  3381. * progress for the iscsi_cmd_t.
  3382. */
  3383. spin_lock(&cmd->t_state_lock);
  3384. pr_debug("SE_LUN[%d] - Setting cmd->transport"
  3385. "_lun_stop for ITT: 0x%08x\n",
  3386. cmd->se_lun->unpacked_lun,
  3387. cmd->se_tfo->get_task_tag(cmd));
  3388. cmd->transport_state |= CMD_T_LUN_STOP;
  3389. spin_unlock(&cmd->t_state_lock);
  3390. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  3391. if (!cmd->se_lun) {
  3392. pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
  3393. cmd->se_tfo->get_task_tag(cmd),
  3394. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
  3395. BUG();
  3396. }
  3397. /*
  3398. * If the Storage engine still owns the iscsi_cmd_t, determine
  3399. * and/or stop its context.
  3400. */
  3401. pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
  3402. "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
  3403. cmd->se_tfo->get_task_tag(cmd));
  3404. if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
  3405. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3406. continue;
  3407. }
  3408. pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
  3409. "_wait_for_tasks(): SUCCESS\n",
  3410. cmd->se_lun->unpacked_lun,
  3411. cmd->se_tfo->get_task_tag(cmd));
  3412. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  3413. if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
  3414. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3415. goto check_cond;
  3416. }
  3417. cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
  3418. target_remove_from_state_list(cmd);
  3419. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3420. /*
  3421. * The Storage engine stopped this struct se_cmd before it was
  3422. * send to the fabric frontend for delivery back to the
  3423. * Initiator Node. Return this SCSI CDB back with an
  3424. * CHECK_CONDITION status.
  3425. */
  3426. check_cond:
  3427. transport_send_check_condition_and_sense(cmd,
  3428. TCM_NON_EXISTENT_LUN, 0);
  3429. /*
  3430. * If the fabric frontend is waiting for this iscsi_cmd_t to
  3431. * be released, notify the waiting thread now that LU has
  3432. * finished accessing it.
  3433. */
  3434. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  3435. if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
  3436. pr_debug("SE_LUN[%d] - Detected FE stop for"
  3437. " struct se_cmd: %p ITT: 0x%08x\n",
  3438. lun->unpacked_lun,
  3439. cmd, cmd->se_tfo->get_task_tag(cmd));
  3440. spin_unlock_irqrestore(&cmd->t_state_lock,
  3441. cmd_flags);
  3442. transport_cmd_check_stop(cmd, 1, 0);
  3443. complete(&cmd->transport_lun_fe_stop_comp);
  3444. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3445. continue;
  3446. }
  3447. pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
  3448. lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
  3449. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3450. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3451. }
  3452. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  3453. }
  3454. static int transport_clear_lun_thread(void *p)
  3455. {
  3456. struct se_lun *lun = p;
  3457. __transport_clear_lun_from_sessions(lun);
  3458. complete(&lun->lun_shutdown_comp);
  3459. return 0;
  3460. }
  3461. int transport_clear_lun_from_sessions(struct se_lun *lun)
  3462. {
  3463. struct task_struct *kt;
  3464. kt = kthread_run(transport_clear_lun_thread, lun,
  3465. "tcm_cl_%u", lun->unpacked_lun);
  3466. if (IS_ERR(kt)) {
  3467. pr_err("Unable to start clear_lun thread\n");
  3468. return PTR_ERR(kt);
  3469. }
  3470. wait_for_completion(&lun->lun_shutdown_comp);
  3471. return 0;
  3472. }
  3473. /**
  3474. * transport_wait_for_tasks - wait for completion to occur
  3475. * @cmd: command to wait
  3476. *
  3477. * Called from frontend fabric context to wait for storage engine
  3478. * to pause and/or release frontend generated struct se_cmd.
  3479. */
  3480. bool transport_wait_for_tasks(struct se_cmd *cmd)
  3481. {
  3482. unsigned long flags;
  3483. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3484. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
  3485. !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
  3486. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3487. return false;
  3488. }
  3489. /*
  3490. * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
  3491. * has been set in transport_set_supported_SAM_opcode().
  3492. */
  3493. if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
  3494. !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
  3495. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3496. return false;
  3497. }
  3498. /*
  3499. * If we are already stopped due to an external event (ie: LUN shutdown)
  3500. * sleep until the connection can have the passed struct se_cmd back.
  3501. * The cmd->transport_lun_stopped_sem will be upped by
  3502. * transport_clear_lun_from_sessions() once the ConfigFS context caller
  3503. * has completed its operation on the struct se_cmd.
  3504. */
  3505. if (cmd->transport_state & CMD_T_LUN_STOP) {
  3506. pr_debug("wait_for_tasks: Stopping"
  3507. " wait_for_completion(&cmd->t_tasktransport_lun_fe"
  3508. "_stop_comp); for ITT: 0x%08x\n",
  3509. cmd->se_tfo->get_task_tag(cmd));
  3510. /*
  3511. * There is a special case for WRITES where a FE exception +
  3512. * LUN shutdown means ConfigFS context is still sleeping on
  3513. * transport_lun_stop_comp in transport_lun_wait_for_tasks().
  3514. * We go ahead and up transport_lun_stop_comp just to be sure
  3515. * here.
  3516. */
  3517. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3518. complete(&cmd->transport_lun_stop_comp);
  3519. wait_for_completion(&cmd->transport_lun_fe_stop_comp);
  3520. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3521. target_remove_from_state_list(cmd);
  3522. /*
  3523. * At this point, the frontend who was the originator of this
  3524. * struct se_cmd, now owns the structure and can be released through
  3525. * normal means below.
  3526. */
  3527. pr_debug("wait_for_tasks: Stopped"
  3528. " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
  3529. "stop_comp); for ITT: 0x%08x\n",
  3530. cmd->se_tfo->get_task_tag(cmd));
  3531. cmd->transport_state &= ~CMD_T_LUN_STOP;
  3532. }
  3533. if (!(cmd->transport_state & CMD_T_ACTIVE)) {
  3534. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3535. return false;
  3536. }
  3537. cmd->transport_state |= CMD_T_STOP;
  3538. pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
  3539. " i_state: %d, t_state: %d, CMD_T_STOP\n",
  3540. cmd, cmd->se_tfo->get_task_tag(cmd),
  3541. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
  3542. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3543. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  3544. wait_for_completion(&cmd->t_transport_stop_comp);
  3545. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3546. cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
  3547. pr_debug("wait_for_tasks: Stopped wait_for_compltion("
  3548. "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
  3549. cmd->se_tfo->get_task_tag(cmd));
  3550. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3551. return true;
  3552. }
  3553. EXPORT_SYMBOL(transport_wait_for_tasks);
  3554. static int transport_get_sense_codes(
  3555. struct se_cmd *cmd,
  3556. u8 *asc,
  3557. u8 *ascq)
  3558. {
  3559. *asc = cmd->scsi_asc;
  3560. *ascq = cmd->scsi_ascq;
  3561. return 0;
  3562. }
  3563. static int transport_set_sense_codes(
  3564. struct se_cmd *cmd,
  3565. u8 asc,
  3566. u8 ascq)
  3567. {
  3568. cmd->scsi_asc = asc;
  3569. cmd->scsi_ascq = ascq;
  3570. return 0;
  3571. }
  3572. int transport_send_check_condition_and_sense(
  3573. struct se_cmd *cmd,
  3574. u8 reason,
  3575. int from_transport)
  3576. {
  3577. unsigned char *buffer = cmd->sense_buffer;
  3578. unsigned long flags;
  3579. int offset;
  3580. u8 asc = 0, ascq = 0;
  3581. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3582. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  3583. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3584. return 0;
  3585. }
  3586. cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
  3587. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3588. if (!reason && from_transport)
  3589. goto after_reason;
  3590. if (!from_transport)
  3591. cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
  3592. /*
  3593. * Data Segment and SenseLength of the fabric response PDU.
  3594. *
  3595. * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
  3596. * from include/scsi/scsi_cmnd.h
  3597. */
  3598. offset = cmd->se_tfo->set_fabric_sense_len(cmd,
  3599. TRANSPORT_SENSE_BUFFER);
  3600. /*
  3601. * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
  3602. * SENSE KEY values from include/scsi/scsi.h
  3603. */
  3604. switch (reason) {
  3605. case TCM_NON_EXISTENT_LUN:
  3606. /* CURRENT ERROR */
  3607. buffer[offset] = 0x70;
  3608. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3609. /* ILLEGAL REQUEST */
  3610. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3611. /* LOGICAL UNIT NOT SUPPORTED */
  3612. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
  3613. break;
  3614. case TCM_UNSUPPORTED_SCSI_OPCODE:
  3615. case TCM_SECTOR_COUNT_TOO_MANY:
  3616. /* CURRENT ERROR */
  3617. buffer[offset] = 0x70;
  3618. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3619. /* ILLEGAL REQUEST */
  3620. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3621. /* INVALID COMMAND OPERATION CODE */
  3622. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
  3623. break;
  3624. case TCM_UNKNOWN_MODE_PAGE:
  3625. /* CURRENT ERROR */
  3626. buffer[offset] = 0x70;
  3627. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3628. /* ILLEGAL REQUEST */
  3629. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3630. /* INVALID FIELD IN CDB */
  3631. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  3632. break;
  3633. case TCM_CHECK_CONDITION_ABORT_CMD:
  3634. /* CURRENT ERROR */
  3635. buffer[offset] = 0x70;
  3636. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3637. /* ABORTED COMMAND */
  3638. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3639. /* BUS DEVICE RESET FUNCTION OCCURRED */
  3640. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
  3641. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
  3642. break;
  3643. case TCM_INCORRECT_AMOUNT_OF_DATA:
  3644. /* CURRENT ERROR */
  3645. buffer[offset] = 0x70;
  3646. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3647. /* ABORTED COMMAND */
  3648. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3649. /* WRITE ERROR */
  3650. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  3651. /* NOT ENOUGH UNSOLICITED DATA */
  3652. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
  3653. break;
  3654. case TCM_INVALID_CDB_FIELD:
  3655. /* CURRENT ERROR */
  3656. buffer[offset] = 0x70;
  3657. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3658. /* ILLEGAL REQUEST */
  3659. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3660. /* INVALID FIELD IN CDB */
  3661. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  3662. break;
  3663. case TCM_INVALID_PARAMETER_LIST:
  3664. /* CURRENT ERROR */
  3665. buffer[offset] = 0x70;
  3666. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3667. /* ILLEGAL REQUEST */
  3668. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3669. /* INVALID FIELD IN PARAMETER LIST */
  3670. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
  3671. break;
  3672. case TCM_UNEXPECTED_UNSOLICITED_DATA:
  3673. /* CURRENT ERROR */
  3674. buffer[offset] = 0x70;
  3675. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3676. /* ABORTED COMMAND */
  3677. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3678. /* WRITE ERROR */
  3679. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  3680. /* UNEXPECTED_UNSOLICITED_DATA */
  3681. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
  3682. break;
  3683. case TCM_SERVICE_CRC_ERROR:
  3684. /* CURRENT ERROR */
  3685. buffer[offset] = 0x70;
  3686. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3687. /* ABORTED COMMAND */
  3688. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3689. /* PROTOCOL SERVICE CRC ERROR */
  3690. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
  3691. /* N/A */
  3692. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
  3693. break;
  3694. case TCM_SNACK_REJECTED:
  3695. /* CURRENT ERROR */
  3696. buffer[offset] = 0x70;
  3697. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3698. /* ABORTED COMMAND */
  3699. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3700. /* READ ERROR */
  3701. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
  3702. /* FAILED RETRANSMISSION REQUEST */
  3703. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
  3704. break;
  3705. case TCM_WRITE_PROTECTED:
  3706. /* CURRENT ERROR */
  3707. buffer[offset] = 0x70;
  3708. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3709. /* DATA PROTECT */
  3710. buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
  3711. /* WRITE PROTECTED */
  3712. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
  3713. break;
  3714. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  3715. /* CURRENT ERROR */
  3716. buffer[offset] = 0x70;
  3717. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3718. /* UNIT ATTENTION */
  3719. buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  3720. core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
  3721. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  3722. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  3723. break;
  3724. case TCM_CHECK_CONDITION_NOT_READY:
  3725. /* CURRENT ERROR */
  3726. buffer[offset] = 0x70;
  3727. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3728. /* Not Ready */
  3729. buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
  3730. transport_get_sense_codes(cmd, &asc, &ascq);
  3731. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  3732. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  3733. break;
  3734. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  3735. default:
  3736. /* CURRENT ERROR */
  3737. buffer[offset] = 0x70;
  3738. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3739. /* ILLEGAL REQUEST */
  3740. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3741. /* LOGICAL UNIT COMMUNICATION FAILURE */
  3742. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
  3743. break;
  3744. }
  3745. /*
  3746. * This code uses linux/include/scsi/scsi.h SAM status codes!
  3747. */
  3748. cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
  3749. /*
  3750. * Automatically padded, this value is encoded in the fabric's
  3751. * data_length response PDU containing the SCSI defined sense data.
  3752. */
  3753. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
  3754. after_reason:
  3755. return cmd->se_tfo->queue_status(cmd);
  3756. }
  3757. EXPORT_SYMBOL(transport_send_check_condition_and_sense);
  3758. int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
  3759. {
  3760. int ret = 0;
  3761. if (cmd->transport_state & CMD_T_ABORTED) {
  3762. if (!send_status ||
  3763. (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
  3764. return 1;
  3765. pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
  3766. " status for CDB: 0x%02x ITT: 0x%08x\n",
  3767. cmd->t_task_cdb[0],
  3768. cmd->se_tfo->get_task_tag(cmd));
  3769. cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
  3770. cmd->se_tfo->queue_status(cmd);
  3771. ret = 1;
  3772. }
  3773. return ret;
  3774. }
  3775. EXPORT_SYMBOL(transport_check_aborted_status);
  3776. void transport_send_task_abort(struct se_cmd *cmd)
  3777. {
  3778. unsigned long flags;
  3779. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3780. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  3781. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3782. return;
  3783. }
  3784. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3785. /*
  3786. * If there are still expected incoming fabric WRITEs, we wait
  3787. * until until they have completed before sending a TASK_ABORTED
  3788. * response. This response with TASK_ABORTED status will be
  3789. * queued back to fabric module by transport_check_aborted_status().
  3790. */
  3791. if (cmd->data_direction == DMA_TO_DEVICE) {
  3792. if (cmd->se_tfo->write_pending_status(cmd) != 0) {
  3793. cmd->transport_state |= CMD_T_ABORTED;
  3794. smp_mb__after_atomic_inc();
  3795. }
  3796. }
  3797. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  3798. pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
  3799. " ITT: 0x%08x\n", cmd->t_task_cdb[0],
  3800. cmd->se_tfo->get_task_tag(cmd));
  3801. cmd->se_tfo->queue_status(cmd);
  3802. }
  3803. static int transport_generic_do_tmr(struct se_cmd *cmd)
  3804. {
  3805. struct se_device *dev = cmd->se_dev;
  3806. struct se_tmr_req *tmr = cmd->se_tmr_req;
  3807. int ret;
  3808. switch (tmr->function) {
  3809. case TMR_ABORT_TASK:
  3810. core_tmr_abort_task(dev, tmr, cmd->se_sess);
  3811. break;
  3812. case TMR_ABORT_TASK_SET:
  3813. case TMR_CLEAR_ACA:
  3814. case TMR_CLEAR_TASK_SET:
  3815. tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  3816. break;
  3817. case TMR_LUN_RESET:
  3818. ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
  3819. tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
  3820. TMR_FUNCTION_REJECTED;
  3821. break;
  3822. case TMR_TARGET_WARM_RESET:
  3823. tmr->response = TMR_FUNCTION_REJECTED;
  3824. break;
  3825. case TMR_TARGET_COLD_RESET:
  3826. tmr->response = TMR_FUNCTION_REJECTED;
  3827. break;
  3828. default:
  3829. pr_err("Uknown TMR function: 0x%02x.\n",
  3830. tmr->function);
  3831. tmr->response = TMR_FUNCTION_REJECTED;
  3832. break;
  3833. }
  3834. cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
  3835. cmd->se_tfo->queue_tm_rsp(cmd);
  3836. transport_cmd_check_stop_to_fabric(cmd);
  3837. return 0;
  3838. }
  3839. /* transport_processing_thread():
  3840. *
  3841. *
  3842. */
  3843. static int transport_processing_thread(void *param)
  3844. {
  3845. int ret;
  3846. struct se_cmd *cmd;
  3847. struct se_device *dev = param;
  3848. while (!kthread_should_stop()) {
  3849. ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
  3850. atomic_read(&dev->dev_queue_obj.queue_cnt) ||
  3851. kthread_should_stop());
  3852. if (ret < 0)
  3853. goto out;
  3854. get_cmd:
  3855. cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
  3856. if (!cmd)
  3857. continue;
  3858. switch (cmd->t_state) {
  3859. case TRANSPORT_NEW_CMD:
  3860. BUG();
  3861. break;
  3862. case TRANSPORT_NEW_CMD_MAP:
  3863. if (!cmd->se_tfo->new_cmd_map) {
  3864. pr_err("cmd->se_tfo->new_cmd_map is"
  3865. " NULL for TRANSPORT_NEW_CMD_MAP\n");
  3866. BUG();
  3867. }
  3868. ret = cmd->se_tfo->new_cmd_map(cmd);
  3869. if (ret < 0) {
  3870. transport_generic_request_failure(cmd);
  3871. break;
  3872. }
  3873. ret = transport_generic_new_cmd(cmd);
  3874. if (ret < 0) {
  3875. transport_generic_request_failure(cmd);
  3876. break;
  3877. }
  3878. break;
  3879. case TRANSPORT_PROCESS_WRITE:
  3880. transport_generic_process_write(cmd);
  3881. break;
  3882. case TRANSPORT_PROCESS_TMR:
  3883. transport_generic_do_tmr(cmd);
  3884. break;
  3885. case TRANSPORT_COMPLETE_QF_WP:
  3886. transport_write_pending_qf(cmd);
  3887. break;
  3888. case TRANSPORT_COMPLETE_QF_OK:
  3889. transport_complete_qf(cmd);
  3890. break;
  3891. default:
  3892. pr_err("Unknown t_state: %d for ITT: 0x%08x "
  3893. "i_state: %d on SE LUN: %u\n",
  3894. cmd->t_state,
  3895. cmd->se_tfo->get_task_tag(cmd),
  3896. cmd->se_tfo->get_cmd_state(cmd),
  3897. cmd->se_lun->unpacked_lun);
  3898. BUG();
  3899. }
  3900. goto get_cmd;
  3901. }
  3902. out:
  3903. WARN_ON(!list_empty(&dev->state_list));
  3904. WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
  3905. dev->process_thread = NULL;
  3906. return 0;
  3907. }