target_core_transport.c 130 KB

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