target_core_transport.c 129 KB

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