target_core_transport.c 130 KB

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