target_core_transport.c 133 KB

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