target_core_transport.c 127 KB

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