target_core_transport.c 129 KB

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