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