target_core_transport.c 132 KB

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