target_core_transport.c 119 KB

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