target_core_transport.c 122 KB

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