target_core_transport.c 129 KB

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