target_core_transport.c 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872
  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. /* transport_generic_allocate_tasks():
  1370. *
  1371. * Called from fabric RX Thread.
  1372. */
  1373. int transport_generic_allocate_tasks(
  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(transport_generic_allocate_tasks);
  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. /*
  1514. * Obtain struct se_cmd->cmd_kref reference and add new cmd to
  1515. * se_sess->sess_cmd_list. A second kref_get here is necessary
  1516. * for fabrics using TARGET_SCF_ACK_KREF that expect a second
  1517. * kref_put() to happen during fabric packet acknowledgement.
  1518. */
  1519. target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
  1520. /*
  1521. * Signal bidirectional data payloads to target-core
  1522. */
  1523. if (flags & TARGET_SCF_BIDI_OP)
  1524. se_cmd->se_cmd_flags |= SCF_BIDI;
  1525. /*
  1526. * Locate se_lun pointer and attach it to struct se_cmd
  1527. */
  1528. if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
  1529. transport_send_check_condition_and_sense(se_cmd,
  1530. se_cmd->scsi_sense_reason, 0);
  1531. target_put_sess_cmd(se_sess, se_cmd);
  1532. return;
  1533. }
  1534. /*
  1535. * Sanitize CDBs via transport_generic_cmd_sequencer() and
  1536. * allocate the necessary tasks to complete the received CDB+data
  1537. */
  1538. rc = transport_generic_allocate_tasks(se_cmd, cdb);
  1539. if (rc != 0) {
  1540. transport_generic_request_failure(se_cmd);
  1541. return;
  1542. }
  1543. /*
  1544. * Dispatch se_cmd descriptor to se_lun->lun_se_dev backend
  1545. * for immediate execution of READs, otherwise wait for
  1546. * transport_generic_handle_data() to be called for WRITEs
  1547. * when fabric has filled the incoming buffer.
  1548. */
  1549. transport_handle_cdb_direct(se_cmd);
  1550. return;
  1551. }
  1552. EXPORT_SYMBOL(target_submit_cmd);
  1553. static void target_complete_tmr_failure(struct work_struct *work)
  1554. {
  1555. struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
  1556. se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
  1557. se_cmd->se_tfo->queue_tm_rsp(se_cmd);
  1558. transport_generic_free_cmd(se_cmd, 0);
  1559. }
  1560. /**
  1561. * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
  1562. * for TMR CDBs
  1563. *
  1564. * @se_cmd: command descriptor to submit
  1565. * @se_sess: associated se_sess for endpoint
  1566. * @sense: pointer to SCSI sense buffer
  1567. * @unpacked_lun: unpacked LUN to reference for struct se_lun
  1568. * @fabric_context: fabric context for TMR req
  1569. * @tm_type: Type of TM request
  1570. * @gfp: gfp type for caller
  1571. * @tag: referenced task tag for TMR_ABORT_TASK
  1572. * @flags: submit cmd flags
  1573. *
  1574. * Callable from all contexts.
  1575. **/
  1576. int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
  1577. unsigned char *sense, u32 unpacked_lun,
  1578. void *fabric_tmr_ptr, unsigned char tm_type,
  1579. gfp_t gfp, unsigned int tag, int flags)
  1580. {
  1581. struct se_portal_group *se_tpg;
  1582. int ret;
  1583. se_tpg = se_sess->se_tpg;
  1584. BUG_ON(!se_tpg);
  1585. transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
  1586. 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
  1587. /*
  1588. * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
  1589. * allocation failure.
  1590. */
  1591. ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
  1592. if (ret < 0)
  1593. return -ENOMEM;
  1594. if (tm_type == TMR_ABORT_TASK)
  1595. se_cmd->se_tmr_req->ref_task_tag = tag;
  1596. /* See target_submit_cmd for commentary */
  1597. target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
  1598. ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
  1599. if (ret) {
  1600. /*
  1601. * For callback during failure handling, push this work off
  1602. * to process context with TMR_LUN_DOES_NOT_EXIST status.
  1603. */
  1604. INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
  1605. schedule_work(&se_cmd->work);
  1606. return 0;
  1607. }
  1608. transport_generic_handle_tmr(se_cmd);
  1609. return 0;
  1610. }
  1611. EXPORT_SYMBOL(target_submit_tmr);
  1612. /*
  1613. * Used by fabric module frontends defining a TFO->new_cmd_map() caller
  1614. * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
  1615. * complete setup in TCM process context w/ TFO->new_cmd_map().
  1616. */
  1617. int transport_generic_handle_cdb_map(
  1618. struct se_cmd *cmd)
  1619. {
  1620. if (!cmd->se_lun) {
  1621. dump_stack();
  1622. pr_err("cmd->se_lun is NULL\n");
  1623. return -EINVAL;
  1624. }
  1625. transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
  1626. return 0;
  1627. }
  1628. EXPORT_SYMBOL(transport_generic_handle_cdb_map);
  1629. /* transport_generic_handle_data():
  1630. *
  1631. *
  1632. */
  1633. int transport_generic_handle_data(
  1634. struct se_cmd *cmd)
  1635. {
  1636. /*
  1637. * For the software fabric case, then we assume the nexus is being
  1638. * failed/shutdown when signals are pending from the kthread context
  1639. * caller, so we return a failure. For the HW target mode case running
  1640. * in interrupt code, the signal_pending() check is skipped.
  1641. */
  1642. if (!in_interrupt() && signal_pending(current))
  1643. return -EPERM;
  1644. /*
  1645. * If the received CDB has aleady been ABORTED by the generic
  1646. * target engine, we now call transport_check_aborted_status()
  1647. * to queue any delated TASK_ABORTED status for the received CDB to the
  1648. * fabric module as we are expecting no further incoming DATA OUT
  1649. * sequences at this point.
  1650. */
  1651. if (transport_check_aborted_status(cmd, 1) != 0)
  1652. return 0;
  1653. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
  1654. return 0;
  1655. }
  1656. EXPORT_SYMBOL(transport_generic_handle_data);
  1657. /* transport_generic_handle_tmr():
  1658. *
  1659. *
  1660. */
  1661. int transport_generic_handle_tmr(
  1662. struct se_cmd *cmd)
  1663. {
  1664. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
  1665. return 0;
  1666. }
  1667. EXPORT_SYMBOL(transport_generic_handle_tmr);
  1668. /*
  1669. * If the task is active, request it to be stopped and sleep until it
  1670. * has completed.
  1671. */
  1672. bool target_stop_task(struct se_task *task, unsigned long *flags)
  1673. {
  1674. struct se_cmd *cmd = task->task_se_cmd;
  1675. bool was_active = false;
  1676. if (task->task_flags & TF_ACTIVE) {
  1677. task->task_flags |= TF_REQUEST_STOP;
  1678. spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
  1679. pr_debug("Task %p waiting to complete\n", task);
  1680. wait_for_completion(&task->task_stop_comp);
  1681. pr_debug("Task %p stopped successfully\n", task);
  1682. spin_lock_irqsave(&cmd->t_state_lock, *flags);
  1683. atomic_dec(&cmd->t_task_cdbs_left);
  1684. task->task_flags &= ~(TF_ACTIVE | TF_REQUEST_STOP);
  1685. was_active = true;
  1686. }
  1687. return was_active;
  1688. }
  1689. static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
  1690. {
  1691. struct se_task *task, *task_tmp;
  1692. unsigned long flags;
  1693. int ret = 0;
  1694. pr_debug("ITT[0x%08x] - Stopping tasks\n",
  1695. cmd->se_tfo->get_task_tag(cmd));
  1696. /*
  1697. * No tasks remain in the execution queue
  1698. */
  1699. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1700. list_for_each_entry_safe(task, task_tmp,
  1701. &cmd->t_task_list, t_list) {
  1702. pr_debug("Processing task %p\n", task);
  1703. /*
  1704. * If the struct se_task has not been sent and is not active,
  1705. * remove the struct se_task from the execution queue.
  1706. */
  1707. if (!(task->task_flags & (TF_ACTIVE | TF_SENT))) {
  1708. spin_unlock_irqrestore(&cmd->t_state_lock,
  1709. flags);
  1710. transport_remove_task_from_execute_queue(task,
  1711. cmd->se_dev);
  1712. pr_debug("Task %p removed from execute queue\n", task);
  1713. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1714. continue;
  1715. }
  1716. if (!target_stop_task(task, &flags)) {
  1717. pr_debug("Task %p - did nothing\n", task);
  1718. ret++;
  1719. }
  1720. }
  1721. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1722. return ret;
  1723. }
  1724. /*
  1725. * Handle SAM-esque emulation for generic transport request failures.
  1726. */
  1727. void transport_generic_request_failure(struct se_cmd *cmd)
  1728. {
  1729. int ret = 0;
  1730. pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
  1731. " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
  1732. cmd->t_task_cdb[0]);
  1733. pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
  1734. cmd->se_tfo->get_cmd_state(cmd),
  1735. cmd->t_state, cmd->scsi_sense_reason);
  1736. pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d"
  1737. " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
  1738. " CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
  1739. cmd->t_task_list_num,
  1740. atomic_read(&cmd->t_task_cdbs_left),
  1741. atomic_read(&cmd->t_task_cdbs_sent),
  1742. atomic_read(&cmd->t_task_cdbs_ex_left),
  1743. (cmd->transport_state & CMD_T_ACTIVE) != 0,
  1744. (cmd->transport_state & CMD_T_STOP) != 0,
  1745. (cmd->transport_state & CMD_T_SENT) != 0);
  1746. /*
  1747. * For SAM Task Attribute emulation for failed struct se_cmd
  1748. */
  1749. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  1750. transport_complete_task_attr(cmd);
  1751. switch (cmd->scsi_sense_reason) {
  1752. case TCM_NON_EXISTENT_LUN:
  1753. case TCM_UNSUPPORTED_SCSI_OPCODE:
  1754. case TCM_INVALID_CDB_FIELD:
  1755. case TCM_INVALID_PARAMETER_LIST:
  1756. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  1757. case TCM_UNKNOWN_MODE_PAGE:
  1758. case TCM_WRITE_PROTECTED:
  1759. case TCM_CHECK_CONDITION_ABORT_CMD:
  1760. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  1761. case TCM_CHECK_CONDITION_NOT_READY:
  1762. break;
  1763. case TCM_RESERVATION_CONFLICT:
  1764. /*
  1765. * No SENSE Data payload for this case, set SCSI Status
  1766. * and queue the response to $FABRIC_MOD.
  1767. *
  1768. * Uses linux/include/scsi/scsi.h SAM status codes defs
  1769. */
  1770. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  1771. /*
  1772. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  1773. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  1774. * CONFLICT STATUS.
  1775. *
  1776. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  1777. */
  1778. if (cmd->se_sess &&
  1779. cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
  1780. core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
  1781. cmd->orig_fe_lun, 0x2C,
  1782. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  1783. ret = cmd->se_tfo->queue_status(cmd);
  1784. if (ret == -EAGAIN || ret == -ENOMEM)
  1785. goto queue_full;
  1786. goto check_stop;
  1787. default:
  1788. pr_err("Unknown transport error for CDB 0x%02x: %d\n",
  1789. cmd->t_task_cdb[0], cmd->scsi_sense_reason);
  1790. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1791. break;
  1792. }
  1793. /*
  1794. * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
  1795. * make the call to transport_send_check_condition_and_sense()
  1796. * directly. Otherwise expect the fabric to make the call to
  1797. * transport_send_check_condition_and_sense() after handling
  1798. * possible unsoliticied write data payloads.
  1799. */
  1800. ret = transport_send_check_condition_and_sense(cmd,
  1801. cmd->scsi_sense_reason, 0);
  1802. if (ret == -EAGAIN || ret == -ENOMEM)
  1803. goto queue_full;
  1804. check_stop:
  1805. transport_lun_remove_cmd(cmd);
  1806. if (!transport_cmd_check_stop_to_fabric(cmd))
  1807. ;
  1808. return;
  1809. queue_full:
  1810. cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
  1811. transport_handle_queue_full(cmd, cmd->se_dev);
  1812. }
  1813. EXPORT_SYMBOL(transport_generic_request_failure);
  1814. static inline u32 transport_lba_21(unsigned char *cdb)
  1815. {
  1816. return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
  1817. }
  1818. static inline u32 transport_lba_32(unsigned char *cdb)
  1819. {
  1820. return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1821. }
  1822. static inline unsigned long long transport_lba_64(unsigned char *cdb)
  1823. {
  1824. unsigned int __v1, __v2;
  1825. __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1826. __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1827. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  1828. }
  1829. /*
  1830. * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
  1831. */
  1832. static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
  1833. {
  1834. unsigned int __v1, __v2;
  1835. __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
  1836. __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
  1837. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  1838. }
  1839. static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
  1840. {
  1841. unsigned long flags;
  1842. spin_lock_irqsave(&se_cmd->t_state_lock, flags);
  1843. se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
  1844. spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
  1845. }
  1846. /*
  1847. * Called from Fabric Module context from transport_execute_tasks()
  1848. *
  1849. * The return of this function determins if the tasks from struct se_cmd
  1850. * get added to the execution queue in transport_execute_tasks(),
  1851. * or are added to the delayed or ordered lists here.
  1852. */
  1853. static inline int transport_execute_task_attr(struct se_cmd *cmd)
  1854. {
  1855. if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  1856. return 1;
  1857. /*
  1858. * Check for the existence of HEAD_OF_QUEUE, and if true return 1
  1859. * to allow the passed struct se_cmd list of tasks to the front of the list.
  1860. */
  1861. if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  1862. pr_debug("Added HEAD_OF_QUEUE for CDB:"
  1863. " 0x%02x, se_ordered_id: %u\n",
  1864. cmd->t_task_cdb[0],
  1865. cmd->se_ordered_id);
  1866. return 1;
  1867. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  1868. atomic_inc(&cmd->se_dev->dev_ordered_sync);
  1869. smp_mb__after_atomic_inc();
  1870. pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
  1871. " list, se_ordered_id: %u\n",
  1872. cmd->t_task_cdb[0],
  1873. cmd->se_ordered_id);
  1874. /*
  1875. * Add ORDERED command to tail of execution queue if
  1876. * no other older commands exist that need to be
  1877. * completed first.
  1878. */
  1879. if (!atomic_read(&cmd->se_dev->simple_cmds))
  1880. return 1;
  1881. } else {
  1882. /*
  1883. * For SIMPLE and UNTAGGED Task Attribute commands
  1884. */
  1885. atomic_inc(&cmd->se_dev->simple_cmds);
  1886. smp_mb__after_atomic_inc();
  1887. }
  1888. /*
  1889. * Otherwise if one or more outstanding ORDERED task attribute exist,
  1890. * add the dormant task(s) built for the passed struct se_cmd to the
  1891. * execution queue and become in Active state for this struct se_device.
  1892. */
  1893. if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
  1894. /*
  1895. * Otherwise, add cmd w/ tasks to delayed cmd queue that
  1896. * will be drained upon completion of HEAD_OF_QUEUE task.
  1897. */
  1898. spin_lock(&cmd->se_dev->delayed_cmd_lock);
  1899. cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
  1900. list_add_tail(&cmd->se_delayed_node,
  1901. &cmd->se_dev->delayed_cmd_list);
  1902. spin_unlock(&cmd->se_dev->delayed_cmd_lock);
  1903. pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
  1904. " delayed CMD list, se_ordered_id: %u\n",
  1905. cmd->t_task_cdb[0], cmd->sam_task_attr,
  1906. cmd->se_ordered_id);
  1907. /*
  1908. * Return zero to let transport_execute_tasks() know
  1909. * not to add the delayed tasks to the execution list.
  1910. */
  1911. return 0;
  1912. }
  1913. /*
  1914. * Otherwise, no ORDERED task attributes exist..
  1915. */
  1916. return 1;
  1917. }
  1918. /*
  1919. * Called from fabric module context in transport_generic_new_cmd() and
  1920. * transport_generic_process_write()
  1921. */
  1922. static int transport_execute_tasks(struct se_cmd *cmd)
  1923. {
  1924. int add_tasks;
  1925. struct se_device *se_dev = cmd->se_dev;
  1926. /*
  1927. * Call transport_cmd_check_stop() to see if a fabric exception
  1928. * has occurred that prevents execution.
  1929. */
  1930. if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
  1931. /*
  1932. * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
  1933. * attribute for the tasks of the received struct se_cmd CDB
  1934. */
  1935. add_tasks = transport_execute_task_attr(cmd);
  1936. if (!add_tasks)
  1937. goto execute_tasks;
  1938. /*
  1939. * __transport_execute_tasks() -> __transport_add_tasks_from_cmd()
  1940. * adds associated se_tasks while holding dev->execute_task_lock
  1941. * before I/O dispath to avoid a double spinlock access.
  1942. */
  1943. __transport_execute_tasks(se_dev, cmd);
  1944. return 0;
  1945. }
  1946. execute_tasks:
  1947. __transport_execute_tasks(se_dev, NULL);
  1948. return 0;
  1949. }
  1950. /*
  1951. * Called to check struct se_device tcq depth window, and once open pull struct se_task
  1952. * from struct se_device->execute_task_list and
  1953. *
  1954. * Called from transport_processing_thread()
  1955. */
  1956. static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *new_cmd)
  1957. {
  1958. int error;
  1959. struct se_cmd *cmd = NULL;
  1960. struct se_task *task = NULL;
  1961. unsigned long flags;
  1962. check_depth:
  1963. spin_lock_irq(&dev->execute_task_lock);
  1964. if (new_cmd != NULL)
  1965. __transport_add_tasks_from_cmd(new_cmd);
  1966. if (list_empty(&dev->execute_task_list)) {
  1967. spin_unlock_irq(&dev->execute_task_lock);
  1968. return 0;
  1969. }
  1970. task = list_first_entry(&dev->execute_task_list,
  1971. struct se_task, t_execute_list);
  1972. __transport_remove_task_from_execute_queue(task, dev);
  1973. spin_unlock_irq(&dev->execute_task_lock);
  1974. cmd = task->task_se_cmd;
  1975. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1976. task->task_flags |= (TF_ACTIVE | TF_SENT);
  1977. atomic_inc(&cmd->t_task_cdbs_sent);
  1978. if (atomic_read(&cmd->t_task_cdbs_sent) ==
  1979. cmd->t_task_list_num)
  1980. cmd->transport_state |= CMD_T_SENT;
  1981. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1982. if (cmd->execute_task)
  1983. error = cmd->execute_task(task);
  1984. else
  1985. error = dev->transport->do_task(task);
  1986. if (error != 0) {
  1987. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1988. task->task_flags &= ~TF_ACTIVE;
  1989. cmd->transport_state &= ~CMD_T_SENT;
  1990. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1991. transport_stop_tasks_for_cmd(cmd);
  1992. transport_generic_request_failure(cmd);
  1993. }
  1994. new_cmd = NULL;
  1995. goto check_depth;
  1996. return 0;
  1997. }
  1998. static inline u32 transport_get_sectors_6(
  1999. unsigned char *cdb,
  2000. struct se_cmd *cmd,
  2001. int *ret)
  2002. {
  2003. struct se_device *dev = cmd->se_dev;
  2004. /*
  2005. * Assume TYPE_DISK for non struct se_device objects.
  2006. * Use 8-bit sector value.
  2007. */
  2008. if (!dev)
  2009. goto type_disk;
  2010. /*
  2011. * Use 24-bit allocation length for TYPE_TAPE.
  2012. */
  2013. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  2014. return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
  2015. /*
  2016. * Everything else assume TYPE_DISK Sector CDB location.
  2017. * Use 8-bit sector value. SBC-3 says:
  2018. *
  2019. * A TRANSFER LENGTH field set to zero specifies that 256
  2020. * logical blocks shall be written. Any other value
  2021. * specifies the number of logical blocks that shall be
  2022. * written.
  2023. */
  2024. type_disk:
  2025. return cdb[4] ? : 256;
  2026. }
  2027. static inline u32 transport_get_sectors_10(
  2028. unsigned char *cdb,
  2029. struct se_cmd *cmd,
  2030. int *ret)
  2031. {
  2032. struct se_device *dev = cmd->se_dev;
  2033. /*
  2034. * Assume TYPE_DISK for non struct se_device objects.
  2035. * Use 16-bit sector value.
  2036. */
  2037. if (!dev)
  2038. goto type_disk;
  2039. /*
  2040. * XXX_10 is not defined in SSC, throw an exception
  2041. */
  2042. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  2043. *ret = -EINVAL;
  2044. return 0;
  2045. }
  2046. /*
  2047. * Everything else assume TYPE_DISK Sector CDB location.
  2048. * Use 16-bit sector value.
  2049. */
  2050. type_disk:
  2051. return (u32)(cdb[7] << 8) + cdb[8];
  2052. }
  2053. static inline u32 transport_get_sectors_12(
  2054. unsigned char *cdb,
  2055. struct se_cmd *cmd,
  2056. int *ret)
  2057. {
  2058. struct se_device *dev = cmd->se_dev;
  2059. /*
  2060. * Assume TYPE_DISK for non struct se_device objects.
  2061. * Use 32-bit sector value.
  2062. */
  2063. if (!dev)
  2064. goto type_disk;
  2065. /*
  2066. * XXX_12 is not defined in SSC, throw an exception
  2067. */
  2068. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  2069. *ret = -EINVAL;
  2070. return 0;
  2071. }
  2072. /*
  2073. * Everything else assume TYPE_DISK Sector CDB location.
  2074. * Use 32-bit sector value.
  2075. */
  2076. type_disk:
  2077. return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
  2078. }
  2079. static inline u32 transport_get_sectors_16(
  2080. unsigned char *cdb,
  2081. struct se_cmd *cmd,
  2082. int *ret)
  2083. {
  2084. struct se_device *dev = cmd->se_dev;
  2085. /*
  2086. * Assume TYPE_DISK for non struct se_device objects.
  2087. * Use 32-bit sector value.
  2088. */
  2089. if (!dev)
  2090. goto type_disk;
  2091. /*
  2092. * Use 24-bit allocation length for TYPE_TAPE.
  2093. */
  2094. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  2095. return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
  2096. type_disk:
  2097. return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
  2098. (cdb[12] << 8) + cdb[13];
  2099. }
  2100. /*
  2101. * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
  2102. */
  2103. static inline u32 transport_get_sectors_32(
  2104. unsigned char *cdb,
  2105. struct se_cmd *cmd,
  2106. int *ret)
  2107. {
  2108. /*
  2109. * Assume TYPE_DISK for non struct se_device objects.
  2110. * Use 32-bit sector value.
  2111. */
  2112. return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
  2113. (cdb[30] << 8) + cdb[31];
  2114. }
  2115. static inline u32 transport_get_size(
  2116. u32 sectors,
  2117. unsigned char *cdb,
  2118. struct se_cmd *cmd)
  2119. {
  2120. struct se_device *dev = cmd->se_dev;
  2121. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  2122. if (cdb[1] & 1) { /* sectors */
  2123. return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
  2124. } else /* bytes */
  2125. return sectors;
  2126. }
  2127. #if 0
  2128. pr_debug("Returning block_size: %u, sectors: %u == %u for"
  2129. " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
  2130. dev->se_sub_dev->se_dev_attrib.block_size * sectors,
  2131. dev->transport->name);
  2132. #endif
  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 transport_generic_allocate_tasks() 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. #if 0
  2324. pr_debug("[%s]: ALUA TG Port not available,"
  2325. " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
  2326. cmd->se_tfo->get_fabric_name(), alua_ascq);
  2327. #endif
  2328. transport_set_sense_codes(cmd, 0x04, alua_ascq);
  2329. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2330. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
  2331. return -EINVAL;
  2332. }
  2333. goto out_invalid_cdb_field;
  2334. }
  2335. /*
  2336. * Check status for SPC-3 Persistent Reservations
  2337. */
  2338. if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
  2339. if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
  2340. cmd, cdb, pr_reg_type) != 0) {
  2341. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2342. cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
  2343. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  2344. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2345. return -EBUSY;
  2346. }
  2347. /*
  2348. * This means the CDB is allowed for the SCSI Initiator port
  2349. * when said port is *NOT* holding the legacy SPC-2 or
  2350. * SPC-3 Persistent Reservation.
  2351. */
  2352. }
  2353. /*
  2354. * If we operate in passthrough mode we skip most CDB emulation and
  2355. * instead hand the commands down to the physical SCSI device.
  2356. */
  2357. passthrough =
  2358. (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
  2359. switch (cdb[0]) {
  2360. case READ_6:
  2361. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2362. if (sector_ret)
  2363. goto out_unsupported_cdb;
  2364. size = transport_get_size(sectors, cdb, cmd);
  2365. cmd->t_task_lba = transport_lba_21(cdb);
  2366. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2367. break;
  2368. case READ_10:
  2369. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2370. if (sector_ret)
  2371. goto out_unsupported_cdb;
  2372. size = transport_get_size(sectors, cdb, cmd);
  2373. cmd->t_task_lba = transport_lba_32(cdb);
  2374. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2375. break;
  2376. case READ_12:
  2377. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2378. if (sector_ret)
  2379. goto out_unsupported_cdb;
  2380. size = transport_get_size(sectors, cdb, cmd);
  2381. cmd->t_task_lba = transport_lba_32(cdb);
  2382. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2383. break;
  2384. case READ_16:
  2385. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2386. if (sector_ret)
  2387. goto out_unsupported_cdb;
  2388. size = transport_get_size(sectors, cdb, cmd);
  2389. cmd->t_task_lba = transport_lba_64(cdb);
  2390. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2391. break;
  2392. case WRITE_6:
  2393. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2394. if (sector_ret)
  2395. goto out_unsupported_cdb;
  2396. size = transport_get_size(sectors, cdb, cmd);
  2397. cmd->t_task_lba = transport_lba_21(cdb);
  2398. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2399. break;
  2400. case WRITE_10:
  2401. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2402. if (sector_ret)
  2403. goto out_unsupported_cdb;
  2404. size = transport_get_size(sectors, cdb, cmd);
  2405. cmd->t_task_lba = transport_lba_32(cdb);
  2406. if (cdb[1] & 0x8)
  2407. cmd->se_cmd_flags |= SCF_FUA;
  2408. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2409. break;
  2410. case WRITE_12:
  2411. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2412. if (sector_ret)
  2413. goto out_unsupported_cdb;
  2414. size = transport_get_size(sectors, cdb, cmd);
  2415. cmd->t_task_lba = transport_lba_32(cdb);
  2416. if (cdb[1] & 0x8)
  2417. cmd->se_cmd_flags |= SCF_FUA;
  2418. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2419. break;
  2420. case WRITE_16:
  2421. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2422. if (sector_ret)
  2423. goto out_unsupported_cdb;
  2424. size = transport_get_size(sectors, cdb, cmd);
  2425. cmd->t_task_lba = transport_lba_64(cdb);
  2426. if (cdb[1] & 0x8)
  2427. cmd->se_cmd_flags |= SCF_FUA;
  2428. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2429. break;
  2430. case XDWRITEREAD_10:
  2431. if ((cmd->data_direction != DMA_TO_DEVICE) ||
  2432. !(cmd->se_cmd_flags & SCF_BIDI))
  2433. goto out_invalid_cdb_field;
  2434. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2435. if (sector_ret)
  2436. goto out_unsupported_cdb;
  2437. size = transport_get_size(sectors, cdb, cmd);
  2438. cmd->t_task_lba = transport_lba_32(cdb);
  2439. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2440. /*
  2441. * Do now allow BIDI commands for passthrough mode.
  2442. */
  2443. if (passthrough)
  2444. goto out_unsupported_cdb;
  2445. /*
  2446. * Setup BIDI XOR callback to be run after I/O completion.
  2447. */
  2448. cmd->transport_complete_callback = &transport_xor_callback;
  2449. if (cdb[1] & 0x8)
  2450. cmd->se_cmd_flags |= SCF_FUA;
  2451. break;
  2452. case VARIABLE_LENGTH_CMD:
  2453. service_action = get_unaligned_be16(&cdb[8]);
  2454. switch (service_action) {
  2455. case XDWRITEREAD_32:
  2456. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2457. if (sector_ret)
  2458. goto out_unsupported_cdb;
  2459. size = transport_get_size(sectors, cdb, cmd);
  2460. /*
  2461. * Use WRITE_32 and READ_32 opcodes for the emulated
  2462. * XDWRITE_READ_32 logic.
  2463. */
  2464. cmd->t_task_lba = transport_lba_64_ext(cdb);
  2465. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2466. /*
  2467. * Do now allow BIDI commands for passthrough mode.
  2468. */
  2469. if (passthrough)
  2470. goto out_unsupported_cdb;
  2471. /*
  2472. * Setup BIDI XOR callback to be run during after I/O
  2473. * completion.
  2474. */
  2475. cmd->transport_complete_callback = &transport_xor_callback;
  2476. if (cdb[1] & 0x8)
  2477. cmd->se_cmd_flags |= SCF_FUA;
  2478. break;
  2479. case WRITE_SAME_32:
  2480. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2481. if (sector_ret)
  2482. goto out_unsupported_cdb;
  2483. if (sectors)
  2484. size = transport_get_size(1, cdb, cmd);
  2485. else {
  2486. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
  2487. " supported\n");
  2488. goto out_invalid_cdb_field;
  2489. }
  2490. cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
  2491. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2492. if (target_check_write_same_discard(&cdb[10], dev) < 0)
  2493. goto out_unsupported_cdb;
  2494. if (!passthrough)
  2495. cmd->execute_task = target_emulate_write_same;
  2496. break;
  2497. default:
  2498. pr_err("VARIABLE_LENGTH_CMD service action"
  2499. " 0x%04x not supported\n", service_action);
  2500. goto out_unsupported_cdb;
  2501. }
  2502. break;
  2503. case MAINTENANCE_IN:
  2504. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2505. /* MAINTENANCE_IN from SCC-2 */
  2506. /*
  2507. * Check for emulated MI_REPORT_TARGET_PGS.
  2508. */
  2509. if (cdb[1] == MI_REPORT_TARGET_PGS &&
  2510. su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
  2511. cmd->execute_task =
  2512. target_emulate_report_target_port_groups;
  2513. }
  2514. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2515. (cdb[8] << 8) | cdb[9];
  2516. } else {
  2517. /* GPCMD_SEND_KEY from multi media commands */
  2518. size = (cdb[8] << 8) + cdb[9];
  2519. }
  2520. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2521. break;
  2522. case MODE_SELECT:
  2523. size = cdb[4];
  2524. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2525. break;
  2526. case MODE_SELECT_10:
  2527. size = (cdb[7] << 8) + cdb[8];
  2528. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2529. break;
  2530. case MODE_SENSE:
  2531. size = cdb[4];
  2532. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2533. if (!passthrough)
  2534. cmd->execute_task = target_emulate_modesense;
  2535. break;
  2536. case MODE_SENSE_10:
  2537. size = (cdb[7] << 8) + cdb[8];
  2538. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2539. if (!passthrough)
  2540. cmd->execute_task = target_emulate_modesense;
  2541. break;
  2542. case GPCMD_READ_BUFFER_CAPACITY:
  2543. case GPCMD_SEND_OPC:
  2544. case LOG_SELECT:
  2545. case LOG_SENSE:
  2546. size = (cdb[7] << 8) + cdb[8];
  2547. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2548. break;
  2549. case READ_BLOCK_LIMITS:
  2550. size = READ_BLOCK_LEN;
  2551. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2552. break;
  2553. case GPCMD_GET_CONFIGURATION:
  2554. case GPCMD_READ_FORMAT_CAPACITIES:
  2555. case GPCMD_READ_DISC_INFO:
  2556. case GPCMD_READ_TRACK_RZONE_INFO:
  2557. size = (cdb[7] << 8) + cdb[8];
  2558. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2559. break;
  2560. case PERSISTENT_RESERVE_IN:
  2561. if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
  2562. cmd->execute_task = target_scsi3_emulate_pr_in;
  2563. size = (cdb[7] << 8) + cdb[8];
  2564. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2565. break;
  2566. case PERSISTENT_RESERVE_OUT:
  2567. if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
  2568. cmd->execute_task = target_scsi3_emulate_pr_out;
  2569. size = (cdb[7] << 8) + cdb[8];
  2570. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2571. break;
  2572. case GPCMD_MECHANISM_STATUS:
  2573. case GPCMD_READ_DVD_STRUCTURE:
  2574. size = (cdb[8] << 8) + cdb[9];
  2575. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2576. break;
  2577. case READ_POSITION:
  2578. size = READ_POSITION_LEN;
  2579. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2580. break;
  2581. case MAINTENANCE_OUT:
  2582. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2583. /* MAINTENANCE_OUT from SCC-2
  2584. *
  2585. * Check for emulated MO_SET_TARGET_PGS.
  2586. */
  2587. if (cdb[1] == MO_SET_TARGET_PGS &&
  2588. su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
  2589. cmd->execute_task =
  2590. target_emulate_set_target_port_groups;
  2591. }
  2592. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2593. (cdb[8] << 8) | cdb[9];
  2594. } else {
  2595. /* GPCMD_REPORT_KEY from multi media commands */
  2596. size = (cdb[8] << 8) + cdb[9];
  2597. }
  2598. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2599. break;
  2600. case INQUIRY:
  2601. size = (cdb[3] << 8) + cdb[4];
  2602. /*
  2603. * Do implict HEAD_OF_QUEUE processing for INQUIRY.
  2604. * See spc4r17 section 5.3
  2605. */
  2606. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2607. cmd->sam_task_attr = MSG_HEAD_TAG;
  2608. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2609. if (!passthrough)
  2610. cmd->execute_task = target_emulate_inquiry;
  2611. break;
  2612. case READ_BUFFER:
  2613. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2614. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2615. break;
  2616. case READ_CAPACITY:
  2617. size = READ_CAP_LEN;
  2618. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2619. if (!passthrough)
  2620. cmd->execute_task = target_emulate_readcapacity;
  2621. break;
  2622. case READ_MEDIA_SERIAL_NUMBER:
  2623. case SECURITY_PROTOCOL_IN:
  2624. case SECURITY_PROTOCOL_OUT:
  2625. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  2626. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2627. break;
  2628. case SERVICE_ACTION_IN:
  2629. switch (cmd->t_task_cdb[1] & 0x1f) {
  2630. case SAI_READ_CAPACITY_16:
  2631. if (!passthrough)
  2632. cmd->execute_task =
  2633. target_emulate_readcapacity_16;
  2634. break;
  2635. default:
  2636. if (passthrough)
  2637. break;
  2638. pr_err("Unsupported SA: 0x%02x\n",
  2639. cmd->t_task_cdb[1] & 0x1f);
  2640. goto out_invalid_cdb_field;
  2641. }
  2642. /*FALLTHROUGH*/
  2643. case ACCESS_CONTROL_IN:
  2644. case ACCESS_CONTROL_OUT:
  2645. case EXTENDED_COPY:
  2646. case READ_ATTRIBUTE:
  2647. case RECEIVE_COPY_RESULTS:
  2648. case WRITE_ATTRIBUTE:
  2649. size = (cdb[10] << 24) | (cdb[11] << 16) |
  2650. (cdb[12] << 8) | cdb[13];
  2651. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2652. break;
  2653. case RECEIVE_DIAGNOSTIC:
  2654. case SEND_DIAGNOSTIC:
  2655. size = (cdb[3] << 8) | cdb[4];
  2656. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2657. break;
  2658. /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
  2659. #if 0
  2660. case GPCMD_READ_CD:
  2661. sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2662. size = (2336 * sectors);
  2663. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2664. break;
  2665. #endif
  2666. case READ_TOC:
  2667. size = cdb[8];
  2668. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2669. break;
  2670. case REQUEST_SENSE:
  2671. size = cdb[4];
  2672. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2673. if (!passthrough)
  2674. cmd->execute_task = target_emulate_request_sense;
  2675. break;
  2676. case READ_ELEMENT_STATUS:
  2677. size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
  2678. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2679. break;
  2680. case WRITE_BUFFER:
  2681. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2682. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2683. break;
  2684. case RESERVE:
  2685. case RESERVE_10:
  2686. /*
  2687. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  2688. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  2689. */
  2690. if (cdb[0] == RESERVE_10)
  2691. size = (cdb[7] << 8) | cdb[8];
  2692. else
  2693. size = cmd->data_length;
  2694. /*
  2695. * Setup the legacy emulated handler for SPC-2 and
  2696. * >= SPC-3 compatible reservation handling (CRH=1)
  2697. * Otherwise, we assume the underlying SCSI logic is
  2698. * is running in SPC_PASSTHROUGH, and wants reservations
  2699. * emulation disabled.
  2700. */
  2701. if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
  2702. cmd->execute_task = target_scsi2_reservation_reserve;
  2703. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2704. break;
  2705. case RELEASE:
  2706. case RELEASE_10:
  2707. /*
  2708. * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
  2709. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  2710. */
  2711. if (cdb[0] == RELEASE_10)
  2712. size = (cdb[7] << 8) | cdb[8];
  2713. else
  2714. size = cmd->data_length;
  2715. if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
  2716. cmd->execute_task = target_scsi2_reservation_release;
  2717. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2718. break;
  2719. case SYNCHRONIZE_CACHE:
  2720. case SYNCHRONIZE_CACHE_16:
  2721. /*
  2722. * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
  2723. */
  2724. if (cdb[0] == SYNCHRONIZE_CACHE) {
  2725. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2726. cmd->t_task_lba = transport_lba_32(cdb);
  2727. } else {
  2728. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2729. cmd->t_task_lba = transport_lba_64(cdb);
  2730. }
  2731. if (sector_ret)
  2732. goto out_unsupported_cdb;
  2733. size = transport_get_size(sectors, cdb, cmd);
  2734. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2735. if (passthrough)
  2736. break;
  2737. /*
  2738. * Check to ensure that LBA + Range does not exceed past end of
  2739. * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
  2740. */
  2741. if ((cmd->t_task_lba != 0) || (sectors != 0)) {
  2742. if (transport_cmd_get_valid_sectors(cmd) < 0)
  2743. goto out_invalid_cdb_field;
  2744. }
  2745. cmd->execute_task = target_emulate_synchronize_cache;
  2746. break;
  2747. case UNMAP:
  2748. size = get_unaligned_be16(&cdb[7]);
  2749. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2750. if (!passthrough)
  2751. cmd->execute_task = target_emulate_unmap;
  2752. break;
  2753. case WRITE_SAME_16:
  2754. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2755. if (sector_ret)
  2756. goto out_unsupported_cdb;
  2757. if (sectors)
  2758. size = transport_get_size(1, cdb, cmd);
  2759. else {
  2760. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
  2761. goto out_invalid_cdb_field;
  2762. }
  2763. cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
  2764. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2765. if (target_check_write_same_discard(&cdb[1], dev) < 0)
  2766. goto out_unsupported_cdb;
  2767. if (!passthrough)
  2768. cmd->execute_task = target_emulate_write_same;
  2769. break;
  2770. case WRITE_SAME:
  2771. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2772. if (sector_ret)
  2773. goto out_unsupported_cdb;
  2774. if (sectors)
  2775. size = transport_get_size(1, cdb, cmd);
  2776. else {
  2777. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
  2778. goto out_invalid_cdb_field;
  2779. }
  2780. cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
  2781. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2782. /*
  2783. * Follow sbcr26 with WRITE_SAME (10) and check for the existence
  2784. * of byte 1 bit 3 UNMAP instead of original reserved field
  2785. */
  2786. if (target_check_write_same_discard(&cdb[1], dev) < 0)
  2787. goto out_unsupported_cdb;
  2788. if (!passthrough)
  2789. cmd->execute_task = target_emulate_write_same;
  2790. break;
  2791. case ALLOW_MEDIUM_REMOVAL:
  2792. case ERASE:
  2793. case REZERO_UNIT:
  2794. case SEEK_10:
  2795. case SPACE:
  2796. case START_STOP:
  2797. case TEST_UNIT_READY:
  2798. case VERIFY:
  2799. case WRITE_FILEMARKS:
  2800. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2801. if (!passthrough)
  2802. cmd->execute_task = target_emulate_noop;
  2803. break;
  2804. case GPCMD_CLOSE_TRACK:
  2805. case INITIALIZE_ELEMENT_STATUS:
  2806. case GPCMD_LOAD_UNLOAD:
  2807. case GPCMD_SET_SPEED:
  2808. case MOVE_MEDIUM:
  2809. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2810. break;
  2811. case REPORT_LUNS:
  2812. cmd->execute_task = target_report_luns;
  2813. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  2814. /*
  2815. * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
  2816. * See spc4r17 section 5.3
  2817. */
  2818. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2819. cmd->sam_task_attr = MSG_HEAD_TAG;
  2820. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2821. break;
  2822. default:
  2823. pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
  2824. " 0x%02x, sending CHECK_CONDITION.\n",
  2825. cmd->se_tfo->get_fabric_name(), cdb[0]);
  2826. goto out_unsupported_cdb;
  2827. }
  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. printk_ratelimited(KERN_ERR "SCSI OP %02xh with too big sectors %u\n",
  2862. cdb[0], sectors);
  2863. goto out_invalid_cdb_field;
  2864. }
  2865. /* reject any command that we don't have a handler for */
  2866. if (!(passthrough || cmd->execute_task ||
  2867. (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
  2868. goto out_unsupported_cdb;
  2869. transport_set_supported_SAM_opcode(cmd);
  2870. return ret;
  2871. out_unsupported_cdb:
  2872. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2873. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  2874. return -EINVAL;
  2875. out_invalid_cdb_field:
  2876. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2877. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  2878. return -EINVAL;
  2879. }
  2880. /*
  2881. * Called from I/O completion to determine which dormant/delayed
  2882. * and ordered cmds need to have their tasks added to the execution queue.
  2883. */
  2884. static void transport_complete_task_attr(struct se_cmd *cmd)
  2885. {
  2886. struct se_device *dev = cmd->se_dev;
  2887. struct se_cmd *cmd_p, *cmd_tmp;
  2888. int new_active_tasks = 0;
  2889. if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
  2890. atomic_dec(&dev->simple_cmds);
  2891. smp_mb__after_atomic_dec();
  2892. dev->dev_cur_ordered_id++;
  2893. pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
  2894. " SIMPLE: %u\n", dev->dev_cur_ordered_id,
  2895. cmd->se_ordered_id);
  2896. } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  2897. dev->dev_cur_ordered_id++;
  2898. pr_debug("Incremented dev_cur_ordered_id: %u for"
  2899. " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
  2900. cmd->se_ordered_id);
  2901. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  2902. atomic_dec(&dev->dev_ordered_sync);
  2903. smp_mb__after_atomic_dec();
  2904. dev->dev_cur_ordered_id++;
  2905. pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
  2906. " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
  2907. }
  2908. /*
  2909. * Process all commands up to the last received
  2910. * ORDERED task attribute which requires another blocking
  2911. * boundary
  2912. */
  2913. spin_lock(&dev->delayed_cmd_lock);
  2914. list_for_each_entry_safe(cmd_p, cmd_tmp,
  2915. &dev->delayed_cmd_list, se_delayed_node) {
  2916. list_del(&cmd_p->se_delayed_node);
  2917. spin_unlock(&dev->delayed_cmd_lock);
  2918. pr_debug("Calling add_tasks() for"
  2919. " cmd_p: 0x%02x Task Attr: 0x%02x"
  2920. " Dormant -> Active, se_ordered_id: %u\n",
  2921. cmd_p->t_task_cdb[0],
  2922. cmd_p->sam_task_attr, cmd_p->se_ordered_id);
  2923. transport_add_tasks_from_cmd(cmd_p);
  2924. new_active_tasks++;
  2925. spin_lock(&dev->delayed_cmd_lock);
  2926. if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
  2927. break;
  2928. }
  2929. spin_unlock(&dev->delayed_cmd_lock);
  2930. /*
  2931. * If new tasks have become active, wake up the transport thread
  2932. * to do the processing of the Active tasks.
  2933. */
  2934. if (new_active_tasks != 0)
  2935. wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
  2936. }
  2937. static void transport_complete_qf(struct se_cmd *cmd)
  2938. {
  2939. int ret = 0;
  2940. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2941. transport_complete_task_attr(cmd);
  2942. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  2943. ret = cmd->se_tfo->queue_status(cmd);
  2944. if (ret)
  2945. goto out;
  2946. }
  2947. switch (cmd->data_direction) {
  2948. case DMA_FROM_DEVICE:
  2949. ret = cmd->se_tfo->queue_data_in(cmd);
  2950. break;
  2951. case DMA_TO_DEVICE:
  2952. if (cmd->t_bidi_data_sg) {
  2953. ret = cmd->se_tfo->queue_data_in(cmd);
  2954. if (ret < 0)
  2955. break;
  2956. }
  2957. /* Fall through for DMA_TO_DEVICE */
  2958. case DMA_NONE:
  2959. ret = cmd->se_tfo->queue_status(cmd);
  2960. break;
  2961. default:
  2962. break;
  2963. }
  2964. out:
  2965. if (ret < 0) {
  2966. transport_handle_queue_full(cmd, cmd->se_dev);
  2967. return;
  2968. }
  2969. transport_lun_remove_cmd(cmd);
  2970. transport_cmd_check_stop_to_fabric(cmd);
  2971. }
  2972. static void transport_handle_queue_full(
  2973. struct se_cmd *cmd,
  2974. struct se_device *dev)
  2975. {
  2976. spin_lock_irq(&dev->qf_cmd_lock);
  2977. list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
  2978. atomic_inc(&dev->dev_qf_count);
  2979. smp_mb__after_atomic_inc();
  2980. spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
  2981. schedule_work(&cmd->se_dev->qf_work_queue);
  2982. }
  2983. static void target_complete_ok_work(struct work_struct *work)
  2984. {
  2985. struct se_cmd *cmd = container_of(work, struct se_cmd, work);
  2986. int reason = 0, ret;
  2987. /*
  2988. * Check if we need to move delayed/dormant tasks from cmds on the
  2989. * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
  2990. * Attribute.
  2991. */
  2992. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2993. transport_complete_task_attr(cmd);
  2994. /*
  2995. * Check to schedule QUEUE_FULL work, or execute an existing
  2996. * cmd->transport_qf_callback()
  2997. */
  2998. if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
  2999. schedule_work(&cmd->se_dev->qf_work_queue);
  3000. /*
  3001. * Check if we need to retrieve a sense buffer from
  3002. * the struct se_cmd in question.
  3003. */
  3004. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  3005. if (transport_get_sense_data(cmd) < 0)
  3006. reason = TCM_NON_EXISTENT_LUN;
  3007. /*
  3008. * Only set when an struct se_task->task_scsi_status returned
  3009. * a non GOOD status.
  3010. */
  3011. if (cmd->scsi_status) {
  3012. ret = transport_send_check_condition_and_sense(
  3013. cmd, reason, 1);
  3014. if (ret == -EAGAIN || ret == -ENOMEM)
  3015. goto queue_full;
  3016. transport_lun_remove_cmd(cmd);
  3017. transport_cmd_check_stop_to_fabric(cmd);
  3018. return;
  3019. }
  3020. }
  3021. /*
  3022. * Check for a callback, used by amongst other things
  3023. * XDWRITE_READ_10 emulation.
  3024. */
  3025. if (cmd->transport_complete_callback)
  3026. cmd->transport_complete_callback(cmd);
  3027. switch (cmd->data_direction) {
  3028. case DMA_FROM_DEVICE:
  3029. spin_lock(&cmd->se_lun->lun_sep_lock);
  3030. if (cmd->se_lun->lun_sep) {
  3031. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  3032. cmd->data_length;
  3033. }
  3034. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3035. ret = cmd->se_tfo->queue_data_in(cmd);
  3036. if (ret == -EAGAIN || ret == -ENOMEM)
  3037. goto queue_full;
  3038. break;
  3039. case DMA_TO_DEVICE:
  3040. spin_lock(&cmd->se_lun->lun_sep_lock);
  3041. if (cmd->se_lun->lun_sep) {
  3042. cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
  3043. cmd->data_length;
  3044. }
  3045. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3046. /*
  3047. * Check if we need to send READ payload for BIDI-COMMAND
  3048. */
  3049. if (cmd->t_bidi_data_sg) {
  3050. spin_lock(&cmd->se_lun->lun_sep_lock);
  3051. if (cmd->se_lun->lun_sep) {
  3052. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  3053. cmd->data_length;
  3054. }
  3055. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3056. ret = cmd->se_tfo->queue_data_in(cmd);
  3057. if (ret == -EAGAIN || ret == -ENOMEM)
  3058. goto queue_full;
  3059. break;
  3060. }
  3061. /* Fall through for DMA_TO_DEVICE */
  3062. case DMA_NONE:
  3063. ret = cmd->se_tfo->queue_status(cmd);
  3064. if (ret == -EAGAIN || ret == -ENOMEM)
  3065. goto queue_full;
  3066. break;
  3067. default:
  3068. break;
  3069. }
  3070. transport_lun_remove_cmd(cmd);
  3071. transport_cmd_check_stop_to_fabric(cmd);
  3072. return;
  3073. queue_full:
  3074. pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
  3075. " data_direction: %d\n", cmd, cmd->data_direction);
  3076. cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
  3077. transport_handle_queue_full(cmd, cmd->se_dev);
  3078. }
  3079. static void transport_free_dev_tasks(struct se_cmd *cmd)
  3080. {
  3081. struct se_task *task, *task_tmp;
  3082. unsigned long flags;
  3083. LIST_HEAD(dispose_list);
  3084. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3085. list_for_each_entry_safe(task, task_tmp,
  3086. &cmd->t_task_list, t_list) {
  3087. if (!(task->task_flags & TF_ACTIVE))
  3088. list_move_tail(&task->t_list, &dispose_list);
  3089. }
  3090. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3091. while (!list_empty(&dispose_list)) {
  3092. task = list_first_entry(&dispose_list, struct se_task, t_list);
  3093. if (task->task_sg != cmd->t_data_sg &&
  3094. task->task_sg != cmd->t_bidi_data_sg)
  3095. kfree(task->task_sg);
  3096. list_del(&task->t_list);
  3097. cmd->se_dev->transport->free_task(task);
  3098. }
  3099. }
  3100. static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
  3101. {
  3102. struct scatterlist *sg;
  3103. int count;
  3104. for_each_sg(sgl, sg, nents, count)
  3105. __free_page(sg_page(sg));
  3106. kfree(sgl);
  3107. }
  3108. static inline void transport_free_pages(struct se_cmd *cmd)
  3109. {
  3110. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
  3111. return;
  3112. transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
  3113. cmd->t_data_sg = NULL;
  3114. cmd->t_data_nents = 0;
  3115. transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
  3116. cmd->t_bidi_data_sg = NULL;
  3117. cmd->t_bidi_data_nents = 0;
  3118. }
  3119. /**
  3120. * transport_release_cmd - free a command
  3121. * @cmd: command to free
  3122. *
  3123. * This routine unconditionally frees a command, and reference counting
  3124. * or list removal must be done in the caller.
  3125. */
  3126. static void transport_release_cmd(struct se_cmd *cmd)
  3127. {
  3128. BUG_ON(!cmd->se_tfo);
  3129. if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
  3130. core_tmr_release_req(cmd->se_tmr_req);
  3131. if (cmd->t_task_cdb != cmd->__t_task_cdb)
  3132. kfree(cmd->t_task_cdb);
  3133. /*
  3134. * If this cmd has been setup with target_get_sess_cmd(), drop
  3135. * the kref and call ->release_cmd() in kref callback.
  3136. */
  3137. if (cmd->check_release != 0) {
  3138. target_put_sess_cmd(cmd->se_sess, cmd);
  3139. return;
  3140. }
  3141. cmd->se_tfo->release_cmd(cmd);
  3142. }
  3143. /**
  3144. * transport_put_cmd - release a reference to a command
  3145. * @cmd: command to release
  3146. *
  3147. * This routine releases our reference to the command and frees it if possible.
  3148. */
  3149. static void transport_put_cmd(struct se_cmd *cmd)
  3150. {
  3151. unsigned long flags;
  3152. int free_tasks = 0;
  3153. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3154. if (atomic_read(&cmd->t_fe_count)) {
  3155. if (!atomic_dec_and_test(&cmd->t_fe_count))
  3156. goto out_busy;
  3157. }
  3158. if (atomic_read(&cmd->t_se_count)) {
  3159. if (!atomic_dec_and_test(&cmd->t_se_count))
  3160. goto out_busy;
  3161. }
  3162. if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
  3163. cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
  3164. transport_all_task_dev_remove_state(cmd);
  3165. free_tasks = 1;
  3166. }
  3167. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3168. if (free_tasks != 0)
  3169. transport_free_dev_tasks(cmd);
  3170. transport_free_pages(cmd);
  3171. transport_release_cmd(cmd);
  3172. return;
  3173. out_busy:
  3174. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3175. }
  3176. /*
  3177. * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
  3178. * allocating in the core.
  3179. * @cmd: Associated se_cmd descriptor
  3180. * @mem: SGL style memory for TCM WRITE / READ
  3181. * @sg_mem_num: Number of SGL elements
  3182. * @mem_bidi_in: SGL style memory for TCM BIDI READ
  3183. * @sg_mem_bidi_num: Number of BIDI READ SGL elements
  3184. *
  3185. * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
  3186. * of parameters.
  3187. */
  3188. int transport_generic_map_mem_to_cmd(
  3189. struct se_cmd *cmd,
  3190. struct scatterlist *sgl,
  3191. u32 sgl_count,
  3192. struct scatterlist *sgl_bidi,
  3193. u32 sgl_bidi_count)
  3194. {
  3195. if (!sgl || !sgl_count)
  3196. return 0;
  3197. if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
  3198. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
  3199. /*
  3200. * Reject SCSI data overflow with map_mem_to_cmd() as incoming
  3201. * scatterlists already have been set to follow what the fabric
  3202. * passes for the original expected data transfer length.
  3203. */
  3204. if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
  3205. pr_warn("Rejecting SCSI DATA overflow for fabric using"
  3206. " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
  3207. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3208. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3209. return -EINVAL;
  3210. }
  3211. cmd->t_data_sg = sgl;
  3212. cmd->t_data_nents = sgl_count;
  3213. if (sgl_bidi && sgl_bidi_count) {
  3214. cmd->t_bidi_data_sg = sgl_bidi;
  3215. cmd->t_bidi_data_nents = sgl_bidi_count;
  3216. }
  3217. cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  3218. }
  3219. return 0;
  3220. }
  3221. EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
  3222. void *transport_kmap_data_sg(struct se_cmd *cmd)
  3223. {
  3224. struct scatterlist *sg = cmd->t_data_sg;
  3225. struct page **pages;
  3226. int i;
  3227. BUG_ON(!sg);
  3228. /*
  3229. * We need to take into account a possible offset here for fabrics like
  3230. * tcm_loop who may be using a contig buffer from the SCSI midlayer for
  3231. * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
  3232. */
  3233. if (!cmd->t_data_nents)
  3234. return NULL;
  3235. else if (cmd->t_data_nents == 1)
  3236. return kmap(sg_page(sg)) + sg->offset;
  3237. /* >1 page. use vmap */
  3238. pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
  3239. if (!pages)
  3240. return NULL;
  3241. /* convert sg[] to pages[] */
  3242. for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
  3243. pages[i] = sg_page(sg);
  3244. }
  3245. cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
  3246. kfree(pages);
  3247. if (!cmd->t_data_vmap)
  3248. return NULL;
  3249. return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
  3250. }
  3251. EXPORT_SYMBOL(transport_kmap_data_sg);
  3252. void transport_kunmap_data_sg(struct se_cmd *cmd)
  3253. {
  3254. if (!cmd->t_data_nents) {
  3255. return;
  3256. } else if (cmd->t_data_nents == 1) {
  3257. kunmap(sg_page(cmd->t_data_sg));
  3258. return;
  3259. }
  3260. vunmap(cmd->t_data_vmap);
  3261. cmd->t_data_vmap = NULL;
  3262. }
  3263. EXPORT_SYMBOL(transport_kunmap_data_sg);
  3264. static int
  3265. transport_generic_get_mem(struct se_cmd *cmd)
  3266. {
  3267. u32 length = cmd->data_length;
  3268. unsigned int nents;
  3269. struct page *page;
  3270. gfp_t zero_flag;
  3271. int i = 0;
  3272. nents = DIV_ROUND_UP(length, PAGE_SIZE);
  3273. cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
  3274. if (!cmd->t_data_sg)
  3275. return -ENOMEM;
  3276. cmd->t_data_nents = nents;
  3277. sg_init_table(cmd->t_data_sg, nents);
  3278. zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB ? 0 : __GFP_ZERO;
  3279. while (length) {
  3280. u32 page_len = min_t(u32, length, PAGE_SIZE);
  3281. page = alloc_page(GFP_KERNEL | zero_flag);
  3282. if (!page)
  3283. goto out;
  3284. sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
  3285. length -= page_len;
  3286. i++;
  3287. }
  3288. return 0;
  3289. out:
  3290. while (i >= 0) {
  3291. __free_page(sg_page(&cmd->t_data_sg[i]));
  3292. i--;
  3293. }
  3294. kfree(cmd->t_data_sg);
  3295. cmd->t_data_sg = NULL;
  3296. return -ENOMEM;
  3297. }
  3298. /* Reduce sectors if they are too long for the device */
  3299. static inline sector_t transport_limit_task_sectors(
  3300. struct se_device *dev,
  3301. unsigned long long lba,
  3302. sector_t sectors)
  3303. {
  3304. sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
  3305. if (dev->transport->get_device_type(dev) == TYPE_DISK)
  3306. if ((lba + sectors) > transport_dev_end_lba(dev))
  3307. sectors = ((transport_dev_end_lba(dev) - lba) + 1);
  3308. return sectors;
  3309. }
  3310. /*
  3311. * This function can be used by HW target mode drivers to create a linked
  3312. * scatterlist from all contiguously allocated struct se_task->task_sg[].
  3313. * This is intended to be called during the completion path by TCM Core
  3314. * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
  3315. */
  3316. void transport_do_task_sg_chain(struct se_cmd *cmd)
  3317. {
  3318. struct scatterlist *sg_first = NULL;
  3319. struct scatterlist *sg_prev = NULL;
  3320. int sg_prev_nents = 0;
  3321. struct scatterlist *sg;
  3322. struct se_task *task;
  3323. u32 chained_nents = 0;
  3324. int i;
  3325. BUG_ON(!cmd->se_tfo->task_sg_chaining);
  3326. /*
  3327. * Walk the struct se_task list and setup scatterlist chains
  3328. * for each contiguously allocated struct se_task->task_sg[].
  3329. */
  3330. list_for_each_entry(task, &cmd->t_task_list, t_list) {
  3331. if (!task->task_sg)
  3332. continue;
  3333. if (!sg_first) {
  3334. sg_first = task->task_sg;
  3335. chained_nents = task->task_sg_nents;
  3336. } else {
  3337. sg_chain(sg_prev, sg_prev_nents, task->task_sg);
  3338. chained_nents += task->task_sg_nents;
  3339. }
  3340. /*
  3341. * For the padded tasks, use the extra SGL vector allocated
  3342. * in transport_allocate_data_tasks() for the sg_prev_nents
  3343. * offset into sg_chain() above.
  3344. *
  3345. * We do not need the padding for the last task (or a single
  3346. * task), but in that case we will never use the sg_prev_nents
  3347. * value below which would be incorrect.
  3348. */
  3349. sg_prev_nents = (task->task_sg_nents + 1);
  3350. sg_prev = task->task_sg;
  3351. }
  3352. /*
  3353. * Setup the starting pointer and total t_tasks_sg_linked_no including
  3354. * padding SGs for linking and to mark the end.
  3355. */
  3356. cmd->t_tasks_sg_chained = sg_first;
  3357. cmd->t_tasks_sg_chained_no = chained_nents;
  3358. pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
  3359. " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
  3360. cmd->t_tasks_sg_chained_no);
  3361. for_each_sg(cmd->t_tasks_sg_chained, sg,
  3362. cmd->t_tasks_sg_chained_no, i) {
  3363. pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
  3364. i, sg, sg_page(sg), sg->length, sg->offset);
  3365. if (sg_is_chain(sg))
  3366. pr_debug("SG: %p sg_is_chain=1\n", sg);
  3367. if (sg_is_last(sg))
  3368. pr_debug("SG: %p sg_is_last=1\n", sg);
  3369. }
  3370. }
  3371. EXPORT_SYMBOL(transport_do_task_sg_chain);
  3372. /*
  3373. * Break up cmd into chunks transport can handle
  3374. */
  3375. static int
  3376. transport_allocate_data_tasks(struct se_cmd *cmd,
  3377. enum dma_data_direction data_direction,
  3378. struct scatterlist *cmd_sg, unsigned int sgl_nents)
  3379. {
  3380. struct se_device *dev = cmd->se_dev;
  3381. int task_count, i;
  3382. unsigned long long lba;
  3383. sector_t sectors, dev_max_sectors;
  3384. u32 sector_size;
  3385. if (transport_cmd_get_valid_sectors(cmd) < 0)
  3386. return -EINVAL;
  3387. dev_max_sectors = dev->se_sub_dev->se_dev_attrib.max_sectors;
  3388. sector_size = dev->se_sub_dev->se_dev_attrib.block_size;
  3389. WARN_ON(cmd->data_length % sector_size);
  3390. lba = cmd->t_task_lba;
  3391. sectors = DIV_ROUND_UP(cmd->data_length, sector_size);
  3392. task_count = DIV_ROUND_UP_SECTOR_T(sectors, dev_max_sectors);
  3393. /*
  3394. * If we need just a single task reuse the SG list in the command
  3395. * and avoid a lot of work.
  3396. */
  3397. if (task_count == 1) {
  3398. struct se_task *task;
  3399. unsigned long flags;
  3400. task = transport_generic_get_task(cmd, data_direction);
  3401. if (!task)
  3402. return -ENOMEM;
  3403. task->task_sg = cmd_sg;
  3404. task->task_sg_nents = sgl_nents;
  3405. task->task_lba = lba;
  3406. task->task_sectors = sectors;
  3407. task->task_size = task->task_sectors * sector_size;
  3408. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3409. list_add_tail(&task->t_list, &cmd->t_task_list);
  3410. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3411. return task_count;
  3412. }
  3413. for (i = 0; i < task_count; i++) {
  3414. struct se_task *task;
  3415. unsigned int task_size, task_sg_nents_padded;
  3416. struct scatterlist *sg;
  3417. unsigned long flags;
  3418. int count;
  3419. task = transport_generic_get_task(cmd, data_direction);
  3420. if (!task)
  3421. return -ENOMEM;
  3422. task->task_lba = lba;
  3423. task->task_sectors = min(sectors, dev_max_sectors);
  3424. task->task_size = task->task_sectors * sector_size;
  3425. /*
  3426. * This now assumes that passed sg_ents are in PAGE_SIZE chunks
  3427. * in order to calculate the number per task SGL entries
  3428. */
  3429. task->task_sg_nents = DIV_ROUND_UP(task->task_size, PAGE_SIZE);
  3430. /*
  3431. * Check if the fabric module driver is requesting that all
  3432. * struct se_task->task_sg[] be chained together.. If so,
  3433. * then allocate an extra padding SG entry for linking and
  3434. * marking the end of the chained SGL for every task except
  3435. * the last one for (task_count > 1) operation, or skipping
  3436. * the extra padding for the (task_count == 1) case.
  3437. */
  3438. if (cmd->se_tfo->task_sg_chaining && (i < (task_count - 1))) {
  3439. task_sg_nents_padded = (task->task_sg_nents + 1);
  3440. } else
  3441. task_sg_nents_padded = task->task_sg_nents;
  3442. task->task_sg = kmalloc(sizeof(struct scatterlist) *
  3443. task_sg_nents_padded, GFP_KERNEL);
  3444. if (!task->task_sg) {
  3445. cmd->se_dev->transport->free_task(task);
  3446. return -ENOMEM;
  3447. }
  3448. sg_init_table(task->task_sg, task_sg_nents_padded);
  3449. task_size = task->task_size;
  3450. /* Build new sgl, only up to task_size */
  3451. for_each_sg(task->task_sg, sg, task->task_sg_nents, count) {
  3452. if (cmd_sg->length > task_size)
  3453. break;
  3454. *sg = *cmd_sg;
  3455. task_size -= cmd_sg->length;
  3456. cmd_sg = sg_next(cmd_sg);
  3457. }
  3458. lba += task->task_sectors;
  3459. sectors -= task->task_sectors;
  3460. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3461. list_add_tail(&task->t_list, &cmd->t_task_list);
  3462. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3463. }
  3464. return task_count;
  3465. }
  3466. static int
  3467. transport_allocate_control_task(struct se_cmd *cmd)
  3468. {
  3469. struct se_task *task;
  3470. unsigned long flags;
  3471. /* Workaround for handling zero-length control CDBs */
  3472. if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
  3473. !cmd->data_length)
  3474. return 0;
  3475. task = transport_generic_get_task(cmd, cmd->data_direction);
  3476. if (!task)
  3477. return -ENOMEM;
  3478. task->task_sg = cmd->t_data_sg;
  3479. task->task_size = cmd->data_length;
  3480. task->task_sg_nents = cmd->t_data_nents;
  3481. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3482. list_add_tail(&task->t_list, &cmd->t_task_list);
  3483. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3484. /* Success! Return number of tasks allocated */
  3485. return 1;
  3486. }
  3487. /*
  3488. * Allocate any required ressources to execute the command, and either place
  3489. * it on the execution queue if possible. For writes we might not have the
  3490. * payload yet, thus notify the fabric via a call to ->write_pending instead.
  3491. */
  3492. int transport_generic_new_cmd(struct se_cmd *cmd)
  3493. {
  3494. struct se_device *dev = cmd->se_dev;
  3495. int task_cdbs, task_cdbs_bidi = 0;
  3496. int set_counts = 1;
  3497. int ret = 0;
  3498. /*
  3499. * Determine is the TCM fabric module has already allocated physical
  3500. * memory, and is directly calling transport_generic_map_mem_to_cmd()
  3501. * beforehand.
  3502. */
  3503. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
  3504. cmd->data_length) {
  3505. ret = transport_generic_get_mem(cmd);
  3506. if (ret < 0)
  3507. goto out_fail;
  3508. }
  3509. /*
  3510. * For BIDI command set up the read tasks first.
  3511. */
  3512. if (cmd->t_bidi_data_sg &&
  3513. dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
  3514. BUG_ON(!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB));
  3515. task_cdbs_bidi = transport_allocate_data_tasks(cmd,
  3516. DMA_FROM_DEVICE, cmd->t_bidi_data_sg,
  3517. cmd->t_bidi_data_nents);
  3518. if (task_cdbs_bidi <= 0)
  3519. goto out_fail;
  3520. atomic_inc(&cmd->t_fe_count);
  3521. atomic_inc(&cmd->t_se_count);
  3522. set_counts = 0;
  3523. }
  3524. if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
  3525. task_cdbs = transport_allocate_data_tasks(cmd,
  3526. cmd->data_direction, cmd->t_data_sg,
  3527. cmd->t_data_nents);
  3528. } else {
  3529. task_cdbs = transport_allocate_control_task(cmd);
  3530. }
  3531. if (task_cdbs < 0)
  3532. goto out_fail;
  3533. else if (!task_cdbs && (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)) {
  3534. spin_lock_irq(&cmd->t_state_lock);
  3535. cmd->t_state = TRANSPORT_COMPLETE;
  3536. cmd->transport_state |= CMD_T_ACTIVE;
  3537. spin_unlock_irq(&cmd->t_state_lock);
  3538. if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
  3539. u8 ua_asc = 0, ua_ascq = 0;
  3540. core_scsi3_ua_clear_for_request_sense(cmd,
  3541. &ua_asc, &ua_ascq);
  3542. }
  3543. INIT_WORK(&cmd->work, target_complete_ok_work);
  3544. queue_work(target_completion_wq, &cmd->work);
  3545. return 0;
  3546. }
  3547. if (set_counts) {
  3548. atomic_inc(&cmd->t_fe_count);
  3549. atomic_inc(&cmd->t_se_count);
  3550. }
  3551. cmd->t_task_list_num = (task_cdbs + task_cdbs_bidi);
  3552. atomic_set(&cmd->t_task_cdbs_left, cmd->t_task_list_num);
  3553. atomic_set(&cmd->t_task_cdbs_ex_left, cmd->t_task_list_num);
  3554. /*
  3555. * For WRITEs, let the fabric know its buffer is ready..
  3556. * This WRITE struct se_cmd (and all of its associated struct se_task's)
  3557. * will be added to the struct se_device execution queue after its WRITE
  3558. * data has arrived. (ie: It gets handled by the transport processing
  3559. * thread a second time)
  3560. */
  3561. if (cmd->data_direction == DMA_TO_DEVICE) {
  3562. transport_add_tasks_to_state_queue(cmd);
  3563. return transport_generic_write_pending(cmd);
  3564. }
  3565. /*
  3566. * Everything else but a WRITE, add the struct se_cmd's struct se_task's
  3567. * to the execution queue.
  3568. */
  3569. transport_execute_tasks(cmd);
  3570. return 0;
  3571. out_fail:
  3572. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3573. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3574. return -EINVAL;
  3575. }
  3576. EXPORT_SYMBOL(transport_generic_new_cmd);
  3577. /* transport_generic_process_write():
  3578. *
  3579. *
  3580. */
  3581. void transport_generic_process_write(struct se_cmd *cmd)
  3582. {
  3583. transport_execute_tasks(cmd);
  3584. }
  3585. EXPORT_SYMBOL(transport_generic_process_write);
  3586. static void transport_write_pending_qf(struct se_cmd *cmd)
  3587. {
  3588. int ret;
  3589. ret = cmd->se_tfo->write_pending(cmd);
  3590. if (ret == -EAGAIN || ret == -ENOMEM) {
  3591. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
  3592. cmd);
  3593. transport_handle_queue_full(cmd, cmd->se_dev);
  3594. }
  3595. }
  3596. static int transport_generic_write_pending(struct se_cmd *cmd)
  3597. {
  3598. unsigned long flags;
  3599. int ret;
  3600. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3601. cmd->t_state = TRANSPORT_WRITE_PENDING;
  3602. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3603. /*
  3604. * Clear the se_cmd for WRITE_PENDING status in order to set
  3605. * CMD_T_ACTIVE so that transport_generic_handle_data can be called
  3606. * from HW target mode interrupt code. This is safe to be called
  3607. * with transport_off=1 before the cmd->se_tfo->write_pending
  3608. * because the se_cmd->se_lun pointer is not being cleared.
  3609. */
  3610. transport_cmd_check_stop(cmd, 1, 0);
  3611. /*
  3612. * Call the fabric write_pending function here to let the
  3613. * frontend know that WRITE buffers are ready.
  3614. */
  3615. ret = cmd->se_tfo->write_pending(cmd);
  3616. if (ret == -EAGAIN || ret == -ENOMEM)
  3617. goto queue_full;
  3618. else if (ret < 0)
  3619. return ret;
  3620. return 1;
  3621. queue_full:
  3622. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
  3623. cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
  3624. transport_handle_queue_full(cmd, cmd->se_dev);
  3625. return 0;
  3626. }
  3627. void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
  3628. {
  3629. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
  3630. if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
  3631. transport_wait_for_tasks(cmd);
  3632. transport_release_cmd(cmd);
  3633. } else {
  3634. if (wait_for_tasks)
  3635. transport_wait_for_tasks(cmd);
  3636. core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
  3637. if (cmd->se_lun)
  3638. transport_lun_remove_cmd(cmd);
  3639. transport_free_dev_tasks(cmd);
  3640. transport_put_cmd(cmd);
  3641. }
  3642. }
  3643. EXPORT_SYMBOL(transport_generic_free_cmd);
  3644. /* target_get_sess_cmd - Add command to active ->sess_cmd_list
  3645. * @se_sess: session to reference
  3646. * @se_cmd: command descriptor to add
  3647. * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
  3648. */
  3649. void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
  3650. bool ack_kref)
  3651. {
  3652. unsigned long flags;
  3653. kref_init(&se_cmd->cmd_kref);
  3654. /*
  3655. * Add a second kref if the fabric caller is expecting to handle
  3656. * fabric acknowledgement that requires two target_put_sess_cmd()
  3657. * invocations before se_cmd descriptor release.
  3658. */
  3659. if (ack_kref == true) {
  3660. kref_get(&se_cmd->cmd_kref);
  3661. se_cmd->se_cmd_flags |= SCF_ACK_KREF;
  3662. }
  3663. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3664. list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
  3665. se_cmd->check_release = 1;
  3666. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3667. }
  3668. EXPORT_SYMBOL(target_get_sess_cmd);
  3669. static void target_release_cmd_kref(struct kref *kref)
  3670. {
  3671. struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
  3672. struct se_session *se_sess = se_cmd->se_sess;
  3673. unsigned long flags;
  3674. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3675. if (list_empty(&se_cmd->se_cmd_list)) {
  3676. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3677. se_cmd->se_tfo->release_cmd(se_cmd);
  3678. return;
  3679. }
  3680. if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
  3681. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3682. complete(&se_cmd->cmd_wait_comp);
  3683. return;
  3684. }
  3685. list_del(&se_cmd->se_cmd_list);
  3686. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3687. se_cmd->se_tfo->release_cmd(se_cmd);
  3688. }
  3689. /* target_put_sess_cmd - Check for active I/O shutdown via kref_put
  3690. * @se_sess: session to reference
  3691. * @se_cmd: command descriptor to drop
  3692. */
  3693. int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
  3694. {
  3695. return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
  3696. }
  3697. EXPORT_SYMBOL(target_put_sess_cmd);
  3698. /* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
  3699. * @se_sess: session to split
  3700. */
  3701. void target_splice_sess_cmd_list(struct se_session *se_sess)
  3702. {
  3703. struct se_cmd *se_cmd;
  3704. unsigned long flags;
  3705. WARN_ON(!list_empty(&se_sess->sess_wait_list));
  3706. INIT_LIST_HEAD(&se_sess->sess_wait_list);
  3707. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3708. se_sess->sess_tearing_down = 1;
  3709. list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
  3710. list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
  3711. se_cmd->cmd_wait_set = 1;
  3712. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3713. }
  3714. EXPORT_SYMBOL(target_splice_sess_cmd_list);
  3715. /* target_wait_for_sess_cmds - Wait for outstanding descriptors
  3716. * @se_sess: session to wait for active I/O
  3717. * @wait_for_tasks: Make extra transport_wait_for_tasks call
  3718. */
  3719. void target_wait_for_sess_cmds(
  3720. struct se_session *se_sess,
  3721. int wait_for_tasks)
  3722. {
  3723. struct se_cmd *se_cmd, *tmp_cmd;
  3724. bool rc = false;
  3725. list_for_each_entry_safe(se_cmd, tmp_cmd,
  3726. &se_sess->sess_wait_list, se_cmd_list) {
  3727. list_del(&se_cmd->se_cmd_list);
  3728. pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
  3729. " %d\n", se_cmd, se_cmd->t_state,
  3730. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3731. if (wait_for_tasks) {
  3732. pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
  3733. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3734. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3735. rc = transport_wait_for_tasks(se_cmd);
  3736. pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
  3737. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3738. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3739. }
  3740. if (!rc) {
  3741. wait_for_completion(&se_cmd->cmd_wait_comp);
  3742. pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
  3743. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3744. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3745. }
  3746. se_cmd->se_tfo->release_cmd(se_cmd);
  3747. }
  3748. }
  3749. EXPORT_SYMBOL(target_wait_for_sess_cmds);
  3750. /* transport_lun_wait_for_tasks():
  3751. *
  3752. * Called from ConfigFS context to stop the passed struct se_cmd to allow
  3753. * an struct se_lun to be successfully shutdown.
  3754. */
  3755. static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
  3756. {
  3757. unsigned long flags;
  3758. int ret;
  3759. /*
  3760. * If the frontend has already requested this struct se_cmd to
  3761. * be stopped, we can safely ignore this struct se_cmd.
  3762. */
  3763. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3764. if (cmd->transport_state & CMD_T_STOP) {
  3765. cmd->transport_state &= ~CMD_T_LUN_STOP;
  3766. pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
  3767. cmd->se_tfo->get_task_tag(cmd));
  3768. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3769. transport_cmd_check_stop(cmd, 1, 0);
  3770. return -EPERM;
  3771. }
  3772. cmd->transport_state |= CMD_T_LUN_FE_STOP;
  3773. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3774. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  3775. ret = transport_stop_tasks_for_cmd(cmd);
  3776. pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
  3777. " %d\n", cmd, cmd->t_task_list_num, ret);
  3778. if (!ret) {
  3779. pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
  3780. cmd->se_tfo->get_task_tag(cmd));
  3781. wait_for_completion(&cmd->transport_lun_stop_comp);
  3782. pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
  3783. cmd->se_tfo->get_task_tag(cmd));
  3784. }
  3785. transport_remove_cmd_from_queue(cmd);
  3786. return 0;
  3787. }
  3788. static void __transport_clear_lun_from_sessions(struct se_lun *lun)
  3789. {
  3790. struct se_cmd *cmd = NULL;
  3791. unsigned long lun_flags, cmd_flags;
  3792. /*
  3793. * Do exception processing and return CHECK_CONDITION status to the
  3794. * Initiator Port.
  3795. */
  3796. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3797. while (!list_empty(&lun->lun_cmd_list)) {
  3798. cmd = list_first_entry(&lun->lun_cmd_list,
  3799. struct se_cmd, se_lun_node);
  3800. list_del_init(&cmd->se_lun_node);
  3801. /*
  3802. * This will notify iscsi_target_transport.c:
  3803. * transport_cmd_check_stop() that a LUN shutdown is in
  3804. * progress for the iscsi_cmd_t.
  3805. */
  3806. spin_lock(&cmd->t_state_lock);
  3807. pr_debug("SE_LUN[%d] - Setting cmd->transport"
  3808. "_lun_stop for ITT: 0x%08x\n",
  3809. cmd->se_lun->unpacked_lun,
  3810. cmd->se_tfo->get_task_tag(cmd));
  3811. cmd->transport_state |= CMD_T_LUN_STOP;
  3812. spin_unlock(&cmd->t_state_lock);
  3813. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  3814. if (!cmd->se_lun) {
  3815. pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
  3816. cmd->se_tfo->get_task_tag(cmd),
  3817. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
  3818. BUG();
  3819. }
  3820. /*
  3821. * If the Storage engine still owns the iscsi_cmd_t, determine
  3822. * and/or stop its context.
  3823. */
  3824. pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
  3825. "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
  3826. cmd->se_tfo->get_task_tag(cmd));
  3827. if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
  3828. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3829. continue;
  3830. }
  3831. pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
  3832. "_wait_for_tasks(): SUCCESS\n",
  3833. cmd->se_lun->unpacked_lun,
  3834. cmd->se_tfo->get_task_tag(cmd));
  3835. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  3836. if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
  3837. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3838. goto check_cond;
  3839. }
  3840. cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
  3841. transport_all_task_dev_remove_state(cmd);
  3842. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3843. transport_free_dev_tasks(cmd);
  3844. /*
  3845. * The Storage engine stopped this struct se_cmd before it was
  3846. * send to the fabric frontend for delivery back to the
  3847. * Initiator Node. Return this SCSI CDB back with an
  3848. * CHECK_CONDITION status.
  3849. */
  3850. check_cond:
  3851. transport_send_check_condition_and_sense(cmd,
  3852. TCM_NON_EXISTENT_LUN, 0);
  3853. /*
  3854. * If the fabric frontend is waiting for this iscsi_cmd_t to
  3855. * be released, notify the waiting thread now that LU has
  3856. * finished accessing it.
  3857. */
  3858. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  3859. if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
  3860. pr_debug("SE_LUN[%d] - Detected FE stop for"
  3861. " struct se_cmd: %p ITT: 0x%08x\n",
  3862. lun->unpacked_lun,
  3863. cmd, cmd->se_tfo->get_task_tag(cmd));
  3864. spin_unlock_irqrestore(&cmd->t_state_lock,
  3865. cmd_flags);
  3866. transport_cmd_check_stop(cmd, 1, 0);
  3867. complete(&cmd->transport_lun_fe_stop_comp);
  3868. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3869. continue;
  3870. }
  3871. pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
  3872. lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
  3873. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3874. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3875. }
  3876. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  3877. }
  3878. static int transport_clear_lun_thread(void *p)
  3879. {
  3880. struct se_lun *lun = p;
  3881. __transport_clear_lun_from_sessions(lun);
  3882. complete(&lun->lun_shutdown_comp);
  3883. return 0;
  3884. }
  3885. int transport_clear_lun_from_sessions(struct se_lun *lun)
  3886. {
  3887. struct task_struct *kt;
  3888. kt = kthread_run(transport_clear_lun_thread, lun,
  3889. "tcm_cl_%u", lun->unpacked_lun);
  3890. if (IS_ERR(kt)) {
  3891. pr_err("Unable to start clear_lun thread\n");
  3892. return PTR_ERR(kt);
  3893. }
  3894. wait_for_completion(&lun->lun_shutdown_comp);
  3895. return 0;
  3896. }
  3897. /**
  3898. * transport_wait_for_tasks - wait for completion to occur
  3899. * @cmd: command to wait
  3900. *
  3901. * Called from frontend fabric context to wait for storage engine
  3902. * to pause and/or release frontend generated struct se_cmd.
  3903. */
  3904. bool transport_wait_for_tasks(struct se_cmd *cmd)
  3905. {
  3906. unsigned long flags;
  3907. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3908. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
  3909. !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
  3910. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3911. return false;
  3912. }
  3913. /*
  3914. * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
  3915. * has been set in transport_set_supported_SAM_opcode().
  3916. */
  3917. if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
  3918. !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
  3919. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3920. return false;
  3921. }
  3922. /*
  3923. * If we are already stopped due to an external event (ie: LUN shutdown)
  3924. * sleep until the connection can have the passed struct se_cmd back.
  3925. * The cmd->transport_lun_stopped_sem will be upped by
  3926. * transport_clear_lun_from_sessions() once the ConfigFS context caller
  3927. * has completed its operation on the struct se_cmd.
  3928. */
  3929. if (cmd->transport_state & CMD_T_LUN_STOP) {
  3930. pr_debug("wait_for_tasks: Stopping"
  3931. " wait_for_completion(&cmd->t_tasktransport_lun_fe"
  3932. "_stop_comp); for ITT: 0x%08x\n",
  3933. cmd->se_tfo->get_task_tag(cmd));
  3934. /*
  3935. * There is a special case for WRITES where a FE exception +
  3936. * LUN shutdown means ConfigFS context is still sleeping on
  3937. * transport_lun_stop_comp in transport_lun_wait_for_tasks().
  3938. * We go ahead and up transport_lun_stop_comp just to be sure
  3939. * here.
  3940. */
  3941. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3942. complete(&cmd->transport_lun_stop_comp);
  3943. wait_for_completion(&cmd->transport_lun_fe_stop_comp);
  3944. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3945. transport_all_task_dev_remove_state(cmd);
  3946. /*
  3947. * At this point, the frontend who was the originator of this
  3948. * struct se_cmd, now owns the structure and can be released through
  3949. * normal means below.
  3950. */
  3951. pr_debug("wait_for_tasks: Stopped"
  3952. " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
  3953. "stop_comp); for ITT: 0x%08x\n",
  3954. cmd->se_tfo->get_task_tag(cmd));
  3955. cmd->transport_state &= ~CMD_T_LUN_STOP;
  3956. }
  3957. if (!(cmd->transport_state & CMD_T_ACTIVE)) {
  3958. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3959. return false;
  3960. }
  3961. cmd->transport_state |= CMD_T_STOP;
  3962. pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
  3963. " i_state: %d, t_state: %d, CMD_T_STOP\n",
  3964. cmd, cmd->se_tfo->get_task_tag(cmd),
  3965. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
  3966. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3967. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  3968. wait_for_completion(&cmd->t_transport_stop_comp);
  3969. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3970. cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
  3971. pr_debug("wait_for_tasks: Stopped wait_for_compltion("
  3972. "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
  3973. cmd->se_tfo->get_task_tag(cmd));
  3974. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3975. return true;
  3976. }
  3977. EXPORT_SYMBOL(transport_wait_for_tasks);
  3978. static int transport_get_sense_codes(
  3979. struct se_cmd *cmd,
  3980. u8 *asc,
  3981. u8 *ascq)
  3982. {
  3983. *asc = cmd->scsi_asc;
  3984. *ascq = cmd->scsi_ascq;
  3985. return 0;
  3986. }
  3987. static int transport_set_sense_codes(
  3988. struct se_cmd *cmd,
  3989. u8 asc,
  3990. u8 ascq)
  3991. {
  3992. cmd->scsi_asc = asc;
  3993. cmd->scsi_ascq = ascq;
  3994. return 0;
  3995. }
  3996. int transport_send_check_condition_and_sense(
  3997. struct se_cmd *cmd,
  3998. u8 reason,
  3999. int from_transport)
  4000. {
  4001. unsigned char *buffer = cmd->sense_buffer;
  4002. unsigned long flags;
  4003. int offset;
  4004. u8 asc = 0, ascq = 0;
  4005. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4006. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  4007. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4008. return 0;
  4009. }
  4010. cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
  4011. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4012. if (!reason && from_transport)
  4013. goto after_reason;
  4014. if (!from_transport)
  4015. cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
  4016. /*
  4017. * Data Segment and SenseLength of the fabric response PDU.
  4018. *
  4019. * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
  4020. * from include/scsi/scsi_cmnd.h
  4021. */
  4022. offset = cmd->se_tfo->set_fabric_sense_len(cmd,
  4023. TRANSPORT_SENSE_BUFFER);
  4024. /*
  4025. * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
  4026. * SENSE KEY values from include/scsi/scsi.h
  4027. */
  4028. switch (reason) {
  4029. case TCM_NON_EXISTENT_LUN:
  4030. /* CURRENT ERROR */
  4031. buffer[offset] = 0x70;
  4032. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4033. /* ILLEGAL REQUEST */
  4034. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4035. /* LOGICAL UNIT NOT SUPPORTED */
  4036. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
  4037. break;
  4038. case TCM_UNSUPPORTED_SCSI_OPCODE:
  4039. case TCM_SECTOR_COUNT_TOO_MANY:
  4040. /* CURRENT ERROR */
  4041. buffer[offset] = 0x70;
  4042. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4043. /* ILLEGAL REQUEST */
  4044. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4045. /* INVALID COMMAND OPERATION CODE */
  4046. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
  4047. break;
  4048. case TCM_UNKNOWN_MODE_PAGE:
  4049. /* CURRENT ERROR */
  4050. buffer[offset] = 0x70;
  4051. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4052. /* ILLEGAL REQUEST */
  4053. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4054. /* INVALID FIELD IN CDB */
  4055. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  4056. break;
  4057. case TCM_CHECK_CONDITION_ABORT_CMD:
  4058. /* CURRENT ERROR */
  4059. buffer[offset] = 0x70;
  4060. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4061. /* ABORTED COMMAND */
  4062. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4063. /* BUS DEVICE RESET FUNCTION OCCURRED */
  4064. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
  4065. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
  4066. break;
  4067. case TCM_INCORRECT_AMOUNT_OF_DATA:
  4068. /* CURRENT ERROR */
  4069. buffer[offset] = 0x70;
  4070. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4071. /* ABORTED COMMAND */
  4072. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4073. /* WRITE ERROR */
  4074. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  4075. /* NOT ENOUGH UNSOLICITED DATA */
  4076. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
  4077. break;
  4078. case TCM_INVALID_CDB_FIELD:
  4079. /* CURRENT ERROR */
  4080. buffer[offset] = 0x70;
  4081. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4082. /* ILLEGAL REQUEST */
  4083. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4084. /* INVALID FIELD IN CDB */
  4085. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  4086. break;
  4087. case TCM_INVALID_PARAMETER_LIST:
  4088. /* CURRENT ERROR */
  4089. buffer[offset] = 0x70;
  4090. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4091. /* ILLEGAL REQUEST */
  4092. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4093. /* INVALID FIELD IN PARAMETER LIST */
  4094. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
  4095. break;
  4096. case TCM_UNEXPECTED_UNSOLICITED_DATA:
  4097. /* CURRENT ERROR */
  4098. buffer[offset] = 0x70;
  4099. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4100. /* ABORTED COMMAND */
  4101. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4102. /* WRITE ERROR */
  4103. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  4104. /* UNEXPECTED_UNSOLICITED_DATA */
  4105. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
  4106. break;
  4107. case TCM_SERVICE_CRC_ERROR:
  4108. /* CURRENT ERROR */
  4109. buffer[offset] = 0x70;
  4110. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4111. /* ABORTED COMMAND */
  4112. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4113. /* PROTOCOL SERVICE CRC ERROR */
  4114. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
  4115. /* N/A */
  4116. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
  4117. break;
  4118. case TCM_SNACK_REJECTED:
  4119. /* CURRENT ERROR */
  4120. buffer[offset] = 0x70;
  4121. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4122. /* ABORTED COMMAND */
  4123. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  4124. /* READ ERROR */
  4125. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
  4126. /* FAILED RETRANSMISSION REQUEST */
  4127. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
  4128. break;
  4129. case TCM_WRITE_PROTECTED:
  4130. /* CURRENT ERROR */
  4131. buffer[offset] = 0x70;
  4132. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4133. /* DATA PROTECT */
  4134. buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
  4135. /* WRITE PROTECTED */
  4136. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
  4137. break;
  4138. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  4139. /* CURRENT ERROR */
  4140. buffer[offset] = 0x70;
  4141. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4142. /* UNIT ATTENTION */
  4143. buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  4144. core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
  4145. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  4146. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  4147. break;
  4148. case TCM_CHECK_CONDITION_NOT_READY:
  4149. /* CURRENT ERROR */
  4150. buffer[offset] = 0x70;
  4151. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4152. /* Not Ready */
  4153. buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
  4154. transport_get_sense_codes(cmd, &asc, &ascq);
  4155. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  4156. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  4157. break;
  4158. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  4159. default:
  4160. /* CURRENT ERROR */
  4161. buffer[offset] = 0x70;
  4162. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  4163. /* ILLEGAL REQUEST */
  4164. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  4165. /* LOGICAL UNIT COMMUNICATION FAILURE */
  4166. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
  4167. break;
  4168. }
  4169. /*
  4170. * This code uses linux/include/scsi/scsi.h SAM status codes!
  4171. */
  4172. cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
  4173. /*
  4174. * Automatically padded, this value is encoded in the fabric's
  4175. * data_length response PDU containing the SCSI defined sense data.
  4176. */
  4177. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
  4178. after_reason:
  4179. return cmd->se_tfo->queue_status(cmd);
  4180. }
  4181. EXPORT_SYMBOL(transport_send_check_condition_and_sense);
  4182. int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
  4183. {
  4184. int ret = 0;
  4185. if (cmd->transport_state & CMD_T_ABORTED) {
  4186. if (!send_status ||
  4187. (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
  4188. return 1;
  4189. #if 0
  4190. pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
  4191. " status for CDB: 0x%02x ITT: 0x%08x\n",
  4192. cmd->t_task_cdb[0],
  4193. cmd->se_tfo->get_task_tag(cmd));
  4194. #endif
  4195. cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
  4196. cmd->se_tfo->queue_status(cmd);
  4197. ret = 1;
  4198. }
  4199. return ret;
  4200. }
  4201. EXPORT_SYMBOL(transport_check_aborted_status);
  4202. void transport_send_task_abort(struct se_cmd *cmd)
  4203. {
  4204. unsigned long flags;
  4205. spin_lock_irqsave(&cmd->t_state_lock, flags);
  4206. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  4207. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4208. return;
  4209. }
  4210. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  4211. /*
  4212. * If there are still expected incoming fabric WRITEs, we wait
  4213. * until until they have completed before sending a TASK_ABORTED
  4214. * response. This response with TASK_ABORTED status will be
  4215. * queued back to fabric module by transport_check_aborted_status().
  4216. */
  4217. if (cmd->data_direction == DMA_TO_DEVICE) {
  4218. if (cmd->se_tfo->write_pending_status(cmd) != 0) {
  4219. cmd->transport_state |= CMD_T_ABORTED;
  4220. smp_mb__after_atomic_inc();
  4221. }
  4222. }
  4223. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  4224. #if 0
  4225. pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
  4226. " ITT: 0x%08x\n", cmd->t_task_cdb[0],
  4227. cmd->se_tfo->get_task_tag(cmd));
  4228. #endif
  4229. cmd->se_tfo->queue_status(cmd);
  4230. }
  4231. static int transport_generic_do_tmr(struct se_cmd *cmd)
  4232. {
  4233. struct se_device *dev = cmd->se_dev;
  4234. struct se_tmr_req *tmr = cmd->se_tmr_req;
  4235. int ret;
  4236. switch (tmr->function) {
  4237. case TMR_ABORT_TASK:
  4238. core_tmr_abort_task(dev, tmr, cmd->se_sess);
  4239. break;
  4240. case TMR_ABORT_TASK_SET:
  4241. case TMR_CLEAR_ACA:
  4242. case TMR_CLEAR_TASK_SET:
  4243. tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  4244. break;
  4245. case TMR_LUN_RESET:
  4246. ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
  4247. tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
  4248. TMR_FUNCTION_REJECTED;
  4249. break;
  4250. case TMR_TARGET_WARM_RESET:
  4251. tmr->response = TMR_FUNCTION_REJECTED;
  4252. break;
  4253. case TMR_TARGET_COLD_RESET:
  4254. tmr->response = TMR_FUNCTION_REJECTED;
  4255. break;
  4256. default:
  4257. pr_err("Uknown TMR function: 0x%02x.\n",
  4258. tmr->function);
  4259. tmr->response = TMR_FUNCTION_REJECTED;
  4260. break;
  4261. }
  4262. cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
  4263. cmd->se_tfo->queue_tm_rsp(cmd);
  4264. transport_cmd_check_stop_to_fabric(cmd);
  4265. return 0;
  4266. }
  4267. /* transport_processing_thread():
  4268. *
  4269. *
  4270. */
  4271. static int transport_processing_thread(void *param)
  4272. {
  4273. int ret;
  4274. struct se_cmd *cmd;
  4275. struct se_device *dev = param;
  4276. while (!kthread_should_stop()) {
  4277. ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
  4278. atomic_read(&dev->dev_queue_obj.queue_cnt) ||
  4279. kthread_should_stop());
  4280. if (ret < 0)
  4281. goto out;
  4282. get_cmd:
  4283. cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
  4284. if (!cmd)
  4285. continue;
  4286. switch (cmd->t_state) {
  4287. case TRANSPORT_NEW_CMD:
  4288. BUG();
  4289. break;
  4290. case TRANSPORT_NEW_CMD_MAP:
  4291. if (!cmd->se_tfo->new_cmd_map) {
  4292. pr_err("cmd->se_tfo->new_cmd_map is"
  4293. " NULL for TRANSPORT_NEW_CMD_MAP\n");
  4294. BUG();
  4295. }
  4296. ret = cmd->se_tfo->new_cmd_map(cmd);
  4297. if (ret < 0) {
  4298. transport_generic_request_failure(cmd);
  4299. break;
  4300. }
  4301. ret = transport_generic_new_cmd(cmd);
  4302. if (ret < 0) {
  4303. transport_generic_request_failure(cmd);
  4304. break;
  4305. }
  4306. break;
  4307. case TRANSPORT_PROCESS_WRITE:
  4308. transport_generic_process_write(cmd);
  4309. break;
  4310. case TRANSPORT_PROCESS_TMR:
  4311. transport_generic_do_tmr(cmd);
  4312. break;
  4313. case TRANSPORT_COMPLETE_QF_WP:
  4314. transport_write_pending_qf(cmd);
  4315. break;
  4316. case TRANSPORT_COMPLETE_QF_OK:
  4317. transport_complete_qf(cmd);
  4318. break;
  4319. default:
  4320. pr_err("Unknown t_state: %d for ITT: 0x%08x "
  4321. "i_state: %d on SE LUN: %u\n",
  4322. cmd->t_state,
  4323. cmd->se_tfo->get_task_tag(cmd),
  4324. cmd->se_tfo->get_cmd_state(cmd),
  4325. cmd->se_lun->unpacked_lun);
  4326. BUG();
  4327. }
  4328. goto get_cmd;
  4329. }
  4330. out:
  4331. WARN_ON(!list_empty(&dev->state_task_list));
  4332. WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
  4333. dev->process_thread = NULL;
  4334. return 0;
  4335. }