target_core_transport.c 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389
  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. break;
  1166. default:
  1167. cdb[1] &= 0x1f; /* clear logical unit number */
  1168. break;
  1169. }
  1170. }
  1171. static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
  1172. /*
  1173. * Used by fabric modules containing a local struct se_cmd within their
  1174. * fabric dependent per I/O descriptor.
  1175. */
  1176. void transport_init_se_cmd(
  1177. struct se_cmd *cmd,
  1178. struct target_core_fabric_ops *tfo,
  1179. struct se_session *se_sess,
  1180. u32 data_length,
  1181. int data_direction,
  1182. int task_attr,
  1183. unsigned char *sense_buffer)
  1184. {
  1185. INIT_LIST_HEAD(&cmd->se_lun_node);
  1186. INIT_LIST_HEAD(&cmd->se_delayed_node);
  1187. INIT_LIST_HEAD(&cmd->se_qf_node);
  1188. INIT_LIST_HEAD(&cmd->se_queue_node);
  1189. INIT_LIST_HEAD(&cmd->se_cmd_list);
  1190. INIT_LIST_HEAD(&cmd->execute_list);
  1191. INIT_LIST_HEAD(&cmd->state_list);
  1192. init_completion(&cmd->transport_lun_fe_stop_comp);
  1193. init_completion(&cmd->transport_lun_stop_comp);
  1194. init_completion(&cmd->t_transport_stop_comp);
  1195. init_completion(&cmd->cmd_wait_comp);
  1196. init_completion(&cmd->task_stop_comp);
  1197. spin_lock_init(&cmd->t_state_lock);
  1198. cmd->transport_state = CMD_T_DEV_ACTIVE;
  1199. cmd->se_tfo = tfo;
  1200. cmd->se_sess = se_sess;
  1201. cmd->data_length = data_length;
  1202. cmd->data_direction = data_direction;
  1203. cmd->sam_task_attr = task_attr;
  1204. cmd->sense_buffer = sense_buffer;
  1205. cmd->state_active = false;
  1206. }
  1207. EXPORT_SYMBOL(transport_init_se_cmd);
  1208. static int transport_check_alloc_task_attr(struct se_cmd *cmd)
  1209. {
  1210. /*
  1211. * Check if SAM Task Attribute emulation is enabled for this
  1212. * struct se_device storage object
  1213. */
  1214. if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  1215. return 0;
  1216. if (cmd->sam_task_attr == MSG_ACA_TAG) {
  1217. pr_debug("SAM Task Attribute ACA"
  1218. " emulation is not supported\n");
  1219. return -EINVAL;
  1220. }
  1221. /*
  1222. * Used to determine when ORDERED commands should go from
  1223. * Dormant to Active status.
  1224. */
  1225. cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
  1226. smp_mb__after_atomic_inc();
  1227. pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
  1228. cmd->se_ordered_id, cmd->sam_task_attr,
  1229. cmd->se_dev->transport->name);
  1230. return 0;
  1231. }
  1232. /* target_setup_cmd_from_cdb():
  1233. *
  1234. * Called from fabric RX Thread.
  1235. */
  1236. int target_setup_cmd_from_cdb(
  1237. struct se_cmd *cmd,
  1238. unsigned char *cdb)
  1239. {
  1240. int ret;
  1241. transport_generic_prepare_cdb(cdb);
  1242. /*
  1243. * Ensure that the received CDB is less than the max (252 + 8) bytes
  1244. * for VARIABLE_LENGTH_CMD
  1245. */
  1246. if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
  1247. pr_err("Received SCSI CDB with command_size: %d that"
  1248. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  1249. scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
  1250. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1251. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1252. return -EINVAL;
  1253. }
  1254. /*
  1255. * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
  1256. * allocate the additional extended CDB buffer now.. Otherwise
  1257. * setup the pointer from __t_task_cdb to t_task_cdb.
  1258. */
  1259. if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
  1260. cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
  1261. GFP_KERNEL);
  1262. if (!cmd->t_task_cdb) {
  1263. pr_err("Unable to allocate cmd->t_task_cdb"
  1264. " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
  1265. scsi_command_size(cdb),
  1266. (unsigned long)sizeof(cmd->__t_task_cdb));
  1267. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1268. cmd->scsi_sense_reason =
  1269. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1270. return -ENOMEM;
  1271. }
  1272. } else
  1273. cmd->t_task_cdb = &cmd->__t_task_cdb[0];
  1274. /*
  1275. * Copy the original CDB into cmd->
  1276. */
  1277. memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
  1278. /*
  1279. * Setup the received CDB based on SCSI defined opcodes and
  1280. * perform unit attention, persistent reservations and ALUA
  1281. * checks for virtual device backends. The cmd->t_task_cdb
  1282. * pointer is expected to be setup before we reach this point.
  1283. */
  1284. ret = transport_generic_cmd_sequencer(cmd, cdb);
  1285. if (ret < 0)
  1286. return ret;
  1287. /*
  1288. * Check for SAM Task Attribute Emulation
  1289. */
  1290. if (transport_check_alloc_task_attr(cmd) < 0) {
  1291. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1292. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1293. return -EINVAL;
  1294. }
  1295. spin_lock(&cmd->se_lun->lun_sep_lock);
  1296. if (cmd->se_lun->lun_sep)
  1297. cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
  1298. spin_unlock(&cmd->se_lun->lun_sep_lock);
  1299. return 0;
  1300. }
  1301. EXPORT_SYMBOL(target_setup_cmd_from_cdb);
  1302. /*
  1303. * Used by fabric module frontends to queue tasks directly.
  1304. * Many only be used from process context only
  1305. */
  1306. int transport_handle_cdb_direct(
  1307. struct se_cmd *cmd)
  1308. {
  1309. int ret;
  1310. if (!cmd->se_lun) {
  1311. dump_stack();
  1312. pr_err("cmd->se_lun is NULL\n");
  1313. return -EINVAL;
  1314. }
  1315. if (in_interrupt()) {
  1316. dump_stack();
  1317. pr_err("transport_generic_handle_cdb cannot be called"
  1318. " from interrupt context\n");
  1319. return -EINVAL;
  1320. }
  1321. /*
  1322. * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE following
  1323. * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
  1324. * in existing usage to ensure that outstanding descriptors are handled
  1325. * correctly during shutdown via transport_wait_for_tasks()
  1326. *
  1327. * Also, we don't take cmd->t_state_lock here as we only expect
  1328. * this to be called for initial descriptor submission.
  1329. */
  1330. cmd->t_state = TRANSPORT_NEW_CMD;
  1331. cmd->transport_state |= CMD_T_ACTIVE;
  1332. /*
  1333. * transport_generic_new_cmd() is already handling QUEUE_FULL,
  1334. * so follow TRANSPORT_NEW_CMD processing thread context usage
  1335. * and call transport_generic_request_failure() if necessary..
  1336. */
  1337. ret = transport_generic_new_cmd(cmd);
  1338. if (ret < 0)
  1339. transport_generic_request_failure(cmd);
  1340. return 0;
  1341. }
  1342. EXPORT_SYMBOL(transport_handle_cdb_direct);
  1343. /**
  1344. * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
  1345. *
  1346. * @se_cmd: command descriptor to submit
  1347. * @se_sess: associated se_sess for endpoint
  1348. * @cdb: pointer to SCSI CDB
  1349. * @sense: pointer to SCSI sense buffer
  1350. * @unpacked_lun: unpacked LUN to reference for struct se_lun
  1351. * @data_length: fabric expected data transfer length
  1352. * @task_addr: SAM task attribute
  1353. * @data_dir: DMA data direction
  1354. * @flags: flags for command submission from target_sc_flags_tables
  1355. *
  1356. * This may only be called from process context, and also currently
  1357. * assumes internal allocation of fabric payload buffer by target-core.
  1358. **/
  1359. void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
  1360. unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
  1361. u32 data_length, int task_attr, int data_dir, int flags)
  1362. {
  1363. struct se_portal_group *se_tpg;
  1364. int rc;
  1365. se_tpg = se_sess->se_tpg;
  1366. BUG_ON(!se_tpg);
  1367. BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
  1368. BUG_ON(in_interrupt());
  1369. /*
  1370. * Initialize se_cmd for target operation. From this point
  1371. * exceptions are handled by sending exception status via
  1372. * target_core_fabric_ops->queue_status() callback
  1373. */
  1374. transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
  1375. data_length, data_dir, task_attr, sense);
  1376. if (flags & TARGET_SCF_UNKNOWN_SIZE)
  1377. se_cmd->unknown_data_length = 1;
  1378. /*
  1379. * Obtain struct se_cmd->cmd_kref reference and add new cmd to
  1380. * se_sess->sess_cmd_list. A second kref_get here is necessary
  1381. * for fabrics using TARGET_SCF_ACK_KREF that expect a second
  1382. * kref_put() to happen during fabric packet acknowledgement.
  1383. */
  1384. target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
  1385. /*
  1386. * Signal bidirectional data payloads to target-core
  1387. */
  1388. if (flags & TARGET_SCF_BIDI_OP)
  1389. se_cmd->se_cmd_flags |= SCF_BIDI;
  1390. /*
  1391. * Locate se_lun pointer and attach it to struct se_cmd
  1392. */
  1393. if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
  1394. transport_send_check_condition_and_sense(se_cmd,
  1395. se_cmd->scsi_sense_reason, 0);
  1396. target_put_sess_cmd(se_sess, se_cmd);
  1397. return;
  1398. }
  1399. /*
  1400. * Sanitize CDBs via transport_generic_cmd_sequencer() and
  1401. * allocate the necessary tasks to complete the received CDB+data
  1402. */
  1403. rc = target_setup_cmd_from_cdb(se_cmd, cdb);
  1404. if (rc != 0) {
  1405. transport_generic_request_failure(se_cmd);
  1406. return;
  1407. }
  1408. /*
  1409. * Check if we need to delay processing because of ALUA
  1410. * Active/NonOptimized primary access state..
  1411. */
  1412. core_alua_check_nonop_delay(se_cmd);
  1413. /*
  1414. * Dispatch se_cmd descriptor to se_lun->lun_se_dev backend
  1415. * for immediate execution of READs, otherwise wait for
  1416. * transport_generic_handle_data() to be called for WRITEs
  1417. * when fabric has filled the incoming buffer.
  1418. */
  1419. transport_handle_cdb_direct(se_cmd);
  1420. return;
  1421. }
  1422. EXPORT_SYMBOL(target_submit_cmd);
  1423. static void target_complete_tmr_failure(struct work_struct *work)
  1424. {
  1425. struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
  1426. se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
  1427. se_cmd->se_tfo->queue_tm_rsp(se_cmd);
  1428. transport_generic_free_cmd(se_cmd, 0);
  1429. }
  1430. /**
  1431. * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
  1432. * for TMR CDBs
  1433. *
  1434. * @se_cmd: command descriptor to submit
  1435. * @se_sess: associated se_sess for endpoint
  1436. * @sense: pointer to SCSI sense buffer
  1437. * @unpacked_lun: unpacked LUN to reference for struct se_lun
  1438. * @fabric_context: fabric context for TMR req
  1439. * @tm_type: Type of TM request
  1440. * @gfp: gfp type for caller
  1441. * @tag: referenced task tag for TMR_ABORT_TASK
  1442. * @flags: submit cmd flags
  1443. *
  1444. * Callable from all contexts.
  1445. **/
  1446. int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
  1447. unsigned char *sense, u32 unpacked_lun,
  1448. void *fabric_tmr_ptr, unsigned char tm_type,
  1449. gfp_t gfp, unsigned int tag, int flags)
  1450. {
  1451. struct se_portal_group *se_tpg;
  1452. int ret;
  1453. se_tpg = se_sess->se_tpg;
  1454. BUG_ON(!se_tpg);
  1455. transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
  1456. 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
  1457. /*
  1458. * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
  1459. * allocation failure.
  1460. */
  1461. ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
  1462. if (ret < 0)
  1463. return -ENOMEM;
  1464. if (tm_type == TMR_ABORT_TASK)
  1465. se_cmd->se_tmr_req->ref_task_tag = tag;
  1466. /* See target_submit_cmd for commentary */
  1467. target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
  1468. ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
  1469. if (ret) {
  1470. /*
  1471. * For callback during failure handling, push this work off
  1472. * to process context with TMR_LUN_DOES_NOT_EXIST status.
  1473. */
  1474. INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
  1475. schedule_work(&se_cmd->work);
  1476. return 0;
  1477. }
  1478. transport_generic_handle_tmr(se_cmd);
  1479. return 0;
  1480. }
  1481. EXPORT_SYMBOL(target_submit_tmr);
  1482. /*
  1483. * Used by fabric module frontends defining a TFO->new_cmd_map() caller
  1484. * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
  1485. * complete setup in TCM process context w/ TFO->new_cmd_map().
  1486. */
  1487. int transport_generic_handle_cdb_map(
  1488. struct se_cmd *cmd)
  1489. {
  1490. if (!cmd->se_lun) {
  1491. dump_stack();
  1492. pr_err("cmd->se_lun is NULL\n");
  1493. return -EINVAL;
  1494. }
  1495. transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
  1496. return 0;
  1497. }
  1498. EXPORT_SYMBOL(transport_generic_handle_cdb_map);
  1499. /* transport_generic_handle_data():
  1500. *
  1501. *
  1502. */
  1503. int transport_generic_handle_data(
  1504. struct se_cmd *cmd)
  1505. {
  1506. /*
  1507. * For the software fabric case, then we assume the nexus is being
  1508. * failed/shutdown when signals are pending from the kthread context
  1509. * caller, so we return a failure. For the HW target mode case running
  1510. * in interrupt code, the signal_pending() check is skipped.
  1511. */
  1512. if (!in_interrupt() && signal_pending(current))
  1513. return -EPERM;
  1514. /*
  1515. * If the received CDB has aleady been ABORTED by the generic
  1516. * target engine, we now call transport_check_aborted_status()
  1517. * to queue any delated TASK_ABORTED status for the received CDB to the
  1518. * fabric module as we are expecting no further incoming DATA OUT
  1519. * sequences at this point.
  1520. */
  1521. if (transport_check_aborted_status(cmd, 1) != 0)
  1522. return 0;
  1523. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
  1524. return 0;
  1525. }
  1526. EXPORT_SYMBOL(transport_generic_handle_data);
  1527. /* transport_generic_handle_tmr():
  1528. *
  1529. *
  1530. */
  1531. int transport_generic_handle_tmr(
  1532. struct se_cmd *cmd)
  1533. {
  1534. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
  1535. return 0;
  1536. }
  1537. EXPORT_SYMBOL(transport_generic_handle_tmr);
  1538. /*
  1539. * If the cmd is active, request it to be stopped and sleep until it
  1540. * has completed.
  1541. */
  1542. bool target_stop_cmd(struct se_cmd *cmd, unsigned long *flags)
  1543. {
  1544. bool was_active = false;
  1545. if (cmd->transport_state & CMD_T_BUSY) {
  1546. cmd->transport_state |= CMD_T_REQUEST_STOP;
  1547. spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
  1548. pr_debug("cmd %p waiting to complete\n", cmd);
  1549. wait_for_completion(&cmd->task_stop_comp);
  1550. pr_debug("cmd %p stopped successfully\n", cmd);
  1551. spin_lock_irqsave(&cmd->t_state_lock, *flags);
  1552. cmd->transport_state &= ~CMD_T_REQUEST_STOP;
  1553. cmd->transport_state &= ~CMD_T_BUSY;
  1554. was_active = true;
  1555. }
  1556. return was_active;
  1557. }
  1558. /*
  1559. * Handle SAM-esque emulation for generic transport request failures.
  1560. */
  1561. void transport_generic_request_failure(struct se_cmd *cmd)
  1562. {
  1563. int ret = 0;
  1564. pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
  1565. " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
  1566. cmd->t_task_cdb[0]);
  1567. pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
  1568. cmd->se_tfo->get_cmd_state(cmd),
  1569. cmd->t_state, cmd->scsi_sense_reason);
  1570. pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
  1571. (cmd->transport_state & CMD_T_ACTIVE) != 0,
  1572. (cmd->transport_state & CMD_T_STOP) != 0,
  1573. (cmd->transport_state & CMD_T_SENT) != 0);
  1574. /*
  1575. * For SAM Task Attribute emulation for failed struct se_cmd
  1576. */
  1577. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  1578. transport_complete_task_attr(cmd);
  1579. switch (cmd->scsi_sense_reason) {
  1580. case TCM_NON_EXISTENT_LUN:
  1581. case TCM_UNSUPPORTED_SCSI_OPCODE:
  1582. case TCM_INVALID_CDB_FIELD:
  1583. case TCM_INVALID_PARAMETER_LIST:
  1584. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  1585. case TCM_UNKNOWN_MODE_PAGE:
  1586. case TCM_WRITE_PROTECTED:
  1587. case TCM_CHECK_CONDITION_ABORT_CMD:
  1588. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  1589. case TCM_CHECK_CONDITION_NOT_READY:
  1590. break;
  1591. case TCM_RESERVATION_CONFLICT:
  1592. /*
  1593. * No SENSE Data payload for this case, set SCSI Status
  1594. * and queue the response to $FABRIC_MOD.
  1595. *
  1596. * Uses linux/include/scsi/scsi.h SAM status codes defs
  1597. */
  1598. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  1599. /*
  1600. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  1601. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  1602. * CONFLICT STATUS.
  1603. *
  1604. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  1605. */
  1606. if (cmd->se_sess &&
  1607. cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
  1608. core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
  1609. cmd->orig_fe_lun, 0x2C,
  1610. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  1611. ret = cmd->se_tfo->queue_status(cmd);
  1612. if (ret == -EAGAIN || ret == -ENOMEM)
  1613. goto queue_full;
  1614. goto check_stop;
  1615. default:
  1616. pr_err("Unknown transport error for CDB 0x%02x: %d\n",
  1617. cmd->t_task_cdb[0], cmd->scsi_sense_reason);
  1618. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1619. break;
  1620. }
  1621. /*
  1622. * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
  1623. * make the call to transport_send_check_condition_and_sense()
  1624. * directly. Otherwise expect the fabric to make the call to
  1625. * transport_send_check_condition_and_sense() after handling
  1626. * possible unsoliticied write data payloads.
  1627. */
  1628. ret = transport_send_check_condition_and_sense(cmd,
  1629. cmd->scsi_sense_reason, 0);
  1630. if (ret == -EAGAIN || ret == -ENOMEM)
  1631. goto queue_full;
  1632. check_stop:
  1633. transport_lun_remove_cmd(cmd);
  1634. if (!transport_cmd_check_stop_to_fabric(cmd))
  1635. ;
  1636. return;
  1637. queue_full:
  1638. cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
  1639. transport_handle_queue_full(cmd, cmd->se_dev);
  1640. }
  1641. EXPORT_SYMBOL(transport_generic_request_failure);
  1642. static inline u32 transport_lba_21(unsigned char *cdb)
  1643. {
  1644. return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
  1645. }
  1646. static inline u32 transport_lba_32(unsigned char *cdb)
  1647. {
  1648. return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1649. }
  1650. static inline unsigned long long transport_lba_64(unsigned char *cdb)
  1651. {
  1652. unsigned int __v1, __v2;
  1653. __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  1654. __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1655. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  1656. }
  1657. /*
  1658. * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
  1659. */
  1660. static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
  1661. {
  1662. unsigned int __v1, __v2;
  1663. __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
  1664. __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
  1665. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  1666. }
  1667. static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
  1668. {
  1669. unsigned long flags;
  1670. spin_lock_irqsave(&se_cmd->t_state_lock, flags);
  1671. se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
  1672. spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
  1673. }
  1674. /*
  1675. * Called from Fabric Module context from transport_execute_tasks()
  1676. *
  1677. * The return of this function determins if the tasks from struct se_cmd
  1678. * get added to the execution queue in transport_execute_tasks(),
  1679. * or are added to the delayed or ordered lists here.
  1680. */
  1681. static inline int transport_execute_task_attr(struct se_cmd *cmd)
  1682. {
  1683. if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  1684. return 1;
  1685. /*
  1686. * Check for the existence of HEAD_OF_QUEUE, and if true return 1
  1687. * to allow the passed struct se_cmd list of tasks to the front of the list.
  1688. */
  1689. if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  1690. pr_debug("Added HEAD_OF_QUEUE for CDB:"
  1691. " 0x%02x, se_ordered_id: %u\n",
  1692. cmd->t_task_cdb[0],
  1693. cmd->se_ordered_id);
  1694. return 1;
  1695. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  1696. atomic_inc(&cmd->se_dev->dev_ordered_sync);
  1697. smp_mb__after_atomic_inc();
  1698. pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
  1699. " list, se_ordered_id: %u\n",
  1700. cmd->t_task_cdb[0],
  1701. cmd->se_ordered_id);
  1702. /*
  1703. * Add ORDERED command to tail of execution queue if
  1704. * no other older commands exist that need to be
  1705. * completed first.
  1706. */
  1707. if (!atomic_read(&cmd->se_dev->simple_cmds))
  1708. return 1;
  1709. } else {
  1710. /*
  1711. * For SIMPLE and UNTAGGED Task Attribute commands
  1712. */
  1713. atomic_inc(&cmd->se_dev->simple_cmds);
  1714. smp_mb__after_atomic_inc();
  1715. }
  1716. /*
  1717. * Otherwise if one or more outstanding ORDERED task attribute exist,
  1718. * add the dormant task(s) built for the passed struct se_cmd to the
  1719. * execution queue and become in Active state for this struct se_device.
  1720. */
  1721. if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
  1722. /*
  1723. * Otherwise, add cmd w/ tasks to delayed cmd queue that
  1724. * will be drained upon completion of HEAD_OF_QUEUE task.
  1725. */
  1726. spin_lock(&cmd->se_dev->delayed_cmd_lock);
  1727. cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
  1728. list_add_tail(&cmd->se_delayed_node,
  1729. &cmd->se_dev->delayed_cmd_list);
  1730. spin_unlock(&cmd->se_dev->delayed_cmd_lock);
  1731. pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
  1732. " delayed CMD list, se_ordered_id: %u\n",
  1733. cmd->t_task_cdb[0], cmd->sam_task_attr,
  1734. cmd->se_ordered_id);
  1735. /*
  1736. * Return zero to let transport_execute_tasks() know
  1737. * not to add the delayed tasks to the execution list.
  1738. */
  1739. return 0;
  1740. }
  1741. /*
  1742. * Otherwise, no ORDERED task attributes exist..
  1743. */
  1744. return 1;
  1745. }
  1746. /*
  1747. * Called from fabric module context in transport_generic_new_cmd() and
  1748. * transport_generic_process_write()
  1749. */
  1750. static void transport_execute_tasks(struct se_cmd *cmd)
  1751. {
  1752. int add_tasks;
  1753. struct se_device *se_dev = cmd->se_dev;
  1754. /*
  1755. * Call transport_cmd_check_stop() to see if a fabric exception
  1756. * has occurred that prevents execution.
  1757. */
  1758. if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
  1759. /*
  1760. * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
  1761. * attribute for the tasks of the received struct se_cmd CDB
  1762. */
  1763. add_tasks = transport_execute_task_attr(cmd);
  1764. if (add_tasks) {
  1765. __transport_execute_tasks(se_dev, cmd);
  1766. return;
  1767. }
  1768. }
  1769. __transport_execute_tasks(se_dev, NULL);
  1770. }
  1771. static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *new_cmd)
  1772. {
  1773. int error;
  1774. struct se_cmd *cmd = NULL;
  1775. unsigned long flags;
  1776. check_depth:
  1777. spin_lock_irq(&dev->execute_task_lock);
  1778. if (new_cmd != NULL)
  1779. __target_add_to_execute_list(new_cmd);
  1780. if (list_empty(&dev->execute_list)) {
  1781. spin_unlock_irq(&dev->execute_task_lock);
  1782. return 0;
  1783. }
  1784. cmd = list_first_entry(&dev->execute_list, struct se_cmd, execute_list);
  1785. __target_remove_from_execute_list(cmd);
  1786. spin_unlock_irq(&dev->execute_task_lock);
  1787. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1788. cmd->transport_state |= CMD_T_BUSY;
  1789. cmd->transport_state |= CMD_T_SENT;
  1790. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1791. if (cmd->execute_cmd)
  1792. error = cmd->execute_cmd(cmd);
  1793. else {
  1794. error = dev->transport->execute_cmd(cmd, cmd->t_data_sg,
  1795. cmd->t_data_nents, cmd->data_direction);
  1796. }
  1797. if (error != 0) {
  1798. spin_lock_irqsave(&cmd->t_state_lock, flags);
  1799. cmd->transport_state &= ~CMD_T_BUSY;
  1800. cmd->transport_state &= ~CMD_T_SENT;
  1801. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  1802. transport_generic_request_failure(cmd);
  1803. }
  1804. new_cmd = NULL;
  1805. goto check_depth;
  1806. return 0;
  1807. }
  1808. static inline u32 transport_get_sectors_6(
  1809. unsigned char *cdb,
  1810. struct se_cmd *cmd,
  1811. int *ret)
  1812. {
  1813. struct se_device *dev = cmd->se_dev;
  1814. /*
  1815. * Assume TYPE_DISK for non struct se_device objects.
  1816. * Use 8-bit sector value.
  1817. */
  1818. if (!dev)
  1819. goto type_disk;
  1820. /*
  1821. * Use 24-bit allocation length for TYPE_TAPE.
  1822. */
  1823. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  1824. return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
  1825. /*
  1826. * Everything else assume TYPE_DISK Sector CDB location.
  1827. * Use 8-bit sector value. SBC-3 says:
  1828. *
  1829. * A TRANSFER LENGTH field set to zero specifies that 256
  1830. * logical blocks shall be written. Any other value
  1831. * specifies the number of logical blocks that shall be
  1832. * written.
  1833. */
  1834. type_disk:
  1835. return cdb[4] ? : 256;
  1836. }
  1837. static inline u32 transport_get_sectors_10(
  1838. unsigned char *cdb,
  1839. struct se_cmd *cmd,
  1840. int *ret)
  1841. {
  1842. struct se_device *dev = cmd->se_dev;
  1843. /*
  1844. * Assume TYPE_DISK for non struct se_device objects.
  1845. * Use 16-bit sector value.
  1846. */
  1847. if (!dev)
  1848. goto type_disk;
  1849. /*
  1850. * XXX_10 is not defined in SSC, throw an exception
  1851. */
  1852. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  1853. *ret = -EINVAL;
  1854. return 0;
  1855. }
  1856. /*
  1857. * Everything else assume TYPE_DISK Sector CDB location.
  1858. * Use 16-bit sector value.
  1859. */
  1860. type_disk:
  1861. return (u32)(cdb[7] << 8) + cdb[8];
  1862. }
  1863. static inline u32 transport_get_sectors_12(
  1864. unsigned char *cdb,
  1865. struct se_cmd *cmd,
  1866. int *ret)
  1867. {
  1868. struct se_device *dev = cmd->se_dev;
  1869. /*
  1870. * Assume TYPE_DISK for non struct se_device objects.
  1871. * Use 32-bit sector value.
  1872. */
  1873. if (!dev)
  1874. goto type_disk;
  1875. /*
  1876. * XXX_12 is not defined in SSC, throw an exception
  1877. */
  1878. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  1879. *ret = -EINVAL;
  1880. return 0;
  1881. }
  1882. /*
  1883. * Everything else assume TYPE_DISK Sector CDB location.
  1884. * Use 32-bit sector value.
  1885. */
  1886. type_disk:
  1887. return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
  1888. }
  1889. static inline u32 transport_get_sectors_16(
  1890. unsigned char *cdb,
  1891. struct se_cmd *cmd,
  1892. int *ret)
  1893. {
  1894. struct se_device *dev = cmd->se_dev;
  1895. /*
  1896. * Assume TYPE_DISK for non struct se_device objects.
  1897. * Use 32-bit sector value.
  1898. */
  1899. if (!dev)
  1900. goto type_disk;
  1901. /*
  1902. * Use 24-bit allocation length for TYPE_TAPE.
  1903. */
  1904. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  1905. return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
  1906. type_disk:
  1907. return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
  1908. (cdb[12] << 8) + cdb[13];
  1909. }
  1910. /*
  1911. * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
  1912. */
  1913. static inline u32 transport_get_sectors_32(
  1914. unsigned char *cdb,
  1915. struct se_cmd *cmd,
  1916. int *ret)
  1917. {
  1918. /*
  1919. * Assume TYPE_DISK for non struct se_device objects.
  1920. * Use 32-bit sector value.
  1921. */
  1922. return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
  1923. (cdb[30] << 8) + cdb[31];
  1924. }
  1925. static inline u32 transport_get_size(
  1926. u32 sectors,
  1927. unsigned char *cdb,
  1928. struct se_cmd *cmd)
  1929. {
  1930. struct se_device *dev = cmd->se_dev;
  1931. if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
  1932. if (cdb[1] & 1) { /* sectors */
  1933. return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
  1934. } else /* bytes */
  1935. return sectors;
  1936. }
  1937. pr_debug("Returning block_size: %u, sectors: %u == %u for"
  1938. " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size,
  1939. sectors, dev->se_sub_dev->se_dev_attrib.block_size * sectors,
  1940. dev->transport->name);
  1941. return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
  1942. }
  1943. static void transport_xor_callback(struct se_cmd *cmd)
  1944. {
  1945. unsigned char *buf, *addr;
  1946. struct scatterlist *sg;
  1947. unsigned int offset;
  1948. int i;
  1949. int count;
  1950. /*
  1951. * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
  1952. *
  1953. * 1) read the specified logical block(s);
  1954. * 2) transfer logical blocks from the data-out buffer;
  1955. * 3) XOR the logical blocks transferred from the data-out buffer with
  1956. * the logical blocks read, storing the resulting XOR data in a buffer;
  1957. * 4) if the DISABLE WRITE bit is set to zero, then write the logical
  1958. * blocks transferred from the data-out buffer; and
  1959. * 5) transfer the resulting XOR data to the data-in buffer.
  1960. */
  1961. buf = kmalloc(cmd->data_length, GFP_KERNEL);
  1962. if (!buf) {
  1963. pr_err("Unable to allocate xor_callback buf\n");
  1964. return;
  1965. }
  1966. /*
  1967. * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
  1968. * into the locally allocated *buf
  1969. */
  1970. sg_copy_to_buffer(cmd->t_data_sg,
  1971. cmd->t_data_nents,
  1972. buf,
  1973. cmd->data_length);
  1974. /*
  1975. * Now perform the XOR against the BIDI read memory located at
  1976. * cmd->t_mem_bidi_list
  1977. */
  1978. offset = 0;
  1979. for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
  1980. addr = kmap_atomic(sg_page(sg));
  1981. if (!addr)
  1982. goto out;
  1983. for (i = 0; i < sg->length; i++)
  1984. *(addr + sg->offset + i) ^= *(buf + offset + i);
  1985. offset += sg->length;
  1986. kunmap_atomic(addr);
  1987. }
  1988. out:
  1989. kfree(buf);
  1990. }
  1991. /*
  1992. * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
  1993. */
  1994. static int transport_get_sense_data(struct se_cmd *cmd)
  1995. {
  1996. unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
  1997. struct se_device *dev = cmd->se_dev;
  1998. unsigned long flags;
  1999. u32 offset = 0;
  2000. WARN_ON(!cmd->se_lun);
  2001. if (!dev)
  2002. return 0;
  2003. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2004. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  2005. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2006. return 0;
  2007. }
  2008. if (!(cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
  2009. goto out;
  2010. if (!dev->transport->get_sense_buffer) {
  2011. pr_err("dev->transport->get_sense_buffer is NULL\n");
  2012. goto out;
  2013. }
  2014. sense_buffer = dev->transport->get_sense_buffer(cmd);
  2015. if (!sense_buffer) {
  2016. pr_err("ITT 0x%08x cmd %p: Unable to locate"
  2017. " sense buffer for task with sense\n",
  2018. cmd->se_tfo->get_task_tag(cmd), cmd);
  2019. goto out;
  2020. }
  2021. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2022. offset = cmd->se_tfo->set_fabric_sense_len(cmd, TRANSPORT_SENSE_BUFFER);
  2023. memcpy(&buffer[offset], sense_buffer, TRANSPORT_SENSE_BUFFER);
  2024. /* Automatically padded */
  2025. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
  2026. pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x and sense\n",
  2027. dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status);
  2028. return 0;
  2029. out:
  2030. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2031. return -1;
  2032. }
  2033. static inline long long transport_dev_end_lba(struct se_device *dev)
  2034. {
  2035. return dev->transport->get_blocks(dev) + 1;
  2036. }
  2037. static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
  2038. {
  2039. struct se_device *dev = cmd->se_dev;
  2040. u32 sectors;
  2041. if (dev->transport->get_device_type(dev) != TYPE_DISK)
  2042. return 0;
  2043. sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
  2044. if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
  2045. pr_err("LBA: %llu Sectors: %u exceeds"
  2046. " transport_dev_end_lba(): %llu\n",
  2047. cmd->t_task_lba, sectors,
  2048. transport_dev_end_lba(dev));
  2049. return -EINVAL;
  2050. }
  2051. return 0;
  2052. }
  2053. static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
  2054. {
  2055. /*
  2056. * Determine if the received WRITE_SAME is used to for direct
  2057. * passthrough into Linux/SCSI with struct request via TCM/pSCSI
  2058. * or we are signaling the use of internal WRITE_SAME + UNMAP=1
  2059. * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
  2060. */
  2061. int passthrough = (dev->transport->transport_type ==
  2062. TRANSPORT_PLUGIN_PHBA_PDEV);
  2063. if (!passthrough) {
  2064. if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
  2065. pr_err("WRITE_SAME PBDATA and LBDATA"
  2066. " bits not supported for Block Discard"
  2067. " Emulation\n");
  2068. return -ENOSYS;
  2069. }
  2070. /*
  2071. * Currently for the emulated case we only accept
  2072. * tpws with the UNMAP=1 bit set.
  2073. */
  2074. if (!(flags[0] & 0x08)) {
  2075. pr_err("WRITE_SAME w/o UNMAP bit not"
  2076. " supported for Block Discard Emulation\n");
  2077. return -ENOSYS;
  2078. }
  2079. }
  2080. return 0;
  2081. }
  2082. /* transport_generic_cmd_sequencer():
  2083. *
  2084. * Generic Command Sequencer that should work for most DAS transport
  2085. * drivers.
  2086. *
  2087. * Called from target_setup_cmd_from_cdb() in the $FABRIC_MOD
  2088. * RX Thread.
  2089. *
  2090. * FIXME: Need to support other SCSI OPCODES where as well.
  2091. */
  2092. static int transport_generic_cmd_sequencer(
  2093. struct se_cmd *cmd,
  2094. unsigned char *cdb)
  2095. {
  2096. struct se_device *dev = cmd->se_dev;
  2097. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  2098. int ret = 0, sector_ret = 0, passthrough;
  2099. u32 sectors = 0, size = 0, pr_reg_type = 0;
  2100. u16 service_action;
  2101. u8 alua_ascq = 0;
  2102. /*
  2103. * Check for an existing UNIT ATTENTION condition
  2104. */
  2105. if (core_scsi3_ua_check(cmd, cdb) < 0) {
  2106. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2107. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
  2108. return -EINVAL;
  2109. }
  2110. /*
  2111. * Check status of Asymmetric Logical Unit Assignment port
  2112. */
  2113. ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
  2114. if (ret != 0) {
  2115. /*
  2116. * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
  2117. * The ALUA additional sense code qualifier (ASCQ) is determined
  2118. * by the ALUA primary or secondary access state..
  2119. */
  2120. if (ret > 0) {
  2121. pr_debug("[%s]: ALUA TG Port not available,"
  2122. " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
  2123. cmd->se_tfo->get_fabric_name(), alua_ascq);
  2124. transport_set_sense_codes(cmd, 0x04, alua_ascq);
  2125. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2126. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
  2127. return -EINVAL;
  2128. }
  2129. goto out_invalid_cdb_field;
  2130. }
  2131. /*
  2132. * Check status for SPC-3 Persistent Reservations
  2133. */
  2134. if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
  2135. if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
  2136. cmd, cdb, pr_reg_type) != 0) {
  2137. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2138. cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
  2139. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  2140. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2141. return -EBUSY;
  2142. }
  2143. /*
  2144. * This means the CDB is allowed for the SCSI Initiator port
  2145. * when said port is *NOT* holding the legacy SPC-2 or
  2146. * SPC-3 Persistent Reservation.
  2147. */
  2148. }
  2149. /*
  2150. * If we operate in passthrough mode we skip most CDB emulation and
  2151. * instead hand the commands down to the physical SCSI device.
  2152. */
  2153. passthrough =
  2154. (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
  2155. switch (cdb[0]) {
  2156. case READ_6:
  2157. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2158. if (sector_ret)
  2159. goto out_unsupported_cdb;
  2160. size = transport_get_size(sectors, cdb, cmd);
  2161. cmd->t_task_lba = transport_lba_21(cdb);
  2162. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2163. break;
  2164. case READ_10:
  2165. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2166. if (sector_ret)
  2167. goto out_unsupported_cdb;
  2168. size = transport_get_size(sectors, cdb, cmd);
  2169. cmd->t_task_lba = transport_lba_32(cdb);
  2170. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2171. break;
  2172. case READ_12:
  2173. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2174. if (sector_ret)
  2175. goto out_unsupported_cdb;
  2176. size = transport_get_size(sectors, cdb, cmd);
  2177. cmd->t_task_lba = transport_lba_32(cdb);
  2178. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2179. break;
  2180. case READ_16:
  2181. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2182. if (sector_ret)
  2183. goto out_unsupported_cdb;
  2184. size = transport_get_size(sectors, cdb, cmd);
  2185. cmd->t_task_lba = transport_lba_64(cdb);
  2186. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2187. break;
  2188. case WRITE_6:
  2189. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2190. if (sector_ret)
  2191. goto out_unsupported_cdb;
  2192. size = transport_get_size(sectors, cdb, cmd);
  2193. cmd->t_task_lba = transport_lba_21(cdb);
  2194. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2195. break;
  2196. case WRITE_10:
  2197. case WRITE_VERIFY:
  2198. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2199. if (sector_ret)
  2200. goto out_unsupported_cdb;
  2201. size = transport_get_size(sectors, cdb, cmd);
  2202. cmd->t_task_lba = transport_lba_32(cdb);
  2203. if (cdb[1] & 0x8)
  2204. cmd->se_cmd_flags |= SCF_FUA;
  2205. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2206. break;
  2207. case WRITE_12:
  2208. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2209. if (sector_ret)
  2210. goto out_unsupported_cdb;
  2211. size = transport_get_size(sectors, cdb, cmd);
  2212. cmd->t_task_lba = transport_lba_32(cdb);
  2213. if (cdb[1] & 0x8)
  2214. cmd->se_cmd_flags |= SCF_FUA;
  2215. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2216. break;
  2217. case WRITE_16:
  2218. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2219. if (sector_ret)
  2220. goto out_unsupported_cdb;
  2221. size = transport_get_size(sectors, cdb, cmd);
  2222. cmd->t_task_lba = transport_lba_64(cdb);
  2223. if (cdb[1] & 0x8)
  2224. cmd->se_cmd_flags |= SCF_FUA;
  2225. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2226. break;
  2227. case XDWRITEREAD_10:
  2228. if ((cmd->data_direction != DMA_TO_DEVICE) ||
  2229. !(cmd->se_cmd_flags & SCF_BIDI))
  2230. goto out_invalid_cdb_field;
  2231. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2232. if (sector_ret)
  2233. goto out_unsupported_cdb;
  2234. size = transport_get_size(sectors, cdb, cmd);
  2235. cmd->t_task_lba = transport_lba_32(cdb);
  2236. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2237. /*
  2238. * Do now allow BIDI commands for passthrough mode.
  2239. */
  2240. if (passthrough)
  2241. goto out_unsupported_cdb;
  2242. /*
  2243. * Setup BIDI XOR callback to be run after I/O completion.
  2244. */
  2245. cmd->transport_complete_callback = &transport_xor_callback;
  2246. if (cdb[1] & 0x8)
  2247. cmd->se_cmd_flags |= SCF_FUA;
  2248. break;
  2249. case VARIABLE_LENGTH_CMD:
  2250. service_action = get_unaligned_be16(&cdb[8]);
  2251. switch (service_action) {
  2252. case XDWRITEREAD_32:
  2253. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2254. if (sector_ret)
  2255. goto out_unsupported_cdb;
  2256. size = transport_get_size(sectors, cdb, cmd);
  2257. /*
  2258. * Use WRITE_32 and READ_32 opcodes for the emulated
  2259. * XDWRITE_READ_32 logic.
  2260. */
  2261. cmd->t_task_lba = transport_lba_64_ext(cdb);
  2262. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2263. /*
  2264. * Do now allow BIDI commands for passthrough mode.
  2265. */
  2266. if (passthrough)
  2267. goto out_unsupported_cdb;
  2268. /*
  2269. * Setup BIDI XOR callback to be run during after I/O
  2270. * completion.
  2271. */
  2272. cmd->transport_complete_callback = &transport_xor_callback;
  2273. if (cdb[1] & 0x8)
  2274. cmd->se_cmd_flags |= SCF_FUA;
  2275. break;
  2276. case WRITE_SAME_32:
  2277. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2278. if (sector_ret)
  2279. goto out_unsupported_cdb;
  2280. if (sectors)
  2281. size = transport_get_size(1, cdb, cmd);
  2282. else {
  2283. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
  2284. " supported\n");
  2285. goto out_invalid_cdb_field;
  2286. }
  2287. cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
  2288. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2289. if (target_check_write_same_discard(&cdb[10], dev) < 0)
  2290. goto out_unsupported_cdb;
  2291. if (!passthrough)
  2292. cmd->execute_cmd = target_emulate_write_same;
  2293. break;
  2294. default:
  2295. pr_err("VARIABLE_LENGTH_CMD service action"
  2296. " 0x%04x not supported\n", service_action);
  2297. goto out_unsupported_cdb;
  2298. }
  2299. break;
  2300. case MAINTENANCE_IN:
  2301. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2302. /* MAINTENANCE_IN from SCC-2 */
  2303. /*
  2304. * Check for emulated MI_REPORT_TARGET_PGS.
  2305. */
  2306. if (cdb[1] == MI_REPORT_TARGET_PGS &&
  2307. su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
  2308. cmd->execute_cmd =
  2309. target_emulate_report_target_port_groups;
  2310. }
  2311. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2312. (cdb[8] << 8) | cdb[9];
  2313. } else {
  2314. /* GPCMD_SEND_KEY from multi media commands */
  2315. size = (cdb[8] << 8) + cdb[9];
  2316. }
  2317. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2318. break;
  2319. case MODE_SELECT:
  2320. size = cdb[4];
  2321. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2322. break;
  2323. case MODE_SELECT_10:
  2324. size = (cdb[7] << 8) + cdb[8];
  2325. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2326. break;
  2327. case MODE_SENSE:
  2328. size = cdb[4];
  2329. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2330. if (!passthrough)
  2331. cmd->execute_cmd = target_emulate_modesense;
  2332. break;
  2333. case MODE_SENSE_10:
  2334. size = (cdb[7] << 8) + cdb[8];
  2335. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2336. if (!passthrough)
  2337. cmd->execute_cmd = target_emulate_modesense;
  2338. break;
  2339. case GPCMD_READ_BUFFER_CAPACITY:
  2340. case GPCMD_SEND_OPC:
  2341. case LOG_SELECT:
  2342. case LOG_SENSE:
  2343. size = (cdb[7] << 8) + cdb[8];
  2344. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2345. break;
  2346. case READ_BLOCK_LIMITS:
  2347. size = READ_BLOCK_LEN;
  2348. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2349. break;
  2350. case GPCMD_GET_CONFIGURATION:
  2351. case GPCMD_READ_FORMAT_CAPACITIES:
  2352. case GPCMD_READ_DISC_INFO:
  2353. case GPCMD_READ_TRACK_RZONE_INFO:
  2354. size = (cdb[7] << 8) + cdb[8];
  2355. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2356. break;
  2357. case PERSISTENT_RESERVE_IN:
  2358. if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
  2359. cmd->execute_cmd = target_scsi3_emulate_pr_in;
  2360. size = (cdb[7] << 8) + cdb[8];
  2361. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2362. break;
  2363. case PERSISTENT_RESERVE_OUT:
  2364. if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
  2365. cmd->execute_cmd = target_scsi3_emulate_pr_out;
  2366. size = (cdb[7] << 8) + cdb[8];
  2367. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2368. break;
  2369. case GPCMD_MECHANISM_STATUS:
  2370. case GPCMD_READ_DVD_STRUCTURE:
  2371. size = (cdb[8] << 8) + cdb[9];
  2372. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2373. break;
  2374. case READ_POSITION:
  2375. size = READ_POSITION_LEN;
  2376. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2377. break;
  2378. case MAINTENANCE_OUT:
  2379. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  2380. /* MAINTENANCE_OUT from SCC-2
  2381. *
  2382. * Check for emulated MO_SET_TARGET_PGS.
  2383. */
  2384. if (cdb[1] == MO_SET_TARGET_PGS &&
  2385. su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
  2386. cmd->execute_cmd =
  2387. target_emulate_set_target_port_groups;
  2388. }
  2389. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2390. (cdb[8] << 8) | cdb[9];
  2391. } else {
  2392. /* GPCMD_REPORT_KEY from multi media commands */
  2393. size = (cdb[8] << 8) + cdb[9];
  2394. }
  2395. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2396. break;
  2397. case INQUIRY:
  2398. size = (cdb[3] << 8) + cdb[4];
  2399. /*
  2400. * Do implict HEAD_OF_QUEUE processing for INQUIRY.
  2401. * See spc4r17 section 5.3
  2402. */
  2403. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2404. cmd->sam_task_attr = MSG_HEAD_TAG;
  2405. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2406. if (!passthrough)
  2407. cmd->execute_cmd = target_emulate_inquiry;
  2408. break;
  2409. case READ_BUFFER:
  2410. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2411. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2412. break;
  2413. case READ_CAPACITY:
  2414. size = READ_CAP_LEN;
  2415. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2416. if (!passthrough)
  2417. cmd->execute_cmd = target_emulate_readcapacity;
  2418. break;
  2419. case READ_MEDIA_SERIAL_NUMBER:
  2420. case SECURITY_PROTOCOL_IN:
  2421. case SECURITY_PROTOCOL_OUT:
  2422. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  2423. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2424. break;
  2425. case SERVICE_ACTION_IN:
  2426. switch (cmd->t_task_cdb[1] & 0x1f) {
  2427. case SAI_READ_CAPACITY_16:
  2428. if (!passthrough)
  2429. cmd->execute_cmd =
  2430. target_emulate_readcapacity_16;
  2431. break;
  2432. default:
  2433. if (passthrough)
  2434. break;
  2435. pr_err("Unsupported SA: 0x%02x\n",
  2436. cmd->t_task_cdb[1] & 0x1f);
  2437. goto out_invalid_cdb_field;
  2438. }
  2439. /*FALLTHROUGH*/
  2440. case ACCESS_CONTROL_IN:
  2441. case ACCESS_CONTROL_OUT:
  2442. case EXTENDED_COPY:
  2443. case READ_ATTRIBUTE:
  2444. case RECEIVE_COPY_RESULTS:
  2445. case WRITE_ATTRIBUTE:
  2446. size = (cdb[10] << 24) | (cdb[11] << 16) |
  2447. (cdb[12] << 8) | cdb[13];
  2448. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2449. break;
  2450. case RECEIVE_DIAGNOSTIC:
  2451. case SEND_DIAGNOSTIC:
  2452. size = (cdb[3] << 8) | cdb[4];
  2453. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2454. break;
  2455. /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
  2456. #if 0
  2457. case GPCMD_READ_CD:
  2458. sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2459. size = (2336 * sectors);
  2460. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2461. break;
  2462. #endif
  2463. case READ_TOC:
  2464. size = cdb[8];
  2465. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2466. break;
  2467. case REQUEST_SENSE:
  2468. size = cdb[4];
  2469. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2470. if (!passthrough)
  2471. cmd->execute_cmd = target_emulate_request_sense;
  2472. break;
  2473. case READ_ELEMENT_STATUS:
  2474. size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
  2475. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2476. break;
  2477. case WRITE_BUFFER:
  2478. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  2479. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2480. break;
  2481. case RESERVE:
  2482. case RESERVE_10:
  2483. /*
  2484. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  2485. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  2486. */
  2487. if (cdb[0] == RESERVE_10)
  2488. size = (cdb[7] << 8) | cdb[8];
  2489. else
  2490. size = cmd->data_length;
  2491. /*
  2492. * Setup the legacy emulated handler for SPC-2 and
  2493. * >= SPC-3 compatible reservation handling (CRH=1)
  2494. * Otherwise, we assume the underlying SCSI logic is
  2495. * is running in SPC_PASSTHROUGH, and wants reservations
  2496. * emulation disabled.
  2497. */
  2498. if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
  2499. cmd->execute_cmd = target_scsi2_reservation_reserve;
  2500. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2501. break;
  2502. case RELEASE:
  2503. case RELEASE_10:
  2504. /*
  2505. * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
  2506. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  2507. */
  2508. if (cdb[0] == RELEASE_10)
  2509. size = (cdb[7] << 8) | cdb[8];
  2510. else
  2511. size = cmd->data_length;
  2512. if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
  2513. cmd->execute_cmd = target_scsi2_reservation_release;
  2514. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2515. break;
  2516. case SYNCHRONIZE_CACHE:
  2517. case SYNCHRONIZE_CACHE_16:
  2518. /*
  2519. * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
  2520. */
  2521. if (cdb[0] == SYNCHRONIZE_CACHE) {
  2522. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2523. cmd->t_task_lba = transport_lba_32(cdb);
  2524. } else {
  2525. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2526. cmd->t_task_lba = transport_lba_64(cdb);
  2527. }
  2528. if (sector_ret)
  2529. goto out_unsupported_cdb;
  2530. size = transport_get_size(sectors, cdb, cmd);
  2531. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2532. if (passthrough)
  2533. break;
  2534. /*
  2535. * Check to ensure that LBA + Range does not exceed past end of
  2536. * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
  2537. */
  2538. if ((cmd->t_task_lba != 0) || (sectors != 0)) {
  2539. if (transport_cmd_get_valid_sectors(cmd) < 0)
  2540. goto out_invalid_cdb_field;
  2541. }
  2542. cmd->execute_cmd = target_emulate_synchronize_cache;
  2543. break;
  2544. case UNMAP:
  2545. size = get_unaligned_be16(&cdb[7]);
  2546. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2547. if (!passthrough)
  2548. cmd->execute_cmd = target_emulate_unmap;
  2549. break;
  2550. case WRITE_SAME_16:
  2551. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2552. if (sector_ret)
  2553. goto out_unsupported_cdb;
  2554. if (sectors)
  2555. size = transport_get_size(1, cdb, cmd);
  2556. else {
  2557. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
  2558. goto out_invalid_cdb_field;
  2559. }
  2560. cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
  2561. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2562. if (target_check_write_same_discard(&cdb[1], dev) < 0)
  2563. goto out_unsupported_cdb;
  2564. if (!passthrough)
  2565. cmd->execute_cmd = target_emulate_write_same;
  2566. break;
  2567. case WRITE_SAME:
  2568. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2569. if (sector_ret)
  2570. goto out_unsupported_cdb;
  2571. if (sectors)
  2572. size = transport_get_size(1, cdb, cmd);
  2573. else {
  2574. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
  2575. goto out_invalid_cdb_field;
  2576. }
  2577. cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
  2578. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2579. /*
  2580. * Follow sbcr26 with WRITE_SAME (10) and check for the existence
  2581. * of byte 1 bit 3 UNMAP instead of original reserved field
  2582. */
  2583. if (target_check_write_same_discard(&cdb[1], dev) < 0)
  2584. goto out_unsupported_cdb;
  2585. if (!passthrough)
  2586. cmd->execute_cmd = target_emulate_write_same;
  2587. break;
  2588. case ALLOW_MEDIUM_REMOVAL:
  2589. case ERASE:
  2590. case REZERO_UNIT:
  2591. case SEEK_10:
  2592. case SPACE:
  2593. case START_STOP:
  2594. case TEST_UNIT_READY:
  2595. case VERIFY:
  2596. case WRITE_FILEMARKS:
  2597. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2598. if (!passthrough)
  2599. cmd->execute_cmd = target_emulate_noop;
  2600. break;
  2601. case GPCMD_CLOSE_TRACK:
  2602. case INITIALIZE_ELEMENT_STATUS:
  2603. case GPCMD_LOAD_UNLOAD:
  2604. case GPCMD_SET_SPEED:
  2605. case MOVE_MEDIUM:
  2606. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  2607. break;
  2608. case REPORT_LUNS:
  2609. cmd->execute_cmd = target_report_luns;
  2610. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  2611. /*
  2612. * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
  2613. * See spc4r17 section 5.3
  2614. */
  2615. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2616. cmd->sam_task_attr = MSG_HEAD_TAG;
  2617. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2618. break;
  2619. case GET_EVENT_STATUS_NOTIFICATION:
  2620. size = (cdb[7] << 8) | cdb[8];
  2621. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2622. break;
  2623. default:
  2624. pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
  2625. " 0x%02x, sending CHECK_CONDITION.\n",
  2626. cmd->se_tfo->get_fabric_name(), cdb[0]);
  2627. goto out_unsupported_cdb;
  2628. }
  2629. if (cmd->unknown_data_length)
  2630. cmd->data_length = size;
  2631. if (size != cmd->data_length) {
  2632. pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
  2633. " %u does not match SCSI CDB Length: %u for SAM Opcode:"
  2634. " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
  2635. cmd->data_length, size, cdb[0]);
  2636. cmd->cmd_spdtl = size;
  2637. if (cmd->data_direction == DMA_TO_DEVICE) {
  2638. pr_err("Rejecting underflow/overflow"
  2639. " WRITE data\n");
  2640. goto out_invalid_cdb_field;
  2641. }
  2642. /*
  2643. * Reject READ_* or WRITE_* with overflow/underflow for
  2644. * type SCF_SCSI_DATA_SG_IO_CDB.
  2645. */
  2646. if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
  2647. pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
  2648. " CDB on non 512-byte sector setup subsystem"
  2649. " plugin: %s\n", dev->transport->name);
  2650. /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
  2651. goto out_invalid_cdb_field;
  2652. }
  2653. if (size > cmd->data_length) {
  2654. cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
  2655. cmd->residual_count = (size - cmd->data_length);
  2656. } else {
  2657. cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  2658. cmd->residual_count = (cmd->data_length - size);
  2659. }
  2660. cmd->data_length = size;
  2661. }
  2662. if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
  2663. if (sectors > su_dev->se_dev_attrib.fabric_max_sectors) {
  2664. printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
  2665. " big sectors %u exceeds fabric_max_sectors:"
  2666. " %u\n", cdb[0], sectors,
  2667. su_dev->se_dev_attrib.fabric_max_sectors);
  2668. goto out_invalid_cdb_field;
  2669. }
  2670. if (sectors > su_dev->se_dev_attrib.hw_max_sectors) {
  2671. printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
  2672. " big sectors %u exceeds backend hw_max_sectors:"
  2673. " %u\n", cdb[0], sectors,
  2674. su_dev->se_dev_attrib.hw_max_sectors);
  2675. goto out_invalid_cdb_field;
  2676. }
  2677. }
  2678. /* reject any command that we don't have a handler for */
  2679. if (!(passthrough || cmd->execute_cmd ||
  2680. (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
  2681. goto out_unsupported_cdb;
  2682. transport_set_supported_SAM_opcode(cmd);
  2683. return ret;
  2684. out_unsupported_cdb:
  2685. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2686. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  2687. return -EINVAL;
  2688. out_invalid_cdb_field:
  2689. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2690. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  2691. return -EINVAL;
  2692. }
  2693. /*
  2694. * Called from I/O completion to determine which dormant/delayed
  2695. * and ordered cmds need to have their tasks added to the execution queue.
  2696. */
  2697. static void transport_complete_task_attr(struct se_cmd *cmd)
  2698. {
  2699. struct se_device *dev = cmd->se_dev;
  2700. struct se_cmd *cmd_p, *cmd_tmp;
  2701. int new_active_tasks = 0;
  2702. if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
  2703. atomic_dec(&dev->simple_cmds);
  2704. smp_mb__after_atomic_dec();
  2705. dev->dev_cur_ordered_id++;
  2706. pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
  2707. " SIMPLE: %u\n", dev->dev_cur_ordered_id,
  2708. cmd->se_ordered_id);
  2709. } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
  2710. dev->dev_cur_ordered_id++;
  2711. pr_debug("Incremented dev_cur_ordered_id: %u for"
  2712. " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
  2713. cmd->se_ordered_id);
  2714. } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
  2715. atomic_dec(&dev->dev_ordered_sync);
  2716. smp_mb__after_atomic_dec();
  2717. dev->dev_cur_ordered_id++;
  2718. pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
  2719. " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
  2720. }
  2721. /*
  2722. * Process all commands up to the last received
  2723. * ORDERED task attribute which requires another blocking
  2724. * boundary
  2725. */
  2726. spin_lock(&dev->delayed_cmd_lock);
  2727. list_for_each_entry_safe(cmd_p, cmd_tmp,
  2728. &dev->delayed_cmd_list, se_delayed_node) {
  2729. list_del(&cmd_p->se_delayed_node);
  2730. spin_unlock(&dev->delayed_cmd_lock);
  2731. pr_debug("Calling add_tasks() for"
  2732. " cmd_p: 0x%02x Task Attr: 0x%02x"
  2733. " Dormant -> Active, se_ordered_id: %u\n",
  2734. cmd_p->t_task_cdb[0],
  2735. cmd_p->sam_task_attr, cmd_p->se_ordered_id);
  2736. target_add_to_execute_list(cmd_p);
  2737. new_active_tasks++;
  2738. spin_lock(&dev->delayed_cmd_lock);
  2739. if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
  2740. break;
  2741. }
  2742. spin_unlock(&dev->delayed_cmd_lock);
  2743. /*
  2744. * If new tasks have become active, wake up the transport thread
  2745. * to do the processing of the Active tasks.
  2746. */
  2747. if (new_active_tasks != 0)
  2748. wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
  2749. }
  2750. static void transport_complete_qf(struct se_cmd *cmd)
  2751. {
  2752. int ret = 0;
  2753. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2754. transport_complete_task_attr(cmd);
  2755. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  2756. ret = cmd->se_tfo->queue_status(cmd);
  2757. if (ret)
  2758. goto out;
  2759. }
  2760. switch (cmd->data_direction) {
  2761. case DMA_FROM_DEVICE:
  2762. ret = cmd->se_tfo->queue_data_in(cmd);
  2763. break;
  2764. case DMA_TO_DEVICE:
  2765. if (cmd->t_bidi_data_sg) {
  2766. ret = cmd->se_tfo->queue_data_in(cmd);
  2767. if (ret < 0)
  2768. break;
  2769. }
  2770. /* Fall through for DMA_TO_DEVICE */
  2771. case DMA_NONE:
  2772. ret = cmd->se_tfo->queue_status(cmd);
  2773. break;
  2774. default:
  2775. break;
  2776. }
  2777. out:
  2778. if (ret < 0) {
  2779. transport_handle_queue_full(cmd, cmd->se_dev);
  2780. return;
  2781. }
  2782. transport_lun_remove_cmd(cmd);
  2783. transport_cmd_check_stop_to_fabric(cmd);
  2784. }
  2785. static void transport_handle_queue_full(
  2786. struct se_cmd *cmd,
  2787. struct se_device *dev)
  2788. {
  2789. spin_lock_irq(&dev->qf_cmd_lock);
  2790. list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
  2791. atomic_inc(&dev->dev_qf_count);
  2792. smp_mb__after_atomic_inc();
  2793. spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
  2794. schedule_work(&cmd->se_dev->qf_work_queue);
  2795. }
  2796. static void target_complete_ok_work(struct work_struct *work)
  2797. {
  2798. struct se_cmd *cmd = container_of(work, struct se_cmd, work);
  2799. int reason = 0, ret;
  2800. /*
  2801. * Check if we need to move delayed/dormant tasks from cmds on the
  2802. * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
  2803. * Attribute.
  2804. */
  2805. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  2806. transport_complete_task_attr(cmd);
  2807. /*
  2808. * Check to schedule QUEUE_FULL work, or execute an existing
  2809. * cmd->transport_qf_callback()
  2810. */
  2811. if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
  2812. schedule_work(&cmd->se_dev->qf_work_queue);
  2813. /*
  2814. * Check if we need to retrieve a sense buffer from
  2815. * the struct se_cmd in question.
  2816. */
  2817. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  2818. if (transport_get_sense_data(cmd) < 0)
  2819. reason = TCM_NON_EXISTENT_LUN;
  2820. if (cmd->scsi_status) {
  2821. ret = transport_send_check_condition_and_sense(
  2822. cmd, reason, 1);
  2823. if (ret == -EAGAIN || ret == -ENOMEM)
  2824. goto queue_full;
  2825. transport_lun_remove_cmd(cmd);
  2826. transport_cmd_check_stop_to_fabric(cmd);
  2827. return;
  2828. }
  2829. }
  2830. /*
  2831. * Check for a callback, used by amongst other things
  2832. * XDWRITE_READ_10 emulation.
  2833. */
  2834. if (cmd->transport_complete_callback)
  2835. cmd->transport_complete_callback(cmd);
  2836. switch (cmd->data_direction) {
  2837. case DMA_FROM_DEVICE:
  2838. spin_lock(&cmd->se_lun->lun_sep_lock);
  2839. if (cmd->se_lun->lun_sep) {
  2840. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  2841. cmd->data_length;
  2842. }
  2843. spin_unlock(&cmd->se_lun->lun_sep_lock);
  2844. ret = cmd->se_tfo->queue_data_in(cmd);
  2845. if (ret == -EAGAIN || ret == -ENOMEM)
  2846. goto queue_full;
  2847. break;
  2848. case DMA_TO_DEVICE:
  2849. spin_lock(&cmd->se_lun->lun_sep_lock);
  2850. if (cmd->se_lun->lun_sep) {
  2851. cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
  2852. cmd->data_length;
  2853. }
  2854. spin_unlock(&cmd->se_lun->lun_sep_lock);
  2855. /*
  2856. * Check if we need to send READ payload for BIDI-COMMAND
  2857. */
  2858. if (cmd->t_bidi_data_sg) {
  2859. spin_lock(&cmd->se_lun->lun_sep_lock);
  2860. if (cmd->se_lun->lun_sep) {
  2861. cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
  2862. cmd->data_length;
  2863. }
  2864. spin_unlock(&cmd->se_lun->lun_sep_lock);
  2865. ret = cmd->se_tfo->queue_data_in(cmd);
  2866. if (ret == -EAGAIN || ret == -ENOMEM)
  2867. goto queue_full;
  2868. break;
  2869. }
  2870. /* Fall through for DMA_TO_DEVICE */
  2871. case DMA_NONE:
  2872. ret = cmd->se_tfo->queue_status(cmd);
  2873. if (ret == -EAGAIN || ret == -ENOMEM)
  2874. goto queue_full;
  2875. break;
  2876. default:
  2877. break;
  2878. }
  2879. transport_lun_remove_cmd(cmd);
  2880. transport_cmd_check_stop_to_fabric(cmd);
  2881. return;
  2882. queue_full:
  2883. pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
  2884. " data_direction: %d\n", cmd, cmd->data_direction);
  2885. cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
  2886. transport_handle_queue_full(cmd, cmd->se_dev);
  2887. }
  2888. static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
  2889. {
  2890. struct scatterlist *sg;
  2891. int count;
  2892. for_each_sg(sgl, sg, nents, count)
  2893. __free_page(sg_page(sg));
  2894. kfree(sgl);
  2895. }
  2896. static inline void transport_free_pages(struct se_cmd *cmd)
  2897. {
  2898. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
  2899. return;
  2900. transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
  2901. cmd->t_data_sg = NULL;
  2902. cmd->t_data_nents = 0;
  2903. transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
  2904. cmd->t_bidi_data_sg = NULL;
  2905. cmd->t_bidi_data_nents = 0;
  2906. }
  2907. /**
  2908. * transport_release_cmd - free a command
  2909. * @cmd: command to free
  2910. *
  2911. * This routine unconditionally frees a command, and reference counting
  2912. * or list removal must be done in the caller.
  2913. */
  2914. static void transport_release_cmd(struct se_cmd *cmd)
  2915. {
  2916. BUG_ON(!cmd->se_tfo);
  2917. if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
  2918. core_tmr_release_req(cmd->se_tmr_req);
  2919. if (cmd->t_task_cdb != cmd->__t_task_cdb)
  2920. kfree(cmd->t_task_cdb);
  2921. /*
  2922. * If this cmd has been setup with target_get_sess_cmd(), drop
  2923. * the kref and call ->release_cmd() in kref callback.
  2924. */
  2925. if (cmd->check_release != 0) {
  2926. target_put_sess_cmd(cmd->se_sess, cmd);
  2927. return;
  2928. }
  2929. cmd->se_tfo->release_cmd(cmd);
  2930. }
  2931. /**
  2932. * transport_put_cmd - release a reference to a command
  2933. * @cmd: command to release
  2934. *
  2935. * This routine releases our reference to the command and frees it if possible.
  2936. */
  2937. static void transport_put_cmd(struct se_cmd *cmd)
  2938. {
  2939. unsigned long flags;
  2940. spin_lock_irqsave(&cmd->t_state_lock, flags);
  2941. if (atomic_read(&cmd->t_fe_count)) {
  2942. if (!atomic_dec_and_test(&cmd->t_fe_count))
  2943. goto out_busy;
  2944. }
  2945. if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
  2946. cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
  2947. target_remove_from_state_list(cmd);
  2948. }
  2949. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2950. transport_free_pages(cmd);
  2951. transport_release_cmd(cmd);
  2952. return;
  2953. out_busy:
  2954. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  2955. }
  2956. /*
  2957. * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
  2958. * allocating in the core.
  2959. * @cmd: Associated se_cmd descriptor
  2960. * @mem: SGL style memory for TCM WRITE / READ
  2961. * @sg_mem_num: Number of SGL elements
  2962. * @mem_bidi_in: SGL style memory for TCM BIDI READ
  2963. * @sg_mem_bidi_num: Number of BIDI READ SGL elements
  2964. *
  2965. * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
  2966. * of parameters.
  2967. */
  2968. int transport_generic_map_mem_to_cmd(
  2969. struct se_cmd *cmd,
  2970. struct scatterlist *sgl,
  2971. u32 sgl_count,
  2972. struct scatterlist *sgl_bidi,
  2973. u32 sgl_bidi_count)
  2974. {
  2975. if (!sgl || !sgl_count)
  2976. return 0;
  2977. if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
  2978. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
  2979. /*
  2980. * Reject SCSI data overflow with map_mem_to_cmd() as incoming
  2981. * scatterlists already have been set to follow what the fabric
  2982. * passes for the original expected data transfer length.
  2983. */
  2984. if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
  2985. pr_warn("Rejecting SCSI DATA overflow for fabric using"
  2986. " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
  2987. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2988. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  2989. return -EINVAL;
  2990. }
  2991. cmd->t_data_sg = sgl;
  2992. cmd->t_data_nents = sgl_count;
  2993. if (sgl_bidi && sgl_bidi_count) {
  2994. cmd->t_bidi_data_sg = sgl_bidi;
  2995. cmd->t_bidi_data_nents = sgl_bidi_count;
  2996. }
  2997. cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  2998. }
  2999. return 0;
  3000. }
  3001. EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
  3002. void *transport_kmap_data_sg(struct se_cmd *cmd)
  3003. {
  3004. struct scatterlist *sg = cmd->t_data_sg;
  3005. struct page **pages;
  3006. int i;
  3007. BUG_ON(!sg);
  3008. /*
  3009. * We need to take into account a possible offset here for fabrics like
  3010. * tcm_loop who may be using a contig buffer from the SCSI midlayer for
  3011. * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
  3012. */
  3013. if (!cmd->t_data_nents)
  3014. return NULL;
  3015. else if (cmd->t_data_nents == 1)
  3016. return kmap(sg_page(sg)) + sg->offset;
  3017. /* >1 page. use vmap */
  3018. pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
  3019. if (!pages)
  3020. return NULL;
  3021. /* convert sg[] to pages[] */
  3022. for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
  3023. pages[i] = sg_page(sg);
  3024. }
  3025. cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
  3026. kfree(pages);
  3027. if (!cmd->t_data_vmap)
  3028. return NULL;
  3029. return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
  3030. }
  3031. EXPORT_SYMBOL(transport_kmap_data_sg);
  3032. void transport_kunmap_data_sg(struct se_cmd *cmd)
  3033. {
  3034. if (!cmd->t_data_nents) {
  3035. return;
  3036. } else if (cmd->t_data_nents == 1) {
  3037. kunmap(sg_page(cmd->t_data_sg));
  3038. return;
  3039. }
  3040. vunmap(cmd->t_data_vmap);
  3041. cmd->t_data_vmap = NULL;
  3042. }
  3043. EXPORT_SYMBOL(transport_kunmap_data_sg);
  3044. static int
  3045. transport_generic_get_mem(struct se_cmd *cmd)
  3046. {
  3047. u32 length = cmd->data_length;
  3048. unsigned int nents;
  3049. struct page *page;
  3050. gfp_t zero_flag;
  3051. int i = 0;
  3052. nents = DIV_ROUND_UP(length, PAGE_SIZE);
  3053. cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
  3054. if (!cmd->t_data_sg)
  3055. return -ENOMEM;
  3056. cmd->t_data_nents = nents;
  3057. sg_init_table(cmd->t_data_sg, nents);
  3058. zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB ? 0 : __GFP_ZERO;
  3059. while (length) {
  3060. u32 page_len = min_t(u32, length, PAGE_SIZE);
  3061. page = alloc_page(GFP_KERNEL | zero_flag);
  3062. if (!page)
  3063. goto out;
  3064. sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
  3065. length -= page_len;
  3066. i++;
  3067. }
  3068. return 0;
  3069. out:
  3070. while (i >= 0) {
  3071. __free_page(sg_page(&cmd->t_data_sg[i]));
  3072. i--;
  3073. }
  3074. kfree(cmd->t_data_sg);
  3075. cmd->t_data_sg = NULL;
  3076. return -ENOMEM;
  3077. }
  3078. /*
  3079. * Allocate any required resources to execute the command. For writes we
  3080. * might not have the payload yet, so notify the fabric via a call to
  3081. * ->write_pending instead. Otherwise place it on the execution queue.
  3082. */
  3083. int transport_generic_new_cmd(struct se_cmd *cmd)
  3084. {
  3085. struct se_device *dev = cmd->se_dev;
  3086. int ret = 0;
  3087. /*
  3088. * Determine is the TCM fabric module has already allocated physical
  3089. * memory, and is directly calling transport_generic_map_mem_to_cmd()
  3090. * beforehand.
  3091. */
  3092. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
  3093. cmd->data_length) {
  3094. ret = transport_generic_get_mem(cmd);
  3095. if (ret < 0)
  3096. goto out_fail;
  3097. }
  3098. /* Workaround for handling zero-length control CDBs */
  3099. if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
  3100. !cmd->data_length) {
  3101. spin_lock_irq(&cmd->t_state_lock);
  3102. cmd->t_state = TRANSPORT_COMPLETE;
  3103. cmd->transport_state |= CMD_T_ACTIVE;
  3104. spin_unlock_irq(&cmd->t_state_lock);
  3105. if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
  3106. u8 ua_asc = 0, ua_ascq = 0;
  3107. core_scsi3_ua_clear_for_request_sense(cmd,
  3108. &ua_asc, &ua_ascq);
  3109. }
  3110. INIT_WORK(&cmd->work, target_complete_ok_work);
  3111. queue_work(target_completion_wq, &cmd->work);
  3112. return 0;
  3113. }
  3114. if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
  3115. struct se_dev_attrib *attr = &dev->se_sub_dev->se_dev_attrib;
  3116. if (transport_cmd_get_valid_sectors(cmd) < 0)
  3117. return -EINVAL;
  3118. BUG_ON(cmd->data_length % attr->block_size);
  3119. BUG_ON(DIV_ROUND_UP(cmd->data_length, attr->block_size) >
  3120. attr->hw_max_sectors);
  3121. }
  3122. atomic_inc(&cmd->t_fe_count);
  3123. /*
  3124. * For WRITEs, let the fabric know its buffer is ready.
  3125. *
  3126. * The command will be added to the execution queue after its write
  3127. * data has arrived.
  3128. */
  3129. if (cmd->data_direction == DMA_TO_DEVICE) {
  3130. target_add_to_state_list(cmd);
  3131. return transport_generic_write_pending(cmd);
  3132. }
  3133. /*
  3134. * Everything else but a WRITE, add the command to the execution queue.
  3135. */
  3136. transport_execute_tasks(cmd);
  3137. return 0;
  3138. out_fail:
  3139. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3140. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3141. return -EINVAL;
  3142. }
  3143. EXPORT_SYMBOL(transport_generic_new_cmd);
  3144. /* transport_generic_process_write():
  3145. *
  3146. *
  3147. */
  3148. void transport_generic_process_write(struct se_cmd *cmd)
  3149. {
  3150. transport_execute_tasks(cmd);
  3151. }
  3152. EXPORT_SYMBOL(transport_generic_process_write);
  3153. static void transport_write_pending_qf(struct se_cmd *cmd)
  3154. {
  3155. int ret;
  3156. ret = cmd->se_tfo->write_pending(cmd);
  3157. if (ret == -EAGAIN || ret == -ENOMEM) {
  3158. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
  3159. cmd);
  3160. transport_handle_queue_full(cmd, cmd->se_dev);
  3161. }
  3162. }
  3163. static int transport_generic_write_pending(struct se_cmd *cmd)
  3164. {
  3165. unsigned long flags;
  3166. int ret;
  3167. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3168. cmd->t_state = TRANSPORT_WRITE_PENDING;
  3169. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3170. /*
  3171. * Clear the se_cmd for WRITE_PENDING status in order to set
  3172. * CMD_T_ACTIVE so that transport_generic_handle_data can be called
  3173. * from HW target mode interrupt code. This is safe to be called
  3174. * with transport_off=1 before the cmd->se_tfo->write_pending
  3175. * because the se_cmd->se_lun pointer is not being cleared.
  3176. */
  3177. transport_cmd_check_stop(cmd, 1, 0);
  3178. /*
  3179. * Call the fabric write_pending function here to let the
  3180. * frontend know that WRITE buffers are ready.
  3181. */
  3182. ret = cmd->se_tfo->write_pending(cmd);
  3183. if (ret == -EAGAIN || ret == -ENOMEM)
  3184. goto queue_full;
  3185. else if (ret < 0)
  3186. return ret;
  3187. return 1;
  3188. queue_full:
  3189. pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
  3190. cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
  3191. transport_handle_queue_full(cmd, cmd->se_dev);
  3192. return 0;
  3193. }
  3194. void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
  3195. {
  3196. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
  3197. if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
  3198. transport_wait_for_tasks(cmd);
  3199. transport_release_cmd(cmd);
  3200. } else {
  3201. if (wait_for_tasks)
  3202. transport_wait_for_tasks(cmd);
  3203. core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
  3204. if (cmd->se_lun)
  3205. transport_lun_remove_cmd(cmd);
  3206. transport_put_cmd(cmd);
  3207. }
  3208. }
  3209. EXPORT_SYMBOL(transport_generic_free_cmd);
  3210. /* target_get_sess_cmd - Add command to active ->sess_cmd_list
  3211. * @se_sess: session to reference
  3212. * @se_cmd: command descriptor to add
  3213. * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
  3214. */
  3215. void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
  3216. bool ack_kref)
  3217. {
  3218. unsigned long flags;
  3219. kref_init(&se_cmd->cmd_kref);
  3220. /*
  3221. * Add a second kref if the fabric caller is expecting to handle
  3222. * fabric acknowledgement that requires two target_put_sess_cmd()
  3223. * invocations before se_cmd descriptor release.
  3224. */
  3225. if (ack_kref == true) {
  3226. kref_get(&se_cmd->cmd_kref);
  3227. se_cmd->se_cmd_flags |= SCF_ACK_KREF;
  3228. }
  3229. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3230. list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
  3231. se_cmd->check_release = 1;
  3232. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3233. }
  3234. EXPORT_SYMBOL(target_get_sess_cmd);
  3235. static void target_release_cmd_kref(struct kref *kref)
  3236. {
  3237. struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
  3238. struct se_session *se_sess = se_cmd->se_sess;
  3239. unsigned long flags;
  3240. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3241. if (list_empty(&se_cmd->se_cmd_list)) {
  3242. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3243. se_cmd->se_tfo->release_cmd(se_cmd);
  3244. return;
  3245. }
  3246. if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
  3247. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3248. complete(&se_cmd->cmd_wait_comp);
  3249. return;
  3250. }
  3251. list_del(&se_cmd->se_cmd_list);
  3252. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3253. se_cmd->se_tfo->release_cmd(se_cmd);
  3254. }
  3255. /* target_put_sess_cmd - Check for active I/O shutdown via kref_put
  3256. * @se_sess: session to reference
  3257. * @se_cmd: command descriptor to drop
  3258. */
  3259. int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
  3260. {
  3261. return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
  3262. }
  3263. EXPORT_SYMBOL(target_put_sess_cmd);
  3264. /* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
  3265. * @se_sess: session to split
  3266. */
  3267. void target_splice_sess_cmd_list(struct se_session *se_sess)
  3268. {
  3269. struct se_cmd *se_cmd;
  3270. unsigned long flags;
  3271. WARN_ON(!list_empty(&se_sess->sess_wait_list));
  3272. INIT_LIST_HEAD(&se_sess->sess_wait_list);
  3273. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  3274. se_sess->sess_tearing_down = 1;
  3275. list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
  3276. list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
  3277. se_cmd->cmd_wait_set = 1;
  3278. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  3279. }
  3280. EXPORT_SYMBOL(target_splice_sess_cmd_list);
  3281. /* target_wait_for_sess_cmds - Wait for outstanding descriptors
  3282. * @se_sess: session to wait for active I/O
  3283. * @wait_for_tasks: Make extra transport_wait_for_tasks call
  3284. */
  3285. void target_wait_for_sess_cmds(
  3286. struct se_session *se_sess,
  3287. int wait_for_tasks)
  3288. {
  3289. struct se_cmd *se_cmd, *tmp_cmd;
  3290. bool rc = false;
  3291. list_for_each_entry_safe(se_cmd, tmp_cmd,
  3292. &se_sess->sess_wait_list, se_cmd_list) {
  3293. list_del(&se_cmd->se_cmd_list);
  3294. pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
  3295. " %d\n", se_cmd, se_cmd->t_state,
  3296. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3297. if (wait_for_tasks) {
  3298. pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
  3299. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3300. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3301. rc = transport_wait_for_tasks(se_cmd);
  3302. pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
  3303. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3304. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3305. }
  3306. if (!rc) {
  3307. wait_for_completion(&se_cmd->cmd_wait_comp);
  3308. pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
  3309. " fabric state: %d\n", se_cmd, se_cmd->t_state,
  3310. se_cmd->se_tfo->get_cmd_state(se_cmd));
  3311. }
  3312. se_cmd->se_tfo->release_cmd(se_cmd);
  3313. }
  3314. }
  3315. EXPORT_SYMBOL(target_wait_for_sess_cmds);
  3316. /* transport_lun_wait_for_tasks():
  3317. *
  3318. * Called from ConfigFS context to stop the passed struct se_cmd to allow
  3319. * an struct se_lun to be successfully shutdown.
  3320. */
  3321. static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
  3322. {
  3323. unsigned long flags;
  3324. int ret = 0;
  3325. /*
  3326. * If the frontend has already requested this struct se_cmd to
  3327. * be stopped, we can safely ignore this struct se_cmd.
  3328. */
  3329. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3330. if (cmd->transport_state & CMD_T_STOP) {
  3331. cmd->transport_state &= ~CMD_T_LUN_STOP;
  3332. pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
  3333. cmd->se_tfo->get_task_tag(cmd));
  3334. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3335. transport_cmd_check_stop(cmd, 1, 0);
  3336. return -EPERM;
  3337. }
  3338. cmd->transport_state |= CMD_T_LUN_FE_STOP;
  3339. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3340. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  3341. // XXX: audit task_flags checks.
  3342. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3343. if ((cmd->transport_state & CMD_T_BUSY) &&
  3344. (cmd->transport_state & CMD_T_SENT)) {
  3345. if (!target_stop_cmd(cmd, &flags))
  3346. ret++;
  3347. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3348. } else {
  3349. spin_unlock_irqrestore(&cmd->t_state_lock,
  3350. flags);
  3351. target_remove_from_execute_list(cmd);
  3352. }
  3353. pr_debug("ConfigFS: cmd: %p stop tasks ret:"
  3354. " %d\n", cmd, ret);
  3355. if (!ret) {
  3356. pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
  3357. cmd->se_tfo->get_task_tag(cmd));
  3358. wait_for_completion(&cmd->transport_lun_stop_comp);
  3359. pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
  3360. cmd->se_tfo->get_task_tag(cmd));
  3361. }
  3362. transport_remove_cmd_from_queue(cmd);
  3363. return 0;
  3364. }
  3365. static void __transport_clear_lun_from_sessions(struct se_lun *lun)
  3366. {
  3367. struct se_cmd *cmd = NULL;
  3368. unsigned long lun_flags, cmd_flags;
  3369. /*
  3370. * Do exception processing and return CHECK_CONDITION status to the
  3371. * Initiator Port.
  3372. */
  3373. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3374. while (!list_empty(&lun->lun_cmd_list)) {
  3375. cmd = list_first_entry(&lun->lun_cmd_list,
  3376. struct se_cmd, se_lun_node);
  3377. list_del_init(&cmd->se_lun_node);
  3378. /*
  3379. * This will notify iscsi_target_transport.c:
  3380. * transport_cmd_check_stop() that a LUN shutdown is in
  3381. * progress for the iscsi_cmd_t.
  3382. */
  3383. spin_lock(&cmd->t_state_lock);
  3384. pr_debug("SE_LUN[%d] - Setting cmd->transport"
  3385. "_lun_stop for ITT: 0x%08x\n",
  3386. cmd->se_lun->unpacked_lun,
  3387. cmd->se_tfo->get_task_tag(cmd));
  3388. cmd->transport_state |= CMD_T_LUN_STOP;
  3389. spin_unlock(&cmd->t_state_lock);
  3390. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  3391. if (!cmd->se_lun) {
  3392. pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
  3393. cmd->se_tfo->get_task_tag(cmd),
  3394. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
  3395. BUG();
  3396. }
  3397. /*
  3398. * If the Storage engine still owns the iscsi_cmd_t, determine
  3399. * and/or stop its context.
  3400. */
  3401. pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
  3402. "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
  3403. cmd->se_tfo->get_task_tag(cmd));
  3404. if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
  3405. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3406. continue;
  3407. }
  3408. pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
  3409. "_wait_for_tasks(): SUCCESS\n",
  3410. cmd->se_lun->unpacked_lun,
  3411. cmd->se_tfo->get_task_tag(cmd));
  3412. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  3413. if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
  3414. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3415. goto check_cond;
  3416. }
  3417. cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
  3418. target_remove_from_state_list(cmd);
  3419. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3420. /*
  3421. * The Storage engine stopped this struct se_cmd before it was
  3422. * send to the fabric frontend for delivery back to the
  3423. * Initiator Node. Return this SCSI CDB back with an
  3424. * CHECK_CONDITION status.
  3425. */
  3426. check_cond:
  3427. transport_send_check_condition_and_sense(cmd,
  3428. TCM_NON_EXISTENT_LUN, 0);
  3429. /*
  3430. * If the fabric frontend is waiting for this iscsi_cmd_t to
  3431. * be released, notify the waiting thread now that LU has
  3432. * finished accessing it.
  3433. */
  3434. spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
  3435. if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
  3436. pr_debug("SE_LUN[%d] - Detected FE stop for"
  3437. " struct se_cmd: %p ITT: 0x%08x\n",
  3438. lun->unpacked_lun,
  3439. cmd, cmd->se_tfo->get_task_tag(cmd));
  3440. spin_unlock_irqrestore(&cmd->t_state_lock,
  3441. cmd_flags);
  3442. transport_cmd_check_stop(cmd, 1, 0);
  3443. complete(&cmd->transport_lun_fe_stop_comp);
  3444. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3445. continue;
  3446. }
  3447. pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
  3448. lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
  3449. spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
  3450. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  3451. }
  3452. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  3453. }
  3454. static int transport_clear_lun_thread(void *p)
  3455. {
  3456. struct se_lun *lun = p;
  3457. __transport_clear_lun_from_sessions(lun);
  3458. complete(&lun->lun_shutdown_comp);
  3459. return 0;
  3460. }
  3461. int transport_clear_lun_from_sessions(struct se_lun *lun)
  3462. {
  3463. struct task_struct *kt;
  3464. kt = kthread_run(transport_clear_lun_thread, lun,
  3465. "tcm_cl_%u", lun->unpacked_lun);
  3466. if (IS_ERR(kt)) {
  3467. pr_err("Unable to start clear_lun thread\n");
  3468. return PTR_ERR(kt);
  3469. }
  3470. wait_for_completion(&lun->lun_shutdown_comp);
  3471. return 0;
  3472. }
  3473. /**
  3474. * transport_wait_for_tasks - wait for completion to occur
  3475. * @cmd: command to wait
  3476. *
  3477. * Called from frontend fabric context to wait for storage engine
  3478. * to pause and/or release frontend generated struct se_cmd.
  3479. */
  3480. bool transport_wait_for_tasks(struct se_cmd *cmd)
  3481. {
  3482. unsigned long flags;
  3483. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3484. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
  3485. !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
  3486. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3487. return false;
  3488. }
  3489. /*
  3490. * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
  3491. * has been set in transport_set_supported_SAM_opcode().
  3492. */
  3493. if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
  3494. !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
  3495. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3496. return false;
  3497. }
  3498. /*
  3499. * If we are already stopped due to an external event (ie: LUN shutdown)
  3500. * sleep until the connection can have the passed struct se_cmd back.
  3501. * The cmd->transport_lun_stopped_sem will be upped by
  3502. * transport_clear_lun_from_sessions() once the ConfigFS context caller
  3503. * has completed its operation on the struct se_cmd.
  3504. */
  3505. if (cmd->transport_state & CMD_T_LUN_STOP) {
  3506. pr_debug("wait_for_tasks: Stopping"
  3507. " wait_for_completion(&cmd->t_tasktransport_lun_fe"
  3508. "_stop_comp); for ITT: 0x%08x\n",
  3509. cmd->se_tfo->get_task_tag(cmd));
  3510. /*
  3511. * There is a special case for WRITES where a FE exception +
  3512. * LUN shutdown means ConfigFS context is still sleeping on
  3513. * transport_lun_stop_comp in transport_lun_wait_for_tasks().
  3514. * We go ahead and up transport_lun_stop_comp just to be sure
  3515. * here.
  3516. */
  3517. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3518. complete(&cmd->transport_lun_stop_comp);
  3519. wait_for_completion(&cmd->transport_lun_fe_stop_comp);
  3520. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3521. target_remove_from_state_list(cmd);
  3522. /*
  3523. * At this point, the frontend who was the originator of this
  3524. * struct se_cmd, now owns the structure and can be released through
  3525. * normal means below.
  3526. */
  3527. pr_debug("wait_for_tasks: Stopped"
  3528. " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
  3529. "stop_comp); for ITT: 0x%08x\n",
  3530. cmd->se_tfo->get_task_tag(cmd));
  3531. cmd->transport_state &= ~CMD_T_LUN_STOP;
  3532. }
  3533. if (!(cmd->transport_state & CMD_T_ACTIVE)) {
  3534. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3535. return false;
  3536. }
  3537. cmd->transport_state |= CMD_T_STOP;
  3538. pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
  3539. " i_state: %d, t_state: %d, CMD_T_STOP\n",
  3540. cmd, cmd->se_tfo->get_task_tag(cmd),
  3541. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
  3542. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3543. wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
  3544. wait_for_completion(&cmd->t_transport_stop_comp);
  3545. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3546. cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
  3547. pr_debug("wait_for_tasks: Stopped wait_for_compltion("
  3548. "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
  3549. cmd->se_tfo->get_task_tag(cmd));
  3550. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3551. return true;
  3552. }
  3553. EXPORT_SYMBOL(transport_wait_for_tasks);
  3554. static int transport_get_sense_codes(
  3555. struct se_cmd *cmd,
  3556. u8 *asc,
  3557. u8 *ascq)
  3558. {
  3559. *asc = cmd->scsi_asc;
  3560. *ascq = cmd->scsi_ascq;
  3561. return 0;
  3562. }
  3563. static int transport_set_sense_codes(
  3564. struct se_cmd *cmd,
  3565. u8 asc,
  3566. u8 ascq)
  3567. {
  3568. cmd->scsi_asc = asc;
  3569. cmd->scsi_ascq = ascq;
  3570. return 0;
  3571. }
  3572. int transport_send_check_condition_and_sense(
  3573. struct se_cmd *cmd,
  3574. u8 reason,
  3575. int from_transport)
  3576. {
  3577. unsigned char *buffer = cmd->sense_buffer;
  3578. unsigned long flags;
  3579. int offset;
  3580. u8 asc = 0, ascq = 0;
  3581. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3582. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  3583. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3584. return 0;
  3585. }
  3586. cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
  3587. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3588. if (!reason && from_transport)
  3589. goto after_reason;
  3590. if (!from_transport)
  3591. cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
  3592. /*
  3593. * Data Segment and SenseLength of the fabric response PDU.
  3594. *
  3595. * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
  3596. * from include/scsi/scsi_cmnd.h
  3597. */
  3598. offset = cmd->se_tfo->set_fabric_sense_len(cmd,
  3599. TRANSPORT_SENSE_BUFFER);
  3600. /*
  3601. * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
  3602. * SENSE KEY values from include/scsi/scsi.h
  3603. */
  3604. switch (reason) {
  3605. case TCM_NON_EXISTENT_LUN:
  3606. /* CURRENT ERROR */
  3607. buffer[offset] = 0x70;
  3608. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3609. /* ILLEGAL REQUEST */
  3610. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3611. /* LOGICAL UNIT NOT SUPPORTED */
  3612. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
  3613. break;
  3614. case TCM_UNSUPPORTED_SCSI_OPCODE:
  3615. case TCM_SECTOR_COUNT_TOO_MANY:
  3616. /* CURRENT ERROR */
  3617. buffer[offset] = 0x70;
  3618. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3619. /* ILLEGAL REQUEST */
  3620. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3621. /* INVALID COMMAND OPERATION CODE */
  3622. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
  3623. break;
  3624. case TCM_UNKNOWN_MODE_PAGE:
  3625. /* CURRENT ERROR */
  3626. buffer[offset] = 0x70;
  3627. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3628. /* ILLEGAL REQUEST */
  3629. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3630. /* INVALID FIELD IN CDB */
  3631. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  3632. break;
  3633. case TCM_CHECK_CONDITION_ABORT_CMD:
  3634. /* CURRENT ERROR */
  3635. buffer[offset] = 0x70;
  3636. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3637. /* ABORTED COMMAND */
  3638. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3639. /* BUS DEVICE RESET FUNCTION OCCURRED */
  3640. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
  3641. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
  3642. break;
  3643. case TCM_INCORRECT_AMOUNT_OF_DATA:
  3644. /* CURRENT ERROR */
  3645. buffer[offset] = 0x70;
  3646. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3647. /* ABORTED COMMAND */
  3648. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3649. /* WRITE ERROR */
  3650. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  3651. /* NOT ENOUGH UNSOLICITED DATA */
  3652. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
  3653. break;
  3654. case TCM_INVALID_CDB_FIELD:
  3655. /* CURRENT ERROR */
  3656. buffer[offset] = 0x70;
  3657. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3658. /* ILLEGAL REQUEST */
  3659. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3660. /* INVALID FIELD IN CDB */
  3661. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  3662. break;
  3663. case TCM_INVALID_PARAMETER_LIST:
  3664. /* CURRENT ERROR */
  3665. buffer[offset] = 0x70;
  3666. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3667. /* ILLEGAL REQUEST */
  3668. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3669. /* INVALID FIELD IN PARAMETER LIST */
  3670. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
  3671. break;
  3672. case TCM_UNEXPECTED_UNSOLICITED_DATA:
  3673. /* CURRENT ERROR */
  3674. buffer[offset] = 0x70;
  3675. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3676. /* ABORTED COMMAND */
  3677. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3678. /* WRITE ERROR */
  3679. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  3680. /* UNEXPECTED_UNSOLICITED_DATA */
  3681. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
  3682. break;
  3683. case TCM_SERVICE_CRC_ERROR:
  3684. /* CURRENT ERROR */
  3685. buffer[offset] = 0x70;
  3686. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3687. /* ABORTED COMMAND */
  3688. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3689. /* PROTOCOL SERVICE CRC ERROR */
  3690. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
  3691. /* N/A */
  3692. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
  3693. break;
  3694. case TCM_SNACK_REJECTED:
  3695. /* CURRENT ERROR */
  3696. buffer[offset] = 0x70;
  3697. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3698. /* ABORTED COMMAND */
  3699. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  3700. /* READ ERROR */
  3701. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
  3702. /* FAILED RETRANSMISSION REQUEST */
  3703. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
  3704. break;
  3705. case TCM_WRITE_PROTECTED:
  3706. /* CURRENT ERROR */
  3707. buffer[offset] = 0x70;
  3708. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3709. /* DATA PROTECT */
  3710. buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
  3711. /* WRITE PROTECTED */
  3712. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
  3713. break;
  3714. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  3715. /* CURRENT ERROR */
  3716. buffer[offset] = 0x70;
  3717. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3718. /* UNIT ATTENTION */
  3719. buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  3720. core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
  3721. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  3722. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  3723. break;
  3724. case TCM_CHECK_CONDITION_NOT_READY:
  3725. /* CURRENT ERROR */
  3726. buffer[offset] = 0x70;
  3727. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3728. /* Not Ready */
  3729. buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
  3730. transport_get_sense_codes(cmd, &asc, &ascq);
  3731. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  3732. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  3733. break;
  3734. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  3735. default:
  3736. /* CURRENT ERROR */
  3737. buffer[offset] = 0x70;
  3738. buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
  3739. /* ILLEGAL REQUEST */
  3740. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  3741. /* LOGICAL UNIT COMMUNICATION FAILURE */
  3742. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
  3743. break;
  3744. }
  3745. /*
  3746. * This code uses linux/include/scsi/scsi.h SAM status codes!
  3747. */
  3748. cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
  3749. /*
  3750. * Automatically padded, this value is encoded in the fabric's
  3751. * data_length response PDU containing the SCSI defined sense data.
  3752. */
  3753. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
  3754. after_reason:
  3755. return cmd->se_tfo->queue_status(cmd);
  3756. }
  3757. EXPORT_SYMBOL(transport_send_check_condition_and_sense);
  3758. int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
  3759. {
  3760. int ret = 0;
  3761. if (cmd->transport_state & CMD_T_ABORTED) {
  3762. if (!send_status ||
  3763. (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
  3764. return 1;
  3765. pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
  3766. " status for CDB: 0x%02x ITT: 0x%08x\n",
  3767. cmd->t_task_cdb[0],
  3768. cmd->se_tfo->get_task_tag(cmd));
  3769. cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
  3770. cmd->se_tfo->queue_status(cmd);
  3771. ret = 1;
  3772. }
  3773. return ret;
  3774. }
  3775. EXPORT_SYMBOL(transport_check_aborted_status);
  3776. void transport_send_task_abort(struct se_cmd *cmd)
  3777. {
  3778. unsigned long flags;
  3779. spin_lock_irqsave(&cmd->t_state_lock, flags);
  3780. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  3781. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3782. return;
  3783. }
  3784. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  3785. /*
  3786. * If there are still expected incoming fabric WRITEs, we wait
  3787. * until until they have completed before sending a TASK_ABORTED
  3788. * response. This response with TASK_ABORTED status will be
  3789. * queued back to fabric module by transport_check_aborted_status().
  3790. */
  3791. if (cmd->data_direction == DMA_TO_DEVICE) {
  3792. if (cmd->se_tfo->write_pending_status(cmd) != 0) {
  3793. cmd->transport_state |= CMD_T_ABORTED;
  3794. smp_mb__after_atomic_inc();
  3795. }
  3796. }
  3797. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  3798. pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
  3799. " ITT: 0x%08x\n", cmd->t_task_cdb[0],
  3800. cmd->se_tfo->get_task_tag(cmd));
  3801. cmd->se_tfo->queue_status(cmd);
  3802. }
  3803. static int transport_generic_do_tmr(struct se_cmd *cmd)
  3804. {
  3805. struct se_device *dev = cmd->se_dev;
  3806. struct se_tmr_req *tmr = cmd->se_tmr_req;
  3807. int ret;
  3808. switch (tmr->function) {
  3809. case TMR_ABORT_TASK:
  3810. core_tmr_abort_task(dev, tmr, cmd->se_sess);
  3811. break;
  3812. case TMR_ABORT_TASK_SET:
  3813. case TMR_CLEAR_ACA:
  3814. case TMR_CLEAR_TASK_SET:
  3815. tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  3816. break;
  3817. case TMR_LUN_RESET:
  3818. ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
  3819. tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
  3820. TMR_FUNCTION_REJECTED;
  3821. break;
  3822. case TMR_TARGET_WARM_RESET:
  3823. tmr->response = TMR_FUNCTION_REJECTED;
  3824. break;
  3825. case TMR_TARGET_COLD_RESET:
  3826. tmr->response = TMR_FUNCTION_REJECTED;
  3827. break;
  3828. default:
  3829. pr_err("Uknown TMR function: 0x%02x.\n",
  3830. tmr->function);
  3831. tmr->response = TMR_FUNCTION_REJECTED;
  3832. break;
  3833. }
  3834. cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
  3835. cmd->se_tfo->queue_tm_rsp(cmd);
  3836. transport_cmd_check_stop_to_fabric(cmd);
  3837. return 0;
  3838. }
  3839. /* transport_processing_thread():
  3840. *
  3841. *
  3842. */
  3843. static int transport_processing_thread(void *param)
  3844. {
  3845. int ret;
  3846. struct se_cmd *cmd;
  3847. struct se_device *dev = param;
  3848. while (!kthread_should_stop()) {
  3849. ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
  3850. atomic_read(&dev->dev_queue_obj.queue_cnt) ||
  3851. kthread_should_stop());
  3852. if (ret < 0)
  3853. goto out;
  3854. get_cmd:
  3855. cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
  3856. if (!cmd)
  3857. continue;
  3858. switch (cmd->t_state) {
  3859. case TRANSPORT_NEW_CMD:
  3860. BUG();
  3861. break;
  3862. case TRANSPORT_NEW_CMD_MAP:
  3863. if (!cmd->se_tfo->new_cmd_map) {
  3864. pr_err("cmd->se_tfo->new_cmd_map is"
  3865. " NULL for TRANSPORT_NEW_CMD_MAP\n");
  3866. BUG();
  3867. }
  3868. ret = cmd->se_tfo->new_cmd_map(cmd);
  3869. if (ret < 0) {
  3870. transport_generic_request_failure(cmd);
  3871. break;
  3872. }
  3873. ret = transport_generic_new_cmd(cmd);
  3874. if (ret < 0) {
  3875. transport_generic_request_failure(cmd);
  3876. break;
  3877. }
  3878. break;
  3879. case TRANSPORT_PROCESS_WRITE:
  3880. transport_generic_process_write(cmd);
  3881. break;
  3882. case TRANSPORT_PROCESS_TMR:
  3883. transport_generic_do_tmr(cmd);
  3884. break;
  3885. case TRANSPORT_COMPLETE_QF_WP:
  3886. transport_write_pending_qf(cmd);
  3887. break;
  3888. case TRANSPORT_COMPLETE_QF_OK:
  3889. transport_complete_qf(cmd);
  3890. break;
  3891. default:
  3892. pr_err("Unknown t_state: %d for ITT: 0x%08x "
  3893. "i_state: %d on SE LUN: %u\n",
  3894. cmd->t_state,
  3895. cmd->se_tfo->get_task_tag(cmd),
  3896. cmd->se_tfo->get_cmd_state(cmd),
  3897. cmd->se_lun->unpacked_lun);
  3898. BUG();
  3899. }
  3900. goto get_cmd;
  3901. }
  3902. out:
  3903. WARN_ON(!list_empty(&dev->state_list));
  3904. WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
  3905. dev->process_thread = NULL;
  3906. return 0;
  3907. }