target_core_transport.c 126 KB

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