target_core_transport.c 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863
  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. static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
  1577. {
  1578. struct se_task *task, *task_tmp;
  1579. unsigned long flags;
  1580. int ret = 0;
  1581. pr_debug("ITT[0x%08x] - Stopping tasks\n",
  1582. cmd->se_tfo->get_task_tag(cmd));
  1583. /*
  1584. * No tasks remain in the execution queue
  1585. */
  1586. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1587. list_for_each_entry_safe(task, task_tmp,
  1588. &cmd->t_task_list, t_list) {
  1589. pr_debug("Processing task %p\n", task);
  1590. /*
  1591. * If the struct se_task has not been sent and is not active,
  1592. * remove the struct se_task from the execution queue.
  1593. */
  1594. if (!(task->task_flags & (TF_ACTIVE | TF_SENT))) {
  1595. spin_unlock_irqrestore(&cmd->t_state_lock,
  1596. flags);
  1597. transport_remove_task_from_execute_queue(task,
  1598. cmd->se_dev);
  1599. pr_debug("Task %p removed from execute queue\n", task);
  1600. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1601. continue;
  1602. }
  1603. /*
  1604. * If the struct se_task is active, sleep until it is returned
  1605. * from the plugin.
  1606. */
  1607. if (task->task_flags & TF_ACTIVE) {
  1608. task->task_flags |= TF_REQUEST_STOP;
  1609. spin_unlock_irqrestore(&cmd->t_state_lock,
  1610. flags);
  1611. pr_debug("Task %p waiting to complete\n", task);
  1612. wait_for_completion(&task->task_stop_comp);
  1613. pr_debug("Task %p stopped successfully\n", task);
  1614. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1615. atomic_dec(&cmd->t_task_cdbs_left);
  1616. task->task_flags &= ~(TF_ACTIVE | TF_REQUEST_STOP);
  1617. } else {
  1618. pr_debug("Task %p - did nothing\n", task);
  1619. ret++;
  1620. }
  1621. __transport_stop_task_timer(task, &flags);
  1622. }
  1623. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1624. return ret;
  1625. }
  1626. /*
  1627. * Handle SAM-esque emulation for generic transport request failures.
  1628. */
  1629. static void transport_generic_request_failure(
  1630. struct se_cmd *cmd,
  1631. struct se_device *dev,
  1632. int complete,
  1633. int sc)
  1634. {
  1635. int ret = 0;
  1636. pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
  1637. " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
  1638. cmd->t_task_cdb[0]);
  1639. pr_debug("-----[ i_state: %d t_state/def_t_state:"
  1640. " %d/%d transport_error_status: %d\n",
  1641. cmd->se_tfo->get_cmd_state(cmd),
  1642. cmd->t_state, cmd->deferred_t_state,
  1643. cmd->transport_error_status);
  1644. pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d"
  1645. " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
  1646. " t_transport_active: %d t_transport_stop: %d"
  1647. " t_transport_sent: %d\n", cmd->t_task_list_num,
  1648. atomic_read(&cmd->t_task_cdbs_left),
  1649. atomic_read(&cmd->t_task_cdbs_sent),
  1650. atomic_read(&cmd->t_task_cdbs_ex_left),
  1651. atomic_read(&cmd->t_transport_active),
  1652. atomic_read(&cmd->t_transport_stop),
  1653. atomic_read(&cmd->t_transport_sent));
  1654. transport_stop_all_task_timers(cmd);
  1655. if (dev)
  1656. atomic_inc(&dev->depth_left);
  1657. /*
  1658. * For SAM Task Attribute emulation for failed struct se_cmd
  1659. */
  1660. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  1661. transport_complete_task_attr(cmd);
  1662. if (complete) {
  1663. transport_direct_request_timeout(cmd);
  1664. cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
  1665. }
  1666. switch (cmd->transport_error_status) {
  1667. case PYX_TRANSPORT_UNKNOWN_SAM_OPCODE:
  1668. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1669. break;
  1670. case PYX_TRANSPORT_REQ_TOO_MANY_SECTORS:
  1671. cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY;
  1672. break;
  1673. case PYX_TRANSPORT_INVALID_CDB_FIELD:
  1674. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1675. break;
  1676. case PYX_TRANSPORT_INVALID_PARAMETER_LIST:
  1677. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1678. break;
  1679. case PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES:
  1680. if (!sc)
  1681. transport_new_cmd_failure(cmd);
  1682. /*
  1683. * Currently for PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES,
  1684. * we force this session to fall back to session
  1685. * recovery.
  1686. */
  1687. cmd->se_tfo->fall_back_to_erl0(cmd->se_sess);
  1688. cmd->se_tfo->stop_session(cmd->se_sess, 0, 0);
  1689. goto check_stop;
  1690. case PYX_TRANSPORT_LU_COMM_FAILURE:
  1691. case PYX_TRANSPORT_ILLEGAL_REQUEST:
  1692. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1693. break;
  1694. case PYX_TRANSPORT_UNKNOWN_MODE_PAGE:
  1695. cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
  1696. break;
  1697. case PYX_TRANSPORT_WRITE_PROTECTED:
  1698. cmd->scsi_sense_reason = TCM_WRITE_PROTECTED;
  1699. break;
  1700. case PYX_TRANSPORT_RESERVATION_CONFLICT:
  1701. /*
  1702. * No SENSE Data payload for this case, set SCSI Status
  1703. * and queue the response to $FABRIC_MOD.
  1704. *
  1705. * Uses linux/include/scsi/scsi.h SAM status codes defs
  1706. */
  1707. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  1708. /*
  1709. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  1710. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  1711. * CONFLICT STATUS.
  1712. *
  1713. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  1714. */
  1715. if (cmd->se_sess &&
  1716. cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
  1717. core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
  1718. cmd->orig_fe_lun, 0x2C,
  1719. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  1720. ret = cmd->se_tfo->queue_status(cmd);
  1721. if (ret == -EAGAIN)
  1722. goto queue_full;
  1723. goto check_stop;
  1724. case PYX_TRANSPORT_USE_SENSE_REASON:
  1725. /*
  1726. * struct se_cmd->scsi_sense_reason already set
  1727. */
  1728. break;
  1729. default:
  1730. pr_err("Unknown transport error for CDB 0x%02x: %d\n",
  1731. cmd->t_task_cdb[0],
  1732. cmd->transport_error_status);
  1733. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1734. break;
  1735. }
  1736. /*
  1737. * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
  1738. * make the call to transport_send_check_condition_and_sense()
  1739. * directly. Otherwise expect the fabric to make the call to
  1740. * transport_send_check_condition_and_sense() after handling
  1741. * possible unsoliticied write data payloads.
  1742. */
  1743. if (!sc && !cmd->se_tfo->new_cmd_map)
  1744. transport_new_cmd_failure(cmd);
  1745. else {
  1746. ret = transport_send_check_condition_and_sense(cmd,
  1747. cmd->scsi_sense_reason, 0);
  1748. if (ret == -EAGAIN)
  1749. goto queue_full;
  1750. }
  1751. check_stop:
  1752. transport_lun_remove_cmd(cmd);
  1753. if (!transport_cmd_check_stop_to_fabric(cmd))
  1754. ;
  1755. return;
  1756. queue_full:
  1757. cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
  1758. transport_handle_queue_full(cmd, cmd->se_dev);
  1759. }
  1760. static void transport_direct_request_timeout(struct se_cmd *cmd)
  1761. {
  1762. unsigned long flags;
  1763. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1764. if (!atomic_read(&cmd->t_transport_timeout)) {
  1765. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1766. return;
  1767. }
  1768. if (atomic_read(&cmd->t_task_cdbs_timeout_left)) {
  1769. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1770. return;
  1771. }
  1772. atomic_sub(atomic_read(&cmd->t_transport_timeout),
  1773. &cmd->t_se_count);
  1774. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1775. }
  1776. static void transport_generic_request_timeout(struct se_cmd *cmd)
  1777. {
  1778. unsigned long flags;
  1779. /*
  1780. * Reset cmd->t_se_count to allow transport_put_cmd()
  1781. * to allow last call to free memory resources.
  1782. */
  1783. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1784. if (atomic_read(&cmd->t_transport_timeout) > 1) {
  1785. int tmp = (atomic_read(&cmd->t_transport_timeout) - 1);
  1786. atomic_sub(tmp, &cmd->t_se_count);
  1787. }
  1788. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1789. transport_put_cmd(cmd);
  1790. }
  1791. static inline u32 transport_lba_21(unsigned char *cdb)
  1792. {
  1793. return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
  1794. }
  1795. static inline u32 transport_lba_32(unsigned char *cdb)
  1796. {
  1797. return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1798. }
  1799. static inline unsigned long long transport_lba_64(unsigned char *cdb)
  1800. {
  1801. unsigned int __v1, __v2;
  1802. __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1803. __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1804. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  1805. }
  1806. /*
  1807. * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
  1808. */
  1809. static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
  1810. {
  1811. unsigned int __v1, __v2;
  1812. __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
  1813. __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
  1814. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  1815. }
  1816. static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
  1817. {
  1818. unsigned long flags;
  1819. spin_lock_irqsave(&se_cmd->t_state_lock, flags);
  1820. se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
  1821. spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
  1822. }
  1823. /*
  1824. * Called from interrupt context.
  1825. */
  1826. static void transport_task_timeout_handler(unsigned long data)
  1827. {
  1828. struct se_task *task = (struct se_task *)data;
  1829. struct se_cmd *cmd = task->task_se_cmd;
  1830. unsigned long flags;
  1831. pr_debug("transport task timeout fired! task: %p cmd: %p\n", task, cmd);
  1832. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1833. if (task->task_flags & TF_TIMER_STOP) {
  1834. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1835. return;
  1836. }
  1837. task->task_flags &= ~TF_TIMER_RUNNING;
  1838. /*
  1839. * Determine if transport_complete_task() has already been called.
  1840. */
  1841. if (!(task->task_flags & TF_ACTIVE)) {
  1842. pr_debug("transport task: %p cmd: %p timeout !TF_ACTIVE\n",
  1843. task, cmd);
  1844. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1845. return;
  1846. }
  1847. atomic_inc(&cmd->t_se_count);
  1848. atomic_inc(&cmd->t_transport_timeout);
  1849. cmd->t_tasks_failed = 1;
  1850. task->task_flags |= TF_TIMEOUT;
  1851. task->task_error_status = PYX_TRANSPORT_TASK_TIMEOUT;
  1852. task->task_scsi_status = 1;
  1853. if (task->task_flags & TF_REQUEST_STOP) {
  1854. pr_debug("transport task: %p cmd: %p timeout TF_REQUEST_STOP"
  1855. " == 1\n", task, cmd);
  1856. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1857. complete(&task->task_stop_comp);
  1858. return;
  1859. }
  1860. if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
  1861. pr_debug("transport task: %p cmd: %p timeout non zero"
  1862. " t_task_cdbs_left\n", task, cmd);
  1863. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1864. return;
  1865. }
  1866. pr_debug("transport task: %p cmd: %p timeout ZERO t_task_cdbs_left\n",
  1867. task, cmd);
  1868. cmd->t_state = TRANSPORT_COMPLETE_FAILURE;
  1869. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1870. transport_add_cmd_to_queue(cmd, TRANSPORT_COMPLETE_FAILURE, false);
  1871. }
  1872. /*
  1873. * Called with cmd->t_state_lock held.
  1874. */
  1875. static void transport_start_task_timer(struct se_task *task)
  1876. {
  1877. struct se_device *dev = task->task_se_cmd->se_dev;
  1878. int timeout;
  1879. if (task->task_flags & TF_TIMER_RUNNING)
  1880. return;
  1881. /*
  1882. * If the task_timeout is disabled, exit now.
  1883. */
  1884. timeout = dev->se_sub_dev->se_dev_attrib.task_timeout;
  1885. if (!timeout)
  1886. return;
  1887. init_timer(&task->task_timer);
  1888. task->task_timer.expires = (get_jiffies_64() + timeout * HZ);
  1889. task->task_timer.data = (unsigned long) task;
  1890. task->task_timer.function = transport_task_timeout_handler;
  1891. task->task_flags |= TF_TIMER_RUNNING;
  1892. add_timer(&task->task_timer);
  1893. #if 0
  1894. pr_debug("Starting task timer for cmd: %p task: %p seconds:"
  1895. " %d\n", task->task_se_cmd, task, timeout);
  1896. #endif
  1897. }
  1898. /*
  1899. * Called with spin_lock_irq(&cmd->t_state_lock) held.
  1900. */
  1901. void __transport_stop_task_timer(struct se_task *task, unsigned long *flags)
  1902. {
  1903. struct se_cmd *cmd = task->task_se_cmd;
  1904. if (!(task->task_flags & TF_TIMER_RUNNING))
  1905. return;
  1906. task->task_flags |= TF_TIMER_STOP;
  1907. spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
  1908. del_timer_sync(&task->task_timer);
  1909. spin_lock_irqsave(&cmd->t_state_lock, *flags);
  1910. task->task_flags &= ~TF_TIMER_RUNNING;
  1911. task->task_flags &= ~TF_TIMER_STOP;
  1912. }
  1913. static void transport_stop_all_task_timers(struct se_cmd *cmd)
  1914. {
  1915. struct se_task *task = NULL, *task_tmp;
  1916. unsigned long flags;
  1917. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1918. list_for_each_entry_safe(task, task_tmp,
  1919. &cmd->t_task_list, t_list)
  1920. __transport_stop_task_timer(task, &flags);
  1921. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1922. }
  1923. static inline int transport_tcq_window_closed(struct se_device *dev)
  1924. {
  1925. if (dev->dev_tcq_window_closed++ <
  1926. PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) {
  1927. msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT);
  1928. } else
  1929. msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG);
  1930. wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
  1931. return 0;
  1932. }
  1933. /*
  1934. * Called from Fabric Module context from transport_execute_tasks()
  1935. *
  1936. * The return of this function determins if the tasks from struct se_cmd
  1937. * get added to the execution queue in transport_execute_tasks(),
  1938. * or are added to the delayed or ordered lists here.
  1939. */
  1940. static inline int transport_execute_task_attr(struct se_cmd *cmd)
  1941. {
  1942. if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  1943. return 1;
  1944. /*
  1945. * Check for the existence of HEAD_OF_QUEUE, and if true return 1
  1946. * to allow the passed struct se_cmd list of tasks to the front of the list.
  1947. */
  1948. if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  1949. atomic_inc(&cmd->se_dev->dev_hoq_count);
  1950. smp_mb__after_atomic_inc();
  1951. pr_debug("Added HEAD_OF_QUEUE for CDB:"
  1952. " 0x%02x, se_ordered_id: %u\n",
  1953. cmd->t_task_cdb[0],
  1954. cmd->se_ordered_id);
  1955. return 1;
  1956. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  1957. spin_lock(&cmd->se_dev->ordered_cmd_lock);
  1958. list_add_tail(&cmd->se_ordered_node,
  1959. &cmd->se_dev->ordered_cmd_list);
  1960. spin_unlock(&cmd->se_dev->ordered_cmd_lock);
  1961. atomic_inc(&cmd->se_dev->dev_ordered_sync);
  1962. smp_mb__after_atomic_inc();
  1963. pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
  1964. " list, se_ordered_id: %u\n",
  1965. cmd->t_task_cdb[0],
  1966. cmd->se_ordered_id);
  1967. /*
  1968. * Add ORDERED command to tail of execution queue if
  1969. * no other older commands exist that need to be
  1970. * completed first.
  1971. */
  1972. if (!atomic_read(&cmd->se_dev->simple_cmds))
  1973. return 1;
  1974. } else {
  1975. /*
  1976. * For SIMPLE and UNTAGGED Task Attribute commands
  1977. */
  1978. atomic_inc(&cmd->se_dev->simple_cmds);
  1979. smp_mb__after_atomic_inc();
  1980. }
  1981. /*
  1982. * Otherwise if one or more outstanding ORDERED task attribute exist,
  1983. * add the dormant task(s) built for the passed struct se_cmd to the
  1984. * execution queue and become in Active state for this struct se_device.
  1985. */
  1986. if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
  1987. /*
  1988. * Otherwise, add cmd w/ tasks to delayed cmd queue that
  1989. * will be drained upon completion of HEAD_OF_QUEUE task.
  1990. */
  1991. spin_lock(&cmd->se_dev->delayed_cmd_lock);
  1992. cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
  1993. list_add_tail(&cmd->se_delayed_node,
  1994. &cmd->se_dev->delayed_cmd_list);
  1995. spin_unlock(&cmd->se_dev->delayed_cmd_lock);
  1996. pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
  1997. " delayed CMD list, se_ordered_id: %u\n",
  1998. cmd->t_task_cdb[0], cmd->sam_task_attr,
  1999. cmd->se_ordered_id);
  2000. /*
  2001. * Return zero to let transport_execute_tasks() know
  2002. * not to add the delayed tasks to the execution list.
  2003. */
  2004. return 0;
  2005. }
  2006. /*
  2007. * Otherwise, no ORDERED task attributes exist..
  2008. */
  2009. return 1;
  2010. }
  2011. /*
  2012. * Called from fabric module context in transport_generic_new_cmd() and
  2013. * transport_generic_process_write()
  2014. */
  2015. static int transport_execute_tasks(struct se_cmd *cmd)
  2016. {
  2017. int add_tasks;
  2018. if (se_dev_check_online(cmd->se_orig_obj_ptr) != 0) {
  2019. cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
  2020. transport_generic_request_failure(cmd, NULL, 0, 1);
  2021. return 0;
  2022. }
  2023. /*
  2024. * Call transport_cmd_check_stop() to see if a fabric exception
  2025. * has occurred that prevents execution.
  2026. */
  2027. if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
  2028. /*
  2029. * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
  2030. * attribute for the tasks of the received struct se_cmd CDB
  2031. */
  2032. add_tasks = transport_execute_task_attr(cmd);
  2033. if (!add_tasks)
  2034. goto execute_tasks;
  2035. /*
  2036. * This calls transport_add_tasks_from_cmd() to handle
  2037. * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
  2038. * (if enabled) in __transport_add_task_to_execute_queue() and
  2039. * transport_add_task_check_sam_attr().
  2040. */
  2041. transport_add_tasks_from_cmd(cmd);
  2042. }
  2043. /*
  2044. * Kick the execution queue for the cmd associated struct se_device
  2045. * storage object.
  2046. */
  2047. execute_tasks:
  2048. __transport_execute_tasks(cmd->se_dev);
  2049. return 0;
  2050. }
  2051. /*
  2052. * Called to check struct se_device tcq depth window, and once open pull struct se_task
  2053. * from struct se_device->execute_task_list and
  2054. *
  2055. * Called from transport_processing_thread()
  2056. */
  2057. static int __transport_execute_tasks(struct se_device *dev)
  2058. {
  2059. int error;
  2060. struct se_cmd *cmd = NULL;
  2061. struct se_task *task = NULL;
  2062. unsigned long flags;
  2063. /*
  2064. * Check if there is enough room in the device and HBA queue to send
  2065. * struct se_tasks to the selected transport.
  2066. */
  2067. check_depth:
  2068. if (!atomic_read(&dev->depth_left))
  2069. return transport_tcq_window_closed(dev);
  2070. dev->dev_tcq_window_closed = 0;
  2071. spin_lock_irq(&dev->execute_task_lock);
  2072. if (list_empty(&dev->execute_task_list)) {
  2073. spin_unlock_irq(&dev->execute_task_lock);
  2074. return 0;
  2075. }
  2076. task = list_first_entry(&dev->execute_task_list,
  2077. struct se_task, t_execute_list);
  2078. __transport_remove_task_from_execute_queue(task, dev);
  2079. spin_unlock_irq(&dev->execute_task_lock);
  2080. atomic_dec(&dev->depth_left);
  2081. cmd = task->task_se_cmd;
  2082. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2083. task->task_flags |= (TF_ACTIVE | TF_SENT);
  2084. atomic_inc(&cmd->t_task_cdbs_sent);
  2085. if (atomic_read(&cmd->t_task_cdbs_sent) ==
  2086. cmd->t_task_list_num)
  2087. atomic_set(&cmd->transport_sent, 1);
  2088. transport_start_task_timer(task);
  2089. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2090. /*
  2091. * The struct se_cmd->transport_emulate_cdb() function pointer is used
  2092. * to grab REPORT_LUNS and other CDBs we want to handle before they hit the
  2093. * struct se_subsystem_api->do_task() caller below.
  2094. */
  2095. if (cmd->transport_emulate_cdb) {
  2096. error = cmd->transport_emulate_cdb(cmd);
  2097. if (error != 0) {
  2098. cmd->transport_error_status = error;
  2099. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2100. task->task_flags &= ~TF_ACTIVE;
  2101. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2102. atomic_set(&cmd->transport_sent, 0);
  2103. transport_stop_tasks_for_cmd(cmd);
  2104. transport_generic_request_failure(cmd, dev, 0, 1);
  2105. goto check_depth;
  2106. }
  2107. /*
  2108. * Handle the successful completion for transport_emulate_cdb()
  2109. * for synchronous operation, following SCF_EMULATE_CDB_ASYNC
  2110. * Otherwise the caller is expected to complete the task with
  2111. * proper status.
  2112. */
  2113. if (!(cmd->se_cmd_flags & SCF_EMULATE_CDB_ASYNC)) {
  2114. cmd->scsi_status = SAM_STAT_GOOD;
  2115. task->task_scsi_status = GOOD;
  2116. transport_complete_task(task, 1);
  2117. }
  2118. } else {
  2119. /*
  2120. * Currently for all virtual TCM plugins including IBLOCK, FILEIO and
  2121. * RAMDISK we use the internal transport_emulate_control_cdb() logic
  2122. * with struct se_subsystem_api callers for the primary SPC-3 TYPE_DISK
  2123. * LUN emulation code.
  2124. *
  2125. * For TCM/pSCSI and all other SCF_SCSI_DATA_SG_IO_CDB I/O tasks we
  2126. * call ->do_task() directly and let the underlying TCM subsystem plugin
  2127. * code handle the CDB emulation.
  2128. */
  2129. if ((dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) &&
  2130. (!(task->task_se_cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
  2131. error = transport_emulate_control_cdb(task);
  2132. else
  2133. error = dev->transport->do_task(task);
  2134. if (error != 0) {
  2135. cmd->transport_error_status = error;
  2136. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2137. task->task_flags &= ~TF_ACTIVE;
  2138. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2139. atomic_set(&cmd->transport_sent, 0);
  2140. transport_stop_tasks_for_cmd(cmd);
  2141. transport_generic_request_failure(cmd, dev, 0, 1);
  2142. }
  2143. }
  2144. goto check_depth;
  2145. return 0;
  2146. }
  2147. void transport_new_cmd_failure(struct se_cmd *se_cmd)
  2148. {
  2149. unsigned long flags;
  2150. /*
  2151. * Any unsolicited data will get dumped for failed command inside of
  2152. * the fabric plugin
  2153. */
  2154. spin_lock_irqsave(&se_cmd->t_state_lock, flags);
  2155. se_cmd->se_cmd_flags |= SCF_SE_CMD_FAILED;
  2156. se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2157. spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
  2158. }
  2159. static inline u32 transport_get_sectors_6(
  2160. unsigned char *cdb,
  2161. struct se_cmd *cmd,
  2162. int *ret)
  2163. {
  2164. struct se_device *dev = cmd->se_dev;
  2165. /*
  2166. * Assume TYPE_DISK for non struct se_device objects.
  2167. * Use 8-bit sector value.
  2168. */
  2169. if (!dev)
  2170. goto type_disk;
  2171. /*
  2172. * Use 24-bit allocation length for TYPE_TAPE.
  2173. */
  2174. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  2175. return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
  2176. /*
  2177. * Everything else assume TYPE_DISK Sector CDB location.
  2178. * Use 8-bit sector value.
  2179. */
  2180. type_disk:
  2181. return (u32)cdb[4];
  2182. }
  2183. static inline u32 transport_get_sectors_10(
  2184. unsigned char *cdb,
  2185. struct se_cmd *cmd,
  2186. int *ret)
  2187. {
  2188. struct se_device *dev = cmd->se_dev;
  2189. /*
  2190. * Assume TYPE_DISK for non struct se_device objects.
  2191. * Use 16-bit sector value.
  2192. */
  2193. if (!dev)
  2194. goto type_disk;
  2195. /*
  2196. * XXX_10 is not defined in SSC, throw an exception
  2197. */
  2198. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  2199. *ret = -EINVAL;
  2200. return 0;
  2201. }
  2202. /*
  2203. * Everything else assume TYPE_DISK Sector CDB location.
  2204. * Use 16-bit sector value.
  2205. */
  2206. type_disk:
  2207. return (u32)(cdb[7] << 8) + cdb[8];
  2208. }
  2209. static inline u32 transport_get_sectors_12(
  2210. unsigned char *cdb,
  2211. struct se_cmd *cmd,
  2212. int *ret)
  2213. {
  2214. struct se_device *dev = cmd->se_dev;
  2215. /*
  2216. * Assume TYPE_DISK for non struct se_device objects.
  2217. * Use 32-bit sector value.
  2218. */
  2219. if (!dev)
  2220. goto type_disk;
  2221. /*
  2222. * XXX_12 is not defined in SSC, throw an exception
  2223. */
  2224. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  2225. *ret = -EINVAL;
  2226. return 0;
  2227. }
  2228. /*
  2229. * Everything else assume TYPE_DISK Sector CDB location.
  2230. * Use 32-bit sector value.
  2231. */
  2232. type_disk:
  2233. return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
  2234. }
  2235. static inline u32 transport_get_sectors_16(
  2236. unsigned char *cdb,
  2237. struct se_cmd *cmd,
  2238. int *ret)
  2239. {
  2240. struct se_device *dev = cmd->se_dev;
  2241. /*
  2242. * Assume TYPE_DISK for non struct se_device objects.
  2243. * Use 32-bit sector value.
  2244. */
  2245. if (!dev)
  2246. goto type_disk;
  2247. /*
  2248. * Use 24-bit allocation length for TYPE_TAPE.
  2249. */
  2250. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  2251. return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
  2252. type_disk:
  2253. return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
  2254. (cdb[12] << 8) + cdb[13];
  2255. }
  2256. /*
  2257. * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
  2258. */
  2259. static inline u32 transport_get_sectors_32(
  2260. unsigned char *cdb,
  2261. struct se_cmd *cmd,
  2262. int *ret)
  2263. {
  2264. /*
  2265. * Assume TYPE_DISK for non struct se_device objects.
  2266. * Use 32-bit sector value.
  2267. */
  2268. return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
  2269. (cdb[30] << 8) + cdb[31];
  2270. }
  2271. static inline u32 transport_get_size(
  2272. u32 sectors,
  2273. unsigned char *cdb,
  2274. struct se_cmd *cmd)
  2275. {
  2276. struct se_device *dev = cmd->se_dev;
  2277. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  2278. if (cdb[1] & 1) { /* sectors */
  2279. return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
  2280. } else /* bytes */
  2281. return sectors;
  2282. }
  2283. #if 0
  2284. pr_debug("Returning block_size: %u, sectors: %u == %u for"
  2285. " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
  2286. dev->se_sub_dev->se_dev_attrib.block_size * sectors,
  2287. dev->transport->name);
  2288. #endif
  2289. return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
  2290. }
  2291. static void transport_xor_callback(struct se_cmd *cmd)
  2292. {
  2293. unsigned char *buf, *addr;
  2294. struct scatterlist *sg;
  2295. unsigned int offset;
  2296. int i;
  2297. int count;
  2298. /*
  2299. * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
  2300. *
  2301. * 1) read the specified logical block(s);
  2302. * 2) transfer logical blocks from the data-out buffer;
  2303. * 3) XOR the logical blocks transferred from the data-out buffer with
  2304. * the logical blocks read, storing the resulting XOR data in a buffer;
  2305. * 4) if the DISABLE WRITE bit is set to zero, then write the logical
  2306. * blocks transferred from the data-out buffer; and
  2307. * 5) transfer the resulting XOR data to the data-in buffer.
  2308. */
  2309. buf = kmalloc(cmd->data_length, GFP_KERNEL);
  2310. if (!buf) {
  2311. pr_err("Unable to allocate xor_callback buf\n");
  2312. return;
  2313. }
  2314. /*
  2315. * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
  2316. * into the locally allocated *buf
  2317. */
  2318. sg_copy_to_buffer(cmd->t_data_sg,
  2319. cmd->t_data_nents,
  2320. buf,
  2321. cmd->data_length);
  2322. /*
  2323. * Now perform the XOR against the BIDI read memory located at
  2324. * cmd->t_mem_bidi_list
  2325. */
  2326. offset = 0;
  2327. for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
  2328. addr = kmap_atomic(sg_page(sg), KM_USER0);
  2329. if (!addr)
  2330. goto out;
  2331. for (i = 0; i < sg->length; i++)
  2332. *(addr + sg->offset + i) ^= *(buf + offset + i);
  2333. offset += sg->length;
  2334. kunmap_atomic(addr, KM_USER0);
  2335. }
  2336. out:
  2337. kfree(buf);
  2338. }
  2339. /*
  2340. * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
  2341. */
  2342. static int transport_get_sense_data(struct se_cmd *cmd)
  2343. {
  2344. unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
  2345. struct se_device *dev = cmd->se_dev;
  2346. struct se_task *task = NULL, *task_tmp;
  2347. unsigned long flags;
  2348. u32 offset = 0;
  2349. WARN_ON(!cmd->se_lun);
  2350. if (!dev)
  2351. return 0;
  2352. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2353. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  2354. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2355. return 0;
  2356. }
  2357. list_for_each_entry_safe(task, task_tmp,
  2358. &cmd->t_task_list, t_list) {
  2359. if (!task->task_sense)
  2360. continue;
  2361. if (!dev->transport->get_sense_buffer) {
  2362. pr_err("dev->transport->get_sense_buffer"
  2363. " is NULL\n");
  2364. continue;
  2365. }
  2366. sense_buffer = dev->transport->get_sense_buffer(task);
  2367. if (!sense_buffer) {
  2368. pr_err("ITT[0x%08x]_TASK[%p]: Unable to locate"
  2369. " sense buffer for task with sense\n",
  2370. cmd->se_tfo->get_task_tag(cmd), task);
  2371. continue;
  2372. }
  2373. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2374. offset = cmd->se_tfo->set_fabric_sense_len(cmd,
  2375. TRANSPORT_SENSE_BUFFER);
  2376. memcpy(&buffer[offset], sense_buffer,
  2377. TRANSPORT_SENSE_BUFFER);
  2378. cmd->scsi_status = task->task_scsi_status;
  2379. /* Automatically padded */
  2380. cmd->scsi_sense_length =
  2381. (TRANSPORT_SENSE_BUFFER + offset);
  2382. pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
  2383. " and sense\n",
  2384. dev->se_hba->hba_id, dev->transport->name,
  2385. cmd->scsi_status);
  2386. return 0;
  2387. }
  2388. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2389. return -1;
  2390. }
  2391. static int
  2392. transport_handle_reservation_conflict(struct se_cmd *cmd)
  2393. {
  2394. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2395. cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
  2396. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  2397. /*
  2398. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  2399. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  2400. * CONFLICT STATUS.
  2401. *
  2402. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  2403. */
  2404. if (cmd->se_sess &&
  2405. cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
  2406. core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
  2407. cmd->orig_fe_lun, 0x2C,
  2408. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  2409. return -EINVAL;
  2410. }
  2411. static inline long long transport_dev_end_lba(struct se_device *dev)
  2412. {
  2413. return dev->transport->get_blocks(dev) + 1;
  2414. }
  2415. static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
  2416. {
  2417. struct se_device *dev = cmd->se_dev;
  2418. u32 sectors;
  2419. if (dev->transport->get_device_type(dev) != TYPE_DISK)
  2420. return 0;
  2421. sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
  2422. if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
  2423. pr_err("LBA: %llu Sectors: %u exceeds"
  2424. " transport_dev_end_lba(): %llu\n",
  2425. cmd->t_task_lba, sectors,
  2426. transport_dev_end_lba(dev));
  2427. return -EINVAL;
  2428. }
  2429. return 0;
  2430. }
  2431. static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
  2432. {
  2433. /*
  2434. * Determine if the received WRITE_SAME is used to for direct
  2435. * passthrough into Linux/SCSI with struct request via TCM/pSCSI
  2436. * or we are signaling the use of internal WRITE_SAME + UNMAP=1
  2437. * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
  2438. */
  2439. int passthrough = (dev->transport->transport_type ==
  2440. TRANSPORT_PLUGIN_PHBA_PDEV);
  2441. if (!passthrough) {
  2442. if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
  2443. pr_err("WRITE_SAME PBDATA and LBDATA"
  2444. " bits not supported for Block Discard"
  2445. " Emulation\n");
  2446. return -ENOSYS;
  2447. }
  2448. /*
  2449. * Currently for the emulated case we only accept
  2450. * tpws with the UNMAP=1 bit set.
  2451. */
  2452. if (!(flags[0] & 0x08)) {
  2453. pr_err("WRITE_SAME w/o UNMAP bit not"
  2454. " supported for Block Discard Emulation\n");
  2455. return -ENOSYS;
  2456. }
  2457. }
  2458. return 0;
  2459. }
  2460. /* transport_generic_cmd_sequencer():
  2461. *
  2462. * Generic Command Sequencer that should work for most DAS transport
  2463. * drivers.
  2464. *
  2465. * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
  2466. * RX Thread.
  2467. *
  2468. * FIXME: Need to support other SCSI OPCODES where as well.
  2469. */
  2470. static int transport_generic_cmd_sequencer(
  2471. struct se_cmd *cmd,
  2472. unsigned char *cdb)
  2473. {
  2474. struct se_device *dev = cmd->se_dev;
  2475. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  2476. int ret = 0, sector_ret = 0, passthrough;
  2477. u32 sectors = 0, size = 0, pr_reg_type = 0;
  2478. u16 service_action;
  2479. u8 alua_ascq = 0;
  2480. /*
  2481. * Check for an existing UNIT ATTENTION condition
  2482. */
  2483. if (core_scsi3_ua_check(cmd, cdb) < 0) {
  2484. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2485. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
  2486. return -EINVAL;
  2487. }
  2488. /*
  2489. * Check status of Asymmetric Logical Unit Assignment port
  2490. */
  2491. ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
  2492. if (ret != 0) {
  2493. /*
  2494. * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
  2495. * The ALUA additional sense code qualifier (ASCQ) is determined
  2496. * by the ALUA primary or secondary access state..
  2497. */
  2498. if (ret > 0) {
  2499. #if 0
  2500. pr_debug("[%s]: ALUA TG Port not available,"
  2501. " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
  2502. cmd->se_tfo->get_fabric_name(), alua_ascq);
  2503. #endif
  2504. transport_set_sense_codes(cmd, 0x04, alua_ascq);
  2505. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2506. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
  2507. return -EINVAL;
  2508. }
  2509. goto out_invalid_cdb_field;
  2510. }
  2511. /*
  2512. * Check status for SPC-3 Persistent Reservations
  2513. */
  2514. if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
  2515. if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
  2516. cmd, cdb, pr_reg_type) != 0)
  2517. return transport_handle_reservation_conflict(cmd);
  2518. /*
  2519. * This means the CDB is allowed for the SCSI Initiator port
  2520. * when said port is *NOT* holding the legacy SPC-2 or
  2521. * SPC-3 Persistent Reservation.
  2522. */
  2523. }
  2524. switch (cdb[0]) {
  2525. case READ_6:
  2526. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2527. if (sector_ret)
  2528. goto out_unsupported_cdb;
  2529. size = transport_get_size(sectors, cdb, cmd);
  2530. cmd->t_task_lba = transport_lba_21(cdb);
  2531. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2532. break;
  2533. case READ_10:
  2534. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2535. if (sector_ret)
  2536. goto out_unsupported_cdb;
  2537. size = transport_get_size(sectors, cdb, cmd);
  2538. cmd->t_task_lba = transport_lba_32(cdb);
  2539. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2540. break;
  2541. case READ_12:
  2542. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2543. if (sector_ret)
  2544. goto out_unsupported_cdb;
  2545. size = transport_get_size(sectors, cdb, cmd);
  2546. cmd->t_task_lba = transport_lba_32(cdb);
  2547. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2548. break;
  2549. case READ_16:
  2550. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2551. if (sector_ret)
  2552. goto out_unsupported_cdb;
  2553. size = transport_get_size(sectors, cdb, cmd);
  2554. cmd->t_task_lba = transport_lba_64(cdb);
  2555. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2556. break;
  2557. case WRITE_6:
  2558. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2559. if (sector_ret)
  2560. goto out_unsupported_cdb;
  2561. size = transport_get_size(sectors, cdb, cmd);
  2562. cmd->t_task_lba = transport_lba_21(cdb);
  2563. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2564. break;
  2565. case WRITE_10:
  2566. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2567. if (sector_ret)
  2568. goto out_unsupported_cdb;
  2569. size = transport_get_size(sectors, cdb, cmd);
  2570. cmd->t_task_lba = transport_lba_32(cdb);
  2571. cmd->t_tasks_fua = (cdb[1] & 0x8);
  2572. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2573. break;
  2574. case WRITE_12:
  2575. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2576. if (sector_ret)
  2577. goto out_unsupported_cdb;
  2578. size = transport_get_size(sectors, cdb, cmd);
  2579. cmd->t_task_lba = transport_lba_32(cdb);
  2580. cmd->t_tasks_fua = (cdb[1] & 0x8);
  2581. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2582. break;
  2583. case WRITE_16:
  2584. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2585. if (sector_ret)
  2586. goto out_unsupported_cdb;
  2587. size = transport_get_size(sectors, cdb, cmd);
  2588. cmd->t_task_lba = transport_lba_64(cdb);
  2589. cmd->t_tasks_fua = (cdb[1] & 0x8);
  2590. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2591. break;
  2592. case XDWRITEREAD_10:
  2593. if ((cmd->data_direction != DMA_TO_DEVICE) ||
  2594. !(cmd->t_tasks_bidi))
  2595. goto out_invalid_cdb_field;
  2596. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2597. if (sector_ret)
  2598. goto out_unsupported_cdb;
  2599. size = transport_get_size(sectors, cdb, cmd);
  2600. cmd->t_task_lba = transport_lba_32(cdb);
  2601. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2602. passthrough = (dev->transport->transport_type ==
  2603. TRANSPORT_PLUGIN_PHBA_PDEV);
  2604. /*
  2605. * Skip the remaining assignments for TCM/PSCSI passthrough
  2606. */
  2607. if (passthrough)
  2608. break;
  2609. /*
  2610. * Setup BIDI XOR callback to be run during transport_generic_complete_ok()
  2611. */
  2612. cmd->transport_complete_callback = &transport_xor_callback;
  2613. cmd->t_tasks_fua = (cdb[1] & 0x8);
  2614. break;
  2615. case VARIABLE_LENGTH_CMD:
  2616. service_action = get_unaligned_be16(&cdb[8]);
  2617. /*
  2618. * Determine if this is TCM/PSCSI device and we should disable
  2619. * internal emulation for this CDB.
  2620. */
  2621. passthrough = (dev->transport->transport_type ==
  2622. TRANSPORT_PLUGIN_PHBA_PDEV);
  2623. switch (service_action) {
  2624. case XDWRITEREAD_32:
  2625. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2626. if (sector_ret)
  2627. goto out_unsupported_cdb;
  2628. size = transport_get_size(sectors, cdb, cmd);
  2629. /*
  2630. * Use WRITE_32 and READ_32 opcodes for the emulated
  2631. * XDWRITE_READ_32 logic.
  2632. */
  2633. cmd->t_task_lba = transport_lba_64_ext(cdb);
  2634. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2635. /*
  2636. * Skip the remaining assignments for TCM/PSCSI passthrough
  2637. */
  2638. if (passthrough)
  2639. break;
  2640. /*
  2641. * Setup BIDI XOR callback to be run during
  2642. * transport_generic_complete_ok()
  2643. */
  2644. cmd->transport_complete_callback = &transport_xor_callback;
  2645. cmd->t_tasks_fua = (cdb[10] & 0x8);
  2646. break;
  2647. case WRITE_SAME_32:
  2648. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2649. if (sector_ret)
  2650. goto out_unsupported_cdb;
  2651. if (sectors)
  2652. size = transport_get_size(1, cdb, cmd);
  2653. else {
  2654. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
  2655. " supported\n");
  2656. goto out_invalid_cdb_field;
  2657. }
  2658. cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
  2659. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2660. if (target_check_write_same_discard(&cdb[10], dev) < 0)
  2661. goto out_invalid_cdb_field;
  2662. break;
  2663. default:
  2664. pr_err("VARIABLE_LENGTH_CMD service action"
  2665. " 0x%04x not supported\n", service_action);
  2666. goto out_unsupported_cdb;
  2667. }
  2668. break;
  2669. case MAINTENANCE_IN:
  2670. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2671. /* MAINTENANCE_IN from SCC-2 */
  2672. /*
  2673. * Check for emulated MI_REPORT_TARGET_PGS.
  2674. */
  2675. if (cdb[1] == MI_REPORT_TARGET_PGS) {
  2676. cmd->transport_emulate_cdb =
  2677. (su_dev->t10_alua.alua_type ==
  2678. SPC3_ALUA_EMULATED) ?
  2679. core_emulate_report_target_port_groups :
  2680. NULL;
  2681. }
  2682. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2683. (cdb[8] << 8) | cdb[9];
  2684. } else {
  2685. /* GPCMD_SEND_KEY from multi media commands */
  2686. size = (cdb[8] << 8) + cdb[9];
  2687. }
  2688. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2689. break;
  2690. case MODE_SELECT:
  2691. size = cdb[4];
  2692. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2693. break;
  2694. case MODE_SELECT_10:
  2695. size = (cdb[7] << 8) + cdb[8];
  2696. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2697. break;
  2698. case MODE_SENSE:
  2699. size = cdb[4];
  2700. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2701. break;
  2702. case MODE_SENSE_10:
  2703. case GPCMD_READ_BUFFER_CAPACITY:
  2704. case GPCMD_SEND_OPC:
  2705. case LOG_SELECT:
  2706. case LOG_SENSE:
  2707. size = (cdb[7] << 8) + cdb[8];
  2708. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2709. break;
  2710. case READ_BLOCK_LIMITS:
  2711. size = READ_BLOCK_LEN;
  2712. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2713. break;
  2714. case GPCMD_GET_CONFIGURATION:
  2715. case GPCMD_READ_FORMAT_CAPACITIES:
  2716. case GPCMD_READ_DISC_INFO:
  2717. case GPCMD_READ_TRACK_RZONE_INFO:
  2718. size = (cdb[7] << 8) + cdb[8];
  2719. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2720. break;
  2721. case PERSISTENT_RESERVE_IN:
  2722. case PERSISTENT_RESERVE_OUT:
  2723. cmd->transport_emulate_cdb =
  2724. (su_dev->t10_pr.res_type ==
  2725. SPC3_PERSISTENT_RESERVATIONS) ?
  2726. core_scsi3_emulate_pr : NULL;
  2727. size = (cdb[7] << 8) + cdb[8];
  2728. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2729. break;
  2730. case GPCMD_MECHANISM_STATUS:
  2731. case GPCMD_READ_DVD_STRUCTURE:
  2732. size = (cdb[8] << 8) + cdb[9];
  2733. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2734. break;
  2735. case READ_POSITION:
  2736. size = READ_POSITION_LEN;
  2737. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2738. break;
  2739. case MAINTENANCE_OUT:
  2740. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2741. /* MAINTENANCE_OUT from SCC-2
  2742. *
  2743. * Check for emulated MO_SET_TARGET_PGS.
  2744. */
  2745. if (cdb[1] == MO_SET_TARGET_PGS) {
  2746. cmd->transport_emulate_cdb =
  2747. (su_dev->t10_alua.alua_type ==
  2748. SPC3_ALUA_EMULATED) ?
  2749. core_emulate_set_target_port_groups :
  2750. NULL;
  2751. }
  2752. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2753. (cdb[8] << 8) | cdb[9];
  2754. } else {
  2755. /* GPCMD_REPORT_KEY from multi media commands */
  2756. size = (cdb[8] << 8) + cdb[9];
  2757. }
  2758. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2759. break;
  2760. case INQUIRY:
  2761. size = (cdb[3] << 8) + cdb[4];
  2762. /*
  2763. * Do implict HEAD_OF_QUEUE processing for INQUIRY.
  2764. * See spc4r17 section 5.3
  2765. */
  2766. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2767. cmd->sam_task_attr = MSG_HEAD_TAG;
  2768. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2769. break;
  2770. case READ_BUFFER:
  2771. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2772. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2773. break;
  2774. case READ_CAPACITY:
  2775. size = READ_CAP_LEN;
  2776. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2777. break;
  2778. case READ_MEDIA_SERIAL_NUMBER:
  2779. case SECURITY_PROTOCOL_IN:
  2780. case SECURITY_PROTOCOL_OUT:
  2781. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  2782. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2783. break;
  2784. case SERVICE_ACTION_IN:
  2785. case ACCESS_CONTROL_IN:
  2786. case ACCESS_CONTROL_OUT:
  2787. case EXTENDED_COPY:
  2788. case READ_ATTRIBUTE:
  2789. case RECEIVE_COPY_RESULTS:
  2790. case WRITE_ATTRIBUTE:
  2791. size = (cdb[10] << 24) | (cdb[11] << 16) |
  2792. (cdb[12] << 8) | cdb[13];
  2793. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2794. break;
  2795. case RECEIVE_DIAGNOSTIC:
  2796. case SEND_DIAGNOSTIC:
  2797. size = (cdb[3] << 8) | cdb[4];
  2798. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2799. break;
  2800. /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
  2801. #if 0
  2802. case GPCMD_READ_CD:
  2803. sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2804. size = (2336 * sectors);
  2805. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2806. break;
  2807. #endif
  2808. case READ_TOC:
  2809. size = cdb[8];
  2810. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2811. break;
  2812. case REQUEST_SENSE:
  2813. size = cdb[4];
  2814. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2815. break;
  2816. case READ_ELEMENT_STATUS:
  2817. size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
  2818. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2819. break;
  2820. case WRITE_BUFFER:
  2821. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2822. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2823. break;
  2824. case RESERVE:
  2825. case RESERVE_10:
  2826. /*
  2827. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  2828. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  2829. */
  2830. if (cdb[0] == RESERVE_10)
  2831. size = (cdb[7] << 8) | cdb[8];
  2832. else
  2833. size = cmd->data_length;
  2834. /*
  2835. * Setup the legacy emulated handler for SPC-2 and
  2836. * >= SPC-3 compatible reservation handling (CRH=1)
  2837. * Otherwise, we assume the underlying SCSI logic is
  2838. * is running in SPC_PASSTHROUGH, and wants reservations
  2839. * emulation disabled.
  2840. */
  2841. cmd->transport_emulate_cdb =
  2842. (su_dev->t10_pr.res_type !=
  2843. SPC_PASSTHROUGH) ?
  2844. core_scsi2_emulate_crh : NULL;
  2845. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2846. break;
  2847. case RELEASE:
  2848. case RELEASE_10:
  2849. /*
  2850. * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
  2851. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  2852. */
  2853. if (cdb[0] == RELEASE_10)
  2854. size = (cdb[7] << 8) | cdb[8];
  2855. else
  2856. size = cmd->data_length;
  2857. cmd->transport_emulate_cdb =
  2858. (su_dev->t10_pr.res_type !=
  2859. SPC_PASSTHROUGH) ?
  2860. core_scsi2_emulate_crh : NULL;
  2861. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2862. break;
  2863. case SYNCHRONIZE_CACHE:
  2864. case 0x91: /* SYNCHRONIZE_CACHE_16: */
  2865. /*
  2866. * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
  2867. */
  2868. if (cdb[0] == SYNCHRONIZE_CACHE) {
  2869. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2870. cmd->t_task_lba = transport_lba_32(cdb);
  2871. } else {
  2872. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2873. cmd->t_task_lba = transport_lba_64(cdb);
  2874. }
  2875. if (sector_ret)
  2876. goto out_unsupported_cdb;
  2877. size = transport_get_size(sectors, cdb, cmd);
  2878. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2879. /*
  2880. * For TCM/pSCSI passthrough, skip cmd->transport_emulate_cdb()
  2881. */
  2882. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
  2883. break;
  2884. /*
  2885. * Set SCF_EMULATE_CDB_ASYNC to ensure asynchronous operation
  2886. * for SYNCHRONIZE_CACHE* Immed=1 case in __transport_execute_tasks()
  2887. */
  2888. cmd->se_cmd_flags |= SCF_EMULATE_CDB_ASYNC;
  2889. /*
  2890. * Check to ensure that LBA + Range does not exceed past end of
  2891. * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
  2892. */
  2893. if ((cmd->t_task_lba != 0) || (sectors != 0)) {
  2894. if (transport_cmd_get_valid_sectors(cmd) < 0)
  2895. goto out_invalid_cdb_field;
  2896. }
  2897. break;
  2898. case UNMAP:
  2899. size = get_unaligned_be16(&cdb[7]);
  2900. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2901. break;
  2902. case WRITE_SAME_16:
  2903. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2904. if (sector_ret)
  2905. goto out_unsupported_cdb;
  2906. if (sectors)
  2907. size = transport_get_size(1, cdb, cmd);
  2908. else {
  2909. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
  2910. goto out_invalid_cdb_field;
  2911. }
  2912. cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
  2913. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2914. if (target_check_write_same_discard(&cdb[1], dev) < 0)
  2915. goto out_invalid_cdb_field;
  2916. break;
  2917. case WRITE_SAME:
  2918. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2919. if (sector_ret)
  2920. goto out_unsupported_cdb;
  2921. if (sectors)
  2922. size = transport_get_size(1, cdb, cmd);
  2923. else {
  2924. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
  2925. goto out_invalid_cdb_field;
  2926. }
  2927. cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
  2928. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2929. /*
  2930. * Follow sbcr26 with WRITE_SAME (10) and check for the existence
  2931. * of byte 1 bit 3 UNMAP instead of original reserved field
  2932. */
  2933. if (target_check_write_same_discard(&cdb[1], dev) < 0)
  2934. goto out_invalid_cdb_field;
  2935. break;
  2936. case ALLOW_MEDIUM_REMOVAL:
  2937. case GPCMD_CLOSE_TRACK:
  2938. case ERASE:
  2939. case INITIALIZE_ELEMENT_STATUS:
  2940. case GPCMD_LOAD_UNLOAD:
  2941. case REZERO_UNIT:
  2942. case SEEK_10:
  2943. case GPCMD_SET_SPEED:
  2944. case SPACE:
  2945. case START_STOP:
  2946. case TEST_UNIT_READY:
  2947. case VERIFY:
  2948. case WRITE_FILEMARKS:
  2949. case MOVE_MEDIUM:
  2950. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2951. break;
  2952. case REPORT_LUNS:
  2953. cmd->transport_emulate_cdb =
  2954. transport_core_report_lun_response;
  2955. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  2956. /*
  2957. * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
  2958. * See spc4r17 section 5.3
  2959. */
  2960. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2961. cmd->sam_task_attr = MSG_HEAD_TAG;
  2962. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2963. break;
  2964. default:
  2965. pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
  2966. " 0x%02x, sending CHECK_CONDITION.\n",
  2967. cmd->se_tfo->get_fabric_name(), cdb[0]);
  2968. goto out_unsupported_cdb;
  2969. }
  2970. if (size != cmd->data_length) {
  2971. pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
  2972. " %u does not match SCSI CDB Length: %u for SAM Opcode:"
  2973. " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
  2974. cmd->data_length, size, cdb[0]);
  2975. cmd->cmd_spdtl = size;
  2976. if (cmd->data_direction == DMA_TO_DEVICE) {
  2977. pr_err("Rejecting underflow/overflow"
  2978. " WRITE data\n");
  2979. goto out_invalid_cdb_field;
  2980. }
  2981. /*
  2982. * Reject READ_* or WRITE_* with overflow/underflow for
  2983. * type SCF_SCSI_DATA_SG_IO_CDB.
  2984. */
  2985. if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
  2986. pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
  2987. " CDB on non 512-byte sector setup subsystem"
  2988. " plugin: %s\n", dev->transport->name);
  2989. /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
  2990. goto out_invalid_cdb_field;
  2991. }
  2992. if (size > cmd->data_length) {
  2993. cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
  2994. cmd->residual_count = (size - cmd->data_length);
  2995. } else {
  2996. cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  2997. cmd->residual_count = (cmd->data_length - size);
  2998. }
  2999. cmd->data_length = size;
  3000. }
  3001. /* Let's limit control cdbs to a page, for simplicity's sake. */
  3002. if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
  3003. size > PAGE_SIZE)
  3004. goto out_invalid_cdb_field;
  3005. transport_set_supported_SAM_opcode(cmd);
  3006. return ret;
  3007. out_unsupported_cdb:
  3008. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3009. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  3010. return -EINVAL;
  3011. out_invalid_cdb_field:
  3012. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3013. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3014. return -EINVAL;
  3015. }
  3016. /*
  3017. * Called from transport_generic_complete_ok() and
  3018. * transport_generic_request_failure() to determine which dormant/delayed
  3019. * and ordered cmds need to have their tasks added to the execution queue.
  3020. */
  3021. static void transport_complete_task_attr(struct se_cmd *cmd)
  3022. {
  3023. struct se_device *dev = cmd->se_dev;
  3024. struct se_cmd *cmd_p, *cmd_tmp;
  3025. int new_active_tasks = 0;
  3026. if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
  3027. atomic_dec(&dev->simple_cmds);
  3028. smp_mb__after_atomic_dec();
  3029. dev->dev_cur_ordered_id++;
  3030. pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
  3031. " SIMPLE: %u\n", dev->dev_cur_ordered_id,
  3032. cmd->se_ordered_id);
  3033. } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  3034. atomic_dec(&dev->dev_hoq_count);
  3035. smp_mb__after_atomic_dec();
  3036. dev->dev_cur_ordered_id++;
  3037. pr_debug("Incremented dev_cur_ordered_id: %u for"
  3038. " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
  3039. cmd->se_ordered_id);
  3040. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  3041. spin_lock(&dev->ordered_cmd_lock);
  3042. list_del(&cmd->se_ordered_node);
  3043. atomic_dec(&dev->dev_ordered_sync);
  3044. smp_mb__after_atomic_dec();
  3045. spin_unlock(&dev->ordered_cmd_lock);
  3046. dev->dev_cur_ordered_id++;
  3047. pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
  3048. " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
  3049. }
  3050. /*
  3051. * Process all commands up to the last received
  3052. * ORDERED task attribute which requires another blocking
  3053. * boundary
  3054. */
  3055. spin_lock(&dev->delayed_cmd_lock);
  3056. list_for_each_entry_safe(cmd_p, cmd_tmp,
  3057. &dev->delayed_cmd_list, se_delayed_node) {
  3058. list_del(&cmd_p->se_delayed_node);
  3059. spin_unlock(&dev->delayed_cmd_lock);
  3060. pr_debug("Calling add_tasks() for"
  3061. " cmd_p: 0x%02x Task Attr: 0x%02x"
  3062. " Dormant -> Active, se_ordered_id: %u\n",
  3063. cmd_p->t_task_cdb[0],
  3064. cmd_p->sam_task_attr, cmd_p->se_ordered_id);
  3065. transport_add_tasks_from_cmd(cmd_p);
  3066. new_active_tasks++;
  3067. spin_lock(&dev->delayed_cmd_lock);
  3068. if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
  3069. break;
  3070. }
  3071. spin_unlock(&dev->delayed_cmd_lock);
  3072. /*
  3073. * If new tasks have become active, wake up the transport thread
  3074. * to do the processing of the Active tasks.
  3075. */
  3076. if (new_active_tasks != 0)
  3077. wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
  3078. }
  3079. static void transport_complete_qf(struct se_cmd *cmd)
  3080. {
  3081. int ret = 0;
  3082. transport_stop_all_task_timers(cmd);
  3083. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  3084. transport_complete_task_attr(cmd);
  3085. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  3086. ret = cmd->se_tfo->queue_status(cmd);
  3087. if (ret)
  3088. goto out;
  3089. }
  3090. switch (cmd->data_direction) {
  3091. case DMA_FROM_DEVICE:
  3092. ret = cmd->se_tfo->queue_data_in(cmd);
  3093. break;
  3094. case DMA_TO_DEVICE:
  3095. if (cmd->t_bidi_data_sg) {
  3096. ret = cmd->se_tfo->queue_data_in(cmd);
  3097. if (ret < 0)
  3098. break;
  3099. }
  3100. /* Fall through for DMA_TO_DEVICE */
  3101. case DMA_NONE:
  3102. ret = cmd->se_tfo->queue_status(cmd);
  3103. break;
  3104. default:
  3105. break;
  3106. }
  3107. out:
  3108. if (ret < 0) {
  3109. transport_handle_queue_full(cmd, cmd->se_dev);
  3110. return;
  3111. }
  3112. transport_lun_remove_cmd(cmd);
  3113. transport_cmd_check_stop_to_fabric(cmd);
  3114. }
  3115. static void transport_handle_queue_full(
  3116. struct se_cmd *cmd,
  3117. struct se_device *dev)
  3118. {
  3119. spin_lock_irq(&dev->qf_cmd_lock);
  3120. list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
  3121. atomic_inc(&dev->dev_qf_count);
  3122. smp_mb__after_atomic_inc();
  3123. spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
  3124. schedule_work(&cmd->se_dev->qf_work_queue);
  3125. }
  3126. static void transport_generic_complete_ok(struct se_cmd *cmd)
  3127. {
  3128. int reason = 0, ret;
  3129. /*
  3130. * Check if we need to move delayed/dormant tasks from cmds on the
  3131. * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
  3132. * Attribute.
  3133. */
  3134. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  3135. transport_complete_task_attr(cmd);
  3136. /*
  3137. * Check to schedule QUEUE_FULL work, or execute an existing
  3138. * cmd->transport_qf_callback()
  3139. */
  3140. if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
  3141. schedule_work(&cmd->se_dev->qf_work_queue);
  3142. /*
  3143. * Check if we need to retrieve a sense buffer from
  3144. * the struct se_cmd in question.
  3145. */
  3146. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  3147. if (transport_get_sense_data(cmd) < 0)
  3148. reason = TCM_NON_EXISTENT_LUN;
  3149. /*
  3150. * Only set when an struct se_task->task_scsi_status returned
  3151. * a non GOOD status.
  3152. */
  3153. if (cmd->scsi_status) {
  3154. ret = transport_send_check_condition_and_sense(
  3155. cmd, reason, 1);
  3156. if (ret == -EAGAIN)
  3157. goto queue_full;
  3158. transport_lun_remove_cmd(cmd);
  3159. transport_cmd_check_stop_to_fabric(cmd);
  3160. return;
  3161. }
  3162. }
  3163. /*
  3164. * Check for a callback, used by amongst other things
  3165. * XDWRITE_READ_10 emulation.
  3166. */
  3167. if (cmd->transport_complete_callback)
  3168. cmd->transport_complete_callback(cmd);
  3169. switch (cmd->data_direction) {
  3170. case DMA_FROM_DEVICE:
  3171. spin_lock(&cmd->se_lun->lun_sep_lock);
  3172. if (cmd->se_lun->lun_sep) {
  3173. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  3174. cmd->data_length;
  3175. }
  3176. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3177. ret = cmd->se_tfo->queue_data_in(cmd);
  3178. if (ret == -EAGAIN)
  3179. goto queue_full;
  3180. break;
  3181. case DMA_TO_DEVICE:
  3182. spin_lock(&cmd->se_lun->lun_sep_lock);
  3183. if (cmd->se_lun->lun_sep) {
  3184. cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
  3185. cmd->data_length;
  3186. }
  3187. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3188. /*
  3189. * Check if we need to send READ payload for BIDI-COMMAND
  3190. */
  3191. if (cmd->t_bidi_data_sg) {
  3192. spin_lock(&cmd->se_lun->lun_sep_lock);
  3193. if (cmd->se_lun->lun_sep) {
  3194. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  3195. cmd->data_length;
  3196. }
  3197. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3198. ret = cmd->se_tfo->queue_data_in(cmd);
  3199. if (ret == -EAGAIN)
  3200. goto queue_full;
  3201. break;
  3202. }
  3203. /* Fall through for DMA_TO_DEVICE */
  3204. case DMA_NONE:
  3205. ret = cmd->se_tfo->queue_status(cmd);
  3206. if (ret == -EAGAIN)
  3207. goto queue_full;
  3208. break;
  3209. default:
  3210. break;
  3211. }
  3212. transport_lun_remove_cmd(cmd);
  3213. transport_cmd_check_stop_to_fabric(cmd);
  3214. return;
  3215. queue_full:
  3216. pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
  3217. " data_direction: %d\n", cmd, cmd->data_direction);
  3218. cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
  3219. transport_handle_queue_full(cmd, cmd->se_dev);
  3220. }
  3221. static void transport_free_dev_tasks(struct se_cmd *cmd)
  3222. {
  3223. struct se_task *task, *task_tmp;
  3224. unsigned long flags;
  3225. LIST_HEAD(dispose_list);
  3226. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3227. list_for_each_entry_safe(task, task_tmp,
  3228. &cmd->t_task_list, t_list) {
  3229. if (!(task->task_flags & TF_ACTIVE))
  3230. list_move_tail(&task->t_list, &dispose_list);
  3231. }
  3232. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3233. while (!list_empty(&dispose_list)) {
  3234. task = list_first_entry(&dispose_list, struct se_task, t_list);
  3235. kfree(task->task_sg_bidi);
  3236. kfree(task->task_sg);
  3237. list_del(&task->t_list);
  3238. cmd->se_dev->transport->free_task(task);
  3239. }
  3240. }
  3241. static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
  3242. {
  3243. struct scatterlist *sg;
  3244. int count;
  3245. for_each_sg(sgl, sg, nents, count)
  3246. __free_page(sg_page(sg));
  3247. kfree(sgl);
  3248. }
  3249. static inline void transport_free_pages(struct se_cmd *cmd)
  3250. {
  3251. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
  3252. return;
  3253. transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
  3254. cmd->t_data_sg = NULL;
  3255. cmd->t_data_nents = 0;
  3256. transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
  3257. cmd->t_bidi_data_sg = NULL;
  3258. cmd->t_bidi_data_nents = 0;
  3259. }
  3260. /**
  3261. * transport_put_cmd - release a reference to a command
  3262. * @cmd: command to release
  3263. *
  3264. * This routine releases our reference to the command and frees it if possible.
  3265. */
  3266. static void transport_put_cmd(struct se_cmd *cmd)
  3267. {
  3268. unsigned long flags;
  3269. int free_tasks = 0;
  3270. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3271. if (atomic_read(&cmd->t_fe_count)) {
  3272. if (!atomic_dec_and_test(&cmd->t_fe_count))
  3273. goto out_busy;
  3274. }
  3275. if (atomic_read(&cmd->t_se_count)) {
  3276. if (!atomic_dec_and_test(&cmd->t_se_count))
  3277. goto out_busy;
  3278. }
  3279. if (atomic_read(&cmd->transport_dev_active)) {
  3280. atomic_set(&cmd->transport_dev_active, 0);
  3281. transport_all_task_dev_remove_state(cmd);
  3282. free_tasks = 1;
  3283. }
  3284. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3285. if (free_tasks != 0)
  3286. transport_free_dev_tasks(cmd);
  3287. transport_free_pages(cmd);
  3288. transport_release_cmd(cmd);
  3289. return;
  3290. out_busy:
  3291. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3292. }
  3293. /*
  3294. * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
  3295. * allocating in the core.
  3296. * @cmd: Associated se_cmd descriptor
  3297. * @mem: SGL style memory for TCM WRITE / READ
  3298. * @sg_mem_num: Number of SGL elements
  3299. * @mem_bidi_in: SGL style memory for TCM BIDI READ
  3300. * @sg_mem_bidi_num: Number of BIDI READ SGL elements
  3301. *
  3302. * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
  3303. * of parameters.
  3304. */
  3305. int transport_generic_map_mem_to_cmd(
  3306. struct se_cmd *cmd,
  3307. struct scatterlist *sgl,
  3308. u32 sgl_count,
  3309. struct scatterlist *sgl_bidi,
  3310. u32 sgl_bidi_count)
  3311. {
  3312. if (!sgl || !sgl_count)
  3313. return 0;
  3314. if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
  3315. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
  3316. cmd->t_data_sg = sgl;
  3317. cmd->t_data_nents = sgl_count;
  3318. if (sgl_bidi && sgl_bidi_count) {
  3319. cmd->t_bidi_data_sg = sgl_bidi;
  3320. cmd->t_bidi_data_nents = sgl_bidi_count;
  3321. }
  3322. cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  3323. }
  3324. return 0;
  3325. }
  3326. EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
  3327. static int transport_new_cmd_obj(struct se_cmd *cmd)
  3328. {
  3329. struct se_device *dev = cmd->se_dev;
  3330. int set_counts = 1, rc, task_cdbs;
  3331. /*
  3332. * Setup any BIDI READ tasks and memory from
  3333. * cmd->t_mem_bidi_list so the READ struct se_tasks
  3334. * are queued first for the non pSCSI passthrough case.
  3335. */
  3336. if (cmd->t_bidi_data_sg &&
  3337. (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV)) {
  3338. rc = transport_allocate_tasks(cmd,
  3339. cmd->t_task_lba,
  3340. DMA_FROM_DEVICE,
  3341. cmd->t_bidi_data_sg,
  3342. cmd->t_bidi_data_nents);
  3343. if (rc <= 0) {
  3344. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3345. cmd->scsi_sense_reason =
  3346. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3347. return -EINVAL;
  3348. }
  3349. atomic_inc(&cmd->t_fe_count);
  3350. atomic_inc(&cmd->t_se_count);
  3351. set_counts = 0;
  3352. }
  3353. /*
  3354. * Setup the tasks and memory from cmd->t_mem_list
  3355. * Note for BIDI transfers this will contain the WRITE payload
  3356. */
  3357. task_cdbs = transport_allocate_tasks(cmd,
  3358. cmd->t_task_lba,
  3359. cmd->data_direction,
  3360. cmd->t_data_sg,
  3361. cmd->t_data_nents);
  3362. if (task_cdbs <= 0) {
  3363. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3364. cmd->scsi_sense_reason =
  3365. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3366. return -EINVAL;
  3367. }
  3368. if (set_counts) {
  3369. atomic_inc(&cmd->t_fe_count);
  3370. atomic_inc(&cmd->t_se_count);
  3371. }
  3372. cmd->t_task_list_num = task_cdbs;
  3373. atomic_set(&cmd->t_task_cdbs_left, task_cdbs);
  3374. atomic_set(&cmd->t_task_cdbs_ex_left, task_cdbs);
  3375. atomic_set(&cmd->t_task_cdbs_timeout_left, task_cdbs);
  3376. return 0;
  3377. }
  3378. void *transport_kmap_first_data_page(struct se_cmd *cmd)
  3379. {
  3380. struct scatterlist *sg = cmd->t_data_sg;
  3381. BUG_ON(!sg);
  3382. /*
  3383. * We need to take into account a possible offset here for fabrics like
  3384. * tcm_loop who may be using a contig buffer from the SCSI midlayer for
  3385. * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
  3386. */
  3387. return kmap(sg_page(sg)) + sg->offset;
  3388. }
  3389. EXPORT_SYMBOL(transport_kmap_first_data_page);
  3390. void transport_kunmap_first_data_page(struct se_cmd *cmd)
  3391. {
  3392. kunmap(sg_page(cmd->t_data_sg));
  3393. }
  3394. EXPORT_SYMBOL(transport_kunmap_first_data_page);
  3395. static int
  3396. transport_generic_get_mem(struct se_cmd *cmd)
  3397. {
  3398. u32 length = cmd->data_length;
  3399. unsigned int nents;
  3400. struct page *page;
  3401. int i = 0;
  3402. nents = DIV_ROUND_UP(length, PAGE_SIZE);
  3403. cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
  3404. if (!cmd->t_data_sg)
  3405. return -ENOMEM;
  3406. cmd->t_data_nents = nents;
  3407. sg_init_table(cmd->t_data_sg, nents);
  3408. while (length) {
  3409. u32 page_len = min_t(u32, length, PAGE_SIZE);
  3410. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  3411. if (!page)
  3412. goto out;
  3413. sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
  3414. length -= page_len;
  3415. i++;
  3416. }
  3417. return 0;
  3418. out:
  3419. while (i >= 0) {
  3420. __free_page(sg_page(&cmd->t_data_sg[i]));
  3421. i--;
  3422. }
  3423. kfree(cmd->t_data_sg);
  3424. cmd->t_data_sg = NULL;
  3425. return -ENOMEM;
  3426. }
  3427. /* Reduce sectors if they are too long for the device */
  3428. static inline sector_t transport_limit_task_sectors(
  3429. struct se_device *dev,
  3430. unsigned long long lba,
  3431. sector_t sectors)
  3432. {
  3433. sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
  3434. if (dev->transport->get_device_type(dev) == TYPE_DISK)
  3435. if ((lba + sectors) > transport_dev_end_lba(dev))
  3436. sectors = ((transport_dev_end_lba(dev) - lba) + 1);
  3437. return sectors;
  3438. }
  3439. /*
  3440. * This function can be used by HW target mode drivers to create a linked
  3441. * scatterlist from all contiguously allocated struct se_task->task_sg[].
  3442. * This is intended to be called during the completion path by TCM Core
  3443. * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
  3444. */
  3445. void transport_do_task_sg_chain(struct se_cmd *cmd)
  3446. {
  3447. struct scatterlist *sg_first = NULL;
  3448. struct scatterlist *sg_prev = NULL;
  3449. int sg_prev_nents = 0;
  3450. struct scatterlist *sg;
  3451. struct se_task *task;
  3452. u32 chained_nents = 0;
  3453. int i;
  3454. BUG_ON(!cmd->se_tfo->task_sg_chaining);
  3455. /*
  3456. * Walk the struct se_task list and setup scatterlist chains
  3457. * for each contiguously allocated struct se_task->task_sg[].
  3458. */
  3459. list_for_each_entry(task, &cmd->t_task_list, t_list) {
  3460. if (!task->task_sg)
  3461. continue;
  3462. if (!sg_first) {
  3463. sg_first = task->task_sg;
  3464. chained_nents = task->task_sg_nents;
  3465. } else {
  3466. sg_chain(sg_prev, sg_prev_nents, task->task_sg);
  3467. chained_nents += task->task_sg_nents;
  3468. }
  3469. /*
  3470. * For the padded tasks, use the extra SGL vector allocated
  3471. * in transport_allocate_data_tasks() for the sg_prev_nents
  3472. * offset into sg_chain() above.
  3473. *
  3474. * We do not need the padding for the last task (or a single
  3475. * task), but in that case we will never use the sg_prev_nents
  3476. * value below which would be incorrect.
  3477. */
  3478. sg_prev_nents = (task->task_sg_nents + 1);
  3479. sg_prev = task->task_sg;
  3480. }
  3481. /*
  3482. * Setup the starting pointer and total t_tasks_sg_linked_no including
  3483. * padding SGs for linking and to mark the end.
  3484. */
  3485. cmd->t_tasks_sg_chained = sg_first;
  3486. cmd->t_tasks_sg_chained_no = chained_nents;
  3487. pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
  3488. " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
  3489. cmd->t_tasks_sg_chained_no);
  3490. for_each_sg(cmd->t_tasks_sg_chained, sg,
  3491. cmd->t_tasks_sg_chained_no, i) {
  3492. pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
  3493. i, sg, sg_page(sg), sg->length, sg->offset);
  3494. if (sg_is_chain(sg))
  3495. pr_debug("SG: %p sg_is_chain=1\n", sg);
  3496. if (sg_is_last(sg))
  3497. pr_debug("SG: %p sg_is_last=1\n", sg);
  3498. }
  3499. }
  3500. EXPORT_SYMBOL(transport_do_task_sg_chain);
  3501. /*
  3502. * Break up cmd into chunks transport can handle
  3503. */
  3504. static int transport_allocate_data_tasks(
  3505. struct se_cmd *cmd,
  3506. unsigned long long lba,
  3507. enum dma_data_direction data_direction,
  3508. struct scatterlist *sgl,
  3509. unsigned int sgl_nents)
  3510. {
  3511. struct se_task *task;
  3512. struct se_device *dev = cmd->se_dev;
  3513. unsigned long flags;
  3514. int task_count, i;
  3515. sector_t sectors, dev_max_sectors = dev->se_sub_dev->se_dev_attrib.max_sectors;
  3516. u32 sector_size = dev->se_sub_dev->se_dev_attrib.block_size;
  3517. struct scatterlist *sg;
  3518. struct scatterlist *cmd_sg;
  3519. WARN_ON(cmd->data_length % sector_size);
  3520. sectors = DIV_ROUND_UP(cmd->data_length, sector_size);
  3521. task_count = DIV_ROUND_UP_SECTOR_T(sectors, dev_max_sectors);
  3522. cmd_sg = sgl;
  3523. for (i = 0; i < task_count; i++) {
  3524. unsigned int task_size, task_sg_nents_padded;
  3525. int count;
  3526. task = transport_generic_get_task(cmd, data_direction);
  3527. if (!task)
  3528. return -ENOMEM;
  3529. task->task_lba = lba;
  3530. task->task_sectors = min(sectors, dev_max_sectors);
  3531. task->task_size = task->task_sectors * sector_size;
  3532. /*
  3533. * This now assumes that passed sg_ents are in PAGE_SIZE chunks
  3534. * in order to calculate the number per task SGL entries
  3535. */
  3536. task->task_sg_nents = DIV_ROUND_UP(task->task_size, PAGE_SIZE);
  3537. /*
  3538. * Check if the fabric module driver is requesting that all
  3539. * struct se_task->task_sg[] be chained together.. If so,
  3540. * then allocate an extra padding SG entry for linking and
  3541. * marking the end of the chained SGL for every task except
  3542. * the last one for (task_count > 1) operation, or skipping
  3543. * the extra padding for the (task_count == 1) case.
  3544. */
  3545. if (cmd->se_tfo->task_sg_chaining && (i < (task_count - 1))) {
  3546. task_sg_nents_padded = (task->task_sg_nents + 1);
  3547. } else
  3548. task_sg_nents_padded = task->task_sg_nents;
  3549. task->task_sg = kmalloc(sizeof(struct scatterlist) *
  3550. task_sg_nents_padded, GFP_KERNEL);
  3551. if (!task->task_sg) {
  3552. cmd->se_dev->transport->free_task(task);
  3553. return -ENOMEM;
  3554. }
  3555. sg_init_table(task->task_sg, task_sg_nents_padded);
  3556. task_size = task->task_size;
  3557. /* Build new sgl, only up to task_size */
  3558. for_each_sg(task->task_sg, sg, task->task_sg_nents, count) {
  3559. if (cmd_sg->length > task_size)
  3560. break;
  3561. *sg = *cmd_sg;
  3562. task_size -= cmd_sg->length;
  3563. cmd_sg = sg_next(cmd_sg);
  3564. }
  3565. lba += task->task_sectors;
  3566. sectors -= task->task_sectors;
  3567. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3568. list_add_tail(&task->t_list, &cmd->t_task_list);
  3569. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3570. }
  3571. return task_count;
  3572. }
  3573. static int
  3574. transport_allocate_control_task(struct se_cmd *cmd)
  3575. {
  3576. struct se_task *task;
  3577. unsigned long flags;
  3578. task = transport_generic_get_task(cmd, cmd->data_direction);
  3579. if (!task)
  3580. return -ENOMEM;
  3581. task->task_sg = kmalloc(sizeof(struct scatterlist) * cmd->t_data_nents,
  3582. GFP_KERNEL);
  3583. if (!task->task_sg) {
  3584. cmd->se_dev->transport->free_task(task);
  3585. return -ENOMEM;
  3586. }
  3587. memcpy(task->task_sg, cmd->t_data_sg,
  3588. sizeof(struct scatterlist) * cmd->t_data_nents);
  3589. task->task_size = cmd->data_length;
  3590. task->task_sg_nents = cmd->t_data_nents;
  3591. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3592. list_add_tail(&task->t_list, &cmd->t_task_list);
  3593. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3594. /* Success! Return number of tasks allocated */
  3595. return 1;
  3596. }
  3597. static u32 transport_allocate_tasks(
  3598. struct se_cmd *cmd,
  3599. unsigned long long lba,
  3600. enum dma_data_direction data_direction,
  3601. struct scatterlist *sgl,
  3602. unsigned int sgl_nents)
  3603. {
  3604. if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
  3605. if (transport_cmd_get_valid_sectors(cmd) < 0)
  3606. return -EINVAL;
  3607. return transport_allocate_data_tasks(cmd, lba, data_direction,
  3608. sgl, sgl_nents);
  3609. } else
  3610. return transport_allocate_control_task(cmd);
  3611. }
  3612. /* transport_generic_new_cmd(): Called from transport_processing_thread()
  3613. *
  3614. * Allocate storage transport resources from a set of values predefined
  3615. * by transport_generic_cmd_sequencer() from the iSCSI Target RX process.
  3616. * Any non zero return here is treated as an "out of resource' op here.
  3617. */
  3618. /*
  3619. * Generate struct se_task(s) and/or their payloads for this CDB.
  3620. */
  3621. int transport_generic_new_cmd(struct se_cmd *cmd)
  3622. {
  3623. int ret = 0;
  3624. /*
  3625. * Determine is the TCM fabric module has already allocated physical
  3626. * memory, and is directly calling transport_generic_map_mem_to_cmd()
  3627. * beforehand.
  3628. */
  3629. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
  3630. cmd->data_length) {
  3631. ret = transport_generic_get_mem(cmd);
  3632. if (ret < 0)
  3633. return ret;
  3634. }
  3635. /*
  3636. * Call transport_new_cmd_obj() to invoke transport_allocate_tasks() for
  3637. * control or data CDB types, and perform the map to backend subsystem
  3638. * code from SGL memory allocated here by transport_generic_get_mem(), or
  3639. * via pre-existing SGL memory setup explictly by fabric module code with
  3640. * transport_generic_map_mem_to_cmd().
  3641. */
  3642. ret = transport_new_cmd_obj(cmd);
  3643. if (ret < 0)
  3644. return ret;
  3645. /*
  3646. * For WRITEs, let the fabric know its buffer is ready..
  3647. * This WRITE struct se_cmd (and all of its associated struct se_task's)
  3648. * will be added to the struct se_device execution queue after its WRITE
  3649. * data has arrived. (ie: It gets handled by the transport processing
  3650. * thread a second time)
  3651. */
  3652. if (cmd->data_direction == DMA_TO_DEVICE) {
  3653. transport_add_tasks_to_state_queue(cmd);
  3654. return transport_generic_write_pending(cmd);
  3655. }
  3656. /*
  3657. * Everything else but a WRITE, add the struct se_cmd's struct se_task's
  3658. * to the execution queue.
  3659. */
  3660. transport_execute_tasks(cmd);
  3661. return 0;
  3662. }
  3663. EXPORT_SYMBOL(transport_generic_new_cmd);
  3664. /* transport_generic_process_write():
  3665. *
  3666. *
  3667. */
  3668. void transport_generic_process_write(struct se_cmd *cmd)
  3669. {
  3670. transport_execute_tasks(cmd);
  3671. }
  3672. EXPORT_SYMBOL(transport_generic_process_write);
  3673. static void transport_write_pending_qf(struct se_cmd *cmd)
  3674. {
  3675. if (cmd->se_tfo->write_pending(cmd) == -EAGAIN) {
  3676. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
  3677. cmd);
  3678. transport_handle_queue_full(cmd, cmd->se_dev);
  3679. }
  3680. }
  3681. static int transport_generic_write_pending(struct se_cmd *cmd)
  3682. {
  3683. unsigned long flags;
  3684. int ret;
  3685. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3686. cmd->t_state = TRANSPORT_WRITE_PENDING;
  3687. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3688. /*
  3689. * Clear the se_cmd for WRITE_PENDING status in order to set
  3690. * cmd->t_transport_active=0 so that transport_generic_handle_data
  3691. * can be called from HW target mode interrupt code. This is safe
  3692. * to be called with transport_off=1 before the cmd->se_tfo->write_pending
  3693. * because the se_cmd->se_lun pointer is not being cleared.
  3694. */
  3695. transport_cmd_check_stop(cmd, 1, 0);
  3696. /*
  3697. * Call the fabric write_pending function here to let the
  3698. * frontend know that WRITE buffers are ready.
  3699. */
  3700. ret = cmd->se_tfo->write_pending(cmd);
  3701. if (ret == -EAGAIN)
  3702. goto queue_full;
  3703. else if (ret < 0)
  3704. return ret;
  3705. return PYX_TRANSPORT_WRITE_PENDING;
  3706. queue_full:
  3707. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
  3708. cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
  3709. transport_handle_queue_full(cmd, cmd->se_dev);
  3710. return ret;
  3711. }
  3712. /**
  3713. * transport_release_cmd - free a command
  3714. * @cmd: command to free
  3715. *
  3716. * This routine unconditionally frees a command, and reference counting
  3717. * or list removal must be done in the caller.
  3718. */
  3719. void transport_release_cmd(struct se_cmd *cmd)
  3720. {
  3721. BUG_ON(!cmd->se_tfo);
  3722. if (cmd->se_tmr_req)
  3723. core_tmr_release_req(cmd->se_tmr_req);
  3724. if (cmd->t_task_cdb != cmd->__t_task_cdb)
  3725. kfree(cmd->t_task_cdb);
  3726. cmd->se_tfo->release_cmd(cmd);
  3727. }
  3728. EXPORT_SYMBOL(transport_release_cmd);
  3729. void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
  3730. {
  3731. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
  3732. if (wait_for_tasks && cmd->se_tmr_req)
  3733. transport_wait_for_tasks(cmd);
  3734. transport_release_cmd(cmd);
  3735. } else {
  3736. if (wait_for_tasks)
  3737. transport_wait_for_tasks(cmd);
  3738. core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
  3739. if (cmd->se_lun)
  3740. transport_lun_remove_cmd(cmd);
  3741. transport_free_dev_tasks(cmd);
  3742. transport_put_cmd(cmd);
  3743. }
  3744. }
  3745. EXPORT_SYMBOL(transport_generic_free_cmd);
  3746. /* transport_lun_wait_for_tasks():
  3747. *
  3748. * Called from ConfigFS context to stop the passed struct se_cmd to allow
  3749. * an struct se_lun to be successfully shutdown.
  3750. */
  3751. static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
  3752. {
  3753. unsigned long flags;
  3754. int ret;
  3755. /*
  3756. * If the frontend has already requested this struct se_cmd to
  3757. * be stopped, we can safely ignore this struct se_cmd.
  3758. */
  3759. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3760. if (atomic_read(&cmd->t_transport_stop)) {
  3761. atomic_set(&cmd->transport_lun_stop, 0);
  3762. pr_debug("ConfigFS ITT[0x%08x] - t_transport_stop =="
  3763. " TRUE, skipping\n", cmd->se_tfo->get_task_tag(cmd));
  3764. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3765. transport_cmd_check_stop(cmd, 1, 0);
  3766. return -EPERM;
  3767. }
  3768. atomic_set(&cmd->transport_lun_fe_stop, 1);
  3769. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3770. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  3771. ret = transport_stop_tasks_for_cmd(cmd);
  3772. pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
  3773. " %d\n", cmd, cmd->t_task_list_num, ret);
  3774. if (!ret) {
  3775. pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
  3776. cmd->se_tfo->get_task_tag(cmd));
  3777. wait_for_completion(&cmd->transport_lun_stop_comp);
  3778. pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
  3779. cmd->se_tfo->get_task_tag(cmd));
  3780. }
  3781. transport_remove_cmd_from_queue(cmd);
  3782. return 0;
  3783. }
  3784. static void __transport_clear_lun_from_sessions(struct se_lun *lun)
  3785. {
  3786. struct se_cmd *cmd = NULL;
  3787. unsigned long lun_flags, cmd_flags;
  3788. /*
  3789. * Do exception processing and return CHECK_CONDITION status to the
  3790. * Initiator Port.
  3791. */
  3792. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3793. while (!list_empty(&lun->lun_cmd_list)) {
  3794. cmd = list_first_entry(&lun->lun_cmd_list,
  3795. struct se_cmd, se_lun_node);
  3796. list_del(&cmd->se_lun_node);
  3797. atomic_set(&cmd->transport_lun_active, 0);
  3798. /*
  3799. * This will notify iscsi_target_transport.c:
  3800. * transport_cmd_check_stop() that a LUN shutdown is in
  3801. * progress for the iscsi_cmd_t.
  3802. */
  3803. spin_lock(&cmd->t_state_lock);
  3804. pr_debug("SE_LUN[%d] - Setting cmd->transport"
  3805. "_lun_stop for ITT: 0x%08x\n",
  3806. cmd->se_lun->unpacked_lun,
  3807. cmd->se_tfo->get_task_tag(cmd));
  3808. atomic_set(&cmd->transport_lun_stop, 1);
  3809. spin_unlock(&cmd->t_state_lock);
  3810. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  3811. if (!cmd->se_lun) {
  3812. pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
  3813. cmd->se_tfo->get_task_tag(cmd),
  3814. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
  3815. BUG();
  3816. }
  3817. /*
  3818. * If the Storage engine still owns the iscsi_cmd_t, determine
  3819. * and/or stop its context.
  3820. */
  3821. pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
  3822. "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
  3823. cmd->se_tfo->get_task_tag(cmd));
  3824. if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
  3825. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3826. continue;
  3827. }
  3828. pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
  3829. "_wait_for_tasks(): SUCCESS\n",
  3830. cmd->se_lun->unpacked_lun,
  3831. cmd->se_tfo->get_task_tag(cmd));
  3832. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  3833. if (!atomic_read(&cmd->transport_dev_active)) {
  3834. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3835. goto check_cond;
  3836. }
  3837. atomic_set(&cmd->transport_dev_active, 0);
  3838. transport_all_task_dev_remove_state(cmd);
  3839. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3840. transport_free_dev_tasks(cmd);
  3841. /*
  3842. * The Storage engine stopped this struct se_cmd before it was
  3843. * send to the fabric frontend for delivery back to the
  3844. * Initiator Node. Return this SCSI CDB back with an
  3845. * CHECK_CONDITION status.
  3846. */
  3847. check_cond:
  3848. transport_send_check_condition_and_sense(cmd,
  3849. TCM_NON_EXISTENT_LUN, 0);
  3850. /*
  3851. * If the fabric frontend is waiting for this iscsi_cmd_t to
  3852. * be released, notify the waiting thread now that LU has
  3853. * finished accessing it.
  3854. */
  3855. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  3856. if (atomic_read(&cmd->transport_lun_fe_stop)) {
  3857. pr_debug("SE_LUN[%d] - Detected FE stop for"
  3858. " struct se_cmd: %p ITT: 0x%08x\n",
  3859. lun->unpacked_lun,
  3860. cmd, cmd->se_tfo->get_task_tag(cmd));
  3861. spin_unlock_irqrestore(&cmd->t_state_lock,
  3862. cmd_flags);
  3863. transport_cmd_check_stop(cmd, 1, 0);
  3864. complete(&cmd->transport_lun_fe_stop_comp);
  3865. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3866. continue;
  3867. }
  3868. pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
  3869. lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
  3870. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3871. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3872. }
  3873. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  3874. }
  3875. static int transport_clear_lun_thread(void *p)
  3876. {
  3877. struct se_lun *lun = (struct se_lun *)p;
  3878. __transport_clear_lun_from_sessions(lun);
  3879. complete(&lun->lun_shutdown_comp);
  3880. return 0;
  3881. }
  3882. int transport_clear_lun_from_sessions(struct se_lun *lun)
  3883. {
  3884. struct task_struct *kt;
  3885. kt = kthread_run(transport_clear_lun_thread, lun,
  3886. "tcm_cl_%u", lun->unpacked_lun);
  3887. if (IS_ERR(kt)) {
  3888. pr_err("Unable to start clear_lun thread\n");
  3889. return PTR_ERR(kt);
  3890. }
  3891. wait_for_completion(&lun->lun_shutdown_comp);
  3892. return 0;
  3893. }
  3894. /**
  3895. * transport_wait_for_tasks - wait for completion to occur
  3896. * @cmd: command to wait
  3897. *
  3898. * Called from frontend fabric context to wait for storage engine
  3899. * to pause and/or release frontend generated struct se_cmd.
  3900. */
  3901. void transport_wait_for_tasks(struct se_cmd *cmd)
  3902. {
  3903. unsigned long flags;
  3904. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3905. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req)) {
  3906. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3907. return;
  3908. }
  3909. /*
  3910. * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
  3911. * has been set in transport_set_supported_SAM_opcode().
  3912. */
  3913. if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) && !cmd->se_tmr_req) {
  3914. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3915. return;
  3916. }
  3917. /*
  3918. * If we are already stopped due to an external event (ie: LUN shutdown)
  3919. * sleep until the connection can have the passed struct se_cmd back.
  3920. * The cmd->transport_lun_stopped_sem will be upped by
  3921. * transport_clear_lun_from_sessions() once the ConfigFS context caller
  3922. * has completed its operation on the struct se_cmd.
  3923. */
  3924. if (atomic_read(&cmd->transport_lun_stop)) {
  3925. pr_debug("wait_for_tasks: Stopping"
  3926. " wait_for_completion(&cmd->t_tasktransport_lun_fe"
  3927. "_stop_comp); for ITT: 0x%08x\n",
  3928. cmd->se_tfo->get_task_tag(cmd));
  3929. /*
  3930. * There is a special case for WRITES where a FE exception +
  3931. * LUN shutdown means ConfigFS context is still sleeping on
  3932. * transport_lun_stop_comp in transport_lun_wait_for_tasks().
  3933. * We go ahead and up transport_lun_stop_comp just to be sure
  3934. * here.
  3935. */
  3936. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3937. complete(&cmd->transport_lun_stop_comp);
  3938. wait_for_completion(&cmd->transport_lun_fe_stop_comp);
  3939. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3940. transport_all_task_dev_remove_state(cmd);
  3941. /*
  3942. * At this point, the frontend who was the originator of this
  3943. * struct se_cmd, now owns the structure and can be released through
  3944. * normal means below.
  3945. */
  3946. pr_debug("wait_for_tasks: Stopped"
  3947. " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
  3948. "stop_comp); for ITT: 0x%08x\n",
  3949. cmd->se_tfo->get_task_tag(cmd));
  3950. atomic_set(&cmd->transport_lun_stop, 0);
  3951. }
  3952. if (!atomic_read(&cmd->t_transport_active) ||
  3953. atomic_read(&cmd->t_transport_aborted)) {
  3954. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3955. return;
  3956. }
  3957. atomic_set(&cmd->t_transport_stop, 1);
  3958. pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
  3959. " i_state: %d, t_state/def_t_state: %d/%d, t_transport_stop"
  3960. " = TRUE\n", cmd, cmd->se_tfo->get_task_tag(cmd),
  3961. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
  3962. cmd->deferred_t_state);
  3963. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3964. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  3965. wait_for_completion(&cmd->t_transport_stop_comp);
  3966. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3967. atomic_set(&cmd->t_transport_active, 0);
  3968. atomic_set(&cmd->t_transport_stop, 0);
  3969. pr_debug("wait_for_tasks: Stopped wait_for_compltion("
  3970. "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
  3971. cmd->se_tfo->get_task_tag(cmd));
  3972. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3973. }
  3974. EXPORT_SYMBOL(transport_wait_for_tasks);
  3975. static int transport_get_sense_codes(
  3976. struct se_cmd *cmd,
  3977. u8 *asc,
  3978. u8 *ascq)
  3979. {
  3980. *asc = cmd->scsi_asc;
  3981. *ascq = cmd->scsi_ascq;
  3982. return 0;
  3983. }
  3984. static int transport_set_sense_codes(
  3985. struct se_cmd *cmd,
  3986. u8 asc,
  3987. u8 ascq)
  3988. {
  3989. cmd->scsi_asc = asc;
  3990. cmd->scsi_ascq = ascq;
  3991. return 0;
  3992. }
  3993. int transport_send_check_condition_and_sense(
  3994. struct se_cmd *cmd,
  3995. u8 reason,
  3996. int from_transport)
  3997. {
  3998. unsigned char *buffer = cmd->sense_buffer;
  3999. unsigned long flags;
  4000. int offset;
  4001. u8 asc = 0, ascq = 0;
  4002. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4003. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  4004. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4005. return 0;
  4006. }
  4007. cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
  4008. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4009. if (!reason && from_transport)
  4010. goto after_reason;
  4011. if (!from_transport)
  4012. cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
  4013. /*
  4014. * Data Segment and SenseLength of the fabric response PDU.
  4015. *
  4016. * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
  4017. * from include/scsi/scsi_cmnd.h
  4018. */
  4019. offset = cmd->se_tfo->set_fabric_sense_len(cmd,
  4020. TRANSPORT_SENSE_BUFFER);
  4021. /*
  4022. * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
  4023. * SENSE KEY values from include/scsi/scsi.h
  4024. */
  4025. switch (reason) {
  4026. case TCM_NON_EXISTENT_LUN:
  4027. /* CURRENT ERROR */
  4028. buffer[offset] = 0x70;
  4029. /* ILLEGAL REQUEST */
  4030. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4031. /* LOGICAL UNIT NOT SUPPORTED */
  4032. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
  4033. break;
  4034. case TCM_UNSUPPORTED_SCSI_OPCODE:
  4035. case TCM_SECTOR_COUNT_TOO_MANY:
  4036. /* CURRENT ERROR */
  4037. buffer[offset] = 0x70;
  4038. /* ILLEGAL REQUEST */
  4039. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4040. /* INVALID COMMAND OPERATION CODE */
  4041. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
  4042. break;
  4043. case TCM_UNKNOWN_MODE_PAGE:
  4044. /* CURRENT ERROR */
  4045. buffer[offset] = 0x70;
  4046. /* ILLEGAL REQUEST */
  4047. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4048. /* INVALID FIELD IN CDB */
  4049. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  4050. break;
  4051. case TCM_CHECK_CONDITION_ABORT_CMD:
  4052. /* CURRENT ERROR */
  4053. buffer[offset] = 0x70;
  4054. /* ABORTED COMMAND */
  4055. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4056. /* BUS DEVICE RESET FUNCTION OCCURRED */
  4057. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
  4058. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
  4059. break;
  4060. case TCM_INCORRECT_AMOUNT_OF_DATA:
  4061. /* CURRENT ERROR */
  4062. buffer[offset] = 0x70;
  4063. /* ABORTED COMMAND */
  4064. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4065. /* WRITE ERROR */
  4066. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  4067. /* NOT ENOUGH UNSOLICITED DATA */
  4068. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
  4069. break;
  4070. case TCM_INVALID_CDB_FIELD:
  4071. /* CURRENT ERROR */
  4072. buffer[offset] = 0x70;
  4073. /* ABORTED COMMAND */
  4074. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4075. /* INVALID FIELD IN CDB */
  4076. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  4077. break;
  4078. case TCM_INVALID_PARAMETER_LIST:
  4079. /* CURRENT ERROR */
  4080. buffer[offset] = 0x70;
  4081. /* ABORTED COMMAND */
  4082. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4083. /* INVALID FIELD IN PARAMETER LIST */
  4084. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
  4085. break;
  4086. case TCM_UNEXPECTED_UNSOLICITED_DATA:
  4087. /* CURRENT ERROR */
  4088. buffer[offset] = 0x70;
  4089. /* ABORTED COMMAND */
  4090. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4091. /* WRITE ERROR */
  4092. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  4093. /* UNEXPECTED_UNSOLICITED_DATA */
  4094. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
  4095. break;
  4096. case TCM_SERVICE_CRC_ERROR:
  4097. /* CURRENT ERROR */
  4098. buffer[offset] = 0x70;
  4099. /* ABORTED COMMAND */
  4100. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4101. /* PROTOCOL SERVICE CRC ERROR */
  4102. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
  4103. /* N/A */
  4104. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
  4105. break;
  4106. case TCM_SNACK_REJECTED:
  4107. /* CURRENT ERROR */
  4108. buffer[offset] = 0x70;
  4109. /* ABORTED COMMAND */
  4110. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4111. /* READ ERROR */
  4112. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
  4113. /* FAILED RETRANSMISSION REQUEST */
  4114. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
  4115. break;
  4116. case TCM_WRITE_PROTECTED:
  4117. /* CURRENT ERROR */
  4118. buffer[offset] = 0x70;
  4119. /* DATA PROTECT */
  4120. buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
  4121. /* WRITE PROTECTED */
  4122. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
  4123. break;
  4124. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  4125. /* CURRENT ERROR */
  4126. buffer[offset] = 0x70;
  4127. /* UNIT ATTENTION */
  4128. buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  4129. core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
  4130. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  4131. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  4132. break;
  4133. case TCM_CHECK_CONDITION_NOT_READY:
  4134. /* CURRENT ERROR */
  4135. buffer[offset] = 0x70;
  4136. /* Not Ready */
  4137. buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
  4138. transport_get_sense_codes(cmd, &asc, &ascq);
  4139. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  4140. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  4141. break;
  4142. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  4143. default:
  4144. /* CURRENT ERROR */
  4145. buffer[offset] = 0x70;
  4146. /* ILLEGAL REQUEST */
  4147. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4148. /* LOGICAL UNIT COMMUNICATION FAILURE */
  4149. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
  4150. break;
  4151. }
  4152. /*
  4153. * This code uses linux/include/scsi/scsi.h SAM status codes!
  4154. */
  4155. cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
  4156. /*
  4157. * Automatically padded, this value is encoded in the fabric's
  4158. * data_length response PDU containing the SCSI defined sense data.
  4159. */
  4160. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
  4161. after_reason:
  4162. return cmd->se_tfo->queue_status(cmd);
  4163. }
  4164. EXPORT_SYMBOL(transport_send_check_condition_and_sense);
  4165. int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
  4166. {
  4167. int ret = 0;
  4168. if (atomic_read(&cmd->t_transport_aborted) != 0) {
  4169. if (!send_status ||
  4170. (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
  4171. return 1;
  4172. #if 0
  4173. pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
  4174. " status for CDB: 0x%02x ITT: 0x%08x\n",
  4175. cmd->t_task_cdb[0],
  4176. cmd->se_tfo->get_task_tag(cmd));
  4177. #endif
  4178. cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
  4179. cmd->se_tfo->queue_status(cmd);
  4180. ret = 1;
  4181. }
  4182. return ret;
  4183. }
  4184. EXPORT_SYMBOL(transport_check_aborted_status);
  4185. void transport_send_task_abort(struct se_cmd *cmd)
  4186. {
  4187. unsigned long flags;
  4188. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4189. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  4190. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4191. return;
  4192. }
  4193. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4194. /*
  4195. * If there are still expected incoming fabric WRITEs, we wait
  4196. * until until they have completed before sending a TASK_ABORTED
  4197. * response. This response with TASK_ABORTED status will be
  4198. * queued back to fabric module by transport_check_aborted_status().
  4199. */
  4200. if (cmd->data_direction == DMA_TO_DEVICE) {
  4201. if (cmd->se_tfo->write_pending_status(cmd) != 0) {
  4202. atomic_inc(&cmd->t_transport_aborted);
  4203. smp_mb__after_atomic_inc();
  4204. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  4205. transport_new_cmd_failure(cmd);
  4206. return;
  4207. }
  4208. }
  4209. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  4210. #if 0
  4211. pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
  4212. " ITT: 0x%08x\n", cmd->t_task_cdb[0],
  4213. cmd->se_tfo->get_task_tag(cmd));
  4214. #endif
  4215. cmd->se_tfo->queue_status(cmd);
  4216. }
  4217. /* transport_generic_do_tmr():
  4218. *
  4219. *
  4220. */
  4221. int transport_generic_do_tmr(struct se_cmd *cmd)
  4222. {
  4223. struct se_device *dev = cmd->se_dev;
  4224. struct se_tmr_req *tmr = cmd->se_tmr_req;
  4225. int ret;
  4226. switch (tmr->function) {
  4227. case TMR_ABORT_TASK:
  4228. tmr->response = TMR_FUNCTION_REJECTED;
  4229. break;
  4230. case TMR_ABORT_TASK_SET:
  4231. case TMR_CLEAR_ACA:
  4232. case TMR_CLEAR_TASK_SET:
  4233. tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  4234. break;
  4235. case TMR_LUN_RESET:
  4236. ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
  4237. tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
  4238. TMR_FUNCTION_REJECTED;
  4239. break;
  4240. case TMR_TARGET_WARM_RESET:
  4241. tmr->response = TMR_FUNCTION_REJECTED;
  4242. break;
  4243. case TMR_TARGET_COLD_RESET:
  4244. tmr->response = TMR_FUNCTION_REJECTED;
  4245. break;
  4246. default:
  4247. pr_err("Uknown TMR function: 0x%02x.\n",
  4248. tmr->function);
  4249. tmr->response = TMR_FUNCTION_REJECTED;
  4250. break;
  4251. }
  4252. cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
  4253. cmd->se_tfo->queue_tm_rsp(cmd);
  4254. transport_cmd_check_stop_to_fabric(cmd);
  4255. return 0;
  4256. }
  4257. /* transport_processing_thread():
  4258. *
  4259. *
  4260. */
  4261. static int transport_processing_thread(void *param)
  4262. {
  4263. int ret;
  4264. struct se_cmd *cmd;
  4265. struct se_device *dev = (struct se_device *) param;
  4266. set_user_nice(current, -20);
  4267. while (!kthread_should_stop()) {
  4268. ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
  4269. atomic_read(&dev->dev_queue_obj.queue_cnt) ||
  4270. kthread_should_stop());
  4271. if (ret < 0)
  4272. goto out;
  4273. get_cmd:
  4274. __transport_execute_tasks(dev);
  4275. cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
  4276. if (!cmd)
  4277. continue;
  4278. switch (cmd->t_state) {
  4279. case TRANSPORT_NEW_CMD:
  4280. BUG();
  4281. break;
  4282. case TRANSPORT_NEW_CMD_MAP:
  4283. if (!cmd->se_tfo->new_cmd_map) {
  4284. pr_err("cmd->se_tfo->new_cmd_map is"
  4285. " NULL for TRANSPORT_NEW_CMD_MAP\n");
  4286. BUG();
  4287. }
  4288. ret = cmd->se_tfo->new_cmd_map(cmd);
  4289. if (ret < 0) {
  4290. cmd->transport_error_status = ret;
  4291. transport_generic_request_failure(cmd, NULL,
  4292. 0, (cmd->data_direction !=
  4293. DMA_TO_DEVICE));
  4294. break;
  4295. }
  4296. ret = transport_generic_new_cmd(cmd);
  4297. if (ret == -EAGAIN)
  4298. break;
  4299. else if (ret < 0) {
  4300. cmd->transport_error_status = ret;
  4301. transport_generic_request_failure(cmd, NULL,
  4302. 0, (cmd->data_direction !=
  4303. DMA_TO_DEVICE));
  4304. }
  4305. break;
  4306. case TRANSPORT_PROCESS_WRITE:
  4307. transport_generic_process_write(cmd);
  4308. break;
  4309. case TRANSPORT_COMPLETE_OK:
  4310. transport_stop_all_task_timers(cmd);
  4311. transport_generic_complete_ok(cmd);
  4312. break;
  4313. case TRANSPORT_REMOVE:
  4314. transport_put_cmd(cmd);
  4315. break;
  4316. case TRANSPORT_FREE_CMD_INTR:
  4317. transport_generic_free_cmd(cmd, 0);
  4318. break;
  4319. case TRANSPORT_PROCESS_TMR:
  4320. transport_generic_do_tmr(cmd);
  4321. break;
  4322. case TRANSPORT_COMPLETE_FAILURE:
  4323. transport_generic_request_failure(cmd, NULL, 1, 1);
  4324. break;
  4325. case TRANSPORT_COMPLETE_TIMEOUT:
  4326. transport_stop_all_task_timers(cmd);
  4327. transport_generic_request_timeout(cmd);
  4328. break;
  4329. case TRANSPORT_COMPLETE_QF_WP:
  4330. transport_write_pending_qf(cmd);
  4331. break;
  4332. case TRANSPORT_COMPLETE_QF_OK:
  4333. transport_complete_qf(cmd);
  4334. break;
  4335. default:
  4336. pr_err("Unknown t_state: %d deferred_t_state:"
  4337. " %d for ITT: 0x%08x i_state: %d on SE LUN:"
  4338. " %u\n", cmd->t_state, cmd->deferred_t_state,
  4339. cmd->se_tfo->get_task_tag(cmd),
  4340. cmd->se_tfo->get_cmd_state(cmd),
  4341. cmd->se_lun->unpacked_lun);
  4342. BUG();
  4343. }
  4344. goto get_cmd;
  4345. }
  4346. out:
  4347. WARN_ON(!list_empty(&dev->state_task_list));
  4348. WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
  4349. dev->process_thread = NULL;
  4350. return 0;
  4351. }