target_core_transport.c 127 KB

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