target_core_transport.c 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763
  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. char buf[17];
  1108. int i, device_type;
  1109. /*
  1110. * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
  1111. */
  1112. for (i = 0; i < 8; i++)
  1113. if (wwn->vendor[i] >= 0x20)
  1114. buf[i] = wwn->vendor[i];
  1115. else
  1116. buf[i] = ' ';
  1117. buf[i] = '\0';
  1118. pr_debug(" Vendor: %s\n", buf);
  1119. for (i = 0; i < 16; i++)
  1120. if (wwn->model[i] >= 0x20)
  1121. buf[i] = wwn->model[i];
  1122. else
  1123. buf[i] = ' ';
  1124. buf[i] = '\0';
  1125. pr_debug(" Model: %s\n", buf);
  1126. for (i = 0; i < 4; i++)
  1127. if (wwn->revision[i] >= 0x20)
  1128. buf[i] = wwn->revision[i];
  1129. else
  1130. buf[i] = ' ';
  1131. buf[i] = '\0';
  1132. pr_debug(" Revision: %s\n", buf);
  1133. device_type = dev->transport->get_device_type(dev);
  1134. pr_debug(" Type: %s ", scsi_device_type(device_type));
  1135. pr_debug(" ANSI SCSI revision: %02x\n",
  1136. dev->transport->get_device_rev(dev));
  1137. }
  1138. struct se_device *transport_add_device_to_core_hba(
  1139. struct se_hba *hba,
  1140. struct se_subsystem_api *transport,
  1141. struct se_subsystem_dev *se_dev,
  1142. u32 device_flags,
  1143. void *transport_dev,
  1144. struct se_dev_limits *dev_limits,
  1145. const char *inquiry_prod,
  1146. const char *inquiry_rev)
  1147. {
  1148. int force_pt;
  1149. struct se_device *dev;
  1150. dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
  1151. if (!dev) {
  1152. pr_err("Unable to allocate memory for se_dev_t\n");
  1153. return NULL;
  1154. }
  1155. transport_init_queue_obj(&dev->dev_queue_obj);
  1156. dev->dev_flags = device_flags;
  1157. dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
  1158. dev->dev_ptr = transport_dev;
  1159. dev->se_hba = hba;
  1160. dev->se_sub_dev = se_dev;
  1161. dev->transport = transport;
  1162. INIT_LIST_HEAD(&dev->dev_list);
  1163. INIT_LIST_HEAD(&dev->dev_sep_list);
  1164. INIT_LIST_HEAD(&dev->dev_tmr_list);
  1165. INIT_LIST_HEAD(&dev->execute_task_list);
  1166. INIT_LIST_HEAD(&dev->delayed_cmd_list);
  1167. INIT_LIST_HEAD(&dev->state_task_list);
  1168. INIT_LIST_HEAD(&dev->qf_cmd_list);
  1169. spin_lock_init(&dev->execute_task_lock);
  1170. spin_lock_init(&dev->delayed_cmd_lock);
  1171. spin_lock_init(&dev->dev_reservation_lock);
  1172. spin_lock_init(&dev->dev_status_lock);
  1173. spin_lock_init(&dev->se_port_lock);
  1174. spin_lock_init(&dev->se_tmr_lock);
  1175. spin_lock_init(&dev->qf_cmd_lock);
  1176. atomic_set(&dev->dev_ordered_id, 0);
  1177. se_dev_set_default_attribs(dev, dev_limits);
  1178. dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
  1179. dev->creation_time = get_jiffies_64();
  1180. spin_lock_init(&dev->stats_lock);
  1181. spin_lock(&hba->device_lock);
  1182. list_add_tail(&dev->dev_list, &hba->hba_dev_list);
  1183. hba->dev_count++;
  1184. spin_unlock(&hba->device_lock);
  1185. /*
  1186. * Setup the SAM Task Attribute emulation for struct se_device
  1187. */
  1188. core_setup_task_attr_emulation(dev);
  1189. /*
  1190. * Force PR and ALUA passthrough emulation with internal object use.
  1191. */
  1192. force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
  1193. /*
  1194. * Setup the Reservations infrastructure for struct se_device
  1195. */
  1196. core_setup_reservations(dev, force_pt);
  1197. /*
  1198. * Setup the Asymmetric Logical Unit Assignment for struct se_device
  1199. */
  1200. if (core_setup_alua(dev, force_pt) < 0)
  1201. goto out;
  1202. /*
  1203. * Startup the struct se_device processing thread
  1204. */
  1205. dev->process_thread = kthread_run(transport_processing_thread, dev,
  1206. "LIO_%s", dev->transport->name);
  1207. if (IS_ERR(dev->process_thread)) {
  1208. pr_err("Unable to create kthread: LIO_%s\n",
  1209. dev->transport->name);
  1210. goto out;
  1211. }
  1212. /*
  1213. * Setup work_queue for QUEUE_FULL
  1214. */
  1215. INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
  1216. /*
  1217. * Preload the initial INQUIRY const values if we are doing
  1218. * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
  1219. * passthrough because this is being provided by the backend LLD.
  1220. * This is required so that transport_get_inquiry() copies these
  1221. * originals once back into DEV_T10_WWN(dev) for the virtual device
  1222. * setup.
  1223. */
  1224. if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
  1225. if (!inquiry_prod || !inquiry_rev) {
  1226. pr_err("All non TCM/pSCSI plugins require"
  1227. " INQUIRY consts\n");
  1228. goto out;
  1229. }
  1230. strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
  1231. strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
  1232. strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
  1233. }
  1234. scsi_dump_inquiry(dev);
  1235. return dev;
  1236. out:
  1237. kthread_stop(dev->process_thread);
  1238. spin_lock(&hba->device_lock);
  1239. list_del(&dev->dev_list);
  1240. hba->dev_count--;
  1241. spin_unlock(&hba->device_lock);
  1242. se_release_vpd_for_dev(dev);
  1243. kfree(dev);
  1244. return NULL;
  1245. }
  1246. EXPORT_SYMBOL(transport_add_device_to_core_hba);
  1247. /* transport_generic_prepare_cdb():
  1248. *
  1249. * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
  1250. * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
  1251. * The point of this is since we are mapping iSCSI LUNs to
  1252. * SCSI Target IDs having a non-zero LUN in the CDB will throw the
  1253. * devices and HBAs for a loop.
  1254. */
  1255. static inline void transport_generic_prepare_cdb(
  1256. unsigned char *cdb)
  1257. {
  1258. switch (cdb[0]) {
  1259. case READ_10: /* SBC - RDProtect */
  1260. case READ_12: /* SBC - RDProtect */
  1261. case READ_16: /* SBC - RDProtect */
  1262. case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
  1263. case VERIFY: /* SBC - VRProtect */
  1264. case VERIFY_16: /* SBC - VRProtect */
  1265. case WRITE_VERIFY: /* SBC - VRProtect */
  1266. case WRITE_VERIFY_12: /* SBC - VRProtect */
  1267. break;
  1268. default:
  1269. cdb[1] &= 0x1f; /* clear logical unit number */
  1270. break;
  1271. }
  1272. }
  1273. static struct se_task *
  1274. transport_generic_get_task(struct se_cmd *cmd,
  1275. enum dma_data_direction data_direction)
  1276. {
  1277. struct se_task *task;
  1278. struct se_device *dev = cmd->se_dev;
  1279. task = dev->transport->alloc_task(cmd->t_task_cdb);
  1280. if (!task) {
  1281. pr_err("Unable to allocate struct se_task\n");
  1282. return NULL;
  1283. }
  1284. INIT_LIST_HEAD(&task->t_list);
  1285. INIT_LIST_HEAD(&task->t_execute_list);
  1286. INIT_LIST_HEAD(&task->t_state_list);
  1287. init_completion(&task->task_stop_comp);
  1288. task->task_se_cmd = cmd;
  1289. task->task_data_direction = data_direction;
  1290. return task;
  1291. }
  1292. static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
  1293. /*
  1294. * Used by fabric modules containing a local struct se_cmd within their
  1295. * fabric dependent per I/O descriptor.
  1296. */
  1297. void transport_init_se_cmd(
  1298. struct se_cmd *cmd,
  1299. struct target_core_fabric_ops *tfo,
  1300. struct se_session *se_sess,
  1301. u32 data_length,
  1302. int data_direction,
  1303. int task_attr,
  1304. unsigned char *sense_buffer)
  1305. {
  1306. INIT_LIST_HEAD(&cmd->se_lun_node);
  1307. INIT_LIST_HEAD(&cmd->se_delayed_node);
  1308. INIT_LIST_HEAD(&cmd->se_qf_node);
  1309. INIT_LIST_HEAD(&cmd->se_queue_node);
  1310. INIT_LIST_HEAD(&cmd->se_cmd_list);
  1311. INIT_LIST_HEAD(&cmd->t_task_list);
  1312. init_completion(&cmd->transport_lun_fe_stop_comp);
  1313. init_completion(&cmd->transport_lun_stop_comp);
  1314. init_completion(&cmd->t_transport_stop_comp);
  1315. init_completion(&cmd->cmd_wait_comp);
  1316. spin_lock_init(&cmd->t_state_lock);
  1317. atomic_set(&cmd->transport_dev_active, 1);
  1318. cmd->se_tfo = tfo;
  1319. cmd->se_sess = se_sess;
  1320. cmd->data_length = data_length;
  1321. cmd->data_direction = data_direction;
  1322. cmd->sam_task_attr = task_attr;
  1323. cmd->sense_buffer = sense_buffer;
  1324. }
  1325. EXPORT_SYMBOL(transport_init_se_cmd);
  1326. static int transport_check_alloc_task_attr(struct se_cmd *cmd)
  1327. {
  1328. /*
  1329. * Check if SAM Task Attribute emulation is enabled for this
  1330. * struct se_device storage object
  1331. */
  1332. if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  1333. return 0;
  1334. if (cmd->sam_task_attr == MSG_ACA_TAG) {
  1335. pr_debug("SAM Task Attribute ACA"
  1336. " emulation is not supported\n");
  1337. return -EINVAL;
  1338. }
  1339. /*
  1340. * Used to determine when ORDERED commands should go from
  1341. * Dormant to Active status.
  1342. */
  1343. cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
  1344. smp_mb__after_atomic_inc();
  1345. pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
  1346. cmd->se_ordered_id, cmd->sam_task_attr,
  1347. cmd->se_dev->transport->name);
  1348. return 0;
  1349. }
  1350. /* transport_generic_allocate_tasks():
  1351. *
  1352. * Called from fabric RX Thread.
  1353. */
  1354. int transport_generic_allocate_tasks(
  1355. struct se_cmd *cmd,
  1356. unsigned char *cdb)
  1357. {
  1358. int ret;
  1359. transport_generic_prepare_cdb(cdb);
  1360. /*
  1361. * Ensure that the received CDB is less than the max (252 + 8) bytes
  1362. * for VARIABLE_LENGTH_CMD
  1363. */
  1364. if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
  1365. pr_err("Received SCSI CDB with command_size: %d that"
  1366. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  1367. scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
  1368. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1369. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1370. return -EINVAL;
  1371. }
  1372. /*
  1373. * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
  1374. * allocate the additional extended CDB buffer now.. Otherwise
  1375. * setup the pointer from __t_task_cdb to t_task_cdb.
  1376. */
  1377. if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
  1378. cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
  1379. GFP_KERNEL);
  1380. if (!cmd->t_task_cdb) {
  1381. pr_err("Unable to allocate cmd->t_task_cdb"
  1382. " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
  1383. scsi_command_size(cdb),
  1384. (unsigned long)sizeof(cmd->__t_task_cdb));
  1385. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1386. cmd->scsi_sense_reason =
  1387. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1388. return -ENOMEM;
  1389. }
  1390. } else
  1391. cmd->t_task_cdb = &cmd->__t_task_cdb[0];
  1392. /*
  1393. * Copy the original CDB into cmd->
  1394. */
  1395. memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
  1396. /*
  1397. * Setup the received CDB based on SCSI defined opcodes and
  1398. * perform unit attention, persistent reservations and ALUA
  1399. * checks for virtual device backends. The cmd->t_task_cdb
  1400. * pointer is expected to be setup before we reach this point.
  1401. */
  1402. ret = transport_generic_cmd_sequencer(cmd, cdb);
  1403. if (ret < 0)
  1404. return ret;
  1405. /*
  1406. * Check for SAM Task Attribute Emulation
  1407. */
  1408. if (transport_check_alloc_task_attr(cmd) < 0) {
  1409. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1410. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1411. return -EINVAL;
  1412. }
  1413. spin_lock(&cmd->se_lun->lun_sep_lock);
  1414. if (cmd->se_lun->lun_sep)
  1415. cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
  1416. spin_unlock(&cmd->se_lun->lun_sep_lock);
  1417. return 0;
  1418. }
  1419. EXPORT_SYMBOL(transport_generic_allocate_tasks);
  1420. /*
  1421. * Used by fabric module frontends to queue tasks directly.
  1422. * Many only be used from process context only
  1423. */
  1424. int transport_handle_cdb_direct(
  1425. struct se_cmd *cmd)
  1426. {
  1427. int ret;
  1428. if (!cmd->se_lun) {
  1429. dump_stack();
  1430. pr_err("cmd->se_lun is NULL\n");
  1431. return -EINVAL;
  1432. }
  1433. if (in_interrupt()) {
  1434. dump_stack();
  1435. pr_err("transport_generic_handle_cdb cannot be called"
  1436. " from interrupt context\n");
  1437. return -EINVAL;
  1438. }
  1439. /*
  1440. * Set TRANSPORT_NEW_CMD state and cmd->t_transport_active=1 following
  1441. * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
  1442. * in existing usage to ensure that outstanding descriptors are handled
  1443. * correctly during shutdown via transport_wait_for_tasks()
  1444. *
  1445. * Also, we don't take cmd->t_state_lock here as we only expect
  1446. * this to be called for initial descriptor submission.
  1447. */
  1448. cmd->t_state = TRANSPORT_NEW_CMD;
  1449. atomic_set(&cmd->t_transport_active, 1);
  1450. /*
  1451. * transport_generic_new_cmd() is already handling QUEUE_FULL,
  1452. * so follow TRANSPORT_NEW_CMD processing thread context usage
  1453. * and call transport_generic_request_failure() if necessary..
  1454. */
  1455. ret = transport_generic_new_cmd(cmd);
  1456. if (ret < 0)
  1457. transport_generic_request_failure(cmd);
  1458. return 0;
  1459. }
  1460. EXPORT_SYMBOL(transport_handle_cdb_direct);
  1461. /**
  1462. * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
  1463. *
  1464. * @se_cmd: command descriptor to submit
  1465. * @se_sess: associated se_sess for endpoint
  1466. * @cdb: pointer to SCSI CDB
  1467. * @sense: pointer to SCSI sense buffer
  1468. * @unpacked_lun: unpacked LUN to reference for struct se_lun
  1469. * @data_length: fabric expected data transfer length
  1470. * @task_addr: SAM task attribute
  1471. * @data_dir: DMA data direction
  1472. * @flags: flags for command submission from target_sc_flags_tables
  1473. *
  1474. * This may only be called from process context, and also currently
  1475. * assumes internal allocation of fabric payload buffer by target-core.
  1476. **/
  1477. void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
  1478. unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
  1479. u32 data_length, int task_attr, int data_dir, int flags)
  1480. {
  1481. struct se_portal_group *se_tpg;
  1482. int rc;
  1483. se_tpg = se_sess->se_tpg;
  1484. BUG_ON(!se_tpg);
  1485. BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
  1486. BUG_ON(in_interrupt());
  1487. /*
  1488. * Initialize se_cmd for target operation. From this point
  1489. * exceptions are handled by sending exception status via
  1490. * target_core_fabric_ops->queue_status() callback
  1491. */
  1492. transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
  1493. data_length, data_dir, task_attr, sense);
  1494. /*
  1495. * Obtain struct se_cmd->cmd_kref reference and add new cmd to
  1496. * se_sess->sess_cmd_list. A second kref_get here is necessary
  1497. * for fabrics using TARGET_SCF_ACK_KREF that expect a second
  1498. * kref_put() to happen during fabric packet acknowledgement.
  1499. */
  1500. target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
  1501. /*
  1502. * Signal bidirectional data payloads to target-core
  1503. */
  1504. if (flags & TARGET_SCF_BIDI_OP)
  1505. se_cmd->se_cmd_flags |= SCF_BIDI;
  1506. /*
  1507. * Locate se_lun pointer and attach it to struct se_cmd
  1508. */
  1509. if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
  1510. transport_send_check_condition_and_sense(se_cmd,
  1511. se_cmd->scsi_sense_reason, 0);
  1512. target_put_sess_cmd(se_sess, se_cmd);
  1513. return;
  1514. }
  1515. /*
  1516. * Sanitize CDBs via transport_generic_cmd_sequencer() and
  1517. * allocate the necessary tasks to complete the received CDB+data
  1518. */
  1519. rc = transport_generic_allocate_tasks(se_cmd, cdb);
  1520. if (rc != 0) {
  1521. transport_generic_request_failure(se_cmd);
  1522. return;
  1523. }
  1524. /*
  1525. * Dispatch se_cmd descriptor to se_lun->lun_se_dev backend
  1526. * for immediate execution of READs, otherwise wait for
  1527. * transport_generic_handle_data() to be called for WRITEs
  1528. * when fabric has filled the incoming buffer.
  1529. */
  1530. transport_handle_cdb_direct(se_cmd);
  1531. return;
  1532. }
  1533. EXPORT_SYMBOL(target_submit_cmd);
  1534. /*
  1535. * Used by fabric module frontends defining a TFO->new_cmd_map() caller
  1536. * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
  1537. * complete setup in TCM process context w/ TFO->new_cmd_map().
  1538. */
  1539. int transport_generic_handle_cdb_map(
  1540. struct se_cmd *cmd)
  1541. {
  1542. if (!cmd->se_lun) {
  1543. dump_stack();
  1544. pr_err("cmd->se_lun is NULL\n");
  1545. return -EINVAL;
  1546. }
  1547. transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
  1548. return 0;
  1549. }
  1550. EXPORT_SYMBOL(transport_generic_handle_cdb_map);
  1551. /* transport_generic_handle_data():
  1552. *
  1553. *
  1554. */
  1555. int transport_generic_handle_data(
  1556. struct se_cmd *cmd)
  1557. {
  1558. /*
  1559. * For the software fabric case, then we assume the nexus is being
  1560. * failed/shutdown when signals are pending from the kthread context
  1561. * caller, so we return a failure. For the HW target mode case running
  1562. * in interrupt code, the signal_pending() check is skipped.
  1563. */
  1564. if (!in_interrupt() && signal_pending(current))
  1565. return -EPERM;
  1566. /*
  1567. * If the received CDB has aleady been ABORTED by the generic
  1568. * target engine, we now call transport_check_aborted_status()
  1569. * to queue any delated TASK_ABORTED status for the received CDB to the
  1570. * fabric module as we are expecting no further incoming DATA OUT
  1571. * sequences at this point.
  1572. */
  1573. if (transport_check_aborted_status(cmd, 1) != 0)
  1574. return 0;
  1575. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
  1576. return 0;
  1577. }
  1578. EXPORT_SYMBOL(transport_generic_handle_data);
  1579. /* transport_generic_handle_tmr():
  1580. *
  1581. *
  1582. */
  1583. int transport_generic_handle_tmr(
  1584. struct se_cmd *cmd)
  1585. {
  1586. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
  1587. return 0;
  1588. }
  1589. EXPORT_SYMBOL(transport_generic_handle_tmr);
  1590. /*
  1591. * If the task is active, request it to be stopped and sleep until it
  1592. * has completed.
  1593. */
  1594. bool target_stop_task(struct se_task *task, unsigned long *flags)
  1595. {
  1596. struct se_cmd *cmd = task->task_se_cmd;
  1597. bool was_active = false;
  1598. if (task->task_flags & TF_ACTIVE) {
  1599. task->task_flags |= TF_REQUEST_STOP;
  1600. spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
  1601. pr_debug("Task %p waiting to complete\n", task);
  1602. wait_for_completion(&task->task_stop_comp);
  1603. pr_debug("Task %p stopped successfully\n", task);
  1604. spin_lock_irqsave(&cmd->t_state_lock, *flags);
  1605. atomic_dec(&cmd->t_task_cdbs_left);
  1606. task->task_flags &= ~(TF_ACTIVE | TF_REQUEST_STOP);
  1607. was_active = true;
  1608. }
  1609. return was_active;
  1610. }
  1611. static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
  1612. {
  1613. struct se_task *task, *task_tmp;
  1614. unsigned long flags;
  1615. int ret = 0;
  1616. pr_debug("ITT[0x%08x] - Stopping tasks\n",
  1617. cmd->se_tfo->get_task_tag(cmd));
  1618. /*
  1619. * No tasks remain in the execution queue
  1620. */
  1621. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1622. list_for_each_entry_safe(task, task_tmp,
  1623. &cmd->t_task_list, t_list) {
  1624. pr_debug("Processing task %p\n", task);
  1625. /*
  1626. * If the struct se_task has not been sent and is not active,
  1627. * remove the struct se_task from the execution queue.
  1628. */
  1629. if (!(task->task_flags & (TF_ACTIVE | TF_SENT))) {
  1630. spin_unlock_irqrestore(&cmd->t_state_lock,
  1631. flags);
  1632. transport_remove_task_from_execute_queue(task,
  1633. cmd->se_dev);
  1634. pr_debug("Task %p removed from execute queue\n", task);
  1635. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1636. continue;
  1637. }
  1638. if (!target_stop_task(task, &flags)) {
  1639. pr_debug("Task %p - did nothing\n", task);
  1640. ret++;
  1641. }
  1642. }
  1643. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1644. return ret;
  1645. }
  1646. /*
  1647. * Handle SAM-esque emulation for generic transport request failures.
  1648. */
  1649. static void transport_generic_request_failure(struct se_cmd *cmd)
  1650. {
  1651. int ret = 0;
  1652. pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
  1653. " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
  1654. cmd->t_task_cdb[0]);
  1655. pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
  1656. cmd->se_tfo->get_cmd_state(cmd),
  1657. cmd->t_state, cmd->scsi_sense_reason);
  1658. pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d"
  1659. " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
  1660. " t_transport_active: %d t_transport_stop: %d"
  1661. " t_transport_sent: %d\n", cmd->t_task_list_num,
  1662. atomic_read(&cmd->t_task_cdbs_left),
  1663. atomic_read(&cmd->t_task_cdbs_sent),
  1664. atomic_read(&cmd->t_task_cdbs_ex_left),
  1665. atomic_read(&cmd->t_transport_active),
  1666. atomic_read(&cmd->t_transport_stop),
  1667. atomic_read(&cmd->t_transport_sent));
  1668. /*
  1669. * For SAM Task Attribute emulation for failed struct se_cmd
  1670. */
  1671. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  1672. transport_complete_task_attr(cmd);
  1673. switch (cmd->scsi_sense_reason) {
  1674. case TCM_NON_EXISTENT_LUN:
  1675. case TCM_UNSUPPORTED_SCSI_OPCODE:
  1676. case TCM_INVALID_CDB_FIELD:
  1677. case TCM_INVALID_PARAMETER_LIST:
  1678. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  1679. case TCM_UNKNOWN_MODE_PAGE:
  1680. case TCM_WRITE_PROTECTED:
  1681. case TCM_CHECK_CONDITION_ABORT_CMD:
  1682. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  1683. case TCM_CHECK_CONDITION_NOT_READY:
  1684. break;
  1685. case TCM_RESERVATION_CONFLICT:
  1686. /*
  1687. * No SENSE Data payload for this case, set SCSI Status
  1688. * and queue the response to $FABRIC_MOD.
  1689. *
  1690. * Uses linux/include/scsi/scsi.h SAM status codes defs
  1691. */
  1692. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  1693. /*
  1694. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  1695. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  1696. * CONFLICT STATUS.
  1697. *
  1698. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  1699. */
  1700. if (cmd->se_sess &&
  1701. cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
  1702. core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
  1703. cmd->orig_fe_lun, 0x2C,
  1704. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  1705. ret = cmd->se_tfo->queue_status(cmd);
  1706. if (ret == -EAGAIN || ret == -ENOMEM)
  1707. goto queue_full;
  1708. goto check_stop;
  1709. default:
  1710. pr_err("Unknown transport error for CDB 0x%02x: %d\n",
  1711. cmd->t_task_cdb[0], cmd->scsi_sense_reason);
  1712. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1713. break;
  1714. }
  1715. /*
  1716. * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
  1717. * make the call to transport_send_check_condition_and_sense()
  1718. * directly. Otherwise expect the fabric to make the call to
  1719. * transport_send_check_condition_and_sense() after handling
  1720. * possible unsoliticied write data payloads.
  1721. */
  1722. ret = transport_send_check_condition_and_sense(cmd,
  1723. cmd->scsi_sense_reason, 0);
  1724. if (ret == -EAGAIN || ret == -ENOMEM)
  1725. goto queue_full;
  1726. check_stop:
  1727. transport_lun_remove_cmd(cmd);
  1728. if (!transport_cmd_check_stop_to_fabric(cmd))
  1729. ;
  1730. return;
  1731. queue_full:
  1732. cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
  1733. transport_handle_queue_full(cmd, cmd->se_dev);
  1734. }
  1735. static inline u32 transport_lba_21(unsigned char *cdb)
  1736. {
  1737. return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
  1738. }
  1739. static inline u32 transport_lba_32(unsigned char *cdb)
  1740. {
  1741. return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1742. }
  1743. static inline unsigned long long transport_lba_64(unsigned char *cdb)
  1744. {
  1745. unsigned int __v1, __v2;
  1746. __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1747. __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1748. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  1749. }
  1750. /*
  1751. * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
  1752. */
  1753. static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
  1754. {
  1755. unsigned int __v1, __v2;
  1756. __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
  1757. __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
  1758. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  1759. }
  1760. static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
  1761. {
  1762. unsigned long flags;
  1763. spin_lock_irqsave(&se_cmd->t_state_lock, flags);
  1764. se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
  1765. spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
  1766. }
  1767. /*
  1768. * Called from Fabric Module context from transport_execute_tasks()
  1769. *
  1770. * The return of this function determins if the tasks from struct se_cmd
  1771. * get added to the execution queue in transport_execute_tasks(),
  1772. * or are added to the delayed or ordered lists here.
  1773. */
  1774. static inline int transport_execute_task_attr(struct se_cmd *cmd)
  1775. {
  1776. if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  1777. return 1;
  1778. /*
  1779. * Check for the existence of HEAD_OF_QUEUE, and if true return 1
  1780. * to allow the passed struct se_cmd list of tasks to the front of the list.
  1781. */
  1782. if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  1783. pr_debug("Added HEAD_OF_QUEUE for CDB:"
  1784. " 0x%02x, se_ordered_id: %u\n",
  1785. cmd->t_task_cdb[0],
  1786. cmd->se_ordered_id);
  1787. return 1;
  1788. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  1789. atomic_inc(&cmd->se_dev->dev_ordered_sync);
  1790. smp_mb__after_atomic_inc();
  1791. pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
  1792. " list, se_ordered_id: %u\n",
  1793. cmd->t_task_cdb[0],
  1794. cmd->se_ordered_id);
  1795. /*
  1796. * Add ORDERED command to tail of execution queue if
  1797. * no other older commands exist that need to be
  1798. * completed first.
  1799. */
  1800. if (!atomic_read(&cmd->se_dev->simple_cmds))
  1801. return 1;
  1802. } else {
  1803. /*
  1804. * For SIMPLE and UNTAGGED Task Attribute commands
  1805. */
  1806. atomic_inc(&cmd->se_dev->simple_cmds);
  1807. smp_mb__after_atomic_inc();
  1808. }
  1809. /*
  1810. * Otherwise if one or more outstanding ORDERED task attribute exist,
  1811. * add the dormant task(s) built for the passed struct se_cmd to the
  1812. * execution queue and become in Active state for this struct se_device.
  1813. */
  1814. if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
  1815. /*
  1816. * Otherwise, add cmd w/ tasks to delayed cmd queue that
  1817. * will be drained upon completion of HEAD_OF_QUEUE task.
  1818. */
  1819. spin_lock(&cmd->se_dev->delayed_cmd_lock);
  1820. cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
  1821. list_add_tail(&cmd->se_delayed_node,
  1822. &cmd->se_dev->delayed_cmd_list);
  1823. spin_unlock(&cmd->se_dev->delayed_cmd_lock);
  1824. pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
  1825. " delayed CMD list, se_ordered_id: %u\n",
  1826. cmd->t_task_cdb[0], cmd->sam_task_attr,
  1827. cmd->se_ordered_id);
  1828. /*
  1829. * Return zero to let transport_execute_tasks() know
  1830. * not to add the delayed tasks to the execution list.
  1831. */
  1832. return 0;
  1833. }
  1834. /*
  1835. * Otherwise, no ORDERED task attributes exist..
  1836. */
  1837. return 1;
  1838. }
  1839. /*
  1840. * Called from fabric module context in transport_generic_new_cmd() and
  1841. * transport_generic_process_write()
  1842. */
  1843. static int transport_execute_tasks(struct se_cmd *cmd)
  1844. {
  1845. int add_tasks;
  1846. struct se_device *se_dev = cmd->se_dev;
  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_status = SAM_STAT_RESERVATION_CONFLICT;
  2265. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2266. return -EBUSY;
  2267. }
  2268. /*
  2269. * This means the CDB is allowed for the SCSI Initiator port
  2270. * when said port is *NOT* holding the legacy SPC-2 or
  2271. * SPC-3 Persistent Reservation.
  2272. */
  2273. }
  2274. /*
  2275. * If we operate in passthrough mode we skip most CDB emulation and
  2276. * instead hand the commands down to the physical SCSI device.
  2277. */
  2278. passthrough =
  2279. (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
  2280. switch (cdb[0]) {
  2281. case READ_6:
  2282. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2283. if (sector_ret)
  2284. goto out_unsupported_cdb;
  2285. size = transport_get_size(sectors, cdb, cmd);
  2286. cmd->t_task_lba = transport_lba_21(cdb);
  2287. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2288. break;
  2289. case READ_10:
  2290. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2291. if (sector_ret)
  2292. goto out_unsupported_cdb;
  2293. size = transport_get_size(sectors, cdb, cmd);
  2294. cmd->t_task_lba = transport_lba_32(cdb);
  2295. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2296. break;
  2297. case READ_12:
  2298. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2299. if (sector_ret)
  2300. goto out_unsupported_cdb;
  2301. size = transport_get_size(sectors, cdb, cmd);
  2302. cmd->t_task_lba = transport_lba_32(cdb);
  2303. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2304. break;
  2305. case READ_16:
  2306. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2307. if (sector_ret)
  2308. goto out_unsupported_cdb;
  2309. size = transport_get_size(sectors, cdb, cmd);
  2310. cmd->t_task_lba = transport_lba_64(cdb);
  2311. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2312. break;
  2313. case WRITE_6:
  2314. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2315. if (sector_ret)
  2316. goto out_unsupported_cdb;
  2317. size = transport_get_size(sectors, cdb, cmd);
  2318. cmd->t_task_lba = transport_lba_21(cdb);
  2319. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2320. break;
  2321. case WRITE_10:
  2322. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2323. if (sector_ret)
  2324. goto out_unsupported_cdb;
  2325. size = transport_get_size(sectors, cdb, cmd);
  2326. cmd->t_task_lba = transport_lba_32(cdb);
  2327. if (cdb[1] & 0x8)
  2328. cmd->se_cmd_flags |= SCF_FUA;
  2329. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2330. break;
  2331. case WRITE_12:
  2332. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2333. if (sector_ret)
  2334. goto out_unsupported_cdb;
  2335. size = transport_get_size(sectors, cdb, cmd);
  2336. cmd->t_task_lba = transport_lba_32(cdb);
  2337. if (cdb[1] & 0x8)
  2338. cmd->se_cmd_flags |= SCF_FUA;
  2339. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2340. break;
  2341. case WRITE_16:
  2342. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2343. if (sector_ret)
  2344. goto out_unsupported_cdb;
  2345. size = transport_get_size(sectors, cdb, cmd);
  2346. cmd->t_task_lba = transport_lba_64(cdb);
  2347. if (cdb[1] & 0x8)
  2348. cmd->se_cmd_flags |= SCF_FUA;
  2349. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2350. break;
  2351. case XDWRITEREAD_10:
  2352. if ((cmd->data_direction != DMA_TO_DEVICE) ||
  2353. !(cmd->se_cmd_flags & SCF_BIDI))
  2354. goto out_invalid_cdb_field;
  2355. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2356. if (sector_ret)
  2357. goto out_unsupported_cdb;
  2358. size = transport_get_size(sectors, cdb, cmd);
  2359. cmd->t_task_lba = transport_lba_32(cdb);
  2360. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2361. /*
  2362. * Do now allow BIDI commands for passthrough mode.
  2363. */
  2364. if (passthrough)
  2365. goto out_unsupported_cdb;
  2366. /*
  2367. * Setup BIDI XOR callback to be run after I/O completion.
  2368. */
  2369. cmd->transport_complete_callback = &transport_xor_callback;
  2370. if (cdb[1] & 0x8)
  2371. cmd->se_cmd_flags |= SCF_FUA;
  2372. break;
  2373. case VARIABLE_LENGTH_CMD:
  2374. service_action = get_unaligned_be16(&cdb[8]);
  2375. switch (service_action) {
  2376. case XDWRITEREAD_32:
  2377. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2378. if (sector_ret)
  2379. goto out_unsupported_cdb;
  2380. size = transport_get_size(sectors, cdb, cmd);
  2381. /*
  2382. * Use WRITE_32 and READ_32 opcodes for the emulated
  2383. * XDWRITE_READ_32 logic.
  2384. */
  2385. cmd->t_task_lba = transport_lba_64_ext(cdb);
  2386. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2387. /*
  2388. * Do now allow BIDI commands for passthrough mode.
  2389. */
  2390. if (passthrough)
  2391. goto out_unsupported_cdb;
  2392. /*
  2393. * Setup BIDI XOR callback to be run during after I/O
  2394. * completion.
  2395. */
  2396. cmd->transport_complete_callback = &transport_xor_callback;
  2397. if (cdb[1] & 0x8)
  2398. cmd->se_cmd_flags |= SCF_FUA;
  2399. break;
  2400. case WRITE_SAME_32:
  2401. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2402. if (sector_ret)
  2403. goto out_unsupported_cdb;
  2404. if (sectors)
  2405. size = transport_get_size(1, cdb, cmd);
  2406. else {
  2407. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
  2408. " supported\n");
  2409. goto out_invalid_cdb_field;
  2410. }
  2411. cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
  2412. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2413. if (target_check_write_same_discard(&cdb[10], dev) < 0)
  2414. goto out_unsupported_cdb;
  2415. if (!passthrough)
  2416. cmd->execute_task = target_emulate_write_same;
  2417. break;
  2418. default:
  2419. pr_err("VARIABLE_LENGTH_CMD service action"
  2420. " 0x%04x not supported\n", service_action);
  2421. goto out_unsupported_cdb;
  2422. }
  2423. break;
  2424. case MAINTENANCE_IN:
  2425. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2426. /* MAINTENANCE_IN from SCC-2 */
  2427. /*
  2428. * Check for emulated MI_REPORT_TARGET_PGS.
  2429. */
  2430. if (cdb[1] == MI_REPORT_TARGET_PGS &&
  2431. su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
  2432. cmd->execute_task =
  2433. target_emulate_report_target_port_groups;
  2434. }
  2435. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2436. (cdb[8] << 8) | cdb[9];
  2437. } else {
  2438. /* GPCMD_SEND_KEY from multi media commands */
  2439. size = (cdb[8] << 8) + cdb[9];
  2440. }
  2441. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2442. break;
  2443. case MODE_SELECT:
  2444. size = cdb[4];
  2445. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2446. break;
  2447. case MODE_SELECT_10:
  2448. size = (cdb[7] << 8) + cdb[8];
  2449. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2450. break;
  2451. case MODE_SENSE:
  2452. size = cdb[4];
  2453. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2454. if (!passthrough)
  2455. cmd->execute_task = target_emulate_modesense;
  2456. break;
  2457. case MODE_SENSE_10:
  2458. size = (cdb[7] << 8) + cdb[8];
  2459. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2460. if (!passthrough)
  2461. cmd->execute_task = target_emulate_modesense;
  2462. break;
  2463. case GPCMD_READ_BUFFER_CAPACITY:
  2464. case GPCMD_SEND_OPC:
  2465. case LOG_SELECT:
  2466. case LOG_SENSE:
  2467. size = (cdb[7] << 8) + cdb[8];
  2468. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2469. break;
  2470. case READ_BLOCK_LIMITS:
  2471. size = READ_BLOCK_LEN;
  2472. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2473. break;
  2474. case GPCMD_GET_CONFIGURATION:
  2475. case GPCMD_READ_FORMAT_CAPACITIES:
  2476. case GPCMD_READ_DISC_INFO:
  2477. case GPCMD_READ_TRACK_RZONE_INFO:
  2478. size = (cdb[7] << 8) + cdb[8];
  2479. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2480. break;
  2481. case PERSISTENT_RESERVE_IN:
  2482. if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
  2483. cmd->execute_task = target_scsi3_emulate_pr_in;
  2484. size = (cdb[7] << 8) + cdb[8];
  2485. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2486. break;
  2487. case PERSISTENT_RESERVE_OUT:
  2488. if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
  2489. cmd->execute_task = target_scsi3_emulate_pr_out;
  2490. size = (cdb[7] << 8) + cdb[8];
  2491. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2492. break;
  2493. case GPCMD_MECHANISM_STATUS:
  2494. case GPCMD_READ_DVD_STRUCTURE:
  2495. size = (cdb[8] << 8) + cdb[9];
  2496. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2497. break;
  2498. case READ_POSITION:
  2499. size = READ_POSITION_LEN;
  2500. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2501. break;
  2502. case MAINTENANCE_OUT:
  2503. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2504. /* MAINTENANCE_OUT from SCC-2
  2505. *
  2506. * Check for emulated MO_SET_TARGET_PGS.
  2507. */
  2508. if (cdb[1] == MO_SET_TARGET_PGS &&
  2509. su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
  2510. cmd->execute_task =
  2511. target_emulate_set_target_port_groups;
  2512. }
  2513. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2514. (cdb[8] << 8) | cdb[9];
  2515. } else {
  2516. /* GPCMD_REPORT_KEY from multi media commands */
  2517. size = (cdb[8] << 8) + cdb[9];
  2518. }
  2519. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2520. break;
  2521. case INQUIRY:
  2522. size = (cdb[3] << 8) + cdb[4];
  2523. /*
  2524. * Do implict HEAD_OF_QUEUE processing for INQUIRY.
  2525. * See spc4r17 section 5.3
  2526. */
  2527. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2528. cmd->sam_task_attr = MSG_HEAD_TAG;
  2529. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2530. if (!passthrough)
  2531. cmd->execute_task = target_emulate_inquiry;
  2532. break;
  2533. case READ_BUFFER:
  2534. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2535. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2536. break;
  2537. case READ_CAPACITY:
  2538. size = READ_CAP_LEN;
  2539. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2540. if (!passthrough)
  2541. cmd->execute_task = target_emulate_readcapacity;
  2542. break;
  2543. case READ_MEDIA_SERIAL_NUMBER:
  2544. case SECURITY_PROTOCOL_IN:
  2545. case SECURITY_PROTOCOL_OUT:
  2546. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  2547. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2548. break;
  2549. case SERVICE_ACTION_IN:
  2550. switch (cmd->t_task_cdb[1] & 0x1f) {
  2551. case SAI_READ_CAPACITY_16:
  2552. if (!passthrough)
  2553. cmd->execute_task =
  2554. target_emulate_readcapacity_16;
  2555. break;
  2556. default:
  2557. if (passthrough)
  2558. break;
  2559. pr_err("Unsupported SA: 0x%02x\n",
  2560. cmd->t_task_cdb[1] & 0x1f);
  2561. goto out_unsupported_cdb;
  2562. }
  2563. /*FALLTHROUGH*/
  2564. case ACCESS_CONTROL_IN:
  2565. case ACCESS_CONTROL_OUT:
  2566. case EXTENDED_COPY:
  2567. case READ_ATTRIBUTE:
  2568. case RECEIVE_COPY_RESULTS:
  2569. case WRITE_ATTRIBUTE:
  2570. size = (cdb[10] << 24) | (cdb[11] << 16) |
  2571. (cdb[12] << 8) | cdb[13];
  2572. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2573. break;
  2574. case RECEIVE_DIAGNOSTIC:
  2575. case SEND_DIAGNOSTIC:
  2576. size = (cdb[3] << 8) | cdb[4];
  2577. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2578. break;
  2579. /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
  2580. #if 0
  2581. case GPCMD_READ_CD:
  2582. sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2583. size = (2336 * sectors);
  2584. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2585. break;
  2586. #endif
  2587. case READ_TOC:
  2588. size = cdb[8];
  2589. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2590. break;
  2591. case REQUEST_SENSE:
  2592. size = cdb[4];
  2593. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2594. if (!passthrough)
  2595. cmd->execute_task = target_emulate_request_sense;
  2596. break;
  2597. case READ_ELEMENT_STATUS:
  2598. size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
  2599. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2600. break;
  2601. case WRITE_BUFFER:
  2602. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2603. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2604. break;
  2605. case RESERVE:
  2606. case RESERVE_10:
  2607. /*
  2608. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  2609. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  2610. */
  2611. if (cdb[0] == RESERVE_10)
  2612. size = (cdb[7] << 8) | cdb[8];
  2613. else
  2614. size = cmd->data_length;
  2615. /*
  2616. * Setup the legacy emulated handler for SPC-2 and
  2617. * >= SPC-3 compatible reservation handling (CRH=1)
  2618. * Otherwise, we assume the underlying SCSI logic is
  2619. * is running in SPC_PASSTHROUGH, and wants reservations
  2620. * emulation disabled.
  2621. */
  2622. if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
  2623. cmd->execute_task = target_scsi2_reservation_reserve;
  2624. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2625. break;
  2626. case RELEASE:
  2627. case RELEASE_10:
  2628. /*
  2629. * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
  2630. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  2631. */
  2632. if (cdb[0] == RELEASE_10)
  2633. size = (cdb[7] << 8) | cdb[8];
  2634. else
  2635. size = cmd->data_length;
  2636. if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
  2637. cmd->execute_task = target_scsi2_reservation_release;
  2638. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2639. break;
  2640. case SYNCHRONIZE_CACHE:
  2641. case 0x91: /* SYNCHRONIZE_CACHE_16: */
  2642. /*
  2643. * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
  2644. */
  2645. if (cdb[0] == SYNCHRONIZE_CACHE) {
  2646. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2647. cmd->t_task_lba = transport_lba_32(cdb);
  2648. } else {
  2649. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2650. cmd->t_task_lba = transport_lba_64(cdb);
  2651. }
  2652. if (sector_ret)
  2653. goto out_unsupported_cdb;
  2654. size = transport_get_size(sectors, cdb, cmd);
  2655. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2656. if (passthrough)
  2657. break;
  2658. /*
  2659. * Check to ensure that LBA + Range does not exceed past end of
  2660. * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
  2661. */
  2662. if ((cmd->t_task_lba != 0) || (sectors != 0)) {
  2663. if (transport_cmd_get_valid_sectors(cmd) < 0)
  2664. goto out_invalid_cdb_field;
  2665. }
  2666. cmd->execute_task = target_emulate_synchronize_cache;
  2667. break;
  2668. case UNMAP:
  2669. size = get_unaligned_be16(&cdb[7]);
  2670. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2671. if (!passthrough)
  2672. cmd->execute_task = target_emulate_unmap;
  2673. break;
  2674. case WRITE_SAME_16:
  2675. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2676. if (sector_ret)
  2677. goto out_unsupported_cdb;
  2678. if (sectors)
  2679. size = transport_get_size(1, cdb, cmd);
  2680. else {
  2681. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
  2682. goto out_invalid_cdb_field;
  2683. }
  2684. cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
  2685. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2686. if (target_check_write_same_discard(&cdb[1], dev) < 0)
  2687. goto out_unsupported_cdb;
  2688. if (!passthrough)
  2689. cmd->execute_task = target_emulate_write_same;
  2690. break;
  2691. case WRITE_SAME:
  2692. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2693. if (sector_ret)
  2694. goto out_unsupported_cdb;
  2695. if (sectors)
  2696. size = transport_get_size(1, cdb, cmd);
  2697. else {
  2698. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
  2699. goto out_invalid_cdb_field;
  2700. }
  2701. cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
  2702. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2703. /*
  2704. * Follow sbcr26 with WRITE_SAME (10) and check for the existence
  2705. * of byte 1 bit 3 UNMAP instead of original reserved field
  2706. */
  2707. if (target_check_write_same_discard(&cdb[1], dev) < 0)
  2708. goto out_unsupported_cdb;
  2709. if (!passthrough)
  2710. cmd->execute_task = target_emulate_write_same;
  2711. break;
  2712. case ALLOW_MEDIUM_REMOVAL:
  2713. case ERASE:
  2714. case REZERO_UNIT:
  2715. case SEEK_10:
  2716. case SPACE:
  2717. case START_STOP:
  2718. case TEST_UNIT_READY:
  2719. case VERIFY:
  2720. case WRITE_FILEMARKS:
  2721. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2722. if (!passthrough)
  2723. cmd->execute_task = target_emulate_noop;
  2724. break;
  2725. case GPCMD_CLOSE_TRACK:
  2726. case INITIALIZE_ELEMENT_STATUS:
  2727. case GPCMD_LOAD_UNLOAD:
  2728. case GPCMD_SET_SPEED:
  2729. case MOVE_MEDIUM:
  2730. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2731. break;
  2732. case REPORT_LUNS:
  2733. cmd->execute_task = target_report_luns;
  2734. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  2735. /*
  2736. * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
  2737. * See spc4r17 section 5.3
  2738. */
  2739. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2740. cmd->sam_task_attr = MSG_HEAD_TAG;
  2741. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2742. break;
  2743. default:
  2744. pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
  2745. " 0x%02x, sending CHECK_CONDITION.\n",
  2746. cmd->se_tfo->get_fabric_name(), cdb[0]);
  2747. goto out_unsupported_cdb;
  2748. }
  2749. if (size != cmd->data_length) {
  2750. pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
  2751. " %u does not match SCSI CDB Length: %u for SAM Opcode:"
  2752. " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
  2753. cmd->data_length, size, cdb[0]);
  2754. cmd->cmd_spdtl = size;
  2755. if (cmd->data_direction == DMA_TO_DEVICE) {
  2756. pr_err("Rejecting underflow/overflow"
  2757. " WRITE data\n");
  2758. goto out_invalid_cdb_field;
  2759. }
  2760. /*
  2761. * Reject READ_* or WRITE_* with overflow/underflow for
  2762. * type SCF_SCSI_DATA_SG_IO_CDB.
  2763. */
  2764. if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
  2765. pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
  2766. " CDB on non 512-byte sector setup subsystem"
  2767. " plugin: %s\n", dev->transport->name);
  2768. /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
  2769. goto out_invalid_cdb_field;
  2770. }
  2771. if (size > cmd->data_length) {
  2772. cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
  2773. cmd->residual_count = (size - cmd->data_length);
  2774. } else {
  2775. cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  2776. cmd->residual_count = (cmd->data_length - size);
  2777. }
  2778. cmd->data_length = size;
  2779. }
  2780. /* reject any command that we don't have a handler for */
  2781. if (!(passthrough || cmd->execute_task ||
  2782. (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
  2783. goto out_unsupported_cdb;
  2784. transport_set_supported_SAM_opcode(cmd);
  2785. return ret;
  2786. out_unsupported_cdb:
  2787. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2788. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  2789. return -EINVAL;
  2790. out_invalid_cdb_field:
  2791. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2792. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  2793. return -EINVAL;
  2794. }
  2795. /*
  2796. * Called from I/O completion to determine which dormant/delayed
  2797. * and ordered cmds need to have their tasks added to the execution queue.
  2798. */
  2799. static void transport_complete_task_attr(struct se_cmd *cmd)
  2800. {
  2801. struct se_device *dev = cmd->se_dev;
  2802. struct se_cmd *cmd_p, *cmd_tmp;
  2803. int new_active_tasks = 0;
  2804. if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
  2805. atomic_dec(&dev->simple_cmds);
  2806. smp_mb__after_atomic_dec();
  2807. dev->dev_cur_ordered_id++;
  2808. pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
  2809. " SIMPLE: %u\n", dev->dev_cur_ordered_id,
  2810. cmd->se_ordered_id);
  2811. } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  2812. dev->dev_cur_ordered_id++;
  2813. pr_debug("Incremented dev_cur_ordered_id: %u for"
  2814. " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
  2815. cmd->se_ordered_id);
  2816. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  2817. atomic_dec(&dev->dev_ordered_sync);
  2818. smp_mb__after_atomic_dec();
  2819. dev->dev_cur_ordered_id++;
  2820. pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
  2821. " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
  2822. }
  2823. /*
  2824. * Process all commands up to the last received
  2825. * ORDERED task attribute which requires another blocking
  2826. * boundary
  2827. */
  2828. spin_lock(&dev->delayed_cmd_lock);
  2829. list_for_each_entry_safe(cmd_p, cmd_tmp,
  2830. &dev->delayed_cmd_list, se_delayed_node) {
  2831. list_del(&cmd_p->se_delayed_node);
  2832. spin_unlock(&dev->delayed_cmd_lock);
  2833. pr_debug("Calling add_tasks() for"
  2834. " cmd_p: 0x%02x Task Attr: 0x%02x"
  2835. " Dormant -> Active, se_ordered_id: %u\n",
  2836. cmd_p->t_task_cdb[0],
  2837. cmd_p->sam_task_attr, cmd_p->se_ordered_id);
  2838. transport_add_tasks_from_cmd(cmd_p);
  2839. new_active_tasks++;
  2840. spin_lock(&dev->delayed_cmd_lock);
  2841. if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
  2842. break;
  2843. }
  2844. spin_unlock(&dev->delayed_cmd_lock);
  2845. /*
  2846. * If new tasks have become active, wake up the transport thread
  2847. * to do the processing of the Active tasks.
  2848. */
  2849. if (new_active_tasks != 0)
  2850. wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
  2851. }
  2852. static void transport_complete_qf(struct se_cmd *cmd)
  2853. {
  2854. int ret = 0;
  2855. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2856. transport_complete_task_attr(cmd);
  2857. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  2858. ret = cmd->se_tfo->queue_status(cmd);
  2859. if (ret)
  2860. goto out;
  2861. }
  2862. switch (cmd->data_direction) {
  2863. case DMA_FROM_DEVICE:
  2864. ret = cmd->se_tfo->queue_data_in(cmd);
  2865. break;
  2866. case DMA_TO_DEVICE:
  2867. if (cmd->t_bidi_data_sg) {
  2868. ret = cmd->se_tfo->queue_data_in(cmd);
  2869. if (ret < 0)
  2870. break;
  2871. }
  2872. /* Fall through for DMA_TO_DEVICE */
  2873. case DMA_NONE:
  2874. ret = cmd->se_tfo->queue_status(cmd);
  2875. break;
  2876. default:
  2877. break;
  2878. }
  2879. out:
  2880. if (ret < 0) {
  2881. transport_handle_queue_full(cmd, cmd->se_dev);
  2882. return;
  2883. }
  2884. transport_lun_remove_cmd(cmd);
  2885. transport_cmd_check_stop_to_fabric(cmd);
  2886. }
  2887. static void transport_handle_queue_full(
  2888. struct se_cmd *cmd,
  2889. struct se_device *dev)
  2890. {
  2891. spin_lock_irq(&dev->qf_cmd_lock);
  2892. list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
  2893. atomic_inc(&dev->dev_qf_count);
  2894. smp_mb__after_atomic_inc();
  2895. spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
  2896. schedule_work(&cmd->se_dev->qf_work_queue);
  2897. }
  2898. static void target_complete_ok_work(struct work_struct *work)
  2899. {
  2900. struct se_cmd *cmd = container_of(work, struct se_cmd, work);
  2901. int reason = 0, ret;
  2902. /*
  2903. * Check if we need to move delayed/dormant tasks from cmds on the
  2904. * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
  2905. * Attribute.
  2906. */
  2907. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2908. transport_complete_task_attr(cmd);
  2909. /*
  2910. * Check to schedule QUEUE_FULL work, or execute an existing
  2911. * cmd->transport_qf_callback()
  2912. */
  2913. if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
  2914. schedule_work(&cmd->se_dev->qf_work_queue);
  2915. /*
  2916. * Check if we need to retrieve a sense buffer from
  2917. * the struct se_cmd in question.
  2918. */
  2919. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  2920. if (transport_get_sense_data(cmd) < 0)
  2921. reason = TCM_NON_EXISTENT_LUN;
  2922. /*
  2923. * Only set when an struct se_task->task_scsi_status returned
  2924. * a non GOOD status.
  2925. */
  2926. if (cmd->scsi_status) {
  2927. ret = transport_send_check_condition_and_sense(
  2928. cmd, reason, 1);
  2929. if (ret == -EAGAIN || ret == -ENOMEM)
  2930. goto queue_full;
  2931. transport_lun_remove_cmd(cmd);
  2932. transport_cmd_check_stop_to_fabric(cmd);
  2933. return;
  2934. }
  2935. }
  2936. /*
  2937. * Check for a callback, used by amongst other things
  2938. * XDWRITE_READ_10 emulation.
  2939. */
  2940. if (cmd->transport_complete_callback)
  2941. cmd->transport_complete_callback(cmd);
  2942. switch (cmd->data_direction) {
  2943. case DMA_FROM_DEVICE:
  2944. spin_lock(&cmd->se_lun->lun_sep_lock);
  2945. if (cmd->se_lun->lun_sep) {
  2946. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  2947. cmd->data_length;
  2948. }
  2949. spin_unlock(&cmd->se_lun->lun_sep_lock);
  2950. ret = cmd->se_tfo->queue_data_in(cmd);
  2951. if (ret == -EAGAIN || ret == -ENOMEM)
  2952. goto queue_full;
  2953. break;
  2954. case DMA_TO_DEVICE:
  2955. spin_lock(&cmd->se_lun->lun_sep_lock);
  2956. if (cmd->se_lun->lun_sep) {
  2957. cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
  2958. cmd->data_length;
  2959. }
  2960. spin_unlock(&cmd->se_lun->lun_sep_lock);
  2961. /*
  2962. * Check if we need to send READ payload for BIDI-COMMAND
  2963. */
  2964. if (cmd->t_bidi_data_sg) {
  2965. spin_lock(&cmd->se_lun->lun_sep_lock);
  2966. if (cmd->se_lun->lun_sep) {
  2967. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  2968. cmd->data_length;
  2969. }
  2970. spin_unlock(&cmd->se_lun->lun_sep_lock);
  2971. ret = cmd->se_tfo->queue_data_in(cmd);
  2972. if (ret == -EAGAIN || ret == -ENOMEM)
  2973. goto queue_full;
  2974. break;
  2975. }
  2976. /* Fall through for DMA_TO_DEVICE */
  2977. case DMA_NONE:
  2978. ret = cmd->se_tfo->queue_status(cmd);
  2979. if (ret == -EAGAIN || ret == -ENOMEM)
  2980. goto queue_full;
  2981. break;
  2982. default:
  2983. break;
  2984. }
  2985. transport_lun_remove_cmd(cmd);
  2986. transport_cmd_check_stop_to_fabric(cmd);
  2987. return;
  2988. queue_full:
  2989. pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
  2990. " data_direction: %d\n", cmd, cmd->data_direction);
  2991. cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
  2992. transport_handle_queue_full(cmd, cmd->se_dev);
  2993. }
  2994. static void transport_free_dev_tasks(struct se_cmd *cmd)
  2995. {
  2996. struct se_task *task, *task_tmp;
  2997. unsigned long flags;
  2998. LIST_HEAD(dispose_list);
  2999. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3000. list_for_each_entry_safe(task, task_tmp,
  3001. &cmd->t_task_list, t_list) {
  3002. if (!(task->task_flags & TF_ACTIVE))
  3003. list_move_tail(&task->t_list, &dispose_list);
  3004. }
  3005. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3006. while (!list_empty(&dispose_list)) {
  3007. task = list_first_entry(&dispose_list, struct se_task, t_list);
  3008. if (task->task_sg != cmd->t_data_sg &&
  3009. task->task_sg != cmd->t_bidi_data_sg)
  3010. kfree(task->task_sg);
  3011. list_del(&task->t_list);
  3012. cmd->se_dev->transport->free_task(task);
  3013. }
  3014. }
  3015. static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
  3016. {
  3017. struct scatterlist *sg;
  3018. int count;
  3019. for_each_sg(sgl, sg, nents, count)
  3020. __free_page(sg_page(sg));
  3021. kfree(sgl);
  3022. }
  3023. static inline void transport_free_pages(struct se_cmd *cmd)
  3024. {
  3025. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
  3026. return;
  3027. transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
  3028. cmd->t_data_sg = NULL;
  3029. cmd->t_data_nents = 0;
  3030. transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
  3031. cmd->t_bidi_data_sg = NULL;
  3032. cmd->t_bidi_data_nents = 0;
  3033. }
  3034. /**
  3035. * transport_release_cmd - free a command
  3036. * @cmd: command to free
  3037. *
  3038. * This routine unconditionally frees a command, and reference counting
  3039. * or list removal must be done in the caller.
  3040. */
  3041. static void transport_release_cmd(struct se_cmd *cmd)
  3042. {
  3043. BUG_ON(!cmd->se_tfo);
  3044. if (cmd->se_tmr_req)
  3045. core_tmr_release_req(cmd->se_tmr_req);
  3046. if (cmd->t_task_cdb != cmd->__t_task_cdb)
  3047. kfree(cmd->t_task_cdb);
  3048. /*
  3049. * If this cmd has been setup with target_get_sess_cmd(), drop
  3050. * the kref and call ->release_cmd() in kref callback.
  3051. */
  3052. if (cmd->check_release != 0) {
  3053. target_put_sess_cmd(cmd->se_sess, cmd);
  3054. return;
  3055. }
  3056. cmd->se_tfo->release_cmd(cmd);
  3057. }
  3058. /**
  3059. * transport_put_cmd - release a reference to a command
  3060. * @cmd: command to release
  3061. *
  3062. * This routine releases our reference to the command and frees it if possible.
  3063. */
  3064. static void transport_put_cmd(struct se_cmd *cmd)
  3065. {
  3066. unsigned long flags;
  3067. int free_tasks = 0;
  3068. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3069. if (atomic_read(&cmd->t_fe_count)) {
  3070. if (!atomic_dec_and_test(&cmd->t_fe_count))
  3071. goto out_busy;
  3072. }
  3073. if (atomic_read(&cmd->t_se_count)) {
  3074. if (!atomic_dec_and_test(&cmd->t_se_count))
  3075. goto out_busy;
  3076. }
  3077. if (atomic_read(&cmd->transport_dev_active)) {
  3078. atomic_set(&cmd->transport_dev_active, 0);
  3079. transport_all_task_dev_remove_state(cmd);
  3080. free_tasks = 1;
  3081. }
  3082. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3083. if (free_tasks != 0)
  3084. transport_free_dev_tasks(cmd);
  3085. transport_free_pages(cmd);
  3086. transport_release_cmd(cmd);
  3087. return;
  3088. out_busy:
  3089. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3090. }
  3091. /*
  3092. * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
  3093. * allocating in the core.
  3094. * @cmd: Associated se_cmd descriptor
  3095. * @mem: SGL style memory for TCM WRITE / READ
  3096. * @sg_mem_num: Number of SGL elements
  3097. * @mem_bidi_in: SGL style memory for TCM BIDI READ
  3098. * @sg_mem_bidi_num: Number of BIDI READ SGL elements
  3099. *
  3100. * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
  3101. * of parameters.
  3102. */
  3103. int transport_generic_map_mem_to_cmd(
  3104. struct se_cmd *cmd,
  3105. struct scatterlist *sgl,
  3106. u32 sgl_count,
  3107. struct scatterlist *sgl_bidi,
  3108. u32 sgl_bidi_count)
  3109. {
  3110. if (!sgl || !sgl_count)
  3111. return 0;
  3112. if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
  3113. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
  3114. /*
  3115. * Reject SCSI data overflow with map_mem_to_cmd() as incoming
  3116. * scatterlists already have been set to follow what the fabric
  3117. * passes for the original expected data transfer length.
  3118. */
  3119. if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
  3120. pr_warn("Rejecting SCSI DATA overflow for fabric using"
  3121. " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
  3122. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3123. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3124. return -EINVAL;
  3125. }
  3126. cmd->t_data_sg = sgl;
  3127. cmd->t_data_nents = sgl_count;
  3128. if (sgl_bidi && sgl_bidi_count) {
  3129. cmd->t_bidi_data_sg = sgl_bidi;
  3130. cmd->t_bidi_data_nents = sgl_bidi_count;
  3131. }
  3132. cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  3133. }
  3134. return 0;
  3135. }
  3136. EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
  3137. void *transport_kmap_data_sg(struct se_cmd *cmd)
  3138. {
  3139. struct scatterlist *sg = cmd->t_data_sg;
  3140. struct page **pages;
  3141. int i;
  3142. BUG_ON(!sg);
  3143. /*
  3144. * We need to take into account a possible offset here for fabrics like
  3145. * tcm_loop who may be using a contig buffer from the SCSI midlayer for
  3146. * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
  3147. */
  3148. if (!cmd->t_data_nents)
  3149. return NULL;
  3150. else if (cmd->t_data_nents == 1)
  3151. return kmap(sg_page(sg)) + sg->offset;
  3152. /* >1 page. use vmap */
  3153. pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
  3154. if (!pages)
  3155. return NULL;
  3156. /* convert sg[] to pages[] */
  3157. for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
  3158. pages[i] = sg_page(sg);
  3159. }
  3160. cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
  3161. kfree(pages);
  3162. if (!cmd->t_data_vmap)
  3163. return NULL;
  3164. return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
  3165. }
  3166. EXPORT_SYMBOL(transport_kmap_data_sg);
  3167. void transport_kunmap_data_sg(struct se_cmd *cmd)
  3168. {
  3169. if (!cmd->t_data_nents)
  3170. return;
  3171. else if (cmd->t_data_nents == 1)
  3172. kunmap(sg_page(cmd->t_data_sg));
  3173. vunmap(cmd->t_data_vmap);
  3174. cmd->t_data_vmap = NULL;
  3175. }
  3176. EXPORT_SYMBOL(transport_kunmap_data_sg);
  3177. static int
  3178. transport_generic_get_mem(struct se_cmd *cmd)
  3179. {
  3180. u32 length = cmd->data_length;
  3181. unsigned int nents;
  3182. struct page *page;
  3183. gfp_t zero_flag;
  3184. int i = 0;
  3185. nents = DIV_ROUND_UP(length, PAGE_SIZE);
  3186. cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
  3187. if (!cmd->t_data_sg)
  3188. return -ENOMEM;
  3189. cmd->t_data_nents = nents;
  3190. sg_init_table(cmd->t_data_sg, nents);
  3191. zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB ? 0 : __GFP_ZERO;
  3192. while (length) {
  3193. u32 page_len = min_t(u32, length, PAGE_SIZE);
  3194. page = alloc_page(GFP_KERNEL | zero_flag);
  3195. if (!page)
  3196. goto out;
  3197. sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
  3198. length -= page_len;
  3199. i++;
  3200. }
  3201. return 0;
  3202. out:
  3203. while (i >= 0) {
  3204. __free_page(sg_page(&cmd->t_data_sg[i]));
  3205. i--;
  3206. }
  3207. kfree(cmd->t_data_sg);
  3208. cmd->t_data_sg = NULL;
  3209. return -ENOMEM;
  3210. }
  3211. /* Reduce sectors if they are too long for the device */
  3212. static inline sector_t transport_limit_task_sectors(
  3213. struct se_device *dev,
  3214. unsigned long long lba,
  3215. sector_t sectors)
  3216. {
  3217. sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
  3218. if (dev->transport->get_device_type(dev) == TYPE_DISK)
  3219. if ((lba + sectors) > transport_dev_end_lba(dev))
  3220. sectors = ((transport_dev_end_lba(dev) - lba) + 1);
  3221. return sectors;
  3222. }
  3223. /*
  3224. * This function can be used by HW target mode drivers to create a linked
  3225. * scatterlist from all contiguously allocated struct se_task->task_sg[].
  3226. * This is intended to be called during the completion path by TCM Core
  3227. * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
  3228. */
  3229. void transport_do_task_sg_chain(struct se_cmd *cmd)
  3230. {
  3231. struct scatterlist *sg_first = NULL;
  3232. struct scatterlist *sg_prev = NULL;
  3233. int sg_prev_nents = 0;
  3234. struct scatterlist *sg;
  3235. struct se_task *task;
  3236. u32 chained_nents = 0;
  3237. int i;
  3238. BUG_ON(!cmd->se_tfo->task_sg_chaining);
  3239. /*
  3240. * Walk the struct se_task list and setup scatterlist chains
  3241. * for each contiguously allocated struct se_task->task_sg[].
  3242. */
  3243. list_for_each_entry(task, &cmd->t_task_list, t_list) {
  3244. if (!task->task_sg)
  3245. continue;
  3246. if (!sg_first) {
  3247. sg_first = task->task_sg;
  3248. chained_nents = task->task_sg_nents;
  3249. } else {
  3250. sg_chain(sg_prev, sg_prev_nents, task->task_sg);
  3251. chained_nents += task->task_sg_nents;
  3252. }
  3253. /*
  3254. * For the padded tasks, use the extra SGL vector allocated
  3255. * in transport_allocate_data_tasks() for the sg_prev_nents
  3256. * offset into sg_chain() above.
  3257. *
  3258. * We do not need the padding for the last task (or a single
  3259. * task), but in that case we will never use the sg_prev_nents
  3260. * value below which would be incorrect.
  3261. */
  3262. sg_prev_nents = (task->task_sg_nents + 1);
  3263. sg_prev = task->task_sg;
  3264. }
  3265. /*
  3266. * Setup the starting pointer and total t_tasks_sg_linked_no including
  3267. * padding SGs for linking and to mark the end.
  3268. */
  3269. cmd->t_tasks_sg_chained = sg_first;
  3270. cmd->t_tasks_sg_chained_no = chained_nents;
  3271. pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
  3272. " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
  3273. cmd->t_tasks_sg_chained_no);
  3274. for_each_sg(cmd->t_tasks_sg_chained, sg,
  3275. cmd->t_tasks_sg_chained_no, i) {
  3276. pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
  3277. i, sg, sg_page(sg), sg->length, sg->offset);
  3278. if (sg_is_chain(sg))
  3279. pr_debug("SG: %p sg_is_chain=1\n", sg);
  3280. if (sg_is_last(sg))
  3281. pr_debug("SG: %p sg_is_last=1\n", sg);
  3282. }
  3283. }
  3284. EXPORT_SYMBOL(transport_do_task_sg_chain);
  3285. /*
  3286. * Break up cmd into chunks transport can handle
  3287. */
  3288. static int
  3289. transport_allocate_data_tasks(struct se_cmd *cmd,
  3290. enum dma_data_direction data_direction,
  3291. struct scatterlist *cmd_sg, unsigned int sgl_nents)
  3292. {
  3293. struct se_device *dev = cmd->se_dev;
  3294. int task_count, i;
  3295. unsigned long long lba;
  3296. sector_t sectors, dev_max_sectors;
  3297. u32 sector_size;
  3298. if (transport_cmd_get_valid_sectors(cmd) < 0)
  3299. return -EINVAL;
  3300. dev_max_sectors = dev->se_sub_dev->se_dev_attrib.max_sectors;
  3301. sector_size = dev->se_sub_dev->se_dev_attrib.block_size;
  3302. WARN_ON(cmd->data_length % sector_size);
  3303. lba = cmd->t_task_lba;
  3304. sectors = DIV_ROUND_UP(cmd->data_length, sector_size);
  3305. task_count = DIV_ROUND_UP_SECTOR_T(sectors, dev_max_sectors);
  3306. /*
  3307. * If we need just a single task reuse the SG list in the command
  3308. * and avoid a lot of work.
  3309. */
  3310. if (task_count == 1) {
  3311. struct se_task *task;
  3312. unsigned long flags;
  3313. task = transport_generic_get_task(cmd, data_direction);
  3314. if (!task)
  3315. return -ENOMEM;
  3316. task->task_sg = cmd_sg;
  3317. task->task_sg_nents = sgl_nents;
  3318. task->task_lba = lba;
  3319. task->task_sectors = sectors;
  3320. task->task_size = task->task_sectors * sector_size;
  3321. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3322. list_add_tail(&task->t_list, &cmd->t_task_list);
  3323. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3324. return task_count;
  3325. }
  3326. for (i = 0; i < task_count; i++) {
  3327. struct se_task *task;
  3328. unsigned int task_size, task_sg_nents_padded;
  3329. struct scatterlist *sg;
  3330. unsigned long flags;
  3331. int count;
  3332. task = transport_generic_get_task(cmd, data_direction);
  3333. if (!task)
  3334. return -ENOMEM;
  3335. task->task_lba = lba;
  3336. task->task_sectors = min(sectors, dev_max_sectors);
  3337. task->task_size = task->task_sectors * sector_size;
  3338. /*
  3339. * This now assumes that passed sg_ents are in PAGE_SIZE chunks
  3340. * in order to calculate the number per task SGL entries
  3341. */
  3342. task->task_sg_nents = DIV_ROUND_UP(task->task_size, PAGE_SIZE);
  3343. /*
  3344. * Check if the fabric module driver is requesting that all
  3345. * struct se_task->task_sg[] be chained together.. If so,
  3346. * then allocate an extra padding SG entry for linking and
  3347. * marking the end of the chained SGL for every task except
  3348. * the last one for (task_count > 1) operation, or skipping
  3349. * the extra padding for the (task_count == 1) case.
  3350. */
  3351. if (cmd->se_tfo->task_sg_chaining && (i < (task_count - 1))) {
  3352. task_sg_nents_padded = (task->task_sg_nents + 1);
  3353. } else
  3354. task_sg_nents_padded = task->task_sg_nents;
  3355. task->task_sg = kmalloc(sizeof(struct scatterlist) *
  3356. task_sg_nents_padded, GFP_KERNEL);
  3357. if (!task->task_sg) {
  3358. cmd->se_dev->transport->free_task(task);
  3359. return -ENOMEM;
  3360. }
  3361. sg_init_table(task->task_sg, task_sg_nents_padded);
  3362. task_size = task->task_size;
  3363. /* Build new sgl, only up to task_size */
  3364. for_each_sg(task->task_sg, sg, task->task_sg_nents, count) {
  3365. if (cmd_sg->length > task_size)
  3366. break;
  3367. *sg = *cmd_sg;
  3368. task_size -= cmd_sg->length;
  3369. cmd_sg = sg_next(cmd_sg);
  3370. }
  3371. lba += task->task_sectors;
  3372. sectors -= task->task_sectors;
  3373. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3374. list_add_tail(&task->t_list, &cmd->t_task_list);
  3375. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3376. }
  3377. return task_count;
  3378. }
  3379. static int
  3380. transport_allocate_control_task(struct se_cmd *cmd)
  3381. {
  3382. struct se_task *task;
  3383. unsigned long flags;
  3384. /* Workaround for handling zero-length control CDBs */
  3385. if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
  3386. !cmd->data_length)
  3387. return 0;
  3388. task = transport_generic_get_task(cmd, cmd->data_direction);
  3389. if (!task)
  3390. return -ENOMEM;
  3391. task->task_sg = cmd->t_data_sg;
  3392. task->task_size = cmd->data_length;
  3393. task->task_sg_nents = cmd->t_data_nents;
  3394. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3395. list_add_tail(&task->t_list, &cmd->t_task_list);
  3396. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3397. /* Success! Return number of tasks allocated */
  3398. return 1;
  3399. }
  3400. /*
  3401. * Allocate any required ressources to execute the command, and either place
  3402. * it on the execution queue if possible. For writes we might not have the
  3403. * payload yet, thus notify the fabric via a call to ->write_pending instead.
  3404. */
  3405. int transport_generic_new_cmd(struct se_cmd *cmd)
  3406. {
  3407. struct se_device *dev = cmd->se_dev;
  3408. int task_cdbs, task_cdbs_bidi = 0;
  3409. int set_counts = 1;
  3410. int ret = 0;
  3411. /*
  3412. * Determine is the TCM fabric module has already allocated physical
  3413. * memory, and is directly calling transport_generic_map_mem_to_cmd()
  3414. * beforehand.
  3415. */
  3416. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
  3417. cmd->data_length) {
  3418. ret = transport_generic_get_mem(cmd);
  3419. if (ret < 0)
  3420. goto out_fail;
  3421. }
  3422. /*
  3423. * For BIDI command set up the read tasks first.
  3424. */
  3425. if (cmd->t_bidi_data_sg &&
  3426. dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
  3427. BUG_ON(!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB));
  3428. task_cdbs_bidi = transport_allocate_data_tasks(cmd,
  3429. DMA_FROM_DEVICE, cmd->t_bidi_data_sg,
  3430. cmd->t_bidi_data_nents);
  3431. if (task_cdbs_bidi <= 0)
  3432. goto out_fail;
  3433. atomic_inc(&cmd->t_fe_count);
  3434. atomic_inc(&cmd->t_se_count);
  3435. set_counts = 0;
  3436. }
  3437. if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
  3438. task_cdbs = transport_allocate_data_tasks(cmd,
  3439. cmd->data_direction, cmd->t_data_sg,
  3440. cmd->t_data_nents);
  3441. } else {
  3442. task_cdbs = transport_allocate_control_task(cmd);
  3443. }
  3444. if (task_cdbs < 0)
  3445. goto out_fail;
  3446. else if (!task_cdbs && (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)) {
  3447. cmd->t_state = TRANSPORT_COMPLETE;
  3448. atomic_set(&cmd->t_transport_active, 1);
  3449. if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
  3450. u8 ua_asc = 0, ua_ascq = 0;
  3451. core_scsi3_ua_clear_for_request_sense(cmd,
  3452. &ua_asc, &ua_ascq);
  3453. }
  3454. INIT_WORK(&cmd->work, target_complete_ok_work);
  3455. queue_work(target_completion_wq, &cmd->work);
  3456. return 0;
  3457. }
  3458. if (set_counts) {
  3459. atomic_inc(&cmd->t_fe_count);
  3460. atomic_inc(&cmd->t_se_count);
  3461. }
  3462. cmd->t_task_list_num = (task_cdbs + task_cdbs_bidi);
  3463. atomic_set(&cmd->t_task_cdbs_left, cmd->t_task_list_num);
  3464. atomic_set(&cmd->t_task_cdbs_ex_left, cmd->t_task_list_num);
  3465. /*
  3466. * For WRITEs, let the fabric know its buffer is ready..
  3467. * This WRITE struct se_cmd (and all of its associated struct se_task's)
  3468. * will be added to the struct se_device execution queue after its WRITE
  3469. * data has arrived. (ie: It gets handled by the transport processing
  3470. * thread a second time)
  3471. */
  3472. if (cmd->data_direction == DMA_TO_DEVICE) {
  3473. transport_add_tasks_to_state_queue(cmd);
  3474. return transport_generic_write_pending(cmd);
  3475. }
  3476. /*
  3477. * Everything else but a WRITE, add the struct se_cmd's struct se_task's
  3478. * to the execution queue.
  3479. */
  3480. transport_execute_tasks(cmd);
  3481. return 0;
  3482. out_fail:
  3483. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3484. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3485. return -EINVAL;
  3486. }
  3487. EXPORT_SYMBOL(transport_generic_new_cmd);
  3488. /* transport_generic_process_write():
  3489. *
  3490. *
  3491. */
  3492. void transport_generic_process_write(struct se_cmd *cmd)
  3493. {
  3494. transport_execute_tasks(cmd);
  3495. }
  3496. EXPORT_SYMBOL(transport_generic_process_write);
  3497. static void transport_write_pending_qf(struct se_cmd *cmd)
  3498. {
  3499. int ret;
  3500. ret = cmd->se_tfo->write_pending(cmd);
  3501. if (ret == -EAGAIN || ret == -ENOMEM) {
  3502. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
  3503. cmd);
  3504. transport_handle_queue_full(cmd, cmd->se_dev);
  3505. }
  3506. }
  3507. static int transport_generic_write_pending(struct se_cmd *cmd)
  3508. {
  3509. unsigned long flags;
  3510. int ret;
  3511. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3512. cmd->t_state = TRANSPORT_WRITE_PENDING;
  3513. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3514. /*
  3515. * Clear the se_cmd for WRITE_PENDING status in order to set
  3516. * cmd->t_transport_active=0 so that transport_generic_handle_data
  3517. * can be called from HW target mode interrupt code. This is safe
  3518. * to be called with transport_off=1 before the cmd->se_tfo->write_pending
  3519. * because the se_cmd->se_lun pointer is not being cleared.
  3520. */
  3521. transport_cmd_check_stop(cmd, 1, 0);
  3522. /*
  3523. * Call the fabric write_pending function here to let the
  3524. * frontend know that WRITE buffers are ready.
  3525. */
  3526. ret = cmd->se_tfo->write_pending(cmd);
  3527. if (ret == -EAGAIN || ret == -ENOMEM)
  3528. goto queue_full;
  3529. else if (ret < 0)
  3530. return ret;
  3531. return 1;
  3532. queue_full:
  3533. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
  3534. cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
  3535. transport_handle_queue_full(cmd, cmd->se_dev);
  3536. return 0;
  3537. }
  3538. void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
  3539. {
  3540. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
  3541. if (wait_for_tasks && cmd->se_tmr_req)
  3542. transport_wait_for_tasks(cmd);
  3543. transport_release_cmd(cmd);
  3544. } else {
  3545. if (wait_for_tasks)
  3546. transport_wait_for_tasks(cmd);
  3547. core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
  3548. if (cmd->se_lun)
  3549. transport_lun_remove_cmd(cmd);
  3550. transport_free_dev_tasks(cmd);
  3551. transport_put_cmd(cmd);
  3552. }
  3553. }
  3554. EXPORT_SYMBOL(transport_generic_free_cmd);
  3555. /* target_get_sess_cmd - Add command to active ->sess_cmd_list
  3556. * @se_sess: session to reference
  3557. * @se_cmd: command descriptor to add
  3558. * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
  3559. */
  3560. void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
  3561. bool ack_kref)
  3562. {
  3563. unsigned long flags;
  3564. kref_init(&se_cmd->cmd_kref);
  3565. /*
  3566. * Add a second kref if the fabric caller is expecting to handle
  3567. * fabric acknowledgement that requires two target_put_sess_cmd()
  3568. * invocations before se_cmd descriptor release.
  3569. */
  3570. if (ack_kref == true)
  3571. kref_get(&se_cmd->cmd_kref);
  3572. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3573. list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
  3574. se_cmd->check_release = 1;
  3575. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3576. }
  3577. EXPORT_SYMBOL(target_get_sess_cmd);
  3578. static void target_release_cmd_kref(struct kref *kref)
  3579. {
  3580. struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
  3581. struct se_session *se_sess = se_cmd->se_sess;
  3582. unsigned long flags;
  3583. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3584. if (list_empty(&se_cmd->se_cmd_list)) {
  3585. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3586. WARN_ON(1);
  3587. return;
  3588. }
  3589. if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
  3590. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3591. complete(&se_cmd->cmd_wait_comp);
  3592. return;
  3593. }
  3594. list_del(&se_cmd->se_cmd_list);
  3595. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3596. se_cmd->se_tfo->release_cmd(se_cmd);
  3597. }
  3598. /* target_put_sess_cmd - Check for active I/O shutdown via kref_put
  3599. * @se_sess: session to reference
  3600. * @se_cmd: command descriptor to drop
  3601. */
  3602. int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
  3603. {
  3604. return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
  3605. }
  3606. EXPORT_SYMBOL(target_put_sess_cmd);
  3607. /* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
  3608. * @se_sess: session to split
  3609. */
  3610. void target_splice_sess_cmd_list(struct se_session *se_sess)
  3611. {
  3612. struct se_cmd *se_cmd;
  3613. unsigned long flags;
  3614. WARN_ON(!list_empty(&se_sess->sess_wait_list));
  3615. INIT_LIST_HEAD(&se_sess->sess_wait_list);
  3616. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3617. se_sess->sess_tearing_down = 1;
  3618. list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
  3619. list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
  3620. se_cmd->cmd_wait_set = 1;
  3621. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3622. }
  3623. EXPORT_SYMBOL(target_splice_sess_cmd_list);
  3624. /* target_wait_for_sess_cmds - Wait for outstanding descriptors
  3625. * @se_sess: session to wait for active I/O
  3626. * @wait_for_tasks: Make extra transport_wait_for_tasks call
  3627. */
  3628. void target_wait_for_sess_cmds(
  3629. struct se_session *se_sess,
  3630. int wait_for_tasks)
  3631. {
  3632. struct se_cmd *se_cmd, *tmp_cmd;
  3633. bool rc = false;
  3634. list_for_each_entry_safe(se_cmd, tmp_cmd,
  3635. &se_sess->sess_wait_list, se_cmd_list) {
  3636. list_del(&se_cmd->se_cmd_list);
  3637. pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
  3638. " %d\n", se_cmd, se_cmd->t_state,
  3639. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3640. if (wait_for_tasks) {
  3641. pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
  3642. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3643. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3644. rc = transport_wait_for_tasks(se_cmd);
  3645. pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
  3646. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3647. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3648. }
  3649. if (!rc) {
  3650. wait_for_completion(&se_cmd->cmd_wait_comp);
  3651. pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
  3652. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3653. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3654. }
  3655. se_cmd->se_tfo->release_cmd(se_cmd);
  3656. }
  3657. }
  3658. EXPORT_SYMBOL(target_wait_for_sess_cmds);
  3659. /* transport_lun_wait_for_tasks():
  3660. *
  3661. * Called from ConfigFS context to stop the passed struct se_cmd to allow
  3662. * an struct se_lun to be successfully shutdown.
  3663. */
  3664. static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
  3665. {
  3666. unsigned long flags;
  3667. int ret;
  3668. /*
  3669. * If the frontend has already requested this struct se_cmd to
  3670. * be stopped, we can safely ignore this struct se_cmd.
  3671. */
  3672. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3673. if (atomic_read(&cmd->t_transport_stop)) {
  3674. atomic_set(&cmd->transport_lun_stop, 0);
  3675. pr_debug("ConfigFS ITT[0x%08x] - t_transport_stop =="
  3676. " TRUE, skipping\n", cmd->se_tfo->get_task_tag(cmd));
  3677. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3678. transport_cmd_check_stop(cmd, 1, 0);
  3679. return -EPERM;
  3680. }
  3681. atomic_set(&cmd->transport_lun_fe_stop, 1);
  3682. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3683. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  3684. ret = transport_stop_tasks_for_cmd(cmd);
  3685. pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
  3686. " %d\n", cmd, cmd->t_task_list_num, ret);
  3687. if (!ret) {
  3688. pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
  3689. cmd->se_tfo->get_task_tag(cmd));
  3690. wait_for_completion(&cmd->transport_lun_stop_comp);
  3691. pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
  3692. cmd->se_tfo->get_task_tag(cmd));
  3693. }
  3694. transport_remove_cmd_from_queue(cmd);
  3695. return 0;
  3696. }
  3697. static void __transport_clear_lun_from_sessions(struct se_lun *lun)
  3698. {
  3699. struct se_cmd *cmd = NULL;
  3700. unsigned long lun_flags, cmd_flags;
  3701. /*
  3702. * Do exception processing and return CHECK_CONDITION status to the
  3703. * Initiator Port.
  3704. */
  3705. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3706. while (!list_empty(&lun->lun_cmd_list)) {
  3707. cmd = list_first_entry(&lun->lun_cmd_list,
  3708. struct se_cmd, se_lun_node);
  3709. list_del(&cmd->se_lun_node);
  3710. atomic_set(&cmd->transport_lun_active, 0);
  3711. /*
  3712. * This will notify iscsi_target_transport.c:
  3713. * transport_cmd_check_stop() that a LUN shutdown is in
  3714. * progress for the iscsi_cmd_t.
  3715. */
  3716. spin_lock(&cmd->t_state_lock);
  3717. pr_debug("SE_LUN[%d] - Setting cmd->transport"
  3718. "_lun_stop for ITT: 0x%08x\n",
  3719. cmd->se_lun->unpacked_lun,
  3720. cmd->se_tfo->get_task_tag(cmd));
  3721. atomic_set(&cmd->transport_lun_stop, 1);
  3722. spin_unlock(&cmd->t_state_lock);
  3723. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  3724. if (!cmd->se_lun) {
  3725. pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
  3726. cmd->se_tfo->get_task_tag(cmd),
  3727. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
  3728. BUG();
  3729. }
  3730. /*
  3731. * If the Storage engine still owns the iscsi_cmd_t, determine
  3732. * and/or stop its context.
  3733. */
  3734. pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
  3735. "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
  3736. cmd->se_tfo->get_task_tag(cmd));
  3737. if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
  3738. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3739. continue;
  3740. }
  3741. pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
  3742. "_wait_for_tasks(): SUCCESS\n",
  3743. cmd->se_lun->unpacked_lun,
  3744. cmd->se_tfo->get_task_tag(cmd));
  3745. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  3746. if (!atomic_read(&cmd->transport_dev_active)) {
  3747. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3748. goto check_cond;
  3749. }
  3750. atomic_set(&cmd->transport_dev_active, 0);
  3751. transport_all_task_dev_remove_state(cmd);
  3752. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3753. transport_free_dev_tasks(cmd);
  3754. /*
  3755. * The Storage engine stopped this struct se_cmd before it was
  3756. * send to the fabric frontend for delivery back to the
  3757. * Initiator Node. Return this SCSI CDB back with an
  3758. * CHECK_CONDITION status.
  3759. */
  3760. check_cond:
  3761. transport_send_check_condition_and_sense(cmd,
  3762. TCM_NON_EXISTENT_LUN, 0);
  3763. /*
  3764. * If the fabric frontend is waiting for this iscsi_cmd_t to
  3765. * be released, notify the waiting thread now that LU has
  3766. * finished accessing it.
  3767. */
  3768. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  3769. if (atomic_read(&cmd->transport_lun_fe_stop)) {
  3770. pr_debug("SE_LUN[%d] - Detected FE stop for"
  3771. " struct se_cmd: %p ITT: 0x%08x\n",
  3772. lun->unpacked_lun,
  3773. cmd, cmd->se_tfo->get_task_tag(cmd));
  3774. spin_unlock_irqrestore(&cmd->t_state_lock,
  3775. cmd_flags);
  3776. transport_cmd_check_stop(cmd, 1, 0);
  3777. complete(&cmd->transport_lun_fe_stop_comp);
  3778. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3779. continue;
  3780. }
  3781. pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
  3782. lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
  3783. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3784. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3785. }
  3786. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  3787. }
  3788. static int transport_clear_lun_thread(void *p)
  3789. {
  3790. struct se_lun *lun = p;
  3791. __transport_clear_lun_from_sessions(lun);
  3792. complete(&lun->lun_shutdown_comp);
  3793. return 0;
  3794. }
  3795. int transport_clear_lun_from_sessions(struct se_lun *lun)
  3796. {
  3797. struct task_struct *kt;
  3798. kt = kthread_run(transport_clear_lun_thread, lun,
  3799. "tcm_cl_%u", lun->unpacked_lun);
  3800. if (IS_ERR(kt)) {
  3801. pr_err("Unable to start clear_lun thread\n");
  3802. return PTR_ERR(kt);
  3803. }
  3804. wait_for_completion(&lun->lun_shutdown_comp);
  3805. return 0;
  3806. }
  3807. /**
  3808. * transport_wait_for_tasks - wait for completion to occur
  3809. * @cmd: command to wait
  3810. *
  3811. * Called from frontend fabric context to wait for storage engine
  3812. * to pause and/or release frontend generated struct se_cmd.
  3813. */
  3814. bool transport_wait_for_tasks(struct se_cmd *cmd)
  3815. {
  3816. unsigned long flags;
  3817. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3818. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req)) {
  3819. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3820. return false;
  3821. }
  3822. /*
  3823. * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
  3824. * has been set in transport_set_supported_SAM_opcode().
  3825. */
  3826. if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) && !cmd->se_tmr_req) {
  3827. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3828. return false;
  3829. }
  3830. /*
  3831. * If we are already stopped due to an external event (ie: LUN shutdown)
  3832. * sleep until the connection can have the passed struct se_cmd back.
  3833. * The cmd->transport_lun_stopped_sem will be upped by
  3834. * transport_clear_lun_from_sessions() once the ConfigFS context caller
  3835. * has completed its operation on the struct se_cmd.
  3836. */
  3837. if (atomic_read(&cmd->transport_lun_stop)) {
  3838. pr_debug("wait_for_tasks: Stopping"
  3839. " wait_for_completion(&cmd->t_tasktransport_lun_fe"
  3840. "_stop_comp); for ITT: 0x%08x\n",
  3841. cmd->se_tfo->get_task_tag(cmd));
  3842. /*
  3843. * There is a special case for WRITES where a FE exception +
  3844. * LUN shutdown means ConfigFS context is still sleeping on
  3845. * transport_lun_stop_comp in transport_lun_wait_for_tasks().
  3846. * We go ahead and up transport_lun_stop_comp just to be sure
  3847. * here.
  3848. */
  3849. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3850. complete(&cmd->transport_lun_stop_comp);
  3851. wait_for_completion(&cmd->transport_lun_fe_stop_comp);
  3852. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3853. transport_all_task_dev_remove_state(cmd);
  3854. /*
  3855. * At this point, the frontend who was the originator of this
  3856. * struct se_cmd, now owns the structure and can be released through
  3857. * normal means below.
  3858. */
  3859. pr_debug("wait_for_tasks: Stopped"
  3860. " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
  3861. "stop_comp); for ITT: 0x%08x\n",
  3862. cmd->se_tfo->get_task_tag(cmd));
  3863. atomic_set(&cmd->transport_lun_stop, 0);
  3864. }
  3865. if (!atomic_read(&cmd->t_transport_active) ||
  3866. atomic_read(&cmd->t_transport_aborted)) {
  3867. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3868. return false;
  3869. }
  3870. atomic_set(&cmd->t_transport_stop, 1);
  3871. pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
  3872. " i_state: %d, t_state: %d, t_transport_stop = TRUE\n",
  3873. cmd, cmd->se_tfo->get_task_tag(cmd),
  3874. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
  3875. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3876. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  3877. wait_for_completion(&cmd->t_transport_stop_comp);
  3878. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3879. atomic_set(&cmd->t_transport_active, 0);
  3880. atomic_set(&cmd->t_transport_stop, 0);
  3881. pr_debug("wait_for_tasks: Stopped wait_for_compltion("
  3882. "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
  3883. cmd->se_tfo->get_task_tag(cmd));
  3884. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3885. return true;
  3886. }
  3887. EXPORT_SYMBOL(transport_wait_for_tasks);
  3888. static int transport_get_sense_codes(
  3889. struct se_cmd *cmd,
  3890. u8 *asc,
  3891. u8 *ascq)
  3892. {
  3893. *asc = cmd->scsi_asc;
  3894. *ascq = cmd->scsi_ascq;
  3895. return 0;
  3896. }
  3897. static int transport_set_sense_codes(
  3898. struct se_cmd *cmd,
  3899. u8 asc,
  3900. u8 ascq)
  3901. {
  3902. cmd->scsi_asc = asc;
  3903. cmd->scsi_ascq = ascq;
  3904. return 0;
  3905. }
  3906. int transport_send_check_condition_and_sense(
  3907. struct se_cmd *cmd,
  3908. u8 reason,
  3909. int from_transport)
  3910. {
  3911. unsigned char *buffer = cmd->sense_buffer;
  3912. unsigned long flags;
  3913. int offset;
  3914. u8 asc = 0, ascq = 0;
  3915. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3916. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  3917. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3918. return 0;
  3919. }
  3920. cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
  3921. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3922. if (!reason && from_transport)
  3923. goto after_reason;
  3924. if (!from_transport)
  3925. cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
  3926. /*
  3927. * Data Segment and SenseLength of the fabric response PDU.
  3928. *
  3929. * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
  3930. * from include/scsi/scsi_cmnd.h
  3931. */
  3932. offset = cmd->se_tfo->set_fabric_sense_len(cmd,
  3933. TRANSPORT_SENSE_BUFFER);
  3934. /*
  3935. * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
  3936. * SENSE KEY values from include/scsi/scsi.h
  3937. */
  3938. switch (reason) {
  3939. case TCM_NON_EXISTENT_LUN:
  3940. /* CURRENT ERROR */
  3941. buffer[offset] = 0x70;
  3942. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3943. /* ILLEGAL REQUEST */
  3944. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3945. /* LOGICAL UNIT NOT SUPPORTED */
  3946. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
  3947. break;
  3948. case TCM_UNSUPPORTED_SCSI_OPCODE:
  3949. case TCM_SECTOR_COUNT_TOO_MANY:
  3950. /* CURRENT ERROR */
  3951. buffer[offset] = 0x70;
  3952. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3953. /* ILLEGAL REQUEST */
  3954. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3955. /* INVALID COMMAND OPERATION CODE */
  3956. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
  3957. break;
  3958. case TCM_UNKNOWN_MODE_PAGE:
  3959. /* CURRENT ERROR */
  3960. buffer[offset] = 0x70;
  3961. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3962. /* ILLEGAL REQUEST */
  3963. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3964. /* INVALID FIELD IN CDB */
  3965. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  3966. break;
  3967. case TCM_CHECK_CONDITION_ABORT_CMD:
  3968. /* CURRENT ERROR */
  3969. buffer[offset] = 0x70;
  3970. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3971. /* ABORTED COMMAND */
  3972. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3973. /* BUS DEVICE RESET FUNCTION OCCURRED */
  3974. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
  3975. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
  3976. break;
  3977. case TCM_INCORRECT_AMOUNT_OF_DATA:
  3978. /* CURRENT ERROR */
  3979. buffer[offset] = 0x70;
  3980. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3981. /* ABORTED COMMAND */
  3982. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3983. /* WRITE ERROR */
  3984. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  3985. /* NOT ENOUGH UNSOLICITED DATA */
  3986. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
  3987. break;
  3988. case TCM_INVALID_CDB_FIELD:
  3989. /* CURRENT ERROR */
  3990. buffer[offset] = 0x70;
  3991. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3992. /* ILLEGAL REQUEST */
  3993. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3994. /* INVALID FIELD IN CDB */
  3995. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  3996. break;
  3997. case TCM_INVALID_PARAMETER_LIST:
  3998. /* CURRENT ERROR */
  3999. buffer[offset] = 0x70;
  4000. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4001. /* ILLEGAL REQUEST */
  4002. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4003. /* INVALID FIELD IN PARAMETER LIST */
  4004. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
  4005. break;
  4006. case TCM_UNEXPECTED_UNSOLICITED_DATA:
  4007. /* CURRENT ERROR */
  4008. buffer[offset] = 0x70;
  4009. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4010. /* ABORTED COMMAND */
  4011. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4012. /* WRITE ERROR */
  4013. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  4014. /* UNEXPECTED_UNSOLICITED_DATA */
  4015. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
  4016. break;
  4017. case TCM_SERVICE_CRC_ERROR:
  4018. /* CURRENT ERROR */
  4019. buffer[offset] = 0x70;
  4020. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4021. /* ABORTED COMMAND */
  4022. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4023. /* PROTOCOL SERVICE CRC ERROR */
  4024. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
  4025. /* N/A */
  4026. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
  4027. break;
  4028. case TCM_SNACK_REJECTED:
  4029. /* CURRENT ERROR */
  4030. buffer[offset] = 0x70;
  4031. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4032. /* ABORTED COMMAND */
  4033. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4034. /* READ ERROR */
  4035. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
  4036. /* FAILED RETRANSMISSION REQUEST */
  4037. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
  4038. break;
  4039. case TCM_WRITE_PROTECTED:
  4040. /* CURRENT ERROR */
  4041. buffer[offset] = 0x70;
  4042. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4043. /* DATA PROTECT */
  4044. buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
  4045. /* WRITE PROTECTED */
  4046. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
  4047. break;
  4048. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  4049. /* CURRENT ERROR */
  4050. buffer[offset] = 0x70;
  4051. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4052. /* UNIT ATTENTION */
  4053. buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  4054. core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
  4055. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  4056. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  4057. break;
  4058. case TCM_CHECK_CONDITION_NOT_READY:
  4059. /* CURRENT ERROR */
  4060. buffer[offset] = 0x70;
  4061. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4062. /* Not Ready */
  4063. buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
  4064. transport_get_sense_codes(cmd, &asc, &ascq);
  4065. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  4066. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  4067. break;
  4068. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  4069. default:
  4070. /* CURRENT ERROR */
  4071. buffer[offset] = 0x70;
  4072. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4073. /* ILLEGAL REQUEST */
  4074. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4075. /* LOGICAL UNIT COMMUNICATION FAILURE */
  4076. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
  4077. break;
  4078. }
  4079. /*
  4080. * This code uses linux/include/scsi/scsi.h SAM status codes!
  4081. */
  4082. cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
  4083. /*
  4084. * Automatically padded, this value is encoded in the fabric's
  4085. * data_length response PDU containing the SCSI defined sense data.
  4086. */
  4087. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
  4088. after_reason:
  4089. return cmd->se_tfo->queue_status(cmd);
  4090. }
  4091. EXPORT_SYMBOL(transport_send_check_condition_and_sense);
  4092. int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
  4093. {
  4094. int ret = 0;
  4095. if (atomic_read(&cmd->t_transport_aborted) != 0) {
  4096. if (!send_status ||
  4097. (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
  4098. return 1;
  4099. #if 0
  4100. pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
  4101. " status for CDB: 0x%02x ITT: 0x%08x\n",
  4102. cmd->t_task_cdb[0],
  4103. cmd->se_tfo->get_task_tag(cmd));
  4104. #endif
  4105. cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
  4106. cmd->se_tfo->queue_status(cmd);
  4107. ret = 1;
  4108. }
  4109. return ret;
  4110. }
  4111. EXPORT_SYMBOL(transport_check_aborted_status);
  4112. void transport_send_task_abort(struct se_cmd *cmd)
  4113. {
  4114. unsigned long flags;
  4115. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4116. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  4117. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4118. return;
  4119. }
  4120. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4121. /*
  4122. * If there are still expected incoming fabric WRITEs, we wait
  4123. * until until they have completed before sending a TASK_ABORTED
  4124. * response. This response with TASK_ABORTED status will be
  4125. * queued back to fabric module by transport_check_aborted_status().
  4126. */
  4127. if (cmd->data_direction == DMA_TO_DEVICE) {
  4128. if (cmd->se_tfo->write_pending_status(cmd) != 0) {
  4129. atomic_inc(&cmd->t_transport_aborted);
  4130. smp_mb__after_atomic_inc();
  4131. }
  4132. }
  4133. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  4134. #if 0
  4135. pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
  4136. " ITT: 0x%08x\n", cmd->t_task_cdb[0],
  4137. cmd->se_tfo->get_task_tag(cmd));
  4138. #endif
  4139. cmd->se_tfo->queue_status(cmd);
  4140. }
  4141. static int transport_generic_do_tmr(struct se_cmd *cmd)
  4142. {
  4143. struct se_device *dev = cmd->se_dev;
  4144. struct se_tmr_req *tmr = cmd->se_tmr_req;
  4145. int ret;
  4146. switch (tmr->function) {
  4147. case TMR_ABORT_TASK:
  4148. tmr->response = TMR_FUNCTION_REJECTED;
  4149. break;
  4150. case TMR_ABORT_TASK_SET:
  4151. case TMR_CLEAR_ACA:
  4152. case TMR_CLEAR_TASK_SET:
  4153. tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  4154. break;
  4155. case TMR_LUN_RESET:
  4156. ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
  4157. tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
  4158. TMR_FUNCTION_REJECTED;
  4159. break;
  4160. case TMR_TARGET_WARM_RESET:
  4161. tmr->response = TMR_FUNCTION_REJECTED;
  4162. break;
  4163. case TMR_TARGET_COLD_RESET:
  4164. tmr->response = TMR_FUNCTION_REJECTED;
  4165. break;
  4166. default:
  4167. pr_err("Uknown TMR function: 0x%02x.\n",
  4168. tmr->function);
  4169. tmr->response = TMR_FUNCTION_REJECTED;
  4170. break;
  4171. }
  4172. cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
  4173. cmd->se_tfo->queue_tm_rsp(cmd);
  4174. transport_cmd_check_stop_to_fabric(cmd);
  4175. return 0;
  4176. }
  4177. /* transport_processing_thread():
  4178. *
  4179. *
  4180. */
  4181. static int transport_processing_thread(void *param)
  4182. {
  4183. int ret;
  4184. struct se_cmd *cmd;
  4185. struct se_device *dev = param;
  4186. while (!kthread_should_stop()) {
  4187. ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
  4188. atomic_read(&dev->dev_queue_obj.queue_cnt) ||
  4189. kthread_should_stop());
  4190. if (ret < 0)
  4191. goto out;
  4192. get_cmd:
  4193. cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
  4194. if (!cmd)
  4195. continue;
  4196. switch (cmd->t_state) {
  4197. case TRANSPORT_NEW_CMD:
  4198. BUG();
  4199. break;
  4200. case TRANSPORT_NEW_CMD_MAP:
  4201. if (!cmd->se_tfo->new_cmd_map) {
  4202. pr_err("cmd->se_tfo->new_cmd_map is"
  4203. " NULL for TRANSPORT_NEW_CMD_MAP\n");
  4204. BUG();
  4205. }
  4206. ret = cmd->se_tfo->new_cmd_map(cmd);
  4207. if (ret < 0) {
  4208. transport_generic_request_failure(cmd);
  4209. break;
  4210. }
  4211. ret = transport_generic_new_cmd(cmd);
  4212. if (ret < 0) {
  4213. transport_generic_request_failure(cmd);
  4214. break;
  4215. }
  4216. break;
  4217. case TRANSPORT_PROCESS_WRITE:
  4218. transport_generic_process_write(cmd);
  4219. break;
  4220. case TRANSPORT_PROCESS_TMR:
  4221. transport_generic_do_tmr(cmd);
  4222. break;
  4223. case TRANSPORT_COMPLETE_QF_WP:
  4224. transport_write_pending_qf(cmd);
  4225. break;
  4226. case TRANSPORT_COMPLETE_QF_OK:
  4227. transport_complete_qf(cmd);
  4228. break;
  4229. default:
  4230. pr_err("Unknown t_state: %d for ITT: 0x%08x "
  4231. "i_state: %d on SE LUN: %u\n",
  4232. cmd->t_state,
  4233. cmd->se_tfo->get_task_tag(cmd),
  4234. cmd->se_tfo->get_cmd_state(cmd),
  4235. cmd->se_lun->unpacked_lun);
  4236. BUG();
  4237. }
  4238. goto get_cmd;
  4239. }
  4240. out:
  4241. WARN_ON(!list_empty(&dev->state_task_list));
  4242. WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
  4243. dev->process_thread = NULL;
  4244. return 0;
  4245. }