target_core_transport.c 129 KB

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