target_core_transport.c 120 KB

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