target_core_transport.c 168 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162
  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/version.h>
  29. #include <linux/net.h>
  30. #include <linux/delay.h>
  31. #include <linux/string.h>
  32. #include <linux/timer.h>
  33. #include <linux/slab.h>
  34. #include <linux/blkdev.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/kthread.h>
  37. #include <linux/in.h>
  38. #include <linux/cdrom.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/libsas.h> /* For TASK_ATTR_* */
  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_scdb.h"
  56. #include "target_core_ua.h"
  57. /* #define DEBUG_CDB_HANDLER */
  58. #ifdef DEBUG_CDB_HANDLER
  59. #define DEBUG_CDB_H(x...) printk(KERN_INFO x)
  60. #else
  61. #define DEBUG_CDB_H(x...)
  62. #endif
  63. /* #define DEBUG_CMD_MAP */
  64. #ifdef DEBUG_CMD_MAP
  65. #define DEBUG_CMD_M(x...) printk(KERN_INFO x)
  66. #else
  67. #define DEBUG_CMD_M(x...)
  68. #endif
  69. /* #define DEBUG_MEM_ALLOC */
  70. #ifdef DEBUG_MEM_ALLOC
  71. #define DEBUG_MEM(x...) printk(KERN_INFO x)
  72. #else
  73. #define DEBUG_MEM(x...)
  74. #endif
  75. /* #define DEBUG_MEM2_ALLOC */
  76. #ifdef DEBUG_MEM2_ALLOC
  77. #define DEBUG_MEM2(x...) printk(KERN_INFO x)
  78. #else
  79. #define DEBUG_MEM2(x...)
  80. #endif
  81. /* #define DEBUG_SG_CALC */
  82. #ifdef DEBUG_SG_CALC
  83. #define DEBUG_SC(x...) printk(KERN_INFO x)
  84. #else
  85. #define DEBUG_SC(x...)
  86. #endif
  87. /* #define DEBUG_SE_OBJ */
  88. #ifdef DEBUG_SE_OBJ
  89. #define DEBUG_SO(x...) printk(KERN_INFO x)
  90. #else
  91. #define DEBUG_SO(x...)
  92. #endif
  93. /* #define DEBUG_CMD_VOL */
  94. #ifdef DEBUG_CMD_VOL
  95. #define DEBUG_VOL(x...) printk(KERN_INFO x)
  96. #else
  97. #define DEBUG_VOL(x...)
  98. #endif
  99. /* #define DEBUG_CMD_STOP */
  100. #ifdef DEBUG_CMD_STOP
  101. #define DEBUG_CS(x...) printk(KERN_INFO x)
  102. #else
  103. #define DEBUG_CS(x...)
  104. #endif
  105. /* #define DEBUG_PASSTHROUGH */
  106. #ifdef DEBUG_PASSTHROUGH
  107. #define DEBUG_PT(x...) printk(KERN_INFO x)
  108. #else
  109. #define DEBUG_PT(x...)
  110. #endif
  111. /* #define DEBUG_TASK_STOP */
  112. #ifdef DEBUG_TASK_STOP
  113. #define DEBUG_TS(x...) printk(KERN_INFO x)
  114. #else
  115. #define DEBUG_TS(x...)
  116. #endif
  117. /* #define DEBUG_TRANSPORT_STOP */
  118. #ifdef DEBUG_TRANSPORT_STOP
  119. #define DEBUG_TRANSPORT_S(x...) printk(KERN_INFO x)
  120. #else
  121. #define DEBUG_TRANSPORT_S(x...)
  122. #endif
  123. /* #define DEBUG_TASK_FAILURE */
  124. #ifdef DEBUG_TASK_FAILURE
  125. #define DEBUG_TF(x...) printk(KERN_INFO x)
  126. #else
  127. #define DEBUG_TF(x...)
  128. #endif
  129. /* #define DEBUG_DEV_OFFLINE */
  130. #ifdef DEBUG_DEV_OFFLINE
  131. #define DEBUG_DO(x...) printk(KERN_INFO x)
  132. #else
  133. #define DEBUG_DO(x...)
  134. #endif
  135. /* #define DEBUG_TASK_STATE */
  136. #ifdef DEBUG_TASK_STATE
  137. #define DEBUG_TSTATE(x...) printk(KERN_INFO x)
  138. #else
  139. #define DEBUG_TSTATE(x...)
  140. #endif
  141. /* #define DEBUG_STATUS_THR */
  142. #ifdef DEBUG_STATUS_THR
  143. #define DEBUG_ST(x...) printk(KERN_INFO x)
  144. #else
  145. #define DEBUG_ST(x...)
  146. #endif
  147. /* #define DEBUG_TASK_TIMEOUT */
  148. #ifdef DEBUG_TASK_TIMEOUT
  149. #define DEBUG_TT(x...) printk(KERN_INFO x)
  150. #else
  151. #define DEBUG_TT(x...)
  152. #endif
  153. /* #define DEBUG_GENERIC_REQUEST_FAILURE */
  154. #ifdef DEBUG_GENERIC_REQUEST_FAILURE
  155. #define DEBUG_GRF(x...) printk(KERN_INFO x)
  156. #else
  157. #define DEBUG_GRF(x...)
  158. #endif
  159. /* #define DEBUG_SAM_TASK_ATTRS */
  160. #ifdef DEBUG_SAM_TASK_ATTRS
  161. #define DEBUG_STA(x...) printk(KERN_INFO x)
  162. #else
  163. #define DEBUG_STA(x...)
  164. #endif
  165. struct se_global *se_global;
  166. static struct kmem_cache *se_cmd_cache;
  167. static struct kmem_cache *se_sess_cache;
  168. struct kmem_cache *se_tmr_req_cache;
  169. struct kmem_cache *se_ua_cache;
  170. struct kmem_cache *se_mem_cache;
  171. struct kmem_cache *t10_pr_reg_cache;
  172. struct kmem_cache *t10_alua_lu_gp_cache;
  173. struct kmem_cache *t10_alua_lu_gp_mem_cache;
  174. struct kmem_cache *t10_alua_tg_pt_gp_cache;
  175. struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
  176. /* Used for transport_dev_get_map_*() */
  177. typedef int (*map_func_t)(struct se_task *, u32);
  178. static int transport_generic_write_pending(struct se_cmd *);
  179. static int transport_processing_thread(void *);
  180. static int __transport_execute_tasks(struct se_device *dev);
  181. static void transport_complete_task_attr(struct se_cmd *cmd);
  182. static void transport_direct_request_timeout(struct se_cmd *cmd);
  183. static void transport_free_dev_tasks(struct se_cmd *cmd);
  184. static u32 transport_generic_get_cdb_count(struct se_cmd *cmd,
  185. unsigned long long starting_lba, u32 sectors,
  186. enum dma_data_direction data_direction,
  187. struct list_head *mem_list, int set_counts);
  188. static int transport_generic_get_mem(struct se_cmd *cmd, u32 length,
  189. u32 dma_size);
  190. static int transport_generic_remove(struct se_cmd *cmd,
  191. int release_to_pool, int session_reinstatement);
  192. static int transport_get_sectors(struct se_cmd *cmd);
  193. static struct list_head *transport_init_se_mem_list(void);
  194. static int transport_map_sg_to_mem(struct se_cmd *cmd,
  195. struct list_head *se_mem_list, void *in_mem,
  196. u32 *se_mem_cnt);
  197. static void transport_memcpy_se_mem_read_contig(struct se_cmd *cmd,
  198. unsigned char *dst, struct list_head *se_mem_list);
  199. static void transport_release_fe_cmd(struct se_cmd *cmd);
  200. static void transport_remove_cmd_from_queue(struct se_cmd *cmd,
  201. struct se_queue_obj *qobj);
  202. static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
  203. static void transport_stop_all_task_timers(struct se_cmd *cmd);
  204. int init_se_global(void)
  205. {
  206. struct se_global *global;
  207. global = kzalloc(sizeof(struct se_global), GFP_KERNEL);
  208. if (!(global)) {
  209. printk(KERN_ERR "Unable to allocate memory for struct se_global\n");
  210. return -1;
  211. }
  212. INIT_LIST_HEAD(&global->g_lu_gps_list);
  213. INIT_LIST_HEAD(&global->g_se_tpg_list);
  214. INIT_LIST_HEAD(&global->g_hba_list);
  215. INIT_LIST_HEAD(&global->g_se_dev_list);
  216. spin_lock_init(&global->g_device_lock);
  217. spin_lock_init(&global->hba_lock);
  218. spin_lock_init(&global->se_tpg_lock);
  219. spin_lock_init(&global->lu_gps_lock);
  220. spin_lock_init(&global->plugin_class_lock);
  221. se_cmd_cache = kmem_cache_create("se_cmd_cache",
  222. sizeof(struct se_cmd), __alignof__(struct se_cmd), 0, NULL);
  223. if (!(se_cmd_cache)) {
  224. printk(KERN_ERR "kmem_cache_create for struct se_cmd failed\n");
  225. goto out;
  226. }
  227. se_tmr_req_cache = kmem_cache_create("se_tmr_cache",
  228. sizeof(struct se_tmr_req), __alignof__(struct se_tmr_req),
  229. 0, NULL);
  230. if (!(se_tmr_req_cache)) {
  231. printk(KERN_ERR "kmem_cache_create() for struct se_tmr_req"
  232. " failed\n");
  233. goto out;
  234. }
  235. se_sess_cache = kmem_cache_create("se_sess_cache",
  236. sizeof(struct se_session), __alignof__(struct se_session),
  237. 0, NULL);
  238. if (!(se_sess_cache)) {
  239. printk(KERN_ERR "kmem_cache_create() for struct se_session"
  240. " failed\n");
  241. goto out;
  242. }
  243. se_ua_cache = kmem_cache_create("se_ua_cache",
  244. sizeof(struct se_ua), __alignof__(struct se_ua),
  245. 0, NULL);
  246. if (!(se_ua_cache)) {
  247. printk(KERN_ERR "kmem_cache_create() for struct se_ua failed\n");
  248. goto out;
  249. }
  250. se_mem_cache = kmem_cache_create("se_mem_cache",
  251. sizeof(struct se_mem), __alignof__(struct se_mem), 0, NULL);
  252. if (!(se_mem_cache)) {
  253. printk(KERN_ERR "kmem_cache_create() for struct se_mem failed\n");
  254. goto out;
  255. }
  256. t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
  257. sizeof(struct t10_pr_registration),
  258. __alignof__(struct t10_pr_registration), 0, NULL);
  259. if (!(t10_pr_reg_cache)) {
  260. printk(KERN_ERR "kmem_cache_create() for struct t10_pr_registration"
  261. " failed\n");
  262. goto out;
  263. }
  264. t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
  265. sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
  266. 0, NULL);
  267. if (!(t10_alua_lu_gp_cache)) {
  268. printk(KERN_ERR "kmem_cache_create() for t10_alua_lu_gp_cache"
  269. " failed\n");
  270. goto out;
  271. }
  272. t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
  273. sizeof(struct t10_alua_lu_gp_member),
  274. __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
  275. if (!(t10_alua_lu_gp_mem_cache)) {
  276. printk(KERN_ERR "kmem_cache_create() for t10_alua_lu_gp_mem_"
  277. "cache failed\n");
  278. goto out;
  279. }
  280. t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
  281. sizeof(struct t10_alua_tg_pt_gp),
  282. __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
  283. if (!(t10_alua_tg_pt_gp_cache)) {
  284. printk(KERN_ERR "kmem_cache_create() for t10_alua_tg_pt_gp_"
  285. "cache failed\n");
  286. goto out;
  287. }
  288. t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
  289. "t10_alua_tg_pt_gp_mem_cache",
  290. sizeof(struct t10_alua_tg_pt_gp_member),
  291. __alignof__(struct t10_alua_tg_pt_gp_member),
  292. 0, NULL);
  293. if (!(t10_alua_tg_pt_gp_mem_cache)) {
  294. printk(KERN_ERR "kmem_cache_create() for t10_alua_tg_pt_gp_"
  295. "mem_t failed\n");
  296. goto out;
  297. }
  298. se_global = global;
  299. return 0;
  300. out:
  301. if (se_cmd_cache)
  302. kmem_cache_destroy(se_cmd_cache);
  303. if (se_tmr_req_cache)
  304. kmem_cache_destroy(se_tmr_req_cache);
  305. if (se_sess_cache)
  306. kmem_cache_destroy(se_sess_cache);
  307. if (se_ua_cache)
  308. kmem_cache_destroy(se_ua_cache);
  309. if (se_mem_cache)
  310. kmem_cache_destroy(se_mem_cache);
  311. if (t10_pr_reg_cache)
  312. kmem_cache_destroy(t10_pr_reg_cache);
  313. if (t10_alua_lu_gp_cache)
  314. kmem_cache_destroy(t10_alua_lu_gp_cache);
  315. if (t10_alua_lu_gp_mem_cache)
  316. kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
  317. if (t10_alua_tg_pt_gp_cache)
  318. kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
  319. if (t10_alua_tg_pt_gp_mem_cache)
  320. kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
  321. kfree(global);
  322. return -1;
  323. }
  324. void release_se_global(void)
  325. {
  326. struct se_global *global;
  327. global = se_global;
  328. if (!(global))
  329. return;
  330. kmem_cache_destroy(se_cmd_cache);
  331. kmem_cache_destroy(se_tmr_req_cache);
  332. kmem_cache_destroy(se_sess_cache);
  333. kmem_cache_destroy(se_ua_cache);
  334. kmem_cache_destroy(se_mem_cache);
  335. kmem_cache_destroy(t10_pr_reg_cache);
  336. kmem_cache_destroy(t10_alua_lu_gp_cache);
  337. kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
  338. kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
  339. kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
  340. kfree(global);
  341. se_global = NULL;
  342. }
  343. /* SCSI statistics table index */
  344. static struct scsi_index_table scsi_index_table;
  345. /*
  346. * Initialize the index table for allocating unique row indexes to various mib
  347. * tables.
  348. */
  349. void init_scsi_index_table(void)
  350. {
  351. memset(&scsi_index_table, 0, sizeof(struct scsi_index_table));
  352. spin_lock_init(&scsi_index_table.lock);
  353. }
  354. /*
  355. * Allocate a new row index for the entry type specified
  356. */
  357. u32 scsi_get_new_index(scsi_index_t type)
  358. {
  359. u32 new_index;
  360. if ((type < 0) || (type >= SCSI_INDEX_TYPE_MAX)) {
  361. printk(KERN_ERR "Invalid index type %d\n", type);
  362. return -EINVAL;
  363. }
  364. spin_lock(&scsi_index_table.lock);
  365. new_index = ++scsi_index_table.scsi_mib_index[type];
  366. if (new_index == 0)
  367. new_index = ++scsi_index_table.scsi_mib_index[type];
  368. spin_unlock(&scsi_index_table.lock);
  369. return new_index;
  370. }
  371. void transport_init_queue_obj(struct se_queue_obj *qobj)
  372. {
  373. atomic_set(&qobj->queue_cnt, 0);
  374. INIT_LIST_HEAD(&qobj->qobj_list);
  375. init_waitqueue_head(&qobj->thread_wq);
  376. spin_lock_init(&qobj->cmd_queue_lock);
  377. }
  378. EXPORT_SYMBOL(transport_init_queue_obj);
  379. static int transport_subsystem_reqmods(void)
  380. {
  381. int ret;
  382. ret = request_module("target_core_iblock");
  383. if (ret != 0)
  384. printk(KERN_ERR "Unable to load target_core_iblock\n");
  385. ret = request_module("target_core_file");
  386. if (ret != 0)
  387. printk(KERN_ERR "Unable to load target_core_file\n");
  388. ret = request_module("target_core_pscsi");
  389. if (ret != 0)
  390. printk(KERN_ERR "Unable to load target_core_pscsi\n");
  391. ret = request_module("target_core_stgt");
  392. if (ret != 0)
  393. printk(KERN_ERR "Unable to load target_core_stgt\n");
  394. return 0;
  395. }
  396. int transport_subsystem_check_init(void)
  397. {
  398. if (se_global->g_sub_api_initialized)
  399. return 0;
  400. /*
  401. * Request the loading of known TCM subsystem plugins..
  402. */
  403. if (transport_subsystem_reqmods() < 0)
  404. return -1;
  405. se_global->g_sub_api_initialized = 1;
  406. return 0;
  407. }
  408. struct se_session *transport_init_session(void)
  409. {
  410. struct se_session *se_sess;
  411. se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
  412. if (!(se_sess)) {
  413. printk(KERN_ERR "Unable to allocate struct se_session from"
  414. " se_sess_cache\n");
  415. return ERR_PTR(-ENOMEM);
  416. }
  417. INIT_LIST_HEAD(&se_sess->sess_list);
  418. INIT_LIST_HEAD(&se_sess->sess_acl_list);
  419. return se_sess;
  420. }
  421. EXPORT_SYMBOL(transport_init_session);
  422. /*
  423. * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
  424. */
  425. void __transport_register_session(
  426. struct se_portal_group *se_tpg,
  427. struct se_node_acl *se_nacl,
  428. struct se_session *se_sess,
  429. void *fabric_sess_ptr)
  430. {
  431. unsigned char buf[PR_REG_ISID_LEN];
  432. se_sess->se_tpg = se_tpg;
  433. se_sess->fabric_sess_ptr = fabric_sess_ptr;
  434. /*
  435. * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
  436. *
  437. * Only set for struct se_session's that will actually be moving I/O.
  438. * eg: *NOT* discovery sessions.
  439. */
  440. if (se_nacl) {
  441. /*
  442. * If the fabric module supports an ISID based TransportID,
  443. * save this value in binary from the fabric I_T Nexus now.
  444. */
  445. if (TPG_TFO(se_tpg)->sess_get_initiator_sid != NULL) {
  446. memset(&buf[0], 0, PR_REG_ISID_LEN);
  447. TPG_TFO(se_tpg)->sess_get_initiator_sid(se_sess,
  448. &buf[0], PR_REG_ISID_LEN);
  449. se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
  450. }
  451. spin_lock_irq(&se_nacl->nacl_sess_lock);
  452. /*
  453. * The se_nacl->nacl_sess pointer will be set to the
  454. * last active I_T Nexus for each struct se_node_acl.
  455. */
  456. se_nacl->nacl_sess = se_sess;
  457. list_add_tail(&se_sess->sess_acl_list,
  458. &se_nacl->acl_sess_list);
  459. spin_unlock_irq(&se_nacl->nacl_sess_lock);
  460. }
  461. list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
  462. printk(KERN_INFO "TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
  463. TPG_TFO(se_tpg)->get_fabric_name(), se_sess->fabric_sess_ptr);
  464. }
  465. EXPORT_SYMBOL(__transport_register_session);
  466. void transport_register_session(
  467. struct se_portal_group *se_tpg,
  468. struct se_node_acl *se_nacl,
  469. struct se_session *se_sess,
  470. void *fabric_sess_ptr)
  471. {
  472. spin_lock_bh(&se_tpg->session_lock);
  473. __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
  474. spin_unlock_bh(&se_tpg->session_lock);
  475. }
  476. EXPORT_SYMBOL(transport_register_session);
  477. void transport_deregister_session_configfs(struct se_session *se_sess)
  478. {
  479. struct se_node_acl *se_nacl;
  480. /*
  481. * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
  482. */
  483. se_nacl = se_sess->se_node_acl;
  484. if ((se_nacl)) {
  485. spin_lock_irq(&se_nacl->nacl_sess_lock);
  486. list_del(&se_sess->sess_acl_list);
  487. /*
  488. * If the session list is empty, then clear the pointer.
  489. * Otherwise, set the struct se_session pointer from the tail
  490. * element of the per struct se_node_acl active session list.
  491. */
  492. if (list_empty(&se_nacl->acl_sess_list))
  493. se_nacl->nacl_sess = NULL;
  494. else {
  495. se_nacl->nacl_sess = container_of(
  496. se_nacl->acl_sess_list.prev,
  497. struct se_session, sess_acl_list);
  498. }
  499. spin_unlock_irq(&se_nacl->nacl_sess_lock);
  500. }
  501. }
  502. EXPORT_SYMBOL(transport_deregister_session_configfs);
  503. void transport_free_session(struct se_session *se_sess)
  504. {
  505. kmem_cache_free(se_sess_cache, se_sess);
  506. }
  507. EXPORT_SYMBOL(transport_free_session);
  508. void transport_deregister_session(struct se_session *se_sess)
  509. {
  510. struct se_portal_group *se_tpg = se_sess->se_tpg;
  511. struct se_node_acl *se_nacl;
  512. if (!(se_tpg)) {
  513. transport_free_session(se_sess);
  514. return;
  515. }
  516. spin_lock_bh(&se_tpg->session_lock);
  517. list_del(&se_sess->sess_list);
  518. se_sess->se_tpg = NULL;
  519. se_sess->fabric_sess_ptr = NULL;
  520. spin_unlock_bh(&se_tpg->session_lock);
  521. /*
  522. * Determine if we need to do extra work for this initiator node's
  523. * struct se_node_acl if it had been previously dynamically generated.
  524. */
  525. se_nacl = se_sess->se_node_acl;
  526. if ((se_nacl)) {
  527. spin_lock_bh(&se_tpg->acl_node_lock);
  528. if (se_nacl->dynamic_node_acl) {
  529. if (!(TPG_TFO(se_tpg)->tpg_check_demo_mode_cache(
  530. se_tpg))) {
  531. list_del(&se_nacl->acl_list);
  532. se_tpg->num_node_acls--;
  533. spin_unlock_bh(&se_tpg->acl_node_lock);
  534. core_tpg_wait_for_nacl_pr_ref(se_nacl);
  535. core_free_device_list_for_node(se_nacl, se_tpg);
  536. TPG_TFO(se_tpg)->tpg_release_fabric_acl(se_tpg,
  537. se_nacl);
  538. spin_lock_bh(&se_tpg->acl_node_lock);
  539. }
  540. }
  541. spin_unlock_bh(&se_tpg->acl_node_lock);
  542. }
  543. transport_free_session(se_sess);
  544. printk(KERN_INFO "TARGET_CORE[%s]: Deregistered fabric_sess\n",
  545. TPG_TFO(se_tpg)->get_fabric_name());
  546. }
  547. EXPORT_SYMBOL(transport_deregister_session);
  548. /*
  549. * Called with T_TASK(cmd)->t_state_lock held.
  550. */
  551. static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
  552. {
  553. struct se_device *dev;
  554. struct se_task *task;
  555. unsigned long flags;
  556. if (!T_TASK(cmd))
  557. return;
  558. list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
  559. dev = task->se_dev;
  560. if (!(dev))
  561. continue;
  562. if (atomic_read(&task->task_active))
  563. continue;
  564. if (!(atomic_read(&task->task_state_active)))
  565. continue;
  566. spin_lock_irqsave(&dev->execute_task_lock, flags);
  567. list_del(&task->t_state_list);
  568. DEBUG_TSTATE("Removed ITT: 0x%08x dev: %p task[%p]\n",
  569. CMD_TFO(cmd)->tfo_get_task_tag(cmd), dev, task);
  570. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  571. atomic_set(&task->task_state_active, 0);
  572. atomic_dec(&T_TASK(cmd)->t_task_cdbs_ex_left);
  573. }
  574. }
  575. /* transport_cmd_check_stop():
  576. *
  577. * 'transport_off = 1' determines if t_transport_active should be cleared.
  578. * 'transport_off = 2' determines if task_dev_state should be removed.
  579. *
  580. * A non-zero u8 t_state sets cmd->t_state.
  581. * Returns 1 when command is stopped, else 0.
  582. */
  583. static int transport_cmd_check_stop(
  584. struct se_cmd *cmd,
  585. int transport_off,
  586. u8 t_state)
  587. {
  588. unsigned long flags;
  589. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  590. /*
  591. * Determine if IOCTL context caller in requesting the stopping of this
  592. * command for LUN shutdown purposes.
  593. */
  594. if (atomic_read(&T_TASK(cmd)->transport_lun_stop)) {
  595. DEBUG_CS("%s:%d atomic_read(&T_TASK(cmd)->transport_lun_stop)"
  596. " == TRUE for ITT: 0x%08x\n", __func__, __LINE__,
  597. CMD_TFO(cmd)->get_task_tag(cmd));
  598. cmd->deferred_t_state = cmd->t_state;
  599. cmd->t_state = TRANSPORT_DEFERRED_CMD;
  600. atomic_set(&T_TASK(cmd)->t_transport_active, 0);
  601. if (transport_off == 2)
  602. transport_all_task_dev_remove_state(cmd);
  603. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  604. complete(&T_TASK(cmd)->transport_lun_stop_comp);
  605. return 1;
  606. }
  607. /*
  608. * Determine if frontend context caller is requesting the stopping of
  609. * this command for frontend excpections.
  610. */
  611. if (atomic_read(&T_TASK(cmd)->t_transport_stop)) {
  612. DEBUG_CS("%s:%d atomic_read(&T_TASK(cmd)->t_transport_stop) =="
  613. " TRUE for ITT: 0x%08x\n", __func__, __LINE__,
  614. CMD_TFO(cmd)->get_task_tag(cmd));
  615. cmd->deferred_t_state = cmd->t_state;
  616. cmd->t_state = TRANSPORT_DEFERRED_CMD;
  617. if (transport_off == 2)
  618. transport_all_task_dev_remove_state(cmd);
  619. /*
  620. * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
  621. * to FE.
  622. */
  623. if (transport_off == 2)
  624. cmd->se_lun = NULL;
  625. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  626. complete(&T_TASK(cmd)->t_transport_stop_comp);
  627. return 1;
  628. }
  629. if (transport_off) {
  630. atomic_set(&T_TASK(cmd)->t_transport_active, 0);
  631. if (transport_off == 2) {
  632. transport_all_task_dev_remove_state(cmd);
  633. /*
  634. * Clear struct se_cmd->se_lun before the transport_off == 2
  635. * handoff to fabric module.
  636. */
  637. cmd->se_lun = NULL;
  638. /*
  639. * Some fabric modules like tcm_loop can release
  640. * their internally allocated I/O refrence now and
  641. * struct se_cmd now.
  642. */
  643. if (CMD_TFO(cmd)->check_stop_free != NULL) {
  644. spin_unlock_irqrestore(
  645. &T_TASK(cmd)->t_state_lock, flags);
  646. CMD_TFO(cmd)->check_stop_free(cmd);
  647. return 1;
  648. }
  649. }
  650. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  651. return 0;
  652. } else if (t_state)
  653. cmd->t_state = t_state;
  654. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  655. return 0;
  656. }
  657. static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
  658. {
  659. return transport_cmd_check_stop(cmd, 2, 0);
  660. }
  661. static void transport_lun_remove_cmd(struct se_cmd *cmd)
  662. {
  663. struct se_lun *lun = SE_LUN(cmd);
  664. unsigned long flags;
  665. if (!lun)
  666. return;
  667. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  668. if (!(atomic_read(&T_TASK(cmd)->transport_dev_active))) {
  669. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  670. goto check_lun;
  671. }
  672. atomic_set(&T_TASK(cmd)->transport_dev_active, 0);
  673. transport_all_task_dev_remove_state(cmd);
  674. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  675. transport_free_dev_tasks(cmd);
  676. check_lun:
  677. spin_lock_irqsave(&lun->lun_cmd_lock, flags);
  678. if (atomic_read(&T_TASK(cmd)->transport_lun_active)) {
  679. list_del(&cmd->se_lun_list);
  680. atomic_set(&T_TASK(cmd)->transport_lun_active, 0);
  681. #if 0
  682. printk(KERN_INFO "Removed ITT: 0x%08x from LUN LIST[%d]\n"
  683. CMD_TFO(cmd)->get_task_tag(cmd), lun->unpacked_lun);
  684. #endif
  685. }
  686. spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
  687. }
  688. void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
  689. {
  690. transport_remove_cmd_from_queue(cmd, SE_DEV(cmd)->dev_queue_obj);
  691. transport_lun_remove_cmd(cmd);
  692. if (transport_cmd_check_stop_to_fabric(cmd))
  693. return;
  694. if (remove)
  695. transport_generic_remove(cmd, 0, 0);
  696. }
  697. void transport_cmd_finish_abort_tmr(struct se_cmd *cmd)
  698. {
  699. transport_remove_cmd_from_queue(cmd, SE_DEV(cmd)->dev_queue_obj);
  700. if (transport_cmd_check_stop_to_fabric(cmd))
  701. return;
  702. transport_generic_remove(cmd, 0, 0);
  703. }
  704. static int transport_add_cmd_to_queue(
  705. struct se_cmd *cmd,
  706. int t_state)
  707. {
  708. struct se_device *dev = cmd->se_dev;
  709. struct se_queue_obj *qobj = dev->dev_queue_obj;
  710. struct se_queue_req *qr;
  711. unsigned long flags;
  712. qr = kzalloc(sizeof(struct se_queue_req), GFP_ATOMIC);
  713. if (!(qr)) {
  714. printk(KERN_ERR "Unable to allocate memory for"
  715. " struct se_queue_req\n");
  716. return -1;
  717. }
  718. INIT_LIST_HEAD(&qr->qr_list);
  719. qr->cmd = (void *)cmd;
  720. qr->state = t_state;
  721. if (t_state) {
  722. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  723. cmd->t_state = t_state;
  724. atomic_set(&T_TASK(cmd)->t_transport_active, 1);
  725. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  726. }
  727. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  728. list_add_tail(&qr->qr_list, &qobj->qobj_list);
  729. atomic_inc(&T_TASK(cmd)->t_transport_queue_active);
  730. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  731. atomic_inc(&qobj->queue_cnt);
  732. wake_up_interruptible(&qobj->thread_wq);
  733. return 0;
  734. }
  735. /*
  736. * Called with struct se_queue_obj->cmd_queue_lock held.
  737. */
  738. static struct se_queue_req *
  739. __transport_get_qr_from_queue(struct se_queue_obj *qobj)
  740. {
  741. struct se_cmd *cmd;
  742. struct se_queue_req *qr = NULL;
  743. if (list_empty(&qobj->qobj_list))
  744. return NULL;
  745. list_for_each_entry(qr, &qobj->qobj_list, qr_list)
  746. break;
  747. if (qr->cmd) {
  748. cmd = (struct se_cmd *)qr->cmd;
  749. atomic_dec(&T_TASK(cmd)->t_transport_queue_active);
  750. }
  751. list_del(&qr->qr_list);
  752. atomic_dec(&qobj->queue_cnt);
  753. return qr;
  754. }
  755. static struct se_queue_req *
  756. transport_get_qr_from_queue(struct se_queue_obj *qobj)
  757. {
  758. struct se_cmd *cmd;
  759. struct se_queue_req *qr;
  760. unsigned long flags;
  761. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  762. if (list_empty(&qobj->qobj_list)) {
  763. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  764. return NULL;
  765. }
  766. list_for_each_entry(qr, &qobj->qobj_list, qr_list)
  767. break;
  768. if (qr->cmd) {
  769. cmd = (struct se_cmd *)qr->cmd;
  770. atomic_dec(&T_TASK(cmd)->t_transport_queue_active);
  771. }
  772. list_del(&qr->qr_list);
  773. atomic_dec(&qobj->queue_cnt);
  774. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  775. return qr;
  776. }
  777. static void transport_remove_cmd_from_queue(struct se_cmd *cmd,
  778. struct se_queue_obj *qobj)
  779. {
  780. struct se_cmd *q_cmd;
  781. struct se_queue_req *qr = NULL, *qr_p = NULL;
  782. unsigned long flags;
  783. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  784. if (!(atomic_read(&T_TASK(cmd)->t_transport_queue_active))) {
  785. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  786. return;
  787. }
  788. list_for_each_entry_safe(qr, qr_p, &qobj->qobj_list, qr_list) {
  789. q_cmd = (struct se_cmd *)qr->cmd;
  790. if (q_cmd != cmd)
  791. continue;
  792. atomic_dec(&T_TASK(q_cmd)->t_transport_queue_active);
  793. atomic_dec(&qobj->queue_cnt);
  794. list_del(&qr->qr_list);
  795. kfree(qr);
  796. }
  797. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  798. if (atomic_read(&T_TASK(cmd)->t_transport_queue_active)) {
  799. printk(KERN_ERR "ITT: 0x%08x t_transport_queue_active: %d\n",
  800. CMD_TFO(cmd)->get_task_tag(cmd),
  801. atomic_read(&T_TASK(cmd)->t_transport_queue_active));
  802. }
  803. }
  804. /*
  805. * Completion function used by TCM subsystem plugins (such as FILEIO)
  806. * for queueing up response from struct se_subsystem_api->do_task()
  807. */
  808. void transport_complete_sync_cache(struct se_cmd *cmd, int good)
  809. {
  810. struct se_task *task = list_entry(T_TASK(cmd)->t_task_list.next,
  811. struct se_task, t_list);
  812. if (good) {
  813. cmd->scsi_status = SAM_STAT_GOOD;
  814. task->task_scsi_status = GOOD;
  815. } else {
  816. task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
  817. task->task_error_status = PYX_TRANSPORT_ILLEGAL_REQUEST;
  818. TASK_CMD(task)->transport_error_status =
  819. PYX_TRANSPORT_ILLEGAL_REQUEST;
  820. }
  821. transport_complete_task(task, good);
  822. }
  823. EXPORT_SYMBOL(transport_complete_sync_cache);
  824. /* transport_complete_task():
  825. *
  826. * Called from interrupt and non interrupt context depending
  827. * on the transport plugin.
  828. */
  829. void transport_complete_task(struct se_task *task, int success)
  830. {
  831. struct se_cmd *cmd = TASK_CMD(task);
  832. struct se_device *dev = task->se_dev;
  833. int t_state;
  834. unsigned long flags;
  835. #if 0
  836. printk(KERN_INFO "task: %p CDB: 0x%02x obj_ptr: %p\n", task,
  837. T_TASK(cmd)->t_task_cdb[0], dev);
  838. #endif
  839. if (dev) {
  840. spin_lock_irqsave(&SE_HBA(dev)->hba_queue_lock, flags);
  841. atomic_inc(&dev->depth_left);
  842. atomic_inc(&SE_HBA(dev)->left_queue_depth);
  843. spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
  844. }
  845. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  846. atomic_set(&task->task_active, 0);
  847. /*
  848. * See if any sense data exists, if so set the TASK_SENSE flag.
  849. * Also check for any other post completion work that needs to be
  850. * done by the plugins.
  851. */
  852. if (dev && dev->transport->transport_complete) {
  853. if (dev->transport->transport_complete(task) != 0) {
  854. cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
  855. task->task_sense = 1;
  856. success = 1;
  857. }
  858. }
  859. /*
  860. * See if we are waiting for outstanding struct se_task
  861. * to complete for an exception condition
  862. */
  863. if (atomic_read(&task->task_stop)) {
  864. /*
  865. * Decrement T_TASK(cmd)->t_se_count if this task had
  866. * previously thrown its timeout exception handler.
  867. */
  868. if (atomic_read(&task->task_timeout)) {
  869. atomic_dec(&T_TASK(cmd)->t_se_count);
  870. atomic_set(&task->task_timeout, 0);
  871. }
  872. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  873. complete(&task->task_stop_comp);
  874. return;
  875. }
  876. /*
  877. * If the task's timeout handler has fired, use the t_task_cdbs_timeout
  878. * left counter to determine when the struct se_cmd is ready to be queued to
  879. * the processing thread.
  880. */
  881. if (atomic_read(&task->task_timeout)) {
  882. if (!(atomic_dec_and_test(
  883. &T_TASK(cmd)->t_task_cdbs_timeout_left))) {
  884. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  885. flags);
  886. return;
  887. }
  888. t_state = TRANSPORT_COMPLETE_TIMEOUT;
  889. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  890. transport_add_cmd_to_queue(cmd, t_state);
  891. return;
  892. }
  893. atomic_dec(&T_TASK(cmd)->t_task_cdbs_timeout_left);
  894. /*
  895. * Decrement the outstanding t_task_cdbs_left count. The last
  896. * struct se_task from struct se_cmd will complete itself into the
  897. * device queue depending upon int success.
  898. */
  899. if (!(atomic_dec_and_test(&T_TASK(cmd)->t_task_cdbs_left))) {
  900. if (!success)
  901. T_TASK(cmd)->t_tasks_failed = 1;
  902. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  903. return;
  904. }
  905. if (!success || T_TASK(cmd)->t_tasks_failed) {
  906. t_state = TRANSPORT_COMPLETE_FAILURE;
  907. if (!task->task_error_status) {
  908. task->task_error_status =
  909. PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  910. cmd->transport_error_status =
  911. PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  912. }
  913. } else {
  914. atomic_set(&T_TASK(cmd)->t_transport_complete, 1);
  915. t_state = TRANSPORT_COMPLETE_OK;
  916. }
  917. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  918. transport_add_cmd_to_queue(cmd, t_state);
  919. }
  920. EXPORT_SYMBOL(transport_complete_task);
  921. /*
  922. * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
  923. * struct se_task list are ready to be added to the active execution list
  924. * struct se_device
  925. * Called with se_dev_t->execute_task_lock called.
  926. */
  927. static inline int transport_add_task_check_sam_attr(
  928. struct se_task *task,
  929. struct se_task *task_prev,
  930. struct se_device *dev)
  931. {
  932. /*
  933. * No SAM Task attribute emulation enabled, add to tail of
  934. * execution queue
  935. */
  936. if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
  937. list_add_tail(&task->t_execute_list, &dev->execute_task_list);
  938. return 0;
  939. }
  940. /*
  941. * HEAD_OF_QUEUE attribute for received CDB, which means
  942. * the first task that is associated with a struct se_cmd goes to
  943. * head of the struct se_device->execute_task_list, and task_prev
  944. * after that for each subsequent task
  945. */
  946. if (task->task_se_cmd->sam_task_attr == TASK_ATTR_HOQ) {
  947. list_add(&task->t_execute_list,
  948. (task_prev != NULL) ?
  949. &task_prev->t_execute_list :
  950. &dev->execute_task_list);
  951. DEBUG_STA("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
  952. " in execution queue\n",
  953. T_TASK(task->task_se_cmd)->t_task_cdb[0]);
  954. return 1;
  955. }
  956. /*
  957. * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
  958. * transitioned from Dermant -> Active state, and are added to the end
  959. * of the struct se_device->execute_task_list
  960. */
  961. list_add_tail(&task->t_execute_list, &dev->execute_task_list);
  962. return 0;
  963. }
  964. /* __transport_add_task_to_execute_queue():
  965. *
  966. * Called with se_dev_t->execute_task_lock called.
  967. */
  968. static void __transport_add_task_to_execute_queue(
  969. struct se_task *task,
  970. struct se_task *task_prev,
  971. struct se_device *dev)
  972. {
  973. int head_of_queue;
  974. head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
  975. atomic_inc(&dev->execute_tasks);
  976. if (atomic_read(&task->task_state_active))
  977. return;
  978. /*
  979. * Determine if this task needs to go to HEAD_OF_QUEUE for the
  980. * state list as well. Running with SAM Task Attribute emulation
  981. * will always return head_of_queue == 0 here
  982. */
  983. if (head_of_queue)
  984. list_add(&task->t_state_list, (task_prev) ?
  985. &task_prev->t_state_list :
  986. &dev->state_task_list);
  987. else
  988. list_add_tail(&task->t_state_list, &dev->state_task_list);
  989. atomic_set(&task->task_state_active, 1);
  990. DEBUG_TSTATE("Added ITT: 0x%08x task[%p] to dev: %p\n",
  991. CMD_TFO(task->task_se_cmd)->get_task_tag(task->task_se_cmd),
  992. task, dev);
  993. }
  994. static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
  995. {
  996. struct se_device *dev;
  997. struct se_task *task;
  998. unsigned long flags;
  999. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  1000. list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
  1001. dev = task->se_dev;
  1002. if (atomic_read(&task->task_state_active))
  1003. continue;
  1004. spin_lock(&dev->execute_task_lock);
  1005. list_add_tail(&task->t_state_list, &dev->state_task_list);
  1006. atomic_set(&task->task_state_active, 1);
  1007. DEBUG_TSTATE("Added ITT: 0x%08x task[%p] to dev: %p\n",
  1008. CMD_TFO(task->task_se_cmd)->get_task_tag(
  1009. task->task_se_cmd), task, dev);
  1010. spin_unlock(&dev->execute_task_lock);
  1011. }
  1012. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  1013. }
  1014. static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
  1015. {
  1016. struct se_device *dev = SE_DEV(cmd);
  1017. struct se_task *task, *task_prev = NULL;
  1018. unsigned long flags;
  1019. spin_lock_irqsave(&dev->execute_task_lock, flags);
  1020. list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
  1021. if (atomic_read(&task->task_execute_queue))
  1022. continue;
  1023. /*
  1024. * __transport_add_task_to_execute_queue() handles the
  1025. * SAM Task Attribute emulation if enabled
  1026. */
  1027. __transport_add_task_to_execute_queue(task, task_prev, dev);
  1028. atomic_set(&task->task_execute_queue, 1);
  1029. task_prev = task;
  1030. }
  1031. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  1032. return;
  1033. }
  1034. /* transport_get_task_from_execute_queue():
  1035. *
  1036. * Called with dev->execute_task_lock held.
  1037. */
  1038. static struct se_task *
  1039. transport_get_task_from_execute_queue(struct se_device *dev)
  1040. {
  1041. struct se_task *task;
  1042. if (list_empty(&dev->execute_task_list))
  1043. return NULL;
  1044. list_for_each_entry(task, &dev->execute_task_list, t_execute_list)
  1045. break;
  1046. list_del(&task->t_execute_list);
  1047. atomic_dec(&dev->execute_tasks);
  1048. return task;
  1049. }
  1050. /* transport_remove_task_from_execute_queue():
  1051. *
  1052. *
  1053. */
  1054. void transport_remove_task_from_execute_queue(
  1055. struct se_task *task,
  1056. struct se_device *dev)
  1057. {
  1058. unsigned long flags;
  1059. spin_lock_irqsave(&dev->execute_task_lock, flags);
  1060. list_del(&task->t_execute_list);
  1061. atomic_dec(&dev->execute_tasks);
  1062. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  1063. }
  1064. unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
  1065. {
  1066. switch (cmd->data_direction) {
  1067. case DMA_NONE:
  1068. return "NONE";
  1069. case DMA_FROM_DEVICE:
  1070. return "READ";
  1071. case DMA_TO_DEVICE:
  1072. return "WRITE";
  1073. case DMA_BIDIRECTIONAL:
  1074. return "BIDI";
  1075. default:
  1076. break;
  1077. }
  1078. return "UNKNOWN";
  1079. }
  1080. void transport_dump_dev_state(
  1081. struct se_device *dev,
  1082. char *b,
  1083. int *bl)
  1084. {
  1085. *bl += sprintf(b + *bl, "Status: ");
  1086. switch (dev->dev_status) {
  1087. case TRANSPORT_DEVICE_ACTIVATED:
  1088. *bl += sprintf(b + *bl, "ACTIVATED");
  1089. break;
  1090. case TRANSPORT_DEVICE_DEACTIVATED:
  1091. *bl += sprintf(b + *bl, "DEACTIVATED");
  1092. break;
  1093. case TRANSPORT_DEVICE_SHUTDOWN:
  1094. *bl += sprintf(b + *bl, "SHUTDOWN");
  1095. break;
  1096. case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
  1097. case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
  1098. *bl += sprintf(b + *bl, "OFFLINE");
  1099. break;
  1100. default:
  1101. *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
  1102. break;
  1103. }
  1104. *bl += sprintf(b + *bl, " Execute/Left/Max Queue Depth: %d/%d/%d",
  1105. atomic_read(&dev->execute_tasks), atomic_read(&dev->depth_left),
  1106. dev->queue_depth);
  1107. *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
  1108. DEV_ATTRIB(dev)->block_size, DEV_ATTRIB(dev)->max_sectors);
  1109. *bl += sprintf(b + *bl, " ");
  1110. }
  1111. /* transport_release_all_cmds():
  1112. *
  1113. *
  1114. */
  1115. static void transport_release_all_cmds(struct se_device *dev)
  1116. {
  1117. struct se_cmd *cmd = NULL;
  1118. struct se_queue_req *qr = NULL, *qr_p = NULL;
  1119. int bug_out = 0, t_state;
  1120. unsigned long flags;
  1121. spin_lock_irqsave(&dev->dev_queue_obj->cmd_queue_lock, flags);
  1122. list_for_each_entry_safe(qr, qr_p, &dev->dev_queue_obj->qobj_list,
  1123. qr_list) {
  1124. cmd = (struct se_cmd *)qr->cmd;
  1125. t_state = qr->state;
  1126. list_del(&qr->qr_list);
  1127. kfree(qr);
  1128. spin_unlock_irqrestore(&dev->dev_queue_obj->cmd_queue_lock,
  1129. flags);
  1130. printk(KERN_ERR "Releasing ITT: 0x%08x, i_state: %u,"
  1131. " t_state: %u directly\n",
  1132. CMD_TFO(cmd)->get_task_tag(cmd),
  1133. CMD_TFO(cmd)->get_cmd_state(cmd), t_state);
  1134. transport_release_fe_cmd(cmd);
  1135. bug_out = 1;
  1136. spin_lock_irqsave(&dev->dev_queue_obj->cmd_queue_lock, flags);
  1137. }
  1138. spin_unlock_irqrestore(&dev->dev_queue_obj->cmd_queue_lock, flags);
  1139. #if 0
  1140. if (bug_out)
  1141. BUG();
  1142. #endif
  1143. }
  1144. void transport_dump_vpd_proto_id(
  1145. struct t10_vpd *vpd,
  1146. unsigned char *p_buf,
  1147. int p_buf_len)
  1148. {
  1149. unsigned char buf[VPD_TMP_BUF_SIZE];
  1150. int len;
  1151. memset(buf, 0, VPD_TMP_BUF_SIZE);
  1152. len = sprintf(buf, "T10 VPD Protocol Identifier: ");
  1153. switch (vpd->protocol_identifier) {
  1154. case 0x00:
  1155. sprintf(buf+len, "Fibre Channel\n");
  1156. break;
  1157. case 0x10:
  1158. sprintf(buf+len, "Parallel SCSI\n");
  1159. break;
  1160. case 0x20:
  1161. sprintf(buf+len, "SSA\n");
  1162. break;
  1163. case 0x30:
  1164. sprintf(buf+len, "IEEE 1394\n");
  1165. break;
  1166. case 0x40:
  1167. sprintf(buf+len, "SCSI Remote Direct Memory Access"
  1168. " Protocol\n");
  1169. break;
  1170. case 0x50:
  1171. sprintf(buf+len, "Internet SCSI (iSCSI)\n");
  1172. break;
  1173. case 0x60:
  1174. sprintf(buf+len, "SAS Serial SCSI Protocol\n");
  1175. break;
  1176. case 0x70:
  1177. sprintf(buf+len, "Automation/Drive Interface Transport"
  1178. " Protocol\n");
  1179. break;
  1180. case 0x80:
  1181. sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
  1182. break;
  1183. default:
  1184. sprintf(buf+len, "Unknown 0x%02x\n",
  1185. vpd->protocol_identifier);
  1186. break;
  1187. }
  1188. if (p_buf)
  1189. strncpy(p_buf, buf, p_buf_len);
  1190. else
  1191. printk(KERN_INFO "%s", buf);
  1192. }
  1193. void
  1194. transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
  1195. {
  1196. /*
  1197. * Check if the Protocol Identifier Valid (PIV) bit is set..
  1198. *
  1199. * from spc3r23.pdf section 7.5.1
  1200. */
  1201. if (page_83[1] & 0x80) {
  1202. vpd->protocol_identifier = (page_83[0] & 0xf0);
  1203. vpd->protocol_identifier_set = 1;
  1204. transport_dump_vpd_proto_id(vpd, NULL, 0);
  1205. }
  1206. }
  1207. EXPORT_SYMBOL(transport_set_vpd_proto_id);
  1208. int transport_dump_vpd_assoc(
  1209. struct t10_vpd *vpd,
  1210. unsigned char *p_buf,
  1211. int p_buf_len)
  1212. {
  1213. unsigned char buf[VPD_TMP_BUF_SIZE];
  1214. int ret = 0, len;
  1215. memset(buf, 0, VPD_TMP_BUF_SIZE);
  1216. len = sprintf(buf, "T10 VPD Identifier Association: ");
  1217. switch (vpd->association) {
  1218. case 0x00:
  1219. sprintf(buf+len, "addressed logical unit\n");
  1220. break;
  1221. case 0x10:
  1222. sprintf(buf+len, "target port\n");
  1223. break;
  1224. case 0x20:
  1225. sprintf(buf+len, "SCSI target device\n");
  1226. break;
  1227. default:
  1228. sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
  1229. ret = -1;
  1230. break;
  1231. }
  1232. if (p_buf)
  1233. strncpy(p_buf, buf, p_buf_len);
  1234. else
  1235. printk("%s", buf);
  1236. return ret;
  1237. }
  1238. int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
  1239. {
  1240. /*
  1241. * The VPD identification association..
  1242. *
  1243. * from spc3r23.pdf Section 7.6.3.1 Table 297
  1244. */
  1245. vpd->association = (page_83[1] & 0x30);
  1246. return transport_dump_vpd_assoc(vpd, NULL, 0);
  1247. }
  1248. EXPORT_SYMBOL(transport_set_vpd_assoc);
  1249. int transport_dump_vpd_ident_type(
  1250. struct t10_vpd *vpd,
  1251. unsigned char *p_buf,
  1252. int p_buf_len)
  1253. {
  1254. unsigned char buf[VPD_TMP_BUF_SIZE];
  1255. int ret = 0, len;
  1256. memset(buf, 0, VPD_TMP_BUF_SIZE);
  1257. len = sprintf(buf, "T10 VPD Identifier Type: ");
  1258. switch (vpd->device_identifier_type) {
  1259. case 0x00:
  1260. sprintf(buf+len, "Vendor specific\n");
  1261. break;
  1262. case 0x01:
  1263. sprintf(buf+len, "T10 Vendor ID based\n");
  1264. break;
  1265. case 0x02:
  1266. sprintf(buf+len, "EUI-64 based\n");
  1267. break;
  1268. case 0x03:
  1269. sprintf(buf+len, "NAA\n");
  1270. break;
  1271. case 0x04:
  1272. sprintf(buf+len, "Relative target port identifier\n");
  1273. break;
  1274. case 0x08:
  1275. sprintf(buf+len, "SCSI name string\n");
  1276. break;
  1277. default:
  1278. sprintf(buf+len, "Unsupported: 0x%02x\n",
  1279. vpd->device_identifier_type);
  1280. ret = -1;
  1281. break;
  1282. }
  1283. if (p_buf)
  1284. strncpy(p_buf, buf, p_buf_len);
  1285. else
  1286. printk("%s", buf);
  1287. return ret;
  1288. }
  1289. int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
  1290. {
  1291. /*
  1292. * The VPD identifier type..
  1293. *
  1294. * from spc3r23.pdf Section 7.6.3.1 Table 298
  1295. */
  1296. vpd->device_identifier_type = (page_83[1] & 0x0f);
  1297. return transport_dump_vpd_ident_type(vpd, NULL, 0);
  1298. }
  1299. EXPORT_SYMBOL(transport_set_vpd_ident_type);
  1300. int transport_dump_vpd_ident(
  1301. struct t10_vpd *vpd,
  1302. unsigned char *p_buf,
  1303. int p_buf_len)
  1304. {
  1305. unsigned char buf[VPD_TMP_BUF_SIZE];
  1306. int ret = 0;
  1307. memset(buf, 0, VPD_TMP_BUF_SIZE);
  1308. switch (vpd->device_identifier_code_set) {
  1309. case 0x01: /* Binary */
  1310. sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
  1311. &vpd->device_identifier[0]);
  1312. break;
  1313. case 0x02: /* ASCII */
  1314. sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
  1315. &vpd->device_identifier[0]);
  1316. break;
  1317. case 0x03: /* UTF-8 */
  1318. sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
  1319. &vpd->device_identifier[0]);
  1320. break;
  1321. default:
  1322. sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
  1323. " 0x%02x", vpd->device_identifier_code_set);
  1324. ret = -1;
  1325. break;
  1326. }
  1327. if (p_buf)
  1328. strncpy(p_buf, buf, p_buf_len);
  1329. else
  1330. printk("%s", buf);
  1331. return ret;
  1332. }
  1333. int
  1334. transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
  1335. {
  1336. static const char hex_str[] = "0123456789abcdef";
  1337. int j = 0, i = 4; /* offset to start of the identifer */
  1338. /*
  1339. * The VPD Code Set (encoding)
  1340. *
  1341. * from spc3r23.pdf Section 7.6.3.1 Table 296
  1342. */
  1343. vpd->device_identifier_code_set = (page_83[0] & 0x0f);
  1344. switch (vpd->device_identifier_code_set) {
  1345. case 0x01: /* Binary */
  1346. vpd->device_identifier[j++] =
  1347. hex_str[vpd->device_identifier_type];
  1348. while (i < (4 + page_83[3])) {
  1349. vpd->device_identifier[j++] =
  1350. hex_str[(page_83[i] & 0xf0) >> 4];
  1351. vpd->device_identifier[j++] =
  1352. hex_str[page_83[i] & 0x0f];
  1353. i++;
  1354. }
  1355. break;
  1356. case 0x02: /* ASCII */
  1357. case 0x03: /* UTF-8 */
  1358. while (i < (4 + page_83[3]))
  1359. vpd->device_identifier[j++] = page_83[i++];
  1360. break;
  1361. default:
  1362. break;
  1363. }
  1364. return transport_dump_vpd_ident(vpd, NULL, 0);
  1365. }
  1366. EXPORT_SYMBOL(transport_set_vpd_ident);
  1367. static void core_setup_task_attr_emulation(struct se_device *dev)
  1368. {
  1369. /*
  1370. * If this device is from Target_Core_Mod/pSCSI, disable the
  1371. * SAM Task Attribute emulation.
  1372. *
  1373. * This is currently not available in upsream Linux/SCSI Target
  1374. * mode code, and is assumed to be disabled while using TCM/pSCSI.
  1375. */
  1376. if (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  1377. dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
  1378. return;
  1379. }
  1380. dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
  1381. DEBUG_STA("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
  1382. " device\n", TRANSPORT(dev)->name,
  1383. TRANSPORT(dev)->get_device_rev(dev));
  1384. }
  1385. static void scsi_dump_inquiry(struct se_device *dev)
  1386. {
  1387. struct t10_wwn *wwn = DEV_T10_WWN(dev);
  1388. int i, device_type;
  1389. /*
  1390. * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
  1391. */
  1392. printk(" Vendor: ");
  1393. for (i = 0; i < 8; i++)
  1394. if (wwn->vendor[i] >= 0x20)
  1395. printk("%c", wwn->vendor[i]);
  1396. else
  1397. printk(" ");
  1398. printk(" Model: ");
  1399. for (i = 0; i < 16; i++)
  1400. if (wwn->model[i] >= 0x20)
  1401. printk("%c", wwn->model[i]);
  1402. else
  1403. printk(" ");
  1404. printk(" Revision: ");
  1405. for (i = 0; i < 4; i++)
  1406. if (wwn->revision[i] >= 0x20)
  1407. printk("%c", wwn->revision[i]);
  1408. else
  1409. printk(" ");
  1410. printk("\n");
  1411. device_type = TRANSPORT(dev)->get_device_type(dev);
  1412. printk(" Type: %s ", scsi_device_type(device_type));
  1413. printk(" ANSI SCSI revision: %02x\n",
  1414. TRANSPORT(dev)->get_device_rev(dev));
  1415. }
  1416. struct se_device *transport_add_device_to_core_hba(
  1417. struct se_hba *hba,
  1418. struct se_subsystem_api *transport,
  1419. struct se_subsystem_dev *se_dev,
  1420. u32 device_flags,
  1421. void *transport_dev,
  1422. struct se_dev_limits *dev_limits,
  1423. const char *inquiry_prod,
  1424. const char *inquiry_rev)
  1425. {
  1426. int force_pt;
  1427. struct se_device *dev;
  1428. dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
  1429. if (!(dev)) {
  1430. printk(KERN_ERR "Unable to allocate memory for se_dev_t\n");
  1431. return NULL;
  1432. }
  1433. dev->dev_queue_obj = kzalloc(sizeof(struct se_queue_obj), GFP_KERNEL);
  1434. if (!(dev->dev_queue_obj)) {
  1435. printk(KERN_ERR "Unable to allocate memory for"
  1436. " dev->dev_queue_obj\n");
  1437. kfree(dev);
  1438. return NULL;
  1439. }
  1440. transport_init_queue_obj(dev->dev_queue_obj);
  1441. dev->dev_status_queue_obj = kzalloc(sizeof(struct se_queue_obj),
  1442. GFP_KERNEL);
  1443. if (!(dev->dev_status_queue_obj)) {
  1444. printk(KERN_ERR "Unable to allocate memory for"
  1445. " dev->dev_status_queue_obj\n");
  1446. kfree(dev->dev_queue_obj);
  1447. kfree(dev);
  1448. return NULL;
  1449. }
  1450. transport_init_queue_obj(dev->dev_status_queue_obj);
  1451. dev->dev_flags = device_flags;
  1452. dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
  1453. dev->dev_ptr = (void *) transport_dev;
  1454. dev->se_hba = hba;
  1455. dev->se_sub_dev = se_dev;
  1456. dev->transport = transport;
  1457. atomic_set(&dev->active_cmds, 0);
  1458. INIT_LIST_HEAD(&dev->dev_list);
  1459. INIT_LIST_HEAD(&dev->dev_sep_list);
  1460. INIT_LIST_HEAD(&dev->dev_tmr_list);
  1461. INIT_LIST_HEAD(&dev->execute_task_list);
  1462. INIT_LIST_HEAD(&dev->delayed_cmd_list);
  1463. INIT_LIST_HEAD(&dev->ordered_cmd_list);
  1464. INIT_LIST_HEAD(&dev->state_task_list);
  1465. spin_lock_init(&dev->execute_task_lock);
  1466. spin_lock_init(&dev->delayed_cmd_lock);
  1467. spin_lock_init(&dev->ordered_cmd_lock);
  1468. spin_lock_init(&dev->state_task_lock);
  1469. spin_lock_init(&dev->dev_alua_lock);
  1470. spin_lock_init(&dev->dev_reservation_lock);
  1471. spin_lock_init(&dev->dev_status_lock);
  1472. spin_lock_init(&dev->dev_status_thr_lock);
  1473. spin_lock_init(&dev->se_port_lock);
  1474. spin_lock_init(&dev->se_tmr_lock);
  1475. dev->queue_depth = dev_limits->queue_depth;
  1476. atomic_set(&dev->depth_left, dev->queue_depth);
  1477. atomic_set(&dev->dev_ordered_id, 0);
  1478. se_dev_set_default_attribs(dev, dev_limits);
  1479. dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
  1480. dev->creation_time = get_jiffies_64();
  1481. spin_lock_init(&dev->stats_lock);
  1482. spin_lock(&hba->device_lock);
  1483. list_add_tail(&dev->dev_list, &hba->hba_dev_list);
  1484. hba->dev_count++;
  1485. spin_unlock(&hba->device_lock);
  1486. /*
  1487. * Setup the SAM Task Attribute emulation for struct se_device
  1488. */
  1489. core_setup_task_attr_emulation(dev);
  1490. /*
  1491. * Force PR and ALUA passthrough emulation with internal object use.
  1492. */
  1493. force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
  1494. /*
  1495. * Setup the Reservations infrastructure for struct se_device
  1496. */
  1497. core_setup_reservations(dev, force_pt);
  1498. /*
  1499. * Setup the Asymmetric Logical Unit Assignment for struct se_device
  1500. */
  1501. if (core_setup_alua(dev, force_pt) < 0)
  1502. goto out;
  1503. /*
  1504. * Startup the struct se_device processing thread
  1505. */
  1506. dev->process_thread = kthread_run(transport_processing_thread, dev,
  1507. "LIO_%s", TRANSPORT(dev)->name);
  1508. if (IS_ERR(dev->process_thread)) {
  1509. printk(KERN_ERR "Unable to create kthread: LIO_%s\n",
  1510. TRANSPORT(dev)->name);
  1511. goto out;
  1512. }
  1513. /*
  1514. * Preload the initial INQUIRY const values if we are doing
  1515. * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
  1516. * passthrough because this is being provided by the backend LLD.
  1517. * This is required so that transport_get_inquiry() copies these
  1518. * originals once back into DEV_T10_WWN(dev) for the virtual device
  1519. * setup.
  1520. */
  1521. if (TRANSPORT(dev)->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
  1522. if (!(inquiry_prod) || !(inquiry_prod)) {
  1523. printk(KERN_ERR "All non TCM/pSCSI plugins require"
  1524. " INQUIRY consts\n");
  1525. goto out;
  1526. }
  1527. strncpy(&DEV_T10_WWN(dev)->vendor[0], "LIO-ORG", 8);
  1528. strncpy(&DEV_T10_WWN(dev)->model[0], inquiry_prod, 16);
  1529. strncpy(&DEV_T10_WWN(dev)->revision[0], inquiry_rev, 4);
  1530. }
  1531. scsi_dump_inquiry(dev);
  1532. return dev;
  1533. out:
  1534. kthread_stop(dev->process_thread);
  1535. spin_lock(&hba->device_lock);
  1536. list_del(&dev->dev_list);
  1537. hba->dev_count--;
  1538. spin_unlock(&hba->device_lock);
  1539. se_release_vpd_for_dev(dev);
  1540. kfree(dev->dev_status_queue_obj);
  1541. kfree(dev->dev_queue_obj);
  1542. kfree(dev);
  1543. return NULL;
  1544. }
  1545. EXPORT_SYMBOL(transport_add_device_to_core_hba);
  1546. /* transport_generic_prepare_cdb():
  1547. *
  1548. * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
  1549. * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
  1550. * The point of this is since we are mapping iSCSI LUNs to
  1551. * SCSI Target IDs having a non-zero LUN in the CDB will throw the
  1552. * devices and HBAs for a loop.
  1553. */
  1554. static inline void transport_generic_prepare_cdb(
  1555. unsigned char *cdb)
  1556. {
  1557. switch (cdb[0]) {
  1558. case READ_10: /* SBC - RDProtect */
  1559. case READ_12: /* SBC - RDProtect */
  1560. case READ_16: /* SBC - RDProtect */
  1561. case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
  1562. case VERIFY: /* SBC - VRProtect */
  1563. case VERIFY_16: /* SBC - VRProtect */
  1564. case WRITE_VERIFY: /* SBC - VRProtect */
  1565. case WRITE_VERIFY_12: /* SBC - VRProtect */
  1566. break;
  1567. default:
  1568. cdb[1] &= 0x1f; /* clear logical unit number */
  1569. break;
  1570. }
  1571. }
  1572. static struct se_task *
  1573. transport_generic_get_task(struct se_cmd *cmd,
  1574. enum dma_data_direction data_direction)
  1575. {
  1576. struct se_task *task;
  1577. struct se_device *dev = SE_DEV(cmd);
  1578. unsigned long flags;
  1579. task = dev->transport->alloc_task(cmd);
  1580. if (!task) {
  1581. printk(KERN_ERR "Unable to allocate struct se_task\n");
  1582. return NULL;
  1583. }
  1584. INIT_LIST_HEAD(&task->t_list);
  1585. INIT_LIST_HEAD(&task->t_execute_list);
  1586. INIT_LIST_HEAD(&task->t_state_list);
  1587. init_completion(&task->task_stop_comp);
  1588. task->task_no = T_TASK(cmd)->t_tasks_no++;
  1589. task->task_se_cmd = cmd;
  1590. task->se_dev = dev;
  1591. task->task_data_direction = data_direction;
  1592. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  1593. list_add_tail(&task->t_list, &T_TASK(cmd)->t_task_list);
  1594. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  1595. return task;
  1596. }
  1597. static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
  1598. void transport_device_setup_cmd(struct se_cmd *cmd)
  1599. {
  1600. cmd->se_dev = SE_LUN(cmd)->lun_se_dev;
  1601. }
  1602. EXPORT_SYMBOL(transport_device_setup_cmd);
  1603. /*
  1604. * Used by fabric modules containing a local struct se_cmd within their
  1605. * fabric dependent per I/O descriptor.
  1606. */
  1607. void transport_init_se_cmd(
  1608. struct se_cmd *cmd,
  1609. struct target_core_fabric_ops *tfo,
  1610. struct se_session *se_sess,
  1611. u32 data_length,
  1612. int data_direction,
  1613. int task_attr,
  1614. unsigned char *sense_buffer)
  1615. {
  1616. INIT_LIST_HEAD(&cmd->se_lun_list);
  1617. INIT_LIST_HEAD(&cmd->se_delayed_list);
  1618. INIT_LIST_HEAD(&cmd->se_ordered_list);
  1619. /*
  1620. * Setup t_task pointer to t_task_backstore
  1621. */
  1622. cmd->t_task = &cmd->t_task_backstore;
  1623. INIT_LIST_HEAD(&T_TASK(cmd)->t_task_list);
  1624. init_completion(&T_TASK(cmd)->transport_lun_fe_stop_comp);
  1625. init_completion(&T_TASK(cmd)->transport_lun_stop_comp);
  1626. init_completion(&T_TASK(cmd)->t_transport_stop_comp);
  1627. spin_lock_init(&T_TASK(cmd)->t_state_lock);
  1628. atomic_set(&T_TASK(cmd)->transport_dev_active, 1);
  1629. cmd->se_tfo = tfo;
  1630. cmd->se_sess = se_sess;
  1631. cmd->data_length = data_length;
  1632. cmd->data_direction = data_direction;
  1633. cmd->sam_task_attr = task_attr;
  1634. cmd->sense_buffer = sense_buffer;
  1635. }
  1636. EXPORT_SYMBOL(transport_init_se_cmd);
  1637. static int transport_check_alloc_task_attr(struct se_cmd *cmd)
  1638. {
  1639. /*
  1640. * Check if SAM Task Attribute emulation is enabled for this
  1641. * struct se_device storage object
  1642. */
  1643. if (SE_DEV(cmd)->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  1644. return 0;
  1645. if (cmd->sam_task_attr == TASK_ATTR_ACA) {
  1646. DEBUG_STA("SAM Task Attribute ACA"
  1647. " emulation is not supported\n");
  1648. return -1;
  1649. }
  1650. /*
  1651. * Used to determine when ORDERED commands should go from
  1652. * Dormant to Active status.
  1653. */
  1654. cmd->se_ordered_id = atomic_inc_return(&SE_DEV(cmd)->dev_ordered_id);
  1655. smp_mb__after_atomic_inc();
  1656. DEBUG_STA("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
  1657. cmd->se_ordered_id, cmd->sam_task_attr,
  1658. TRANSPORT(cmd->se_dev)->name);
  1659. return 0;
  1660. }
  1661. void transport_free_se_cmd(
  1662. struct se_cmd *se_cmd)
  1663. {
  1664. if (se_cmd->se_tmr_req)
  1665. core_tmr_release_req(se_cmd->se_tmr_req);
  1666. /*
  1667. * Check and free any extended CDB buffer that was allocated
  1668. */
  1669. if (T_TASK(se_cmd)->t_task_cdb != T_TASK(se_cmd)->__t_task_cdb)
  1670. kfree(T_TASK(se_cmd)->t_task_cdb);
  1671. }
  1672. EXPORT_SYMBOL(transport_free_se_cmd);
  1673. static void transport_generic_wait_for_tasks(struct se_cmd *, int, int);
  1674. /* transport_generic_allocate_tasks():
  1675. *
  1676. * Called from fabric RX Thread.
  1677. */
  1678. int transport_generic_allocate_tasks(
  1679. struct se_cmd *cmd,
  1680. unsigned char *cdb)
  1681. {
  1682. int ret;
  1683. transport_generic_prepare_cdb(cdb);
  1684. /*
  1685. * This is needed for early exceptions.
  1686. */
  1687. cmd->transport_wait_for_tasks = &transport_generic_wait_for_tasks;
  1688. transport_device_setup_cmd(cmd);
  1689. /*
  1690. * Ensure that the received CDB is less than the max (252 + 8) bytes
  1691. * for VARIABLE_LENGTH_CMD
  1692. */
  1693. if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
  1694. printk(KERN_ERR "Received SCSI CDB with command_size: %d that"
  1695. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  1696. scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
  1697. return -1;
  1698. }
  1699. /*
  1700. * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
  1701. * allocate the additional extended CDB buffer now.. Otherwise
  1702. * setup the pointer from __t_task_cdb to t_task_cdb.
  1703. */
  1704. if (scsi_command_size(cdb) > sizeof(T_TASK(cmd)->__t_task_cdb)) {
  1705. T_TASK(cmd)->t_task_cdb = kzalloc(scsi_command_size(cdb),
  1706. GFP_KERNEL);
  1707. if (!(T_TASK(cmd)->t_task_cdb)) {
  1708. printk(KERN_ERR "Unable to allocate T_TASK(cmd)->t_task_cdb"
  1709. " %u > sizeof(T_TASK(cmd)->__t_task_cdb): %lu ops\n",
  1710. scsi_command_size(cdb),
  1711. (unsigned long)sizeof(T_TASK(cmd)->__t_task_cdb));
  1712. return -1;
  1713. }
  1714. } else
  1715. T_TASK(cmd)->t_task_cdb = &T_TASK(cmd)->__t_task_cdb[0];
  1716. /*
  1717. * Copy the original CDB into T_TASK(cmd).
  1718. */
  1719. memcpy(T_TASK(cmd)->t_task_cdb, cdb, scsi_command_size(cdb));
  1720. /*
  1721. * Setup the received CDB based on SCSI defined opcodes and
  1722. * perform unit attention, persistent reservations and ALUA
  1723. * checks for virtual device backends. The T_TASK(cmd)->t_task_cdb
  1724. * pointer is expected to be setup before we reach this point.
  1725. */
  1726. ret = transport_generic_cmd_sequencer(cmd, cdb);
  1727. if (ret < 0)
  1728. return ret;
  1729. /*
  1730. * Check for SAM Task Attribute Emulation
  1731. */
  1732. if (transport_check_alloc_task_attr(cmd) < 0) {
  1733. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1734. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1735. return -2;
  1736. }
  1737. spin_lock(&cmd->se_lun->lun_sep_lock);
  1738. if (cmd->se_lun->lun_sep)
  1739. cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
  1740. spin_unlock(&cmd->se_lun->lun_sep_lock);
  1741. return 0;
  1742. }
  1743. EXPORT_SYMBOL(transport_generic_allocate_tasks);
  1744. /*
  1745. * Used by fabric module frontends not defining a TFO->new_cmd_map()
  1746. * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD statis
  1747. */
  1748. int transport_generic_handle_cdb(
  1749. struct se_cmd *cmd)
  1750. {
  1751. if (!SE_LUN(cmd)) {
  1752. dump_stack();
  1753. printk(KERN_ERR "SE_LUN(cmd) is NULL\n");
  1754. return -1;
  1755. }
  1756. transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD);
  1757. return 0;
  1758. }
  1759. EXPORT_SYMBOL(transport_generic_handle_cdb);
  1760. /*
  1761. * Used by fabric module frontends defining a TFO->new_cmd_map() caller
  1762. * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
  1763. * complete setup in TCM process context w/ TFO->new_cmd_map().
  1764. */
  1765. int transport_generic_handle_cdb_map(
  1766. struct se_cmd *cmd)
  1767. {
  1768. if (!SE_LUN(cmd)) {
  1769. dump_stack();
  1770. printk(KERN_ERR "SE_LUN(cmd) is NULL\n");
  1771. return -1;
  1772. }
  1773. transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP);
  1774. return 0;
  1775. }
  1776. EXPORT_SYMBOL(transport_generic_handle_cdb_map);
  1777. /* transport_generic_handle_data():
  1778. *
  1779. *
  1780. */
  1781. int transport_generic_handle_data(
  1782. struct se_cmd *cmd)
  1783. {
  1784. /*
  1785. * For the software fabric case, then we assume the nexus is being
  1786. * failed/shutdown when signals are pending from the kthread context
  1787. * caller, so we return a failure. For the HW target mode case running
  1788. * in interrupt code, the signal_pending() check is skipped.
  1789. */
  1790. if (!in_interrupt() && signal_pending(current))
  1791. return -1;
  1792. /*
  1793. * If the received CDB has aleady been ABORTED by the generic
  1794. * target engine, we now call transport_check_aborted_status()
  1795. * to queue any delated TASK_ABORTED status for the received CDB to the
  1796. * fabric module as we are expecting no futher incoming DATA OUT
  1797. * sequences at this point.
  1798. */
  1799. if (transport_check_aborted_status(cmd, 1) != 0)
  1800. return 0;
  1801. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE);
  1802. return 0;
  1803. }
  1804. EXPORT_SYMBOL(transport_generic_handle_data);
  1805. /* transport_generic_handle_tmr():
  1806. *
  1807. *
  1808. */
  1809. int transport_generic_handle_tmr(
  1810. struct se_cmd *cmd)
  1811. {
  1812. /*
  1813. * This is needed for early exceptions.
  1814. */
  1815. cmd->transport_wait_for_tasks = &transport_generic_wait_for_tasks;
  1816. transport_device_setup_cmd(cmd);
  1817. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR);
  1818. return 0;
  1819. }
  1820. EXPORT_SYMBOL(transport_generic_handle_tmr);
  1821. static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
  1822. {
  1823. struct se_task *task, *task_tmp;
  1824. unsigned long flags;
  1825. int ret = 0;
  1826. DEBUG_TS("ITT[0x%08x] - Stopping tasks\n",
  1827. CMD_TFO(cmd)->get_task_tag(cmd));
  1828. /*
  1829. * No tasks remain in the execution queue
  1830. */
  1831. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  1832. list_for_each_entry_safe(task, task_tmp,
  1833. &T_TASK(cmd)->t_task_list, t_list) {
  1834. DEBUG_TS("task_no[%d] - Processing task %p\n",
  1835. task->task_no, task);
  1836. /*
  1837. * If the struct se_task has not been sent and is not active,
  1838. * remove the struct se_task from the execution queue.
  1839. */
  1840. if (!atomic_read(&task->task_sent) &&
  1841. !atomic_read(&task->task_active)) {
  1842. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  1843. flags);
  1844. transport_remove_task_from_execute_queue(task,
  1845. task->se_dev);
  1846. DEBUG_TS("task_no[%d] - Removed from execute queue\n",
  1847. task->task_no);
  1848. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  1849. continue;
  1850. }
  1851. /*
  1852. * If the struct se_task is active, sleep until it is returned
  1853. * from the plugin.
  1854. */
  1855. if (atomic_read(&task->task_active)) {
  1856. atomic_set(&task->task_stop, 1);
  1857. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  1858. flags);
  1859. DEBUG_TS("task_no[%d] - Waiting to complete\n",
  1860. task->task_no);
  1861. wait_for_completion(&task->task_stop_comp);
  1862. DEBUG_TS("task_no[%d] - Stopped successfully\n",
  1863. task->task_no);
  1864. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  1865. atomic_dec(&T_TASK(cmd)->t_task_cdbs_left);
  1866. atomic_set(&task->task_active, 0);
  1867. atomic_set(&task->task_stop, 0);
  1868. } else {
  1869. DEBUG_TS("task_no[%d] - Did nothing\n", task->task_no);
  1870. ret++;
  1871. }
  1872. __transport_stop_task_timer(task, &flags);
  1873. }
  1874. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  1875. return ret;
  1876. }
  1877. static void transport_failure_reset_queue_depth(struct se_device *dev)
  1878. {
  1879. unsigned long flags;
  1880. spin_lock_irqsave(&SE_HBA(dev)->hba_queue_lock, flags);;
  1881. atomic_inc(&dev->depth_left);
  1882. atomic_inc(&SE_HBA(dev)->left_queue_depth);
  1883. spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
  1884. }
  1885. /*
  1886. * Handle SAM-esque emulation for generic transport request failures.
  1887. */
  1888. static void transport_generic_request_failure(
  1889. struct se_cmd *cmd,
  1890. struct se_device *dev,
  1891. int complete,
  1892. int sc)
  1893. {
  1894. DEBUG_GRF("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
  1895. " CDB: 0x%02x\n", cmd, CMD_TFO(cmd)->get_task_tag(cmd),
  1896. T_TASK(cmd)->t_task_cdb[0]);
  1897. DEBUG_GRF("-----[ i_state: %d t_state/def_t_state:"
  1898. " %d/%d transport_error_status: %d\n",
  1899. CMD_TFO(cmd)->get_cmd_state(cmd),
  1900. cmd->t_state, cmd->deferred_t_state,
  1901. cmd->transport_error_status);
  1902. DEBUG_GRF("-----[ t_task_cdbs: %d t_task_cdbs_left: %d"
  1903. " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
  1904. " t_transport_active: %d t_transport_stop: %d"
  1905. " t_transport_sent: %d\n", T_TASK(cmd)->t_task_cdbs,
  1906. atomic_read(&T_TASK(cmd)->t_task_cdbs_left),
  1907. atomic_read(&T_TASK(cmd)->t_task_cdbs_sent),
  1908. atomic_read(&T_TASK(cmd)->t_task_cdbs_ex_left),
  1909. atomic_read(&T_TASK(cmd)->t_transport_active),
  1910. atomic_read(&T_TASK(cmd)->t_transport_stop),
  1911. atomic_read(&T_TASK(cmd)->t_transport_sent));
  1912. transport_stop_all_task_timers(cmd);
  1913. if (dev)
  1914. transport_failure_reset_queue_depth(dev);
  1915. /*
  1916. * For SAM Task Attribute emulation for failed struct se_cmd
  1917. */
  1918. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  1919. transport_complete_task_attr(cmd);
  1920. if (complete) {
  1921. transport_direct_request_timeout(cmd);
  1922. cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
  1923. }
  1924. switch (cmd->transport_error_status) {
  1925. case PYX_TRANSPORT_UNKNOWN_SAM_OPCODE:
  1926. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1927. break;
  1928. case PYX_TRANSPORT_REQ_TOO_MANY_SECTORS:
  1929. cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY;
  1930. break;
  1931. case PYX_TRANSPORT_INVALID_CDB_FIELD:
  1932. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1933. break;
  1934. case PYX_TRANSPORT_INVALID_PARAMETER_LIST:
  1935. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1936. break;
  1937. case PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES:
  1938. if (!sc)
  1939. transport_new_cmd_failure(cmd);
  1940. /*
  1941. * Currently for PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES,
  1942. * we force this session to fall back to session
  1943. * recovery.
  1944. */
  1945. CMD_TFO(cmd)->fall_back_to_erl0(cmd->se_sess);
  1946. CMD_TFO(cmd)->stop_session(cmd->se_sess, 0, 0);
  1947. goto check_stop;
  1948. case PYX_TRANSPORT_LU_COMM_FAILURE:
  1949. case PYX_TRANSPORT_ILLEGAL_REQUEST:
  1950. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1951. break;
  1952. case PYX_TRANSPORT_UNKNOWN_MODE_PAGE:
  1953. cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
  1954. break;
  1955. case PYX_TRANSPORT_WRITE_PROTECTED:
  1956. cmd->scsi_sense_reason = TCM_WRITE_PROTECTED;
  1957. break;
  1958. case PYX_TRANSPORT_RESERVATION_CONFLICT:
  1959. /*
  1960. * No SENSE Data payload for this case, set SCSI Status
  1961. * and queue the response to $FABRIC_MOD.
  1962. *
  1963. * Uses linux/include/scsi/scsi.h SAM status codes defs
  1964. */
  1965. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  1966. /*
  1967. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  1968. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  1969. * CONFLICT STATUS.
  1970. *
  1971. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  1972. */
  1973. if (SE_SESS(cmd) &&
  1974. DEV_ATTRIB(cmd->se_dev)->emulate_ua_intlck_ctrl == 2)
  1975. core_scsi3_ua_allocate(SE_SESS(cmd)->se_node_acl,
  1976. cmd->orig_fe_lun, 0x2C,
  1977. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  1978. CMD_TFO(cmd)->queue_status(cmd);
  1979. goto check_stop;
  1980. case PYX_TRANSPORT_USE_SENSE_REASON:
  1981. /*
  1982. * struct se_cmd->scsi_sense_reason already set
  1983. */
  1984. break;
  1985. default:
  1986. printk(KERN_ERR "Unknown transport error for CDB 0x%02x: %d\n",
  1987. T_TASK(cmd)->t_task_cdb[0],
  1988. cmd->transport_error_status);
  1989. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1990. break;
  1991. }
  1992. if (!sc)
  1993. transport_new_cmd_failure(cmd);
  1994. else
  1995. transport_send_check_condition_and_sense(cmd,
  1996. cmd->scsi_sense_reason, 0);
  1997. check_stop:
  1998. transport_lun_remove_cmd(cmd);
  1999. if (!(transport_cmd_check_stop_to_fabric(cmd)))
  2000. ;
  2001. }
  2002. static void transport_direct_request_timeout(struct se_cmd *cmd)
  2003. {
  2004. unsigned long flags;
  2005. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  2006. if (!(atomic_read(&T_TASK(cmd)->t_transport_timeout))) {
  2007. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2008. return;
  2009. }
  2010. if (atomic_read(&T_TASK(cmd)->t_task_cdbs_timeout_left)) {
  2011. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2012. return;
  2013. }
  2014. atomic_sub(atomic_read(&T_TASK(cmd)->t_transport_timeout),
  2015. &T_TASK(cmd)->t_se_count);
  2016. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2017. }
  2018. static void transport_generic_request_timeout(struct se_cmd *cmd)
  2019. {
  2020. unsigned long flags;
  2021. /*
  2022. * Reset T_TASK(cmd)->t_se_count to allow transport_generic_remove()
  2023. * to allow last call to free memory resources.
  2024. */
  2025. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  2026. if (atomic_read(&T_TASK(cmd)->t_transport_timeout) > 1) {
  2027. int tmp = (atomic_read(&T_TASK(cmd)->t_transport_timeout) - 1);
  2028. atomic_sub(tmp, &T_TASK(cmd)->t_se_count);
  2029. }
  2030. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2031. transport_generic_remove(cmd, 0, 0);
  2032. }
  2033. static int
  2034. transport_generic_allocate_buf(struct se_cmd *cmd, u32 data_length)
  2035. {
  2036. unsigned char *buf;
  2037. buf = kzalloc(data_length, GFP_KERNEL);
  2038. if (!(buf)) {
  2039. printk(KERN_ERR "Unable to allocate memory for buffer\n");
  2040. return -1;
  2041. }
  2042. T_TASK(cmd)->t_tasks_se_num = 0;
  2043. T_TASK(cmd)->t_task_buf = buf;
  2044. return 0;
  2045. }
  2046. static inline u32 transport_lba_21(unsigned char *cdb)
  2047. {
  2048. return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
  2049. }
  2050. static inline u32 transport_lba_32(unsigned char *cdb)
  2051. {
  2052. return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  2053. }
  2054. static inline unsigned long long transport_lba_64(unsigned char *cdb)
  2055. {
  2056. unsigned int __v1, __v2;
  2057. __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  2058. __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  2059. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  2060. }
  2061. /*
  2062. * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
  2063. */
  2064. static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
  2065. {
  2066. unsigned int __v1, __v2;
  2067. __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
  2068. __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
  2069. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  2070. }
  2071. static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
  2072. {
  2073. unsigned long flags;
  2074. spin_lock_irqsave(&T_TASK(se_cmd)->t_state_lock, flags);
  2075. se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
  2076. spin_unlock_irqrestore(&T_TASK(se_cmd)->t_state_lock, flags);
  2077. }
  2078. /*
  2079. * Called from interrupt context.
  2080. */
  2081. static void transport_task_timeout_handler(unsigned long data)
  2082. {
  2083. struct se_task *task = (struct se_task *)data;
  2084. struct se_cmd *cmd = TASK_CMD(task);
  2085. unsigned long flags;
  2086. DEBUG_TT("transport task timeout fired! task: %p cmd: %p\n", task, cmd);
  2087. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  2088. if (task->task_flags & TF_STOP) {
  2089. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2090. return;
  2091. }
  2092. task->task_flags &= ~TF_RUNNING;
  2093. /*
  2094. * Determine if transport_complete_task() has already been called.
  2095. */
  2096. if (!(atomic_read(&task->task_active))) {
  2097. DEBUG_TT("transport task: %p cmd: %p timeout task_active"
  2098. " == 0\n", task, cmd);
  2099. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2100. return;
  2101. }
  2102. atomic_inc(&T_TASK(cmd)->t_se_count);
  2103. atomic_inc(&T_TASK(cmd)->t_transport_timeout);
  2104. T_TASK(cmd)->t_tasks_failed = 1;
  2105. atomic_set(&task->task_timeout, 1);
  2106. task->task_error_status = PYX_TRANSPORT_TASK_TIMEOUT;
  2107. task->task_scsi_status = 1;
  2108. if (atomic_read(&task->task_stop)) {
  2109. DEBUG_TT("transport task: %p cmd: %p timeout task_stop"
  2110. " == 1\n", task, cmd);
  2111. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2112. complete(&task->task_stop_comp);
  2113. return;
  2114. }
  2115. if (!(atomic_dec_and_test(&T_TASK(cmd)->t_task_cdbs_left))) {
  2116. DEBUG_TT("transport task: %p cmd: %p timeout non zero"
  2117. " t_task_cdbs_left\n", task, cmd);
  2118. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2119. return;
  2120. }
  2121. DEBUG_TT("transport task: %p cmd: %p timeout ZERO t_task_cdbs_left\n",
  2122. task, cmd);
  2123. cmd->t_state = TRANSPORT_COMPLETE_FAILURE;
  2124. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2125. transport_add_cmd_to_queue(cmd, TRANSPORT_COMPLETE_FAILURE);
  2126. }
  2127. /*
  2128. * Called with T_TASK(cmd)->t_state_lock held.
  2129. */
  2130. static void transport_start_task_timer(struct se_task *task)
  2131. {
  2132. struct se_device *dev = task->se_dev;
  2133. int timeout;
  2134. if (task->task_flags & TF_RUNNING)
  2135. return;
  2136. /*
  2137. * If the task_timeout is disabled, exit now.
  2138. */
  2139. timeout = DEV_ATTRIB(dev)->task_timeout;
  2140. if (!(timeout))
  2141. return;
  2142. init_timer(&task->task_timer);
  2143. task->task_timer.expires = (get_jiffies_64() + timeout * HZ);
  2144. task->task_timer.data = (unsigned long) task;
  2145. task->task_timer.function = transport_task_timeout_handler;
  2146. task->task_flags |= TF_RUNNING;
  2147. add_timer(&task->task_timer);
  2148. #if 0
  2149. printk(KERN_INFO "Starting task timer for cmd: %p task: %p seconds:"
  2150. " %d\n", task->task_se_cmd, task, timeout);
  2151. #endif
  2152. }
  2153. /*
  2154. * Called with spin_lock_irq(&T_TASK(cmd)->t_state_lock) held.
  2155. */
  2156. void __transport_stop_task_timer(struct se_task *task, unsigned long *flags)
  2157. {
  2158. struct se_cmd *cmd = TASK_CMD(task);
  2159. if (!(task->task_flags & TF_RUNNING))
  2160. return;
  2161. task->task_flags |= TF_STOP;
  2162. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, *flags);
  2163. del_timer_sync(&task->task_timer);
  2164. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, *flags);
  2165. task->task_flags &= ~TF_RUNNING;
  2166. task->task_flags &= ~TF_STOP;
  2167. }
  2168. static void transport_stop_all_task_timers(struct se_cmd *cmd)
  2169. {
  2170. struct se_task *task = NULL, *task_tmp;
  2171. unsigned long flags;
  2172. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  2173. list_for_each_entry_safe(task, task_tmp,
  2174. &T_TASK(cmd)->t_task_list, t_list)
  2175. __transport_stop_task_timer(task, &flags);
  2176. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2177. }
  2178. static inline int transport_tcq_window_closed(struct se_device *dev)
  2179. {
  2180. if (dev->dev_tcq_window_closed++ <
  2181. PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) {
  2182. msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT);
  2183. } else
  2184. msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG);
  2185. wake_up_interruptible(&dev->dev_queue_obj->thread_wq);
  2186. return 0;
  2187. }
  2188. /*
  2189. * Called from Fabric Module context from transport_execute_tasks()
  2190. *
  2191. * The return of this function determins if the tasks from struct se_cmd
  2192. * get added to the execution queue in transport_execute_tasks(),
  2193. * or are added to the delayed or ordered lists here.
  2194. */
  2195. static inline int transport_execute_task_attr(struct se_cmd *cmd)
  2196. {
  2197. if (SE_DEV(cmd)->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  2198. return 1;
  2199. /*
  2200. * Check for the existance of HEAD_OF_QUEUE, and if true return 1
  2201. * to allow the passed struct se_cmd list of tasks to the front of the list.
  2202. */
  2203. if (cmd->sam_task_attr == TASK_ATTR_HOQ) {
  2204. atomic_inc(&SE_DEV(cmd)->dev_hoq_count);
  2205. smp_mb__after_atomic_inc();
  2206. DEBUG_STA("Added HEAD_OF_QUEUE for CDB:"
  2207. " 0x%02x, se_ordered_id: %u\n",
  2208. T_TASK(cmd)->t_task_cdb[0],
  2209. cmd->se_ordered_id);
  2210. return 1;
  2211. } else if (cmd->sam_task_attr == TASK_ATTR_ORDERED) {
  2212. spin_lock(&SE_DEV(cmd)->ordered_cmd_lock);
  2213. list_add_tail(&cmd->se_ordered_list,
  2214. &SE_DEV(cmd)->ordered_cmd_list);
  2215. spin_unlock(&SE_DEV(cmd)->ordered_cmd_lock);
  2216. atomic_inc(&SE_DEV(cmd)->dev_ordered_sync);
  2217. smp_mb__after_atomic_inc();
  2218. DEBUG_STA("Added ORDERED for CDB: 0x%02x to ordered"
  2219. " list, se_ordered_id: %u\n",
  2220. T_TASK(cmd)->t_task_cdb[0],
  2221. cmd->se_ordered_id);
  2222. /*
  2223. * Add ORDERED command to tail of execution queue if
  2224. * no other older commands exist that need to be
  2225. * completed first.
  2226. */
  2227. if (!(atomic_read(&SE_DEV(cmd)->simple_cmds)))
  2228. return 1;
  2229. } else {
  2230. /*
  2231. * For SIMPLE and UNTAGGED Task Attribute commands
  2232. */
  2233. atomic_inc(&SE_DEV(cmd)->simple_cmds);
  2234. smp_mb__after_atomic_inc();
  2235. }
  2236. /*
  2237. * Otherwise if one or more outstanding ORDERED task attribute exist,
  2238. * add the dormant task(s) built for the passed struct se_cmd to the
  2239. * execution queue and become in Active state for this struct se_device.
  2240. */
  2241. if (atomic_read(&SE_DEV(cmd)->dev_ordered_sync) != 0) {
  2242. /*
  2243. * Otherwise, add cmd w/ tasks to delayed cmd queue that
  2244. * will be drained upon competion of HEAD_OF_QUEUE task.
  2245. */
  2246. spin_lock(&SE_DEV(cmd)->delayed_cmd_lock);
  2247. cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
  2248. list_add_tail(&cmd->se_delayed_list,
  2249. &SE_DEV(cmd)->delayed_cmd_list);
  2250. spin_unlock(&SE_DEV(cmd)->delayed_cmd_lock);
  2251. DEBUG_STA("Added CDB: 0x%02x Task Attr: 0x%02x to"
  2252. " delayed CMD list, se_ordered_id: %u\n",
  2253. T_TASK(cmd)->t_task_cdb[0], cmd->sam_task_attr,
  2254. cmd->se_ordered_id);
  2255. /*
  2256. * Return zero to let transport_execute_tasks() know
  2257. * not to add the delayed tasks to the execution list.
  2258. */
  2259. return 0;
  2260. }
  2261. /*
  2262. * Otherwise, no ORDERED task attributes exist..
  2263. */
  2264. return 1;
  2265. }
  2266. /*
  2267. * Called from fabric module context in transport_generic_new_cmd() and
  2268. * transport_generic_process_write()
  2269. */
  2270. static int transport_execute_tasks(struct se_cmd *cmd)
  2271. {
  2272. int add_tasks;
  2273. if (!(cmd->se_cmd_flags & SCF_SE_DISABLE_ONLINE_CHECK)) {
  2274. if (se_dev_check_online(cmd->se_orig_obj_ptr) != 0) {
  2275. cmd->transport_error_status =
  2276. PYX_TRANSPORT_LU_COMM_FAILURE;
  2277. transport_generic_request_failure(cmd, NULL, 0, 1);
  2278. return 0;
  2279. }
  2280. }
  2281. /*
  2282. * Call transport_cmd_check_stop() to see if a fabric exception
  2283. * has occured that prevents execution.
  2284. */
  2285. if (!(transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING))) {
  2286. /*
  2287. * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
  2288. * attribute for the tasks of the received struct se_cmd CDB
  2289. */
  2290. add_tasks = transport_execute_task_attr(cmd);
  2291. if (add_tasks == 0)
  2292. goto execute_tasks;
  2293. /*
  2294. * This calls transport_add_tasks_from_cmd() to handle
  2295. * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
  2296. * (if enabled) in __transport_add_task_to_execute_queue() and
  2297. * transport_add_task_check_sam_attr().
  2298. */
  2299. transport_add_tasks_from_cmd(cmd);
  2300. }
  2301. /*
  2302. * Kick the execution queue for the cmd associated struct se_device
  2303. * storage object.
  2304. */
  2305. execute_tasks:
  2306. __transport_execute_tasks(SE_DEV(cmd));
  2307. return 0;
  2308. }
  2309. /*
  2310. * Called to check struct se_device tcq depth window, and once open pull struct se_task
  2311. * from struct se_device->execute_task_list and
  2312. *
  2313. * Called from transport_processing_thread()
  2314. */
  2315. static int __transport_execute_tasks(struct se_device *dev)
  2316. {
  2317. int error;
  2318. struct se_cmd *cmd = NULL;
  2319. struct se_task *task;
  2320. unsigned long flags;
  2321. /*
  2322. * Check if there is enough room in the device and HBA queue to send
  2323. * struct se_transport_task's to the selected transport.
  2324. */
  2325. check_depth:
  2326. spin_lock_irqsave(&SE_HBA(dev)->hba_queue_lock, flags);
  2327. if (!(atomic_read(&dev->depth_left)) ||
  2328. !(atomic_read(&SE_HBA(dev)->left_queue_depth))) {
  2329. spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
  2330. return transport_tcq_window_closed(dev);
  2331. }
  2332. dev->dev_tcq_window_closed = 0;
  2333. spin_lock(&dev->execute_task_lock);
  2334. task = transport_get_task_from_execute_queue(dev);
  2335. spin_unlock(&dev->execute_task_lock);
  2336. if (!task) {
  2337. spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
  2338. return 0;
  2339. }
  2340. atomic_dec(&dev->depth_left);
  2341. atomic_dec(&SE_HBA(dev)->left_queue_depth);
  2342. spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
  2343. cmd = TASK_CMD(task);
  2344. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  2345. atomic_set(&task->task_active, 1);
  2346. atomic_set(&task->task_sent, 1);
  2347. atomic_inc(&T_TASK(cmd)->t_task_cdbs_sent);
  2348. if (atomic_read(&T_TASK(cmd)->t_task_cdbs_sent) ==
  2349. T_TASK(cmd)->t_task_cdbs)
  2350. atomic_set(&cmd->transport_sent, 1);
  2351. transport_start_task_timer(task);
  2352. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2353. /*
  2354. * The struct se_cmd->transport_emulate_cdb() function pointer is used
  2355. * to grab REPORT_LUNS CDBs before they hit the
  2356. * struct se_subsystem_api->do_task() caller below.
  2357. */
  2358. if (cmd->transport_emulate_cdb) {
  2359. error = cmd->transport_emulate_cdb(cmd);
  2360. if (error != 0) {
  2361. cmd->transport_error_status = error;
  2362. atomic_set(&task->task_active, 0);
  2363. atomic_set(&cmd->transport_sent, 0);
  2364. transport_stop_tasks_for_cmd(cmd);
  2365. transport_generic_request_failure(cmd, dev, 0, 1);
  2366. goto check_depth;
  2367. }
  2368. /*
  2369. * Handle the successful completion for transport_emulate_cdb()
  2370. * for synchronous operation, following SCF_EMULATE_CDB_ASYNC
  2371. * Otherwise the caller is expected to complete the task with
  2372. * proper status.
  2373. */
  2374. if (!(cmd->se_cmd_flags & SCF_EMULATE_CDB_ASYNC)) {
  2375. cmd->scsi_status = SAM_STAT_GOOD;
  2376. task->task_scsi_status = GOOD;
  2377. transport_complete_task(task, 1);
  2378. }
  2379. } else {
  2380. /*
  2381. * Currently for all virtual TCM plugins including IBLOCK, FILEIO and
  2382. * RAMDISK we use the internal transport_emulate_control_cdb() logic
  2383. * with struct se_subsystem_api callers for the primary SPC-3 TYPE_DISK
  2384. * LUN emulation code.
  2385. *
  2386. * For TCM/pSCSI and all other SCF_SCSI_DATA_SG_IO_CDB I/O tasks we
  2387. * call ->do_task() directly and let the underlying TCM subsystem plugin
  2388. * code handle the CDB emulation.
  2389. */
  2390. if ((TRANSPORT(dev)->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) &&
  2391. (!(TASK_CMD(task)->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
  2392. error = transport_emulate_control_cdb(task);
  2393. else
  2394. error = TRANSPORT(dev)->do_task(task);
  2395. if (error != 0) {
  2396. cmd->transport_error_status = error;
  2397. atomic_set(&task->task_active, 0);
  2398. atomic_set(&cmd->transport_sent, 0);
  2399. transport_stop_tasks_for_cmd(cmd);
  2400. transport_generic_request_failure(cmd, dev, 0, 1);
  2401. }
  2402. }
  2403. goto check_depth;
  2404. return 0;
  2405. }
  2406. void transport_new_cmd_failure(struct se_cmd *se_cmd)
  2407. {
  2408. unsigned long flags;
  2409. /*
  2410. * Any unsolicited data will get dumped for failed command inside of
  2411. * the fabric plugin
  2412. */
  2413. spin_lock_irqsave(&T_TASK(se_cmd)->t_state_lock, flags);
  2414. se_cmd->se_cmd_flags |= SCF_SE_CMD_FAILED;
  2415. se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2416. spin_unlock_irqrestore(&T_TASK(se_cmd)->t_state_lock, flags);
  2417. CMD_TFO(se_cmd)->new_cmd_failure(se_cmd);
  2418. }
  2419. static void transport_nop_wait_for_tasks(struct se_cmd *, int, int);
  2420. static inline u32 transport_get_sectors_6(
  2421. unsigned char *cdb,
  2422. struct se_cmd *cmd,
  2423. int *ret)
  2424. {
  2425. struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
  2426. /*
  2427. * Assume TYPE_DISK for non struct se_device objects.
  2428. * Use 8-bit sector value.
  2429. */
  2430. if (!dev)
  2431. goto type_disk;
  2432. /*
  2433. * Use 24-bit allocation length for TYPE_TAPE.
  2434. */
  2435. if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE)
  2436. return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
  2437. /*
  2438. * Everything else assume TYPE_DISK Sector CDB location.
  2439. * Use 8-bit sector value.
  2440. */
  2441. type_disk:
  2442. return (u32)cdb[4];
  2443. }
  2444. static inline u32 transport_get_sectors_10(
  2445. unsigned char *cdb,
  2446. struct se_cmd *cmd,
  2447. int *ret)
  2448. {
  2449. struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
  2450. /*
  2451. * Assume TYPE_DISK for non struct se_device objects.
  2452. * Use 16-bit sector value.
  2453. */
  2454. if (!dev)
  2455. goto type_disk;
  2456. /*
  2457. * XXX_10 is not defined in SSC, throw an exception
  2458. */
  2459. if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE) {
  2460. *ret = -1;
  2461. return 0;
  2462. }
  2463. /*
  2464. * Everything else assume TYPE_DISK Sector CDB location.
  2465. * Use 16-bit sector value.
  2466. */
  2467. type_disk:
  2468. return (u32)(cdb[7] << 8) + cdb[8];
  2469. }
  2470. static inline u32 transport_get_sectors_12(
  2471. unsigned char *cdb,
  2472. struct se_cmd *cmd,
  2473. int *ret)
  2474. {
  2475. struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
  2476. /*
  2477. * Assume TYPE_DISK for non struct se_device objects.
  2478. * Use 32-bit sector value.
  2479. */
  2480. if (!dev)
  2481. goto type_disk;
  2482. /*
  2483. * XXX_12 is not defined in SSC, throw an exception
  2484. */
  2485. if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE) {
  2486. *ret = -1;
  2487. return 0;
  2488. }
  2489. /*
  2490. * Everything else assume TYPE_DISK Sector CDB location.
  2491. * Use 32-bit sector value.
  2492. */
  2493. type_disk:
  2494. return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
  2495. }
  2496. static inline u32 transport_get_sectors_16(
  2497. unsigned char *cdb,
  2498. struct se_cmd *cmd,
  2499. int *ret)
  2500. {
  2501. struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
  2502. /*
  2503. * Assume TYPE_DISK for non struct se_device objects.
  2504. * Use 32-bit sector value.
  2505. */
  2506. if (!dev)
  2507. goto type_disk;
  2508. /*
  2509. * Use 24-bit allocation length for TYPE_TAPE.
  2510. */
  2511. if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE)
  2512. return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
  2513. type_disk:
  2514. return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
  2515. (cdb[12] << 8) + cdb[13];
  2516. }
  2517. /*
  2518. * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
  2519. */
  2520. static inline u32 transport_get_sectors_32(
  2521. unsigned char *cdb,
  2522. struct se_cmd *cmd,
  2523. int *ret)
  2524. {
  2525. /*
  2526. * Assume TYPE_DISK for non struct se_device objects.
  2527. * Use 32-bit sector value.
  2528. */
  2529. return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
  2530. (cdb[30] << 8) + cdb[31];
  2531. }
  2532. static inline u32 transport_get_size(
  2533. u32 sectors,
  2534. unsigned char *cdb,
  2535. struct se_cmd *cmd)
  2536. {
  2537. struct se_device *dev = SE_DEV(cmd);
  2538. if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE) {
  2539. if (cdb[1] & 1) { /* sectors */
  2540. return DEV_ATTRIB(dev)->block_size * sectors;
  2541. } else /* bytes */
  2542. return sectors;
  2543. }
  2544. #if 0
  2545. printk(KERN_INFO "Returning block_size: %u, sectors: %u == %u for"
  2546. " %s object\n", DEV_ATTRIB(dev)->block_size, sectors,
  2547. DEV_ATTRIB(dev)->block_size * sectors,
  2548. TRANSPORT(dev)->name);
  2549. #endif
  2550. return DEV_ATTRIB(dev)->block_size * sectors;
  2551. }
  2552. unsigned char transport_asciihex_to_binaryhex(unsigned char val[2])
  2553. {
  2554. unsigned char result = 0;
  2555. /*
  2556. * MSB
  2557. */
  2558. if ((val[0] >= 'a') && (val[0] <= 'f'))
  2559. result = ((val[0] - 'a' + 10) & 0xf) << 4;
  2560. else
  2561. if ((val[0] >= 'A') && (val[0] <= 'F'))
  2562. result = ((val[0] - 'A' + 10) & 0xf) << 4;
  2563. else /* digit */
  2564. result = ((val[0] - '0') & 0xf) << 4;
  2565. /*
  2566. * LSB
  2567. */
  2568. if ((val[1] >= 'a') && (val[1] <= 'f'))
  2569. result |= ((val[1] - 'a' + 10) & 0xf);
  2570. else
  2571. if ((val[1] >= 'A') && (val[1] <= 'F'))
  2572. result |= ((val[1] - 'A' + 10) & 0xf);
  2573. else /* digit */
  2574. result |= ((val[1] - '0') & 0xf);
  2575. return result;
  2576. }
  2577. EXPORT_SYMBOL(transport_asciihex_to_binaryhex);
  2578. static void transport_xor_callback(struct se_cmd *cmd)
  2579. {
  2580. unsigned char *buf, *addr;
  2581. struct se_mem *se_mem;
  2582. unsigned int offset;
  2583. int i;
  2584. /*
  2585. * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
  2586. *
  2587. * 1) read the specified logical block(s);
  2588. * 2) transfer logical blocks from the data-out buffer;
  2589. * 3) XOR the logical blocks transferred from the data-out buffer with
  2590. * the logical blocks read, storing the resulting XOR data in a buffer;
  2591. * 4) if the DISABLE WRITE bit is set to zero, then write the logical
  2592. * blocks transferred from the data-out buffer; and
  2593. * 5) transfer the resulting XOR data to the data-in buffer.
  2594. */
  2595. buf = kmalloc(cmd->data_length, GFP_KERNEL);
  2596. if (!(buf)) {
  2597. printk(KERN_ERR "Unable to allocate xor_callback buf\n");
  2598. return;
  2599. }
  2600. /*
  2601. * Copy the scatterlist WRITE buffer located at T_TASK(cmd)->t_mem_list
  2602. * into the locally allocated *buf
  2603. */
  2604. transport_memcpy_se_mem_read_contig(cmd, buf, T_TASK(cmd)->t_mem_list);
  2605. /*
  2606. * Now perform the XOR against the BIDI read memory located at
  2607. * T_TASK(cmd)->t_mem_bidi_list
  2608. */
  2609. offset = 0;
  2610. list_for_each_entry(se_mem, T_TASK(cmd)->t_mem_bidi_list, se_list) {
  2611. addr = (unsigned char *)kmap_atomic(se_mem->se_page, KM_USER0);
  2612. if (!(addr))
  2613. goto out;
  2614. for (i = 0; i < se_mem->se_len; i++)
  2615. *(addr + se_mem->se_off + i) ^= *(buf + offset + i);
  2616. offset += se_mem->se_len;
  2617. kunmap_atomic(addr, KM_USER0);
  2618. }
  2619. out:
  2620. kfree(buf);
  2621. }
  2622. /*
  2623. * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
  2624. */
  2625. static int transport_get_sense_data(struct se_cmd *cmd)
  2626. {
  2627. unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
  2628. struct se_device *dev;
  2629. struct se_task *task = NULL, *task_tmp;
  2630. unsigned long flags;
  2631. u32 offset = 0;
  2632. if (!SE_LUN(cmd)) {
  2633. printk(KERN_ERR "SE_LUN(cmd) is NULL\n");
  2634. return -1;
  2635. }
  2636. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  2637. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  2638. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2639. return 0;
  2640. }
  2641. list_for_each_entry_safe(task, task_tmp,
  2642. &T_TASK(cmd)->t_task_list, t_list) {
  2643. if (!task->task_sense)
  2644. continue;
  2645. dev = task->se_dev;
  2646. if (!(dev))
  2647. continue;
  2648. if (!TRANSPORT(dev)->get_sense_buffer) {
  2649. printk(KERN_ERR "TRANSPORT(dev)->get_sense_buffer"
  2650. " is NULL\n");
  2651. continue;
  2652. }
  2653. sense_buffer = TRANSPORT(dev)->get_sense_buffer(task);
  2654. if (!(sense_buffer)) {
  2655. printk(KERN_ERR "ITT[0x%08x]_TASK[%d]: Unable to locate"
  2656. " sense buffer for task with sense\n",
  2657. CMD_TFO(cmd)->get_task_tag(cmd), task->task_no);
  2658. continue;
  2659. }
  2660. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2661. offset = CMD_TFO(cmd)->set_fabric_sense_len(cmd,
  2662. TRANSPORT_SENSE_BUFFER);
  2663. memcpy((void *)&buffer[offset], (void *)sense_buffer,
  2664. TRANSPORT_SENSE_BUFFER);
  2665. cmd->scsi_status = task->task_scsi_status;
  2666. /* Automatically padded */
  2667. cmd->scsi_sense_length =
  2668. (TRANSPORT_SENSE_BUFFER + offset);
  2669. printk(KERN_INFO "HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
  2670. " and sense\n",
  2671. dev->se_hba->hba_id, TRANSPORT(dev)->name,
  2672. cmd->scsi_status);
  2673. return 0;
  2674. }
  2675. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2676. return -1;
  2677. }
  2678. static int transport_allocate_resources(struct se_cmd *cmd)
  2679. {
  2680. u32 length = cmd->data_length;
  2681. if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
  2682. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB))
  2683. return transport_generic_get_mem(cmd, length, PAGE_SIZE);
  2684. else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB)
  2685. return transport_generic_allocate_buf(cmd, length);
  2686. else
  2687. return 0;
  2688. }
  2689. static int
  2690. transport_handle_reservation_conflict(struct se_cmd *cmd)
  2691. {
  2692. cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
  2693. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2694. cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
  2695. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  2696. /*
  2697. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  2698. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  2699. * CONFLICT STATUS.
  2700. *
  2701. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  2702. */
  2703. if (SE_SESS(cmd) &&
  2704. DEV_ATTRIB(cmd->se_dev)->emulate_ua_intlck_ctrl == 2)
  2705. core_scsi3_ua_allocate(SE_SESS(cmd)->se_node_acl,
  2706. cmd->orig_fe_lun, 0x2C,
  2707. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  2708. return -2;
  2709. }
  2710. /* transport_generic_cmd_sequencer():
  2711. *
  2712. * Generic Command Sequencer that should work for most DAS transport
  2713. * drivers.
  2714. *
  2715. * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
  2716. * RX Thread.
  2717. *
  2718. * FIXME: Need to support other SCSI OPCODES where as well.
  2719. */
  2720. static int transport_generic_cmd_sequencer(
  2721. struct se_cmd *cmd,
  2722. unsigned char *cdb)
  2723. {
  2724. struct se_device *dev = SE_DEV(cmd);
  2725. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  2726. int ret = 0, sector_ret = 0, passthrough;
  2727. u32 sectors = 0, size = 0, pr_reg_type = 0;
  2728. u16 service_action;
  2729. u8 alua_ascq = 0;
  2730. /*
  2731. * Check for an existing UNIT ATTENTION condition
  2732. */
  2733. if (core_scsi3_ua_check(cmd, cdb) < 0) {
  2734. cmd->transport_wait_for_tasks =
  2735. &transport_nop_wait_for_tasks;
  2736. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2737. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
  2738. return -2;
  2739. }
  2740. /*
  2741. * Check status of Asymmetric Logical Unit Assignment port
  2742. */
  2743. ret = T10_ALUA(su_dev)->alua_state_check(cmd, cdb, &alua_ascq);
  2744. if (ret != 0) {
  2745. cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
  2746. /*
  2747. * Set SCSI additional sense code (ASC) to 'LUN Not Accessable';
  2748. * The ALUA additional sense code qualifier (ASCQ) is determined
  2749. * by the ALUA primary or secondary access state..
  2750. */
  2751. if (ret > 0) {
  2752. #if 0
  2753. printk(KERN_INFO "[%s]: ALUA TG Port not available,"
  2754. " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
  2755. CMD_TFO(cmd)->get_fabric_name(), alua_ascq);
  2756. #endif
  2757. transport_set_sense_codes(cmd, 0x04, alua_ascq);
  2758. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2759. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
  2760. return -2;
  2761. }
  2762. goto out_invalid_cdb_field;
  2763. }
  2764. /*
  2765. * Check status for SPC-3 Persistent Reservations
  2766. */
  2767. if (T10_PR_OPS(su_dev)->t10_reservation_check(cmd, &pr_reg_type) != 0) {
  2768. if (T10_PR_OPS(su_dev)->t10_seq_non_holder(
  2769. cmd, cdb, pr_reg_type) != 0)
  2770. return transport_handle_reservation_conflict(cmd);
  2771. /*
  2772. * This means the CDB is allowed for the SCSI Initiator port
  2773. * when said port is *NOT* holding the legacy SPC-2 or
  2774. * SPC-3 Persistent Reservation.
  2775. */
  2776. }
  2777. switch (cdb[0]) {
  2778. case READ_6:
  2779. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2780. if (sector_ret)
  2781. goto out_unsupported_cdb;
  2782. size = transport_get_size(sectors, cdb, cmd);
  2783. cmd->transport_split_cdb = &split_cdb_XX_6;
  2784. T_TASK(cmd)->t_task_lba = transport_lba_21(cdb);
  2785. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2786. break;
  2787. case READ_10:
  2788. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2789. if (sector_ret)
  2790. goto out_unsupported_cdb;
  2791. size = transport_get_size(sectors, cdb, cmd);
  2792. cmd->transport_split_cdb = &split_cdb_XX_10;
  2793. T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
  2794. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2795. break;
  2796. case READ_12:
  2797. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2798. if (sector_ret)
  2799. goto out_unsupported_cdb;
  2800. size = transport_get_size(sectors, cdb, cmd);
  2801. cmd->transport_split_cdb = &split_cdb_XX_12;
  2802. T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
  2803. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2804. break;
  2805. case READ_16:
  2806. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2807. if (sector_ret)
  2808. goto out_unsupported_cdb;
  2809. size = transport_get_size(sectors, cdb, cmd);
  2810. cmd->transport_split_cdb = &split_cdb_XX_16;
  2811. T_TASK(cmd)->t_task_lba = transport_lba_64(cdb);
  2812. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2813. break;
  2814. case WRITE_6:
  2815. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2816. if (sector_ret)
  2817. goto out_unsupported_cdb;
  2818. size = transport_get_size(sectors, cdb, cmd);
  2819. cmd->transport_split_cdb = &split_cdb_XX_6;
  2820. T_TASK(cmd)->t_task_lba = transport_lba_21(cdb);
  2821. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2822. break;
  2823. case WRITE_10:
  2824. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2825. if (sector_ret)
  2826. goto out_unsupported_cdb;
  2827. size = transport_get_size(sectors, cdb, cmd);
  2828. cmd->transport_split_cdb = &split_cdb_XX_10;
  2829. T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
  2830. T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
  2831. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2832. break;
  2833. case WRITE_12:
  2834. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2835. if (sector_ret)
  2836. goto out_unsupported_cdb;
  2837. size = transport_get_size(sectors, cdb, cmd);
  2838. cmd->transport_split_cdb = &split_cdb_XX_12;
  2839. T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
  2840. T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
  2841. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2842. break;
  2843. case WRITE_16:
  2844. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2845. if (sector_ret)
  2846. goto out_unsupported_cdb;
  2847. size = transport_get_size(sectors, cdb, cmd);
  2848. cmd->transport_split_cdb = &split_cdb_XX_16;
  2849. T_TASK(cmd)->t_task_lba = transport_lba_64(cdb);
  2850. T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
  2851. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2852. break;
  2853. case XDWRITEREAD_10:
  2854. if ((cmd->data_direction != DMA_TO_DEVICE) ||
  2855. !(T_TASK(cmd)->t_tasks_bidi))
  2856. goto out_invalid_cdb_field;
  2857. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2858. if (sector_ret)
  2859. goto out_unsupported_cdb;
  2860. size = transport_get_size(sectors, cdb, cmd);
  2861. cmd->transport_split_cdb = &split_cdb_XX_10;
  2862. T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
  2863. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2864. passthrough = (TRANSPORT(dev)->transport_type ==
  2865. TRANSPORT_PLUGIN_PHBA_PDEV);
  2866. /*
  2867. * Skip the remaining assignments for TCM/PSCSI passthrough
  2868. */
  2869. if (passthrough)
  2870. break;
  2871. /*
  2872. * Setup BIDI XOR callback to be run during transport_generic_complete_ok()
  2873. */
  2874. cmd->transport_complete_callback = &transport_xor_callback;
  2875. T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
  2876. break;
  2877. case VARIABLE_LENGTH_CMD:
  2878. service_action = get_unaligned_be16(&cdb[8]);
  2879. /*
  2880. * Determine if this is TCM/PSCSI device and we should disable
  2881. * internal emulation for this CDB.
  2882. */
  2883. passthrough = (TRANSPORT(dev)->transport_type ==
  2884. TRANSPORT_PLUGIN_PHBA_PDEV);
  2885. switch (service_action) {
  2886. case XDWRITEREAD_32:
  2887. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2888. if (sector_ret)
  2889. goto out_unsupported_cdb;
  2890. size = transport_get_size(sectors, cdb, cmd);
  2891. /*
  2892. * Use WRITE_32 and READ_32 opcodes for the emulated
  2893. * XDWRITE_READ_32 logic.
  2894. */
  2895. cmd->transport_split_cdb = &split_cdb_XX_32;
  2896. T_TASK(cmd)->t_task_lba = transport_lba_64_ext(cdb);
  2897. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2898. /*
  2899. * Skip the remaining assignments for TCM/PSCSI passthrough
  2900. */
  2901. if (passthrough)
  2902. break;
  2903. /*
  2904. * Setup BIDI XOR callback to be run during
  2905. * transport_generic_complete_ok()
  2906. */
  2907. cmd->transport_complete_callback = &transport_xor_callback;
  2908. T_TASK(cmd)->t_tasks_fua = (cdb[10] & 0x8);
  2909. break;
  2910. case WRITE_SAME_32:
  2911. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2912. if (sector_ret)
  2913. goto out_unsupported_cdb;
  2914. size = transport_get_size(sectors, cdb, cmd);
  2915. T_TASK(cmd)->t_task_lba = get_unaligned_be64(&cdb[12]);
  2916. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2917. /*
  2918. * Skip the remaining assignments for TCM/PSCSI passthrough
  2919. */
  2920. if (passthrough)
  2921. break;
  2922. if ((cdb[10] & 0x04) || (cdb[10] & 0x02)) {
  2923. printk(KERN_ERR "WRITE_SAME PBDATA and LBDATA"
  2924. " bits not supported for Block Discard"
  2925. " Emulation\n");
  2926. goto out_invalid_cdb_field;
  2927. }
  2928. /*
  2929. * Currently for the emulated case we only accept
  2930. * tpws with the UNMAP=1 bit set.
  2931. */
  2932. if (!(cdb[10] & 0x08)) {
  2933. printk(KERN_ERR "WRITE_SAME w/o UNMAP bit not"
  2934. " supported for Block Discard Emulation\n");
  2935. goto out_invalid_cdb_field;
  2936. }
  2937. break;
  2938. default:
  2939. printk(KERN_ERR "VARIABLE_LENGTH_CMD service action"
  2940. " 0x%04x not supported\n", service_action);
  2941. goto out_unsupported_cdb;
  2942. }
  2943. break;
  2944. case 0xa3:
  2945. if (TRANSPORT(dev)->get_device_type(dev) != TYPE_ROM) {
  2946. /* MAINTENANCE_IN from SCC-2 */
  2947. /*
  2948. * Check for emulated MI_REPORT_TARGET_PGS.
  2949. */
  2950. if (cdb[1] == MI_REPORT_TARGET_PGS) {
  2951. cmd->transport_emulate_cdb =
  2952. (T10_ALUA(su_dev)->alua_type ==
  2953. SPC3_ALUA_EMULATED) ?
  2954. &core_emulate_report_target_port_groups :
  2955. NULL;
  2956. }
  2957. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2958. (cdb[8] << 8) | cdb[9];
  2959. } else {
  2960. /* GPCMD_SEND_KEY from multi media commands */
  2961. size = (cdb[8] << 8) + cdb[9];
  2962. }
  2963. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2964. break;
  2965. case MODE_SELECT:
  2966. size = cdb[4];
  2967. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2968. break;
  2969. case MODE_SELECT_10:
  2970. size = (cdb[7] << 8) + cdb[8];
  2971. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2972. break;
  2973. case MODE_SENSE:
  2974. size = cdb[4];
  2975. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2976. break;
  2977. case MODE_SENSE_10:
  2978. case GPCMD_READ_BUFFER_CAPACITY:
  2979. case GPCMD_SEND_OPC:
  2980. case LOG_SELECT:
  2981. case LOG_SENSE:
  2982. size = (cdb[7] << 8) + cdb[8];
  2983. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2984. break;
  2985. case READ_BLOCK_LIMITS:
  2986. size = READ_BLOCK_LEN;
  2987. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2988. break;
  2989. case GPCMD_GET_CONFIGURATION:
  2990. case GPCMD_READ_FORMAT_CAPACITIES:
  2991. case GPCMD_READ_DISC_INFO:
  2992. case GPCMD_READ_TRACK_RZONE_INFO:
  2993. size = (cdb[7] << 8) + cdb[8];
  2994. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2995. break;
  2996. case PERSISTENT_RESERVE_IN:
  2997. case PERSISTENT_RESERVE_OUT:
  2998. cmd->transport_emulate_cdb =
  2999. (T10_RES(su_dev)->res_type ==
  3000. SPC3_PERSISTENT_RESERVATIONS) ?
  3001. &core_scsi3_emulate_pr : NULL;
  3002. size = (cdb[7] << 8) + cdb[8];
  3003. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3004. break;
  3005. case GPCMD_MECHANISM_STATUS:
  3006. case GPCMD_READ_DVD_STRUCTURE:
  3007. size = (cdb[8] << 8) + cdb[9];
  3008. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  3009. break;
  3010. case READ_POSITION:
  3011. size = READ_POSITION_LEN;
  3012. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3013. break;
  3014. case 0xa4:
  3015. if (TRANSPORT(dev)->get_device_type(dev) != TYPE_ROM) {
  3016. /* MAINTENANCE_OUT from SCC-2
  3017. *
  3018. * Check for emulated MO_SET_TARGET_PGS.
  3019. */
  3020. if (cdb[1] == MO_SET_TARGET_PGS) {
  3021. cmd->transport_emulate_cdb =
  3022. (T10_ALUA(su_dev)->alua_type ==
  3023. SPC3_ALUA_EMULATED) ?
  3024. &core_emulate_set_target_port_groups :
  3025. NULL;
  3026. }
  3027. size = (cdb[6] << 24) | (cdb[7] << 16) |
  3028. (cdb[8] << 8) | cdb[9];
  3029. } else {
  3030. /* GPCMD_REPORT_KEY from multi media commands */
  3031. size = (cdb[8] << 8) + cdb[9];
  3032. }
  3033. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3034. break;
  3035. case INQUIRY:
  3036. size = (cdb[3] << 8) + cdb[4];
  3037. /*
  3038. * Do implict HEAD_OF_QUEUE processing for INQUIRY.
  3039. * See spc4r17 section 5.3
  3040. */
  3041. if (SE_DEV(cmd)->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  3042. cmd->sam_task_attr = TASK_ATTR_HOQ;
  3043. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3044. break;
  3045. case READ_BUFFER:
  3046. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  3047. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3048. break;
  3049. case READ_CAPACITY:
  3050. size = READ_CAP_LEN;
  3051. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3052. break;
  3053. case READ_MEDIA_SERIAL_NUMBER:
  3054. case SECURITY_PROTOCOL_IN:
  3055. case SECURITY_PROTOCOL_OUT:
  3056. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  3057. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3058. break;
  3059. case SERVICE_ACTION_IN:
  3060. case ACCESS_CONTROL_IN:
  3061. case ACCESS_CONTROL_OUT:
  3062. case EXTENDED_COPY:
  3063. case READ_ATTRIBUTE:
  3064. case RECEIVE_COPY_RESULTS:
  3065. case WRITE_ATTRIBUTE:
  3066. size = (cdb[10] << 24) | (cdb[11] << 16) |
  3067. (cdb[12] << 8) | cdb[13];
  3068. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3069. break;
  3070. case RECEIVE_DIAGNOSTIC:
  3071. case SEND_DIAGNOSTIC:
  3072. size = (cdb[3] << 8) | cdb[4];
  3073. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3074. break;
  3075. /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
  3076. #if 0
  3077. case GPCMD_READ_CD:
  3078. sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  3079. size = (2336 * sectors);
  3080. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3081. break;
  3082. #endif
  3083. case READ_TOC:
  3084. size = cdb[8];
  3085. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3086. break;
  3087. case REQUEST_SENSE:
  3088. size = cdb[4];
  3089. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3090. break;
  3091. case READ_ELEMENT_STATUS:
  3092. size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
  3093. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3094. break;
  3095. case WRITE_BUFFER:
  3096. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  3097. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3098. break;
  3099. case RESERVE:
  3100. case RESERVE_10:
  3101. /*
  3102. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  3103. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  3104. */
  3105. if (cdb[0] == RESERVE_10)
  3106. size = (cdb[7] << 8) | cdb[8];
  3107. else
  3108. size = cmd->data_length;
  3109. /*
  3110. * Setup the legacy emulated handler for SPC-2 and
  3111. * >= SPC-3 compatible reservation handling (CRH=1)
  3112. * Otherwise, we assume the underlying SCSI logic is
  3113. * is running in SPC_PASSTHROUGH, and wants reservations
  3114. * emulation disabled.
  3115. */
  3116. cmd->transport_emulate_cdb =
  3117. (T10_RES(su_dev)->res_type !=
  3118. SPC_PASSTHROUGH) ?
  3119. &core_scsi2_emulate_crh : NULL;
  3120. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  3121. break;
  3122. case RELEASE:
  3123. case RELEASE_10:
  3124. /*
  3125. * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
  3126. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  3127. */
  3128. if (cdb[0] == RELEASE_10)
  3129. size = (cdb[7] << 8) | cdb[8];
  3130. else
  3131. size = cmd->data_length;
  3132. cmd->transport_emulate_cdb =
  3133. (T10_RES(su_dev)->res_type !=
  3134. SPC_PASSTHROUGH) ?
  3135. &core_scsi2_emulate_crh : NULL;
  3136. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  3137. break;
  3138. case SYNCHRONIZE_CACHE:
  3139. case 0x91: /* SYNCHRONIZE_CACHE_16: */
  3140. /*
  3141. * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
  3142. */
  3143. if (cdb[0] == SYNCHRONIZE_CACHE) {
  3144. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  3145. T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
  3146. } else {
  3147. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  3148. T_TASK(cmd)->t_task_lba = transport_lba_64(cdb);
  3149. }
  3150. if (sector_ret)
  3151. goto out_unsupported_cdb;
  3152. size = transport_get_size(sectors, cdb, cmd);
  3153. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  3154. /*
  3155. * For TCM/pSCSI passthrough, skip cmd->transport_emulate_cdb()
  3156. */
  3157. if (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
  3158. break;
  3159. /*
  3160. * Set SCF_EMULATE_CDB_ASYNC to ensure asynchronous operation
  3161. * for SYNCHRONIZE_CACHE* Immed=1 case in __transport_execute_tasks()
  3162. */
  3163. cmd->se_cmd_flags |= SCF_EMULATE_CDB_ASYNC;
  3164. /*
  3165. * Check to ensure that LBA + Range does not exceed past end of
  3166. * device.
  3167. */
  3168. if (transport_get_sectors(cmd) < 0)
  3169. goto out_invalid_cdb_field;
  3170. break;
  3171. case UNMAP:
  3172. size = get_unaligned_be16(&cdb[7]);
  3173. passthrough = (TRANSPORT(dev)->transport_type ==
  3174. TRANSPORT_PLUGIN_PHBA_PDEV);
  3175. /*
  3176. * Determine if the received UNMAP used to for direct passthrough
  3177. * into Linux/SCSI with struct request via TCM/pSCSI or we are
  3178. * signaling the use of internal transport_generic_unmap() emulation
  3179. * for UNMAP -> Linux/BLOCK disbard with TCM/IBLOCK and TCM/FILEIO
  3180. * subsystem plugin backstores.
  3181. */
  3182. if (!(passthrough))
  3183. cmd->se_cmd_flags |= SCF_EMULATE_SYNC_UNMAP;
  3184. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3185. break;
  3186. case WRITE_SAME_16:
  3187. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  3188. if (sector_ret)
  3189. goto out_unsupported_cdb;
  3190. size = transport_get_size(sectors, cdb, cmd);
  3191. T_TASK(cmd)->t_task_lba = get_unaligned_be16(&cdb[2]);
  3192. passthrough = (TRANSPORT(dev)->transport_type ==
  3193. TRANSPORT_PLUGIN_PHBA_PDEV);
  3194. /*
  3195. * Determine if the received WRITE_SAME_16 is used to for direct
  3196. * passthrough into Linux/SCSI with struct request via TCM/pSCSI
  3197. * or we are signaling the use of internal WRITE_SAME + UNMAP=1
  3198. * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK and
  3199. * TCM/FILEIO subsystem plugin backstores.
  3200. */
  3201. if (!(passthrough)) {
  3202. if ((cdb[1] & 0x04) || (cdb[1] & 0x02)) {
  3203. printk(KERN_ERR "WRITE_SAME PBDATA and LBDATA"
  3204. " bits not supported for Block Discard"
  3205. " Emulation\n");
  3206. goto out_invalid_cdb_field;
  3207. }
  3208. /*
  3209. * Currently for the emulated case we only accept
  3210. * tpws with the UNMAP=1 bit set.
  3211. */
  3212. if (!(cdb[1] & 0x08)) {
  3213. printk(KERN_ERR "WRITE_SAME w/o UNMAP bit not "
  3214. " supported for Block Discard Emulation\n");
  3215. goto out_invalid_cdb_field;
  3216. }
  3217. }
  3218. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  3219. break;
  3220. case ALLOW_MEDIUM_REMOVAL:
  3221. case GPCMD_CLOSE_TRACK:
  3222. case ERASE:
  3223. case INITIALIZE_ELEMENT_STATUS:
  3224. case GPCMD_LOAD_UNLOAD:
  3225. case REZERO_UNIT:
  3226. case SEEK_10:
  3227. case GPCMD_SET_SPEED:
  3228. case SPACE:
  3229. case START_STOP:
  3230. case TEST_UNIT_READY:
  3231. case VERIFY:
  3232. case WRITE_FILEMARKS:
  3233. case MOVE_MEDIUM:
  3234. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  3235. break;
  3236. case REPORT_LUNS:
  3237. cmd->transport_emulate_cdb =
  3238. &transport_core_report_lun_response;
  3239. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  3240. /*
  3241. * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
  3242. * See spc4r17 section 5.3
  3243. */
  3244. if (SE_DEV(cmd)->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  3245. cmd->sam_task_attr = TASK_ATTR_HOQ;
  3246. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3247. break;
  3248. default:
  3249. printk(KERN_WARNING "TARGET_CORE[%s]: Unsupported SCSI Opcode"
  3250. " 0x%02x, sending CHECK_CONDITION.\n",
  3251. CMD_TFO(cmd)->get_fabric_name(), cdb[0]);
  3252. cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
  3253. goto out_unsupported_cdb;
  3254. }
  3255. if (size != cmd->data_length) {
  3256. printk(KERN_WARNING "TARGET_CORE[%s]: Expected Transfer Length:"
  3257. " %u does not match SCSI CDB Length: %u for SAM Opcode:"
  3258. " 0x%02x\n", CMD_TFO(cmd)->get_fabric_name(),
  3259. cmd->data_length, size, cdb[0]);
  3260. cmd->cmd_spdtl = size;
  3261. if (cmd->data_direction == DMA_TO_DEVICE) {
  3262. printk(KERN_ERR "Rejecting underflow/overflow"
  3263. " WRITE data\n");
  3264. goto out_invalid_cdb_field;
  3265. }
  3266. /*
  3267. * Reject READ_* or WRITE_* with overflow/underflow for
  3268. * type SCF_SCSI_DATA_SG_IO_CDB.
  3269. */
  3270. if (!(ret) && (DEV_ATTRIB(dev)->block_size != 512)) {
  3271. printk(KERN_ERR "Failing OVERFLOW/UNDERFLOW for LBA op"
  3272. " CDB on non 512-byte sector setup subsystem"
  3273. " plugin: %s\n", TRANSPORT(dev)->name);
  3274. /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
  3275. goto out_invalid_cdb_field;
  3276. }
  3277. if (size > cmd->data_length) {
  3278. cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
  3279. cmd->residual_count = (size - cmd->data_length);
  3280. } else {
  3281. cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  3282. cmd->residual_count = (cmd->data_length - size);
  3283. }
  3284. cmd->data_length = size;
  3285. }
  3286. transport_set_supported_SAM_opcode(cmd);
  3287. return ret;
  3288. out_unsupported_cdb:
  3289. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3290. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  3291. return -2;
  3292. out_invalid_cdb_field:
  3293. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3294. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3295. return -2;
  3296. }
  3297. static inline void transport_release_tasks(struct se_cmd *);
  3298. /*
  3299. * This function will copy a contiguous *src buffer into a destination
  3300. * struct scatterlist array.
  3301. */
  3302. static void transport_memcpy_write_contig(
  3303. struct se_cmd *cmd,
  3304. struct scatterlist *sg_d,
  3305. unsigned char *src)
  3306. {
  3307. u32 i = 0, length = 0, total_length = cmd->data_length;
  3308. void *dst;
  3309. while (total_length) {
  3310. length = sg_d[i].length;
  3311. if (length > total_length)
  3312. length = total_length;
  3313. dst = sg_virt(&sg_d[i]);
  3314. memcpy(dst, src, length);
  3315. if (!(total_length -= length))
  3316. return;
  3317. src += length;
  3318. i++;
  3319. }
  3320. }
  3321. /*
  3322. * This function will copy a struct scatterlist array *sg_s into a destination
  3323. * contiguous *dst buffer.
  3324. */
  3325. static void transport_memcpy_read_contig(
  3326. struct se_cmd *cmd,
  3327. unsigned char *dst,
  3328. struct scatterlist *sg_s)
  3329. {
  3330. u32 i = 0, length = 0, total_length = cmd->data_length;
  3331. void *src;
  3332. while (total_length) {
  3333. length = sg_s[i].length;
  3334. if (length > total_length)
  3335. length = total_length;
  3336. src = sg_virt(&sg_s[i]);
  3337. memcpy(dst, src, length);
  3338. if (!(total_length -= length))
  3339. return;
  3340. dst += length;
  3341. i++;
  3342. }
  3343. }
  3344. static void transport_memcpy_se_mem_read_contig(
  3345. struct se_cmd *cmd,
  3346. unsigned char *dst,
  3347. struct list_head *se_mem_list)
  3348. {
  3349. struct se_mem *se_mem;
  3350. void *src;
  3351. u32 length = 0, total_length = cmd->data_length;
  3352. list_for_each_entry(se_mem, se_mem_list, se_list) {
  3353. length = se_mem->se_len;
  3354. if (length > total_length)
  3355. length = total_length;
  3356. src = page_address(se_mem->se_page) + se_mem->se_off;
  3357. memcpy(dst, src, length);
  3358. if (!(total_length -= length))
  3359. return;
  3360. dst += length;
  3361. }
  3362. }
  3363. /*
  3364. * Called from transport_generic_complete_ok() and
  3365. * transport_generic_request_failure() to determine which dormant/delayed
  3366. * and ordered cmds need to have their tasks added to the execution queue.
  3367. */
  3368. static void transport_complete_task_attr(struct se_cmd *cmd)
  3369. {
  3370. struct se_device *dev = SE_DEV(cmd);
  3371. struct se_cmd *cmd_p, *cmd_tmp;
  3372. int new_active_tasks = 0;
  3373. if (cmd->sam_task_attr == TASK_ATTR_SIMPLE) {
  3374. atomic_dec(&dev->simple_cmds);
  3375. smp_mb__after_atomic_dec();
  3376. dev->dev_cur_ordered_id++;
  3377. DEBUG_STA("Incremented dev->dev_cur_ordered_id: %u for"
  3378. " SIMPLE: %u\n", dev->dev_cur_ordered_id,
  3379. cmd->se_ordered_id);
  3380. } else if (cmd->sam_task_attr == TASK_ATTR_HOQ) {
  3381. atomic_dec(&dev->dev_hoq_count);
  3382. smp_mb__after_atomic_dec();
  3383. dev->dev_cur_ordered_id++;
  3384. DEBUG_STA("Incremented dev_cur_ordered_id: %u for"
  3385. " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
  3386. cmd->se_ordered_id);
  3387. } else if (cmd->sam_task_attr == TASK_ATTR_ORDERED) {
  3388. spin_lock(&dev->ordered_cmd_lock);
  3389. list_del(&cmd->se_ordered_list);
  3390. atomic_dec(&dev->dev_ordered_sync);
  3391. smp_mb__after_atomic_dec();
  3392. spin_unlock(&dev->ordered_cmd_lock);
  3393. dev->dev_cur_ordered_id++;
  3394. DEBUG_STA("Incremented dev_cur_ordered_id: %u for ORDERED:"
  3395. " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
  3396. }
  3397. /*
  3398. * Process all commands up to the last received
  3399. * ORDERED task attribute which requires another blocking
  3400. * boundary
  3401. */
  3402. spin_lock(&dev->delayed_cmd_lock);
  3403. list_for_each_entry_safe(cmd_p, cmd_tmp,
  3404. &dev->delayed_cmd_list, se_delayed_list) {
  3405. list_del(&cmd_p->se_delayed_list);
  3406. spin_unlock(&dev->delayed_cmd_lock);
  3407. DEBUG_STA("Calling add_tasks() for"
  3408. " cmd_p: 0x%02x Task Attr: 0x%02x"
  3409. " Dormant -> Active, se_ordered_id: %u\n",
  3410. T_TASK(cmd_p)->t_task_cdb[0],
  3411. cmd_p->sam_task_attr, cmd_p->se_ordered_id);
  3412. transport_add_tasks_from_cmd(cmd_p);
  3413. new_active_tasks++;
  3414. spin_lock(&dev->delayed_cmd_lock);
  3415. if (cmd_p->sam_task_attr == TASK_ATTR_ORDERED)
  3416. break;
  3417. }
  3418. spin_unlock(&dev->delayed_cmd_lock);
  3419. /*
  3420. * If new tasks have become active, wake up the transport thread
  3421. * to do the processing of the Active tasks.
  3422. */
  3423. if (new_active_tasks != 0)
  3424. wake_up_interruptible(&dev->dev_queue_obj->thread_wq);
  3425. }
  3426. static void transport_generic_complete_ok(struct se_cmd *cmd)
  3427. {
  3428. int reason = 0;
  3429. /*
  3430. * Check if we need to move delayed/dormant tasks from cmds on the
  3431. * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
  3432. * Attribute.
  3433. */
  3434. if (SE_DEV(cmd)->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  3435. transport_complete_task_attr(cmd);
  3436. /*
  3437. * Check if we need to retrieve a sense buffer from
  3438. * the struct se_cmd in question.
  3439. */
  3440. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  3441. if (transport_get_sense_data(cmd) < 0)
  3442. reason = TCM_NON_EXISTENT_LUN;
  3443. /*
  3444. * Only set when an struct se_task->task_scsi_status returned
  3445. * a non GOOD status.
  3446. */
  3447. if (cmd->scsi_status) {
  3448. transport_send_check_condition_and_sense(
  3449. cmd, reason, 1);
  3450. transport_lun_remove_cmd(cmd);
  3451. transport_cmd_check_stop_to_fabric(cmd);
  3452. return;
  3453. }
  3454. }
  3455. /*
  3456. * Check for a callback, used by amoungst other things
  3457. * XDWRITE_READ_10 emulation.
  3458. */
  3459. if (cmd->transport_complete_callback)
  3460. cmd->transport_complete_callback(cmd);
  3461. switch (cmd->data_direction) {
  3462. case DMA_FROM_DEVICE:
  3463. spin_lock(&cmd->se_lun->lun_sep_lock);
  3464. if (SE_LUN(cmd)->lun_sep) {
  3465. SE_LUN(cmd)->lun_sep->sep_stats.tx_data_octets +=
  3466. cmd->data_length;
  3467. }
  3468. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3469. /*
  3470. * If enabled by TCM fabirc module pre-registered SGL
  3471. * memory, perform the memcpy() from the TCM internal
  3472. * contigious buffer back to the original SGL.
  3473. */
  3474. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_CONTIG_TO_SG)
  3475. transport_memcpy_write_contig(cmd,
  3476. T_TASK(cmd)->t_task_pt_sgl,
  3477. T_TASK(cmd)->t_task_buf);
  3478. CMD_TFO(cmd)->queue_data_in(cmd);
  3479. break;
  3480. case DMA_TO_DEVICE:
  3481. spin_lock(&cmd->se_lun->lun_sep_lock);
  3482. if (SE_LUN(cmd)->lun_sep) {
  3483. SE_LUN(cmd)->lun_sep->sep_stats.rx_data_octets +=
  3484. cmd->data_length;
  3485. }
  3486. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3487. /*
  3488. * Check if we need to send READ payload for BIDI-COMMAND
  3489. */
  3490. if (T_TASK(cmd)->t_mem_bidi_list != NULL) {
  3491. spin_lock(&cmd->se_lun->lun_sep_lock);
  3492. if (SE_LUN(cmd)->lun_sep) {
  3493. SE_LUN(cmd)->lun_sep->sep_stats.tx_data_octets +=
  3494. cmd->data_length;
  3495. }
  3496. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3497. CMD_TFO(cmd)->queue_data_in(cmd);
  3498. break;
  3499. }
  3500. /* Fall through for DMA_TO_DEVICE */
  3501. case DMA_NONE:
  3502. CMD_TFO(cmd)->queue_status(cmd);
  3503. break;
  3504. default:
  3505. break;
  3506. }
  3507. transport_lun_remove_cmd(cmd);
  3508. transport_cmd_check_stop_to_fabric(cmd);
  3509. }
  3510. static void transport_free_dev_tasks(struct se_cmd *cmd)
  3511. {
  3512. struct se_task *task, *task_tmp;
  3513. unsigned long flags;
  3514. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  3515. list_for_each_entry_safe(task, task_tmp,
  3516. &T_TASK(cmd)->t_task_list, t_list) {
  3517. if (atomic_read(&task->task_active))
  3518. continue;
  3519. kfree(task->task_sg_bidi);
  3520. kfree(task->task_sg);
  3521. list_del(&task->t_list);
  3522. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3523. if (task->se_dev)
  3524. TRANSPORT(task->se_dev)->free_task(task);
  3525. else
  3526. printk(KERN_ERR "task[%u] - task->se_dev is NULL\n",
  3527. task->task_no);
  3528. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  3529. }
  3530. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3531. }
  3532. static inline void transport_free_pages(struct se_cmd *cmd)
  3533. {
  3534. struct se_mem *se_mem, *se_mem_tmp;
  3535. int free_page = 1;
  3536. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
  3537. free_page = 0;
  3538. if (cmd->se_dev->transport->do_se_mem_map)
  3539. free_page = 0;
  3540. if (T_TASK(cmd)->t_task_buf) {
  3541. kfree(T_TASK(cmd)->t_task_buf);
  3542. T_TASK(cmd)->t_task_buf = NULL;
  3543. return;
  3544. }
  3545. /*
  3546. * Caller will handle releasing of struct se_mem.
  3547. */
  3548. if (cmd->se_cmd_flags & SCF_CMD_PASSTHROUGH_NOALLOC)
  3549. return;
  3550. if (!(T_TASK(cmd)->t_tasks_se_num))
  3551. return;
  3552. list_for_each_entry_safe(se_mem, se_mem_tmp,
  3553. T_TASK(cmd)->t_mem_list, se_list) {
  3554. /*
  3555. * We only release call __free_page(struct se_mem->se_page) when
  3556. * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
  3557. */
  3558. if (free_page)
  3559. __free_page(se_mem->se_page);
  3560. list_del(&se_mem->se_list);
  3561. kmem_cache_free(se_mem_cache, se_mem);
  3562. }
  3563. if (T_TASK(cmd)->t_mem_bidi_list && T_TASK(cmd)->t_tasks_se_bidi_num) {
  3564. list_for_each_entry_safe(se_mem, se_mem_tmp,
  3565. T_TASK(cmd)->t_mem_bidi_list, se_list) {
  3566. /*
  3567. * We only release call __free_page(struct se_mem->se_page) when
  3568. * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
  3569. */
  3570. if (free_page)
  3571. __free_page(se_mem->se_page);
  3572. list_del(&se_mem->se_list);
  3573. kmem_cache_free(se_mem_cache, se_mem);
  3574. }
  3575. }
  3576. kfree(T_TASK(cmd)->t_mem_bidi_list);
  3577. T_TASK(cmd)->t_mem_bidi_list = NULL;
  3578. kfree(T_TASK(cmd)->t_mem_list);
  3579. T_TASK(cmd)->t_mem_list = NULL;
  3580. T_TASK(cmd)->t_tasks_se_num = 0;
  3581. }
  3582. static inline void transport_release_tasks(struct se_cmd *cmd)
  3583. {
  3584. transport_free_dev_tasks(cmd);
  3585. }
  3586. static inline int transport_dec_and_check(struct se_cmd *cmd)
  3587. {
  3588. unsigned long flags;
  3589. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  3590. if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
  3591. if (!(atomic_dec_and_test(&T_TASK(cmd)->t_fe_count))) {
  3592. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  3593. flags);
  3594. return 1;
  3595. }
  3596. }
  3597. if (atomic_read(&T_TASK(cmd)->t_se_count)) {
  3598. if (!(atomic_dec_and_test(&T_TASK(cmd)->t_se_count))) {
  3599. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  3600. flags);
  3601. return 1;
  3602. }
  3603. }
  3604. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3605. return 0;
  3606. }
  3607. static void transport_release_fe_cmd(struct se_cmd *cmd)
  3608. {
  3609. unsigned long flags;
  3610. if (transport_dec_and_check(cmd))
  3611. return;
  3612. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  3613. if (!(atomic_read(&T_TASK(cmd)->transport_dev_active))) {
  3614. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3615. goto free_pages;
  3616. }
  3617. atomic_set(&T_TASK(cmd)->transport_dev_active, 0);
  3618. transport_all_task_dev_remove_state(cmd);
  3619. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3620. transport_release_tasks(cmd);
  3621. free_pages:
  3622. transport_free_pages(cmd);
  3623. transport_free_se_cmd(cmd);
  3624. CMD_TFO(cmd)->release_cmd_direct(cmd);
  3625. }
  3626. static int transport_generic_remove(
  3627. struct se_cmd *cmd,
  3628. int release_to_pool,
  3629. int session_reinstatement)
  3630. {
  3631. unsigned long flags;
  3632. if (!(T_TASK(cmd)))
  3633. goto release_cmd;
  3634. if (transport_dec_and_check(cmd)) {
  3635. if (session_reinstatement) {
  3636. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  3637. transport_all_task_dev_remove_state(cmd);
  3638. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  3639. flags);
  3640. }
  3641. return 1;
  3642. }
  3643. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  3644. if (!(atomic_read(&T_TASK(cmd)->transport_dev_active))) {
  3645. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3646. goto free_pages;
  3647. }
  3648. atomic_set(&T_TASK(cmd)->transport_dev_active, 0);
  3649. transport_all_task_dev_remove_state(cmd);
  3650. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3651. transport_release_tasks(cmd);
  3652. free_pages:
  3653. transport_free_pages(cmd);
  3654. release_cmd:
  3655. if (release_to_pool) {
  3656. transport_release_cmd_to_pool(cmd);
  3657. } else {
  3658. transport_free_se_cmd(cmd);
  3659. CMD_TFO(cmd)->release_cmd_direct(cmd);
  3660. }
  3661. return 0;
  3662. }
  3663. /*
  3664. * transport_generic_map_mem_to_cmd - Perform SGL -> struct se_mem map
  3665. * @cmd: Associated se_cmd descriptor
  3666. * @mem: SGL style memory for TCM WRITE / READ
  3667. * @sg_mem_num: Number of SGL elements
  3668. * @mem_bidi_in: SGL style memory for TCM BIDI READ
  3669. * @sg_mem_bidi_num: Number of BIDI READ SGL elements
  3670. *
  3671. * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
  3672. * of parameters.
  3673. */
  3674. int transport_generic_map_mem_to_cmd(
  3675. struct se_cmd *cmd,
  3676. struct scatterlist *mem,
  3677. u32 sg_mem_num,
  3678. struct scatterlist *mem_bidi_in,
  3679. u32 sg_mem_bidi_num)
  3680. {
  3681. u32 se_mem_cnt_out = 0;
  3682. int ret;
  3683. if (!(mem) || !(sg_mem_num))
  3684. return 0;
  3685. /*
  3686. * Passed *mem will contain a list_head containing preformatted
  3687. * struct se_mem elements...
  3688. */
  3689. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM)) {
  3690. if ((mem_bidi_in) || (sg_mem_bidi_num)) {
  3691. printk(KERN_ERR "SCF_CMD_PASSTHROUGH_NOALLOC not supported"
  3692. " with BIDI-COMMAND\n");
  3693. return -ENOSYS;
  3694. }
  3695. T_TASK(cmd)->t_mem_list = (struct list_head *)mem;
  3696. T_TASK(cmd)->t_tasks_se_num = sg_mem_num;
  3697. cmd->se_cmd_flags |= SCF_CMD_PASSTHROUGH_NOALLOC;
  3698. return 0;
  3699. }
  3700. /*
  3701. * Otherwise, assume the caller is passing a struct scatterlist
  3702. * array from include/linux/scatterlist.h
  3703. */
  3704. if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
  3705. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
  3706. /*
  3707. * For CDB using TCM struct se_mem linked list scatterlist memory
  3708. * processed into a TCM struct se_subsystem_dev, we do the mapping
  3709. * from the passed physical memory to struct se_mem->se_page here.
  3710. */
  3711. T_TASK(cmd)->t_mem_list = transport_init_se_mem_list();
  3712. if (!(T_TASK(cmd)->t_mem_list))
  3713. return -ENOMEM;
  3714. ret = transport_map_sg_to_mem(cmd,
  3715. T_TASK(cmd)->t_mem_list, mem, &se_mem_cnt_out);
  3716. if (ret < 0)
  3717. return -ENOMEM;
  3718. T_TASK(cmd)->t_tasks_se_num = se_mem_cnt_out;
  3719. /*
  3720. * Setup BIDI READ list of struct se_mem elements
  3721. */
  3722. if ((mem_bidi_in) && (sg_mem_bidi_num)) {
  3723. T_TASK(cmd)->t_mem_bidi_list = transport_init_se_mem_list();
  3724. if (!(T_TASK(cmd)->t_mem_bidi_list)) {
  3725. kfree(T_TASK(cmd)->t_mem_list);
  3726. return -ENOMEM;
  3727. }
  3728. se_mem_cnt_out = 0;
  3729. ret = transport_map_sg_to_mem(cmd,
  3730. T_TASK(cmd)->t_mem_bidi_list, mem_bidi_in,
  3731. &se_mem_cnt_out);
  3732. if (ret < 0) {
  3733. kfree(T_TASK(cmd)->t_mem_list);
  3734. return -ENOMEM;
  3735. }
  3736. T_TASK(cmd)->t_tasks_se_bidi_num = se_mem_cnt_out;
  3737. }
  3738. cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  3739. } else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB) {
  3740. if (mem_bidi_in || sg_mem_bidi_num) {
  3741. printk(KERN_ERR "BIDI-Commands not supported using "
  3742. "SCF_SCSI_CONTROL_NONSG_IO_CDB\n");
  3743. return -ENOSYS;
  3744. }
  3745. /*
  3746. * For incoming CDBs using a contiguous buffer internall with TCM,
  3747. * save the passed struct scatterlist memory. After TCM storage object
  3748. * processing has completed for this struct se_cmd, TCM core will call
  3749. * transport_memcpy_[write,read]_contig() as necessary from
  3750. * transport_generic_complete_ok() and transport_write_pending() in order
  3751. * to copy the TCM buffer to/from the original passed *mem in SGL ->
  3752. * struct scatterlist format.
  3753. */
  3754. cmd->se_cmd_flags |= SCF_PASSTHROUGH_CONTIG_TO_SG;
  3755. T_TASK(cmd)->t_task_pt_sgl = mem;
  3756. }
  3757. return 0;
  3758. }
  3759. EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
  3760. static inline long long transport_dev_end_lba(struct se_device *dev)
  3761. {
  3762. return dev->transport->get_blocks(dev) + 1;
  3763. }
  3764. static int transport_get_sectors(struct se_cmd *cmd)
  3765. {
  3766. struct se_device *dev = SE_DEV(cmd);
  3767. T_TASK(cmd)->t_tasks_sectors =
  3768. (cmd->data_length / DEV_ATTRIB(dev)->block_size);
  3769. if (!(T_TASK(cmd)->t_tasks_sectors))
  3770. T_TASK(cmd)->t_tasks_sectors = 1;
  3771. if (TRANSPORT(dev)->get_device_type(dev) != TYPE_DISK)
  3772. return 0;
  3773. if ((T_TASK(cmd)->t_task_lba + T_TASK(cmd)->t_tasks_sectors) >
  3774. transport_dev_end_lba(dev)) {
  3775. printk(KERN_ERR "LBA: %llu Sectors: %u exceeds"
  3776. " transport_dev_end_lba(): %llu\n",
  3777. T_TASK(cmd)->t_task_lba, T_TASK(cmd)->t_tasks_sectors,
  3778. transport_dev_end_lba(dev));
  3779. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3780. cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY;
  3781. return PYX_TRANSPORT_REQ_TOO_MANY_SECTORS;
  3782. }
  3783. return 0;
  3784. }
  3785. static int transport_new_cmd_obj(struct se_cmd *cmd)
  3786. {
  3787. struct se_device *dev = SE_DEV(cmd);
  3788. u32 task_cdbs = 0, rc;
  3789. if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)) {
  3790. task_cdbs++;
  3791. T_TASK(cmd)->t_task_cdbs++;
  3792. } else {
  3793. int set_counts = 1;
  3794. /*
  3795. * Setup any BIDI READ tasks and memory from
  3796. * T_TASK(cmd)->t_mem_bidi_list so the READ struct se_tasks
  3797. * are queued first for the non pSCSI passthrough case.
  3798. */
  3799. if ((T_TASK(cmd)->t_mem_bidi_list != NULL) &&
  3800. (TRANSPORT(dev)->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV)) {
  3801. rc = transport_generic_get_cdb_count(cmd,
  3802. T_TASK(cmd)->t_task_lba,
  3803. T_TASK(cmd)->t_tasks_sectors,
  3804. DMA_FROM_DEVICE, T_TASK(cmd)->t_mem_bidi_list,
  3805. set_counts);
  3806. if (!(rc)) {
  3807. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3808. cmd->scsi_sense_reason =
  3809. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3810. return PYX_TRANSPORT_LU_COMM_FAILURE;
  3811. }
  3812. set_counts = 0;
  3813. }
  3814. /*
  3815. * Setup the tasks and memory from T_TASK(cmd)->t_mem_list
  3816. * Note for BIDI transfers this will contain the WRITE payload
  3817. */
  3818. task_cdbs = transport_generic_get_cdb_count(cmd,
  3819. T_TASK(cmd)->t_task_lba,
  3820. T_TASK(cmd)->t_tasks_sectors,
  3821. cmd->data_direction, T_TASK(cmd)->t_mem_list,
  3822. set_counts);
  3823. if (!(task_cdbs)) {
  3824. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3825. cmd->scsi_sense_reason =
  3826. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3827. return PYX_TRANSPORT_LU_COMM_FAILURE;
  3828. }
  3829. T_TASK(cmd)->t_task_cdbs += task_cdbs;
  3830. #if 0
  3831. printk(KERN_INFO "data_length: %u, LBA: %llu t_tasks_sectors:"
  3832. " %u, t_task_cdbs: %u\n", obj_ptr, cmd->data_length,
  3833. T_TASK(cmd)->t_task_lba, T_TASK(cmd)->t_tasks_sectors,
  3834. T_TASK(cmd)->t_task_cdbs);
  3835. #endif
  3836. }
  3837. atomic_set(&T_TASK(cmd)->t_task_cdbs_left, task_cdbs);
  3838. atomic_set(&T_TASK(cmd)->t_task_cdbs_ex_left, task_cdbs);
  3839. atomic_set(&T_TASK(cmd)->t_task_cdbs_timeout_left, task_cdbs);
  3840. return 0;
  3841. }
  3842. static struct list_head *transport_init_se_mem_list(void)
  3843. {
  3844. struct list_head *se_mem_list;
  3845. se_mem_list = kzalloc(sizeof(struct list_head), GFP_KERNEL);
  3846. if (!(se_mem_list)) {
  3847. printk(KERN_ERR "Unable to allocate memory for se_mem_list\n");
  3848. return NULL;
  3849. }
  3850. INIT_LIST_HEAD(se_mem_list);
  3851. return se_mem_list;
  3852. }
  3853. static int
  3854. transport_generic_get_mem(struct se_cmd *cmd, u32 length, u32 dma_size)
  3855. {
  3856. unsigned char *buf;
  3857. struct se_mem *se_mem;
  3858. T_TASK(cmd)->t_mem_list = transport_init_se_mem_list();
  3859. if (!(T_TASK(cmd)->t_mem_list))
  3860. return -ENOMEM;
  3861. /*
  3862. * If the device uses memory mapping this is enough.
  3863. */
  3864. if (cmd->se_dev->transport->do_se_mem_map)
  3865. return 0;
  3866. /*
  3867. * Setup BIDI-COMMAND READ list of struct se_mem elements
  3868. */
  3869. if (T_TASK(cmd)->t_tasks_bidi) {
  3870. T_TASK(cmd)->t_mem_bidi_list = transport_init_se_mem_list();
  3871. if (!(T_TASK(cmd)->t_mem_bidi_list)) {
  3872. kfree(T_TASK(cmd)->t_mem_list);
  3873. return -ENOMEM;
  3874. }
  3875. }
  3876. while (length) {
  3877. se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
  3878. if (!(se_mem)) {
  3879. printk(KERN_ERR "Unable to allocate struct se_mem\n");
  3880. goto out;
  3881. }
  3882. /* #warning FIXME Allocate contigous pages for struct se_mem elements */
  3883. se_mem->se_page = alloc_pages(GFP_KERNEL, 0);
  3884. if (!(se_mem->se_page)) {
  3885. printk(KERN_ERR "alloc_pages() failed\n");
  3886. goto out;
  3887. }
  3888. buf = kmap_atomic(se_mem->se_page, KM_IRQ0);
  3889. if (!(buf)) {
  3890. printk(KERN_ERR "kmap_atomic() failed\n");
  3891. goto out;
  3892. }
  3893. INIT_LIST_HEAD(&se_mem->se_list);
  3894. se_mem->se_len = (length > dma_size) ? dma_size : length;
  3895. memset(buf, 0, se_mem->se_len);
  3896. kunmap_atomic(buf, KM_IRQ0);
  3897. list_add_tail(&se_mem->se_list, T_TASK(cmd)->t_mem_list);
  3898. T_TASK(cmd)->t_tasks_se_num++;
  3899. DEBUG_MEM("Allocated struct se_mem page(%p) Length(%u)"
  3900. " Offset(%u)\n", se_mem->se_page, se_mem->se_len,
  3901. se_mem->se_off);
  3902. length -= se_mem->se_len;
  3903. }
  3904. DEBUG_MEM("Allocated total struct se_mem elements(%u)\n",
  3905. T_TASK(cmd)->t_tasks_se_num);
  3906. return 0;
  3907. out:
  3908. if (se_mem)
  3909. __free_pages(se_mem->se_page, 0);
  3910. kmem_cache_free(se_mem_cache, se_mem);
  3911. return -1;
  3912. }
  3913. u32 transport_calc_sg_num(
  3914. struct se_task *task,
  3915. struct se_mem *in_se_mem,
  3916. u32 task_offset)
  3917. {
  3918. struct se_cmd *se_cmd = task->task_se_cmd;
  3919. struct se_device *se_dev = SE_DEV(se_cmd);
  3920. struct se_mem *se_mem = in_se_mem;
  3921. struct target_core_fabric_ops *tfo = CMD_TFO(se_cmd);
  3922. u32 sg_length, task_size = task->task_size, task_sg_num_padded;
  3923. while (task_size != 0) {
  3924. DEBUG_SC("se_mem->se_page(%p) se_mem->se_len(%u)"
  3925. " se_mem->se_off(%u) task_offset(%u)\n",
  3926. se_mem->se_page, se_mem->se_len,
  3927. se_mem->se_off, task_offset);
  3928. if (task_offset == 0) {
  3929. if (task_size >= se_mem->se_len) {
  3930. sg_length = se_mem->se_len;
  3931. if (!(list_is_last(&se_mem->se_list,
  3932. T_TASK(se_cmd)->t_mem_list)))
  3933. se_mem = list_entry(se_mem->se_list.next,
  3934. struct se_mem, se_list);
  3935. } else {
  3936. sg_length = task_size;
  3937. task_size -= sg_length;
  3938. goto next;
  3939. }
  3940. DEBUG_SC("sg_length(%u) task_size(%u)\n",
  3941. sg_length, task_size);
  3942. } else {
  3943. if ((se_mem->se_len - task_offset) > task_size) {
  3944. sg_length = task_size;
  3945. task_size -= sg_length;
  3946. goto next;
  3947. } else {
  3948. sg_length = (se_mem->se_len - task_offset);
  3949. if (!(list_is_last(&se_mem->se_list,
  3950. T_TASK(se_cmd)->t_mem_list)))
  3951. se_mem = list_entry(se_mem->se_list.next,
  3952. struct se_mem, se_list);
  3953. }
  3954. DEBUG_SC("sg_length(%u) task_size(%u)\n",
  3955. sg_length, task_size);
  3956. task_offset = 0;
  3957. }
  3958. task_size -= sg_length;
  3959. next:
  3960. DEBUG_SC("task[%u] - Reducing task_size to(%u)\n",
  3961. task->task_no, task_size);
  3962. task->task_sg_num++;
  3963. }
  3964. /*
  3965. * Check if the fabric module driver is requesting that all
  3966. * struct se_task->task_sg[] be chained together.. If so,
  3967. * then allocate an extra padding SG entry for linking and
  3968. * marking the end of the chained SGL.
  3969. */
  3970. if (tfo->task_sg_chaining) {
  3971. task_sg_num_padded = (task->task_sg_num + 1);
  3972. task->task_padded_sg = 1;
  3973. } else
  3974. task_sg_num_padded = task->task_sg_num;
  3975. task->task_sg = kzalloc(task_sg_num_padded *
  3976. sizeof(struct scatterlist), GFP_KERNEL);
  3977. if (!(task->task_sg)) {
  3978. printk(KERN_ERR "Unable to allocate memory for"
  3979. " task->task_sg\n");
  3980. return 0;
  3981. }
  3982. sg_init_table(&task->task_sg[0], task_sg_num_padded);
  3983. /*
  3984. * Setup task->task_sg_bidi for SCSI READ payload for
  3985. * TCM/pSCSI passthrough if present for BIDI-COMMAND
  3986. */
  3987. if ((T_TASK(se_cmd)->t_mem_bidi_list != NULL) &&
  3988. (TRANSPORT(se_dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)) {
  3989. task->task_sg_bidi = kzalloc(task_sg_num_padded *
  3990. sizeof(struct scatterlist), GFP_KERNEL);
  3991. if (!(task->task_sg_bidi)) {
  3992. printk(KERN_ERR "Unable to allocate memory for"
  3993. " task->task_sg_bidi\n");
  3994. return 0;
  3995. }
  3996. sg_init_table(&task->task_sg_bidi[0], task_sg_num_padded);
  3997. }
  3998. /*
  3999. * For the chaining case, setup the proper end of SGL for the
  4000. * initial submission struct task into struct se_subsystem_api.
  4001. * This will be cleared later by transport_do_task_sg_chain()
  4002. */
  4003. if (task->task_padded_sg) {
  4004. sg_mark_end(&task->task_sg[task->task_sg_num - 1]);
  4005. /*
  4006. * Added the 'if' check before marking end of bi-directional
  4007. * scatterlist (which gets created only in case of request
  4008. * (RD + WR).
  4009. */
  4010. if (task->task_sg_bidi)
  4011. sg_mark_end(&task->task_sg_bidi[task->task_sg_num - 1]);
  4012. }
  4013. DEBUG_SC("Successfully allocated task->task_sg_num(%u),"
  4014. " task_sg_num_padded(%u)\n", task->task_sg_num,
  4015. task_sg_num_padded);
  4016. return task->task_sg_num;
  4017. }
  4018. static inline int transport_set_tasks_sectors_disk(
  4019. struct se_task *task,
  4020. struct se_device *dev,
  4021. unsigned long long lba,
  4022. u32 sectors,
  4023. int *max_sectors_set)
  4024. {
  4025. if ((lba + sectors) > transport_dev_end_lba(dev)) {
  4026. task->task_sectors = ((transport_dev_end_lba(dev) - lba) + 1);
  4027. if (task->task_sectors > DEV_ATTRIB(dev)->max_sectors) {
  4028. task->task_sectors = DEV_ATTRIB(dev)->max_sectors;
  4029. *max_sectors_set = 1;
  4030. }
  4031. } else {
  4032. if (sectors > DEV_ATTRIB(dev)->max_sectors) {
  4033. task->task_sectors = DEV_ATTRIB(dev)->max_sectors;
  4034. *max_sectors_set = 1;
  4035. } else
  4036. task->task_sectors = sectors;
  4037. }
  4038. return 0;
  4039. }
  4040. static inline int transport_set_tasks_sectors_non_disk(
  4041. struct se_task *task,
  4042. struct se_device *dev,
  4043. unsigned long long lba,
  4044. u32 sectors,
  4045. int *max_sectors_set)
  4046. {
  4047. if (sectors > DEV_ATTRIB(dev)->max_sectors) {
  4048. task->task_sectors = DEV_ATTRIB(dev)->max_sectors;
  4049. *max_sectors_set = 1;
  4050. } else
  4051. task->task_sectors = sectors;
  4052. return 0;
  4053. }
  4054. static inline int transport_set_tasks_sectors(
  4055. struct se_task *task,
  4056. struct se_device *dev,
  4057. unsigned long long lba,
  4058. u32 sectors,
  4059. int *max_sectors_set)
  4060. {
  4061. return (TRANSPORT(dev)->get_device_type(dev) == TYPE_DISK) ?
  4062. transport_set_tasks_sectors_disk(task, dev, lba, sectors,
  4063. max_sectors_set) :
  4064. transport_set_tasks_sectors_non_disk(task, dev, lba, sectors,
  4065. max_sectors_set);
  4066. }
  4067. static int transport_map_sg_to_mem(
  4068. struct se_cmd *cmd,
  4069. struct list_head *se_mem_list,
  4070. void *in_mem,
  4071. u32 *se_mem_cnt)
  4072. {
  4073. struct se_mem *se_mem;
  4074. struct scatterlist *sg;
  4075. u32 sg_count = 1, cmd_size = cmd->data_length;
  4076. if (!in_mem) {
  4077. printk(KERN_ERR "No source scatterlist\n");
  4078. return -1;
  4079. }
  4080. sg = (struct scatterlist *)in_mem;
  4081. while (cmd_size) {
  4082. se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
  4083. if (!(se_mem)) {
  4084. printk(KERN_ERR "Unable to allocate struct se_mem\n");
  4085. return -1;
  4086. }
  4087. INIT_LIST_HEAD(&se_mem->se_list);
  4088. DEBUG_MEM("sg_to_mem: Starting loop with cmd_size: %u"
  4089. " sg_page: %p offset: %d length: %d\n", cmd_size,
  4090. sg_page(sg), sg->offset, sg->length);
  4091. se_mem->se_page = sg_page(sg);
  4092. se_mem->se_off = sg->offset;
  4093. if (cmd_size > sg->length) {
  4094. se_mem->se_len = sg->length;
  4095. sg = sg_next(sg);
  4096. sg_count++;
  4097. } else
  4098. se_mem->se_len = cmd_size;
  4099. cmd_size -= se_mem->se_len;
  4100. DEBUG_MEM("sg_to_mem: *se_mem_cnt: %u cmd_size: %u\n",
  4101. *se_mem_cnt, cmd_size);
  4102. DEBUG_MEM("sg_to_mem: Final se_page: %p se_off: %d se_len: %d\n",
  4103. se_mem->se_page, se_mem->se_off, se_mem->se_len);
  4104. list_add_tail(&se_mem->se_list, se_mem_list);
  4105. (*se_mem_cnt)++;
  4106. }
  4107. DEBUG_MEM("task[0] - Mapped(%u) struct scatterlist segments to(%u)"
  4108. " struct se_mem\n", sg_count, *se_mem_cnt);
  4109. if (sg_count != *se_mem_cnt)
  4110. BUG();
  4111. return 0;
  4112. }
  4113. /* transport_map_mem_to_sg():
  4114. *
  4115. *
  4116. */
  4117. int transport_map_mem_to_sg(
  4118. struct se_task *task,
  4119. struct list_head *se_mem_list,
  4120. void *in_mem,
  4121. struct se_mem *in_se_mem,
  4122. struct se_mem **out_se_mem,
  4123. u32 *se_mem_cnt,
  4124. u32 *task_offset)
  4125. {
  4126. struct se_cmd *se_cmd = task->task_se_cmd;
  4127. struct se_mem *se_mem = in_se_mem;
  4128. struct scatterlist *sg = (struct scatterlist *)in_mem;
  4129. u32 task_size = task->task_size, sg_no = 0;
  4130. if (!sg) {
  4131. printk(KERN_ERR "Unable to locate valid struct"
  4132. " scatterlist pointer\n");
  4133. return -1;
  4134. }
  4135. while (task_size != 0) {
  4136. /*
  4137. * Setup the contigious array of scatterlists for
  4138. * this struct se_task.
  4139. */
  4140. sg_assign_page(sg, se_mem->se_page);
  4141. if (*task_offset == 0) {
  4142. sg->offset = se_mem->se_off;
  4143. if (task_size >= se_mem->se_len) {
  4144. sg->length = se_mem->se_len;
  4145. if (!(list_is_last(&se_mem->se_list,
  4146. T_TASK(se_cmd)->t_mem_list))) {
  4147. se_mem = list_entry(se_mem->se_list.next,
  4148. struct se_mem, se_list);
  4149. (*se_mem_cnt)++;
  4150. }
  4151. } else {
  4152. sg->length = task_size;
  4153. /*
  4154. * Determine if we need to calculate an offset
  4155. * into the struct se_mem on the next go around..
  4156. */
  4157. task_size -= sg->length;
  4158. if (!(task_size))
  4159. *task_offset = sg->length;
  4160. goto next;
  4161. }
  4162. } else {
  4163. sg->offset = (*task_offset + se_mem->se_off);
  4164. if ((se_mem->se_len - *task_offset) > task_size) {
  4165. sg->length = task_size;
  4166. /*
  4167. * Determine if we need to calculate an offset
  4168. * into the struct se_mem on the next go around..
  4169. */
  4170. task_size -= sg->length;
  4171. if (!(task_size))
  4172. *task_offset += sg->length;
  4173. goto next;
  4174. } else {
  4175. sg->length = (se_mem->se_len - *task_offset);
  4176. if (!(list_is_last(&se_mem->se_list,
  4177. T_TASK(se_cmd)->t_mem_list))) {
  4178. se_mem = list_entry(se_mem->se_list.next,
  4179. struct se_mem, se_list);
  4180. (*se_mem_cnt)++;
  4181. }
  4182. }
  4183. *task_offset = 0;
  4184. }
  4185. task_size -= sg->length;
  4186. next:
  4187. DEBUG_MEM("task[%u] mem_to_sg - sg[%u](%p)(%u)(%u) - Reducing"
  4188. " task_size to(%u), task_offset: %u\n", task->task_no, sg_no,
  4189. sg_page(sg), sg->length, sg->offset, task_size, *task_offset);
  4190. sg_no++;
  4191. if (!(task_size))
  4192. break;
  4193. sg = sg_next(sg);
  4194. if (task_size > se_cmd->data_length)
  4195. BUG();
  4196. }
  4197. *out_se_mem = se_mem;
  4198. DEBUG_MEM("task[%u] - Mapped(%u) struct se_mem segments to total(%u)"
  4199. " SGs\n", task->task_no, *se_mem_cnt, sg_no);
  4200. return 0;
  4201. }
  4202. /*
  4203. * This function can be used by HW target mode drivers to create a linked
  4204. * scatterlist from all contiguously allocated struct se_task->task_sg[].
  4205. * This is intended to be called during the completion path by TCM Core
  4206. * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
  4207. */
  4208. void transport_do_task_sg_chain(struct se_cmd *cmd)
  4209. {
  4210. struct scatterlist *sg_head = NULL, *sg_link = NULL, *sg_first = NULL;
  4211. struct scatterlist *sg_head_cur = NULL, *sg_link_cur = NULL;
  4212. struct scatterlist *sg, *sg_end = NULL, *sg_end_cur = NULL;
  4213. struct se_task *task;
  4214. struct target_core_fabric_ops *tfo = CMD_TFO(cmd);
  4215. u32 task_sg_num = 0, sg_count = 0;
  4216. int i;
  4217. if (tfo->task_sg_chaining == 0) {
  4218. printk(KERN_ERR "task_sg_chaining is diabled for fabric module:"
  4219. " %s\n", tfo->get_fabric_name());
  4220. dump_stack();
  4221. return;
  4222. }
  4223. /*
  4224. * Walk the struct se_task list and setup scatterlist chains
  4225. * for each contiguosly allocated struct se_task->task_sg[].
  4226. */
  4227. list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
  4228. if (!(task->task_sg) || !(task->task_padded_sg))
  4229. continue;
  4230. if (sg_head && sg_link) {
  4231. sg_head_cur = &task->task_sg[0];
  4232. sg_link_cur = &task->task_sg[task->task_sg_num];
  4233. /*
  4234. * Either add chain or mark end of scatterlist
  4235. */
  4236. if (!(list_is_last(&task->t_list,
  4237. &T_TASK(cmd)->t_task_list))) {
  4238. /*
  4239. * Clear existing SGL termination bit set in
  4240. * transport_calc_sg_num(), see sg_mark_end()
  4241. */
  4242. sg_end_cur = &task->task_sg[task->task_sg_num - 1];
  4243. sg_end_cur->page_link &= ~0x02;
  4244. sg_chain(sg_head, task_sg_num, sg_head_cur);
  4245. sg_count += (task->task_sg_num + 1);
  4246. } else
  4247. sg_count += task->task_sg_num;
  4248. sg_head = sg_head_cur;
  4249. sg_link = sg_link_cur;
  4250. task_sg_num = task->task_sg_num;
  4251. continue;
  4252. }
  4253. sg_head = sg_first = &task->task_sg[0];
  4254. sg_link = &task->task_sg[task->task_sg_num];
  4255. task_sg_num = task->task_sg_num;
  4256. /*
  4257. * Check for single task..
  4258. */
  4259. if (!(list_is_last(&task->t_list, &T_TASK(cmd)->t_task_list))) {
  4260. /*
  4261. * Clear existing SGL termination bit set in
  4262. * transport_calc_sg_num(), see sg_mark_end()
  4263. */
  4264. sg_end = &task->task_sg[task->task_sg_num - 1];
  4265. sg_end->page_link &= ~0x02;
  4266. sg_count += (task->task_sg_num + 1);
  4267. } else
  4268. sg_count += task->task_sg_num;
  4269. }
  4270. /*
  4271. * Setup the starting pointer and total t_tasks_sg_linked_no including
  4272. * padding SGs for linking and to mark the end.
  4273. */
  4274. T_TASK(cmd)->t_tasks_sg_chained = sg_first;
  4275. T_TASK(cmd)->t_tasks_sg_chained_no = sg_count;
  4276. DEBUG_CMD_M("Setup T_TASK(cmd)->t_tasks_sg_chained: %p and"
  4277. " t_tasks_sg_chained_no: %u\n", T_TASK(cmd)->t_tasks_sg_chained,
  4278. T_TASK(cmd)->t_tasks_sg_chained_no);
  4279. for_each_sg(T_TASK(cmd)->t_tasks_sg_chained, sg,
  4280. T_TASK(cmd)->t_tasks_sg_chained_no, i) {
  4281. DEBUG_CMD_M("SG: %p page: %p length: %d offset: %d\n",
  4282. sg, sg_page(sg), sg->length, sg->offset);
  4283. if (sg_is_chain(sg))
  4284. DEBUG_CMD_M("SG: %p sg_is_chain=1\n", sg);
  4285. if (sg_is_last(sg))
  4286. DEBUG_CMD_M("SG: %p sg_is_last=1\n", sg);
  4287. }
  4288. }
  4289. EXPORT_SYMBOL(transport_do_task_sg_chain);
  4290. static int transport_do_se_mem_map(
  4291. struct se_device *dev,
  4292. struct se_task *task,
  4293. struct list_head *se_mem_list,
  4294. void *in_mem,
  4295. struct se_mem *in_se_mem,
  4296. struct se_mem **out_se_mem,
  4297. u32 *se_mem_cnt,
  4298. u32 *task_offset_in)
  4299. {
  4300. u32 task_offset = *task_offset_in;
  4301. int ret = 0;
  4302. /*
  4303. * se_subsystem_api_t->do_se_mem_map is used when internal allocation
  4304. * has been done by the transport plugin.
  4305. */
  4306. if (TRANSPORT(dev)->do_se_mem_map) {
  4307. ret = TRANSPORT(dev)->do_se_mem_map(task, se_mem_list,
  4308. in_mem, in_se_mem, out_se_mem, se_mem_cnt,
  4309. task_offset_in);
  4310. if (ret == 0)
  4311. T_TASK(task->task_se_cmd)->t_tasks_se_num += *se_mem_cnt;
  4312. return ret;
  4313. }
  4314. BUG_ON(list_empty(se_mem_list));
  4315. /*
  4316. * This is the normal path for all normal non BIDI and BIDI-COMMAND
  4317. * WRITE payloads.. If we need to do BIDI READ passthrough for
  4318. * TCM/pSCSI the first call to transport_do_se_mem_map ->
  4319. * transport_calc_sg_num() -> transport_map_mem_to_sg() will do the
  4320. * allocation for task->task_sg_bidi, and the subsequent call to
  4321. * transport_do_se_mem_map() from transport_generic_get_cdb_count()
  4322. */
  4323. if (!(task->task_sg_bidi)) {
  4324. /*
  4325. * Assume default that transport plugin speaks preallocated
  4326. * scatterlists.
  4327. */
  4328. if (!(transport_calc_sg_num(task, in_se_mem, task_offset)))
  4329. return -1;
  4330. /*
  4331. * struct se_task->task_sg now contains the struct scatterlist array.
  4332. */
  4333. return transport_map_mem_to_sg(task, se_mem_list, task->task_sg,
  4334. in_se_mem, out_se_mem, se_mem_cnt,
  4335. task_offset_in);
  4336. }
  4337. /*
  4338. * Handle the se_mem_list -> struct task->task_sg_bidi
  4339. * memory map for the extra BIDI READ payload
  4340. */
  4341. return transport_map_mem_to_sg(task, se_mem_list, task->task_sg_bidi,
  4342. in_se_mem, out_se_mem, se_mem_cnt,
  4343. task_offset_in);
  4344. }
  4345. static u32 transport_generic_get_cdb_count(
  4346. struct se_cmd *cmd,
  4347. unsigned long long lba,
  4348. u32 sectors,
  4349. enum dma_data_direction data_direction,
  4350. struct list_head *mem_list,
  4351. int set_counts)
  4352. {
  4353. unsigned char *cdb = NULL;
  4354. struct se_task *task;
  4355. struct se_mem *se_mem = NULL, *se_mem_lout = NULL;
  4356. struct se_mem *se_mem_bidi = NULL, *se_mem_bidi_lout = NULL;
  4357. struct se_device *dev = SE_DEV(cmd);
  4358. int max_sectors_set = 0, ret;
  4359. u32 task_offset_in = 0, se_mem_cnt = 0, se_mem_bidi_cnt = 0, task_cdbs = 0;
  4360. if (!mem_list) {
  4361. printk(KERN_ERR "mem_list is NULL in transport_generic_get"
  4362. "_cdb_count()\n");
  4363. return 0;
  4364. }
  4365. /*
  4366. * While using RAMDISK_DR backstores is the only case where
  4367. * mem_list will ever be empty at this point.
  4368. */
  4369. if (!(list_empty(mem_list)))
  4370. se_mem = list_entry(mem_list->next, struct se_mem, se_list);
  4371. /*
  4372. * Check for extra se_mem_bidi mapping for BIDI-COMMANDs to
  4373. * struct se_task->task_sg_bidi for TCM/pSCSI passthrough operation
  4374. */
  4375. if ((T_TASK(cmd)->t_mem_bidi_list != NULL) &&
  4376. !(list_empty(T_TASK(cmd)->t_mem_bidi_list)) &&
  4377. (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV))
  4378. se_mem_bidi = list_entry(T_TASK(cmd)->t_mem_bidi_list->next,
  4379. struct se_mem, se_list);
  4380. while (sectors) {
  4381. DEBUG_VOL("ITT[0x%08x] LBA(%llu) SectorsLeft(%u) EOBJ(%llu)\n",
  4382. CMD_TFO(cmd)->get_task_tag(cmd), lba, sectors,
  4383. transport_dev_end_lba(dev));
  4384. task = transport_generic_get_task(cmd, data_direction);
  4385. if (!(task))
  4386. goto out;
  4387. transport_set_tasks_sectors(task, dev, lba, sectors,
  4388. &max_sectors_set);
  4389. task->task_lba = lba;
  4390. lba += task->task_sectors;
  4391. sectors -= task->task_sectors;
  4392. task->task_size = (task->task_sectors *
  4393. DEV_ATTRIB(dev)->block_size);
  4394. cdb = TRANSPORT(dev)->get_cdb(task);
  4395. if ((cdb)) {
  4396. memcpy(cdb, T_TASK(cmd)->t_task_cdb,
  4397. scsi_command_size(T_TASK(cmd)->t_task_cdb));
  4398. cmd->transport_split_cdb(task->task_lba,
  4399. &task->task_sectors, cdb);
  4400. }
  4401. /*
  4402. * Perform the SE OBJ plugin and/or Transport plugin specific
  4403. * mapping for T_TASK(cmd)->t_mem_list. And setup the
  4404. * task->task_sg and if necessary task->task_sg_bidi
  4405. */
  4406. ret = transport_do_se_mem_map(dev, task, mem_list,
  4407. NULL, se_mem, &se_mem_lout, &se_mem_cnt,
  4408. &task_offset_in);
  4409. if (ret < 0)
  4410. goto out;
  4411. se_mem = se_mem_lout;
  4412. /*
  4413. * Setup the T_TASK(cmd)->t_mem_bidi_list -> task->task_sg_bidi
  4414. * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI
  4415. *
  4416. * Note that the first call to transport_do_se_mem_map() above will
  4417. * allocate struct se_task->task_sg_bidi in transport_do_se_mem_map()
  4418. * -> transport_calc_sg_num(), and the second here will do the
  4419. * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI.
  4420. */
  4421. if (task->task_sg_bidi != NULL) {
  4422. ret = transport_do_se_mem_map(dev, task,
  4423. T_TASK(cmd)->t_mem_bidi_list, NULL,
  4424. se_mem_bidi, &se_mem_bidi_lout, &se_mem_bidi_cnt,
  4425. &task_offset_in);
  4426. if (ret < 0)
  4427. goto out;
  4428. se_mem_bidi = se_mem_bidi_lout;
  4429. }
  4430. task_cdbs++;
  4431. DEBUG_VOL("Incremented task_cdbs(%u) task->task_sg_num(%u)\n",
  4432. task_cdbs, task->task_sg_num);
  4433. if (max_sectors_set) {
  4434. max_sectors_set = 0;
  4435. continue;
  4436. }
  4437. if (!sectors)
  4438. break;
  4439. }
  4440. if (set_counts) {
  4441. atomic_inc(&T_TASK(cmd)->t_fe_count);
  4442. atomic_inc(&T_TASK(cmd)->t_se_count);
  4443. }
  4444. DEBUG_VOL("ITT[0x%08x] total %s cdbs(%u)\n",
  4445. CMD_TFO(cmd)->get_task_tag(cmd), (data_direction == DMA_TO_DEVICE)
  4446. ? "DMA_TO_DEVICE" : "DMA_FROM_DEVICE", task_cdbs);
  4447. return task_cdbs;
  4448. out:
  4449. return 0;
  4450. }
  4451. static int
  4452. transport_map_control_cmd_to_task(struct se_cmd *cmd)
  4453. {
  4454. struct se_device *dev = SE_DEV(cmd);
  4455. unsigned char *cdb;
  4456. struct se_task *task;
  4457. int ret;
  4458. task = transport_generic_get_task(cmd, cmd->data_direction);
  4459. if (!task)
  4460. return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  4461. cdb = TRANSPORT(dev)->get_cdb(task);
  4462. if (cdb)
  4463. memcpy(cdb, cmd->t_task->t_task_cdb,
  4464. scsi_command_size(cmd->t_task->t_task_cdb));
  4465. task->task_size = cmd->data_length;
  4466. task->task_sg_num =
  4467. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) ? 1 : 0;
  4468. atomic_inc(&cmd->t_task->t_fe_count);
  4469. atomic_inc(&cmd->t_task->t_se_count);
  4470. if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) {
  4471. struct se_mem *se_mem = NULL, *se_mem_lout = NULL;
  4472. u32 se_mem_cnt = 0, task_offset = 0;
  4473. if (!list_empty(T_TASK(cmd)->t_mem_list))
  4474. se_mem = list_entry(T_TASK(cmd)->t_mem_list->next,
  4475. struct se_mem, se_list);
  4476. ret = transport_do_se_mem_map(dev, task,
  4477. cmd->t_task->t_mem_list, NULL, se_mem,
  4478. &se_mem_lout, &se_mem_cnt, &task_offset);
  4479. if (ret < 0)
  4480. return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  4481. if (dev->transport->map_task_SG)
  4482. return dev->transport->map_task_SG(task);
  4483. return 0;
  4484. } else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB) {
  4485. if (dev->transport->map_task_non_SG)
  4486. return dev->transport->map_task_non_SG(task);
  4487. return 0;
  4488. } else if (cmd->se_cmd_flags & SCF_SCSI_NON_DATA_CDB) {
  4489. if (dev->transport->cdb_none)
  4490. return dev->transport->cdb_none(task);
  4491. return 0;
  4492. } else {
  4493. BUG();
  4494. return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  4495. }
  4496. }
  4497. /* transport_generic_new_cmd(): Called from transport_processing_thread()
  4498. *
  4499. * Allocate storage transport resources from a set of values predefined
  4500. * by transport_generic_cmd_sequencer() from the iSCSI Target RX process.
  4501. * Any non zero return here is treated as an "out of resource' op here.
  4502. */
  4503. /*
  4504. * Generate struct se_task(s) and/or their payloads for this CDB.
  4505. */
  4506. static int transport_generic_new_cmd(struct se_cmd *cmd)
  4507. {
  4508. struct se_portal_group *se_tpg;
  4509. struct se_task *task;
  4510. struct se_device *dev = SE_DEV(cmd);
  4511. int ret = 0;
  4512. /*
  4513. * Determine is the TCM fabric module has already allocated physical
  4514. * memory, and is directly calling transport_generic_map_mem_to_cmd()
  4515. * to setup beforehand the linked list of physical memory at
  4516. * T_TASK(cmd)->t_mem_list of struct se_mem->se_page
  4517. */
  4518. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)) {
  4519. ret = transport_allocate_resources(cmd);
  4520. if (ret < 0)
  4521. return ret;
  4522. }
  4523. ret = transport_get_sectors(cmd);
  4524. if (ret < 0)
  4525. return ret;
  4526. ret = transport_new_cmd_obj(cmd);
  4527. if (ret < 0)
  4528. return ret;
  4529. /*
  4530. * Determine if the calling TCM fabric module is talking to
  4531. * Linux/NET via kernel sockets and needs to allocate a
  4532. * struct iovec array to complete the struct se_cmd
  4533. */
  4534. se_tpg = SE_LUN(cmd)->lun_sep->sep_tpg;
  4535. if (TPG_TFO(se_tpg)->alloc_cmd_iovecs != NULL) {
  4536. ret = TPG_TFO(se_tpg)->alloc_cmd_iovecs(cmd);
  4537. if (ret < 0)
  4538. return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  4539. }
  4540. if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
  4541. list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
  4542. if (atomic_read(&task->task_sent))
  4543. continue;
  4544. if (!dev->transport->map_task_SG)
  4545. continue;
  4546. ret = dev->transport->map_task_SG(task);
  4547. if (ret < 0)
  4548. return ret;
  4549. }
  4550. } else {
  4551. ret = transport_map_control_cmd_to_task(cmd);
  4552. if (ret < 0)
  4553. return ret;
  4554. }
  4555. /*
  4556. * For WRITEs, let the iSCSI Target RX Thread know its buffer is ready..
  4557. * This WRITE struct se_cmd (and all of its associated struct se_task's)
  4558. * will be added to the struct se_device execution queue after its WRITE
  4559. * data has arrived. (ie: It gets handled by the transport processing
  4560. * thread a second time)
  4561. */
  4562. if (cmd->data_direction == DMA_TO_DEVICE) {
  4563. transport_add_tasks_to_state_queue(cmd);
  4564. return transport_generic_write_pending(cmd);
  4565. }
  4566. /*
  4567. * Everything else but a WRITE, add the struct se_cmd's struct se_task's
  4568. * to the execution queue.
  4569. */
  4570. transport_execute_tasks(cmd);
  4571. return 0;
  4572. }
  4573. /* transport_generic_process_write():
  4574. *
  4575. *
  4576. */
  4577. void transport_generic_process_write(struct se_cmd *cmd)
  4578. {
  4579. #if 0
  4580. /*
  4581. * Copy SCSI Presented DTL sector(s) from received buffers allocated to
  4582. * original EDTL
  4583. */
  4584. if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
  4585. if (!T_TASK(cmd)->t_tasks_se_num) {
  4586. unsigned char *dst, *buf =
  4587. (unsigned char *)T_TASK(cmd)->t_task_buf;
  4588. dst = kzalloc(cmd->cmd_spdtl), GFP_KERNEL);
  4589. if (!(dst)) {
  4590. printk(KERN_ERR "Unable to allocate memory for"
  4591. " WRITE underflow\n");
  4592. transport_generic_request_failure(cmd, NULL,
  4593. PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
  4594. return;
  4595. }
  4596. memcpy(dst, buf, cmd->cmd_spdtl);
  4597. kfree(T_TASK(cmd)->t_task_buf);
  4598. T_TASK(cmd)->t_task_buf = dst;
  4599. } else {
  4600. struct scatterlist *sg =
  4601. (struct scatterlist *sg)T_TASK(cmd)->t_task_buf;
  4602. struct scatterlist *orig_sg;
  4603. orig_sg = kzalloc(sizeof(struct scatterlist) *
  4604. T_TASK(cmd)->t_tasks_se_num,
  4605. GFP_KERNEL))) {
  4606. if (!(orig_sg)) {
  4607. printk(KERN_ERR "Unable to allocate memory"
  4608. " for WRITE underflow\n");
  4609. transport_generic_request_failure(cmd, NULL,
  4610. PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
  4611. return;
  4612. }
  4613. memcpy(orig_sg, T_TASK(cmd)->t_task_buf,
  4614. sizeof(struct scatterlist) *
  4615. T_TASK(cmd)->t_tasks_se_num);
  4616. cmd->data_length = cmd->cmd_spdtl;
  4617. /*
  4618. * FIXME, clear out original struct se_task and state
  4619. * information.
  4620. */
  4621. if (transport_generic_new_cmd(cmd) < 0) {
  4622. transport_generic_request_failure(cmd, NULL,
  4623. PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
  4624. kfree(orig_sg);
  4625. return;
  4626. }
  4627. transport_memcpy_write_sg(cmd, orig_sg);
  4628. }
  4629. }
  4630. #endif
  4631. transport_execute_tasks(cmd);
  4632. }
  4633. EXPORT_SYMBOL(transport_generic_process_write);
  4634. /* transport_generic_write_pending():
  4635. *
  4636. *
  4637. */
  4638. static int transport_generic_write_pending(struct se_cmd *cmd)
  4639. {
  4640. unsigned long flags;
  4641. int ret;
  4642. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  4643. cmd->t_state = TRANSPORT_WRITE_PENDING;
  4644. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4645. /*
  4646. * For the TCM control CDBs using a contiguous buffer, do the memcpy
  4647. * from the passed Linux/SCSI struct scatterlist located at
  4648. * T_TASK(se_cmd)->t_task_pt_buf to the contiguous buffer at
  4649. * T_TASK(se_cmd)->t_task_buf.
  4650. */
  4651. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_CONTIG_TO_SG)
  4652. transport_memcpy_read_contig(cmd,
  4653. T_TASK(cmd)->t_task_buf,
  4654. T_TASK(cmd)->t_task_pt_sgl);
  4655. /*
  4656. * Clear the se_cmd for WRITE_PENDING status in order to set
  4657. * T_TASK(cmd)->t_transport_active=0 so that transport_generic_handle_data
  4658. * can be called from HW target mode interrupt code. This is safe
  4659. * to be called with transport_off=1 before the CMD_TFO(cmd)->write_pending
  4660. * because the se_cmd->se_lun pointer is not being cleared.
  4661. */
  4662. transport_cmd_check_stop(cmd, 1, 0);
  4663. /*
  4664. * Call the fabric write_pending function here to let the
  4665. * frontend know that WRITE buffers are ready.
  4666. */
  4667. ret = CMD_TFO(cmd)->write_pending(cmd);
  4668. if (ret < 0)
  4669. return ret;
  4670. return PYX_TRANSPORT_WRITE_PENDING;
  4671. }
  4672. /* transport_release_cmd_to_pool():
  4673. *
  4674. *
  4675. */
  4676. void transport_release_cmd_to_pool(struct se_cmd *cmd)
  4677. {
  4678. BUG_ON(!T_TASK(cmd));
  4679. BUG_ON(!CMD_TFO(cmd));
  4680. transport_free_se_cmd(cmd);
  4681. CMD_TFO(cmd)->release_cmd_to_pool(cmd);
  4682. }
  4683. EXPORT_SYMBOL(transport_release_cmd_to_pool);
  4684. /* transport_generic_free_cmd():
  4685. *
  4686. * Called from processing frontend to release storage engine resources
  4687. */
  4688. void transport_generic_free_cmd(
  4689. struct se_cmd *cmd,
  4690. int wait_for_tasks,
  4691. int release_to_pool,
  4692. int session_reinstatement)
  4693. {
  4694. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) || !T_TASK(cmd))
  4695. transport_release_cmd_to_pool(cmd);
  4696. else {
  4697. core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
  4698. if (SE_LUN(cmd)) {
  4699. #if 0
  4700. printk(KERN_INFO "cmd: %p ITT: 0x%08x contains"
  4701. " SE_LUN(cmd)\n", cmd,
  4702. CMD_TFO(cmd)->get_task_tag(cmd));
  4703. #endif
  4704. transport_lun_remove_cmd(cmd);
  4705. }
  4706. if (wait_for_tasks && cmd->transport_wait_for_tasks)
  4707. cmd->transport_wait_for_tasks(cmd, 0, 0);
  4708. transport_generic_remove(cmd, release_to_pool,
  4709. session_reinstatement);
  4710. }
  4711. }
  4712. EXPORT_SYMBOL(transport_generic_free_cmd);
  4713. static void transport_nop_wait_for_tasks(
  4714. struct se_cmd *cmd,
  4715. int remove_cmd,
  4716. int session_reinstatement)
  4717. {
  4718. return;
  4719. }
  4720. /* transport_lun_wait_for_tasks():
  4721. *
  4722. * Called from ConfigFS context to stop the passed struct se_cmd to allow
  4723. * an struct se_lun to be successfully shutdown.
  4724. */
  4725. static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
  4726. {
  4727. unsigned long flags;
  4728. int ret;
  4729. /*
  4730. * If the frontend has already requested this struct se_cmd to
  4731. * be stopped, we can safely ignore this struct se_cmd.
  4732. */
  4733. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  4734. if (atomic_read(&T_TASK(cmd)->t_transport_stop)) {
  4735. atomic_set(&T_TASK(cmd)->transport_lun_stop, 0);
  4736. DEBUG_TRANSPORT_S("ConfigFS ITT[0x%08x] - t_transport_stop =="
  4737. " TRUE, skipping\n", CMD_TFO(cmd)->get_task_tag(cmd));
  4738. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4739. transport_cmd_check_stop(cmd, 1, 0);
  4740. return -1;
  4741. }
  4742. atomic_set(&T_TASK(cmd)->transport_lun_fe_stop, 1);
  4743. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4744. wake_up_interruptible(&SE_DEV(cmd)->dev_queue_obj->thread_wq);
  4745. ret = transport_stop_tasks_for_cmd(cmd);
  4746. DEBUG_TRANSPORT_S("ConfigFS: cmd: %p t_task_cdbs: %d stop tasks ret:"
  4747. " %d\n", cmd, T_TASK(cmd)->t_task_cdbs, ret);
  4748. if (!ret) {
  4749. DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
  4750. CMD_TFO(cmd)->get_task_tag(cmd));
  4751. wait_for_completion(&T_TASK(cmd)->transport_lun_stop_comp);
  4752. DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
  4753. CMD_TFO(cmd)->get_task_tag(cmd));
  4754. }
  4755. transport_remove_cmd_from_queue(cmd, SE_DEV(cmd)->dev_queue_obj);
  4756. return 0;
  4757. }
  4758. /* #define DEBUG_CLEAR_LUN */
  4759. #ifdef DEBUG_CLEAR_LUN
  4760. #define DEBUG_CLEAR_L(x...) printk(KERN_INFO x)
  4761. #else
  4762. #define DEBUG_CLEAR_L(x...)
  4763. #endif
  4764. static void __transport_clear_lun_from_sessions(struct se_lun *lun)
  4765. {
  4766. struct se_cmd *cmd = NULL;
  4767. unsigned long lun_flags, cmd_flags;
  4768. /*
  4769. * Do exception processing and return CHECK_CONDITION status to the
  4770. * Initiator Port.
  4771. */
  4772. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4773. while (!list_empty_careful(&lun->lun_cmd_list)) {
  4774. cmd = list_entry(lun->lun_cmd_list.next,
  4775. struct se_cmd, se_lun_list);
  4776. list_del(&cmd->se_lun_list);
  4777. if (!(T_TASK(cmd))) {
  4778. printk(KERN_ERR "ITT: 0x%08x, T_TASK(cmd) = NULL"
  4779. "[i,t]_state: %u/%u\n",
  4780. CMD_TFO(cmd)->get_task_tag(cmd),
  4781. CMD_TFO(cmd)->get_cmd_state(cmd), cmd->t_state);
  4782. BUG();
  4783. }
  4784. atomic_set(&T_TASK(cmd)->transport_lun_active, 0);
  4785. /*
  4786. * This will notify iscsi_target_transport.c:
  4787. * transport_cmd_check_stop() that a LUN shutdown is in
  4788. * progress for the iscsi_cmd_t.
  4789. */
  4790. spin_lock(&T_TASK(cmd)->t_state_lock);
  4791. DEBUG_CLEAR_L("SE_LUN[%d] - Setting T_TASK(cmd)->transport"
  4792. "_lun_stop for ITT: 0x%08x\n",
  4793. SE_LUN(cmd)->unpacked_lun,
  4794. CMD_TFO(cmd)->get_task_tag(cmd));
  4795. atomic_set(&T_TASK(cmd)->transport_lun_stop, 1);
  4796. spin_unlock(&T_TASK(cmd)->t_state_lock);
  4797. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  4798. if (!(SE_LUN(cmd))) {
  4799. printk(KERN_ERR "ITT: 0x%08x, [i,t]_state: %u/%u\n",
  4800. CMD_TFO(cmd)->get_task_tag(cmd),
  4801. CMD_TFO(cmd)->get_cmd_state(cmd), cmd->t_state);
  4802. BUG();
  4803. }
  4804. /*
  4805. * If the Storage engine still owns the iscsi_cmd_t, determine
  4806. * and/or stop its context.
  4807. */
  4808. DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x before transport"
  4809. "_lun_wait_for_tasks()\n", SE_LUN(cmd)->unpacked_lun,
  4810. CMD_TFO(cmd)->get_task_tag(cmd));
  4811. if (transport_lun_wait_for_tasks(cmd, SE_LUN(cmd)) < 0) {
  4812. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4813. continue;
  4814. }
  4815. DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
  4816. "_wait_for_tasks(): SUCCESS\n",
  4817. SE_LUN(cmd)->unpacked_lun,
  4818. CMD_TFO(cmd)->get_task_tag(cmd));
  4819. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, cmd_flags);
  4820. if (!(atomic_read(&T_TASK(cmd)->transport_dev_active))) {
  4821. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, cmd_flags);
  4822. goto check_cond;
  4823. }
  4824. atomic_set(&T_TASK(cmd)->transport_dev_active, 0);
  4825. transport_all_task_dev_remove_state(cmd);
  4826. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, cmd_flags);
  4827. transport_free_dev_tasks(cmd);
  4828. /*
  4829. * The Storage engine stopped this struct se_cmd before it was
  4830. * send to the fabric frontend for delivery back to the
  4831. * Initiator Node. Return this SCSI CDB back with an
  4832. * CHECK_CONDITION status.
  4833. */
  4834. check_cond:
  4835. transport_send_check_condition_and_sense(cmd,
  4836. TCM_NON_EXISTENT_LUN, 0);
  4837. /*
  4838. * If the fabric frontend is waiting for this iscsi_cmd_t to
  4839. * be released, notify the waiting thread now that LU has
  4840. * finished accessing it.
  4841. */
  4842. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, cmd_flags);
  4843. if (atomic_read(&T_TASK(cmd)->transport_lun_fe_stop)) {
  4844. DEBUG_CLEAR_L("SE_LUN[%d] - Detected FE stop for"
  4845. " struct se_cmd: %p ITT: 0x%08x\n",
  4846. lun->unpacked_lun,
  4847. cmd, CMD_TFO(cmd)->get_task_tag(cmd));
  4848. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  4849. cmd_flags);
  4850. transport_cmd_check_stop(cmd, 1, 0);
  4851. complete(&T_TASK(cmd)->transport_lun_fe_stop_comp);
  4852. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4853. continue;
  4854. }
  4855. DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
  4856. lun->unpacked_lun, CMD_TFO(cmd)->get_task_tag(cmd));
  4857. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, cmd_flags);
  4858. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4859. }
  4860. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  4861. }
  4862. static int transport_clear_lun_thread(void *p)
  4863. {
  4864. struct se_lun *lun = (struct se_lun *)p;
  4865. __transport_clear_lun_from_sessions(lun);
  4866. complete(&lun->lun_shutdown_comp);
  4867. return 0;
  4868. }
  4869. int transport_clear_lun_from_sessions(struct se_lun *lun)
  4870. {
  4871. struct task_struct *kt;
  4872. kt = kthread_run(transport_clear_lun_thread, (void *)lun,
  4873. "tcm_cl_%u", lun->unpacked_lun);
  4874. if (IS_ERR(kt)) {
  4875. printk(KERN_ERR "Unable to start clear_lun thread\n");
  4876. return -1;
  4877. }
  4878. wait_for_completion(&lun->lun_shutdown_comp);
  4879. return 0;
  4880. }
  4881. /* transport_generic_wait_for_tasks():
  4882. *
  4883. * Called from frontend or passthrough context to wait for storage engine
  4884. * to pause and/or release frontend generated struct se_cmd.
  4885. */
  4886. static void transport_generic_wait_for_tasks(
  4887. struct se_cmd *cmd,
  4888. int remove_cmd,
  4889. int session_reinstatement)
  4890. {
  4891. unsigned long flags;
  4892. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req))
  4893. return;
  4894. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  4895. /*
  4896. * If we are already stopped due to an external event (ie: LUN shutdown)
  4897. * sleep until the connection can have the passed struct se_cmd back.
  4898. * The T_TASK(cmd)->transport_lun_stopped_sem will be upped by
  4899. * transport_clear_lun_from_sessions() once the ConfigFS context caller
  4900. * has completed its operation on the struct se_cmd.
  4901. */
  4902. if (atomic_read(&T_TASK(cmd)->transport_lun_stop)) {
  4903. DEBUG_TRANSPORT_S("wait_for_tasks: Stopping"
  4904. " wait_for_completion(&T_TASK(cmd)transport_lun_fe"
  4905. "_stop_comp); for ITT: 0x%08x\n",
  4906. CMD_TFO(cmd)->get_task_tag(cmd));
  4907. /*
  4908. * There is a special case for WRITES where a FE exception +
  4909. * LUN shutdown means ConfigFS context is still sleeping on
  4910. * transport_lun_stop_comp in transport_lun_wait_for_tasks().
  4911. * We go ahead and up transport_lun_stop_comp just to be sure
  4912. * here.
  4913. */
  4914. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4915. complete(&T_TASK(cmd)->transport_lun_stop_comp);
  4916. wait_for_completion(&T_TASK(cmd)->transport_lun_fe_stop_comp);
  4917. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  4918. transport_all_task_dev_remove_state(cmd);
  4919. /*
  4920. * At this point, the frontend who was the originator of this
  4921. * struct se_cmd, now owns the structure and can be released through
  4922. * normal means below.
  4923. */
  4924. DEBUG_TRANSPORT_S("wait_for_tasks: Stopped"
  4925. " wait_for_completion(&T_TASK(cmd)transport_lun_fe_"
  4926. "stop_comp); for ITT: 0x%08x\n",
  4927. CMD_TFO(cmd)->get_task_tag(cmd));
  4928. atomic_set(&T_TASK(cmd)->transport_lun_stop, 0);
  4929. }
  4930. if (!atomic_read(&T_TASK(cmd)->t_transport_active) ||
  4931. atomic_read(&T_TASK(cmd)->t_transport_aborted))
  4932. goto remove;
  4933. atomic_set(&T_TASK(cmd)->t_transport_stop, 1);
  4934. DEBUG_TRANSPORT_S("wait_for_tasks: Stopping %p ITT: 0x%08x"
  4935. " i_state: %d, t_state/def_t_state: %d/%d, t_transport_stop"
  4936. " = TRUE\n", cmd, CMD_TFO(cmd)->get_task_tag(cmd),
  4937. CMD_TFO(cmd)->get_cmd_state(cmd), cmd->t_state,
  4938. cmd->deferred_t_state);
  4939. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4940. wake_up_interruptible(&SE_DEV(cmd)->dev_queue_obj->thread_wq);
  4941. wait_for_completion(&T_TASK(cmd)->t_transport_stop_comp);
  4942. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  4943. atomic_set(&T_TASK(cmd)->t_transport_active, 0);
  4944. atomic_set(&T_TASK(cmd)->t_transport_stop, 0);
  4945. DEBUG_TRANSPORT_S("wait_for_tasks: Stopped wait_for_compltion("
  4946. "&T_TASK(cmd)->t_transport_stop_comp) for ITT: 0x%08x\n",
  4947. CMD_TFO(cmd)->get_task_tag(cmd));
  4948. remove:
  4949. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4950. if (!remove_cmd)
  4951. return;
  4952. transport_generic_free_cmd(cmd, 0, 0, session_reinstatement);
  4953. }
  4954. static int transport_get_sense_codes(
  4955. struct se_cmd *cmd,
  4956. u8 *asc,
  4957. u8 *ascq)
  4958. {
  4959. *asc = cmd->scsi_asc;
  4960. *ascq = cmd->scsi_ascq;
  4961. return 0;
  4962. }
  4963. static int transport_set_sense_codes(
  4964. struct se_cmd *cmd,
  4965. u8 asc,
  4966. u8 ascq)
  4967. {
  4968. cmd->scsi_asc = asc;
  4969. cmd->scsi_ascq = ascq;
  4970. return 0;
  4971. }
  4972. int transport_send_check_condition_and_sense(
  4973. struct se_cmd *cmd,
  4974. u8 reason,
  4975. int from_transport)
  4976. {
  4977. unsigned char *buffer = cmd->sense_buffer;
  4978. unsigned long flags;
  4979. int offset;
  4980. u8 asc = 0, ascq = 0;
  4981. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  4982. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  4983. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4984. return 0;
  4985. }
  4986. cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
  4987. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4988. if (!reason && from_transport)
  4989. goto after_reason;
  4990. if (!from_transport)
  4991. cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
  4992. /*
  4993. * Data Segment and SenseLength of the fabric response PDU.
  4994. *
  4995. * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
  4996. * from include/scsi/scsi_cmnd.h
  4997. */
  4998. offset = CMD_TFO(cmd)->set_fabric_sense_len(cmd,
  4999. TRANSPORT_SENSE_BUFFER);
  5000. /*
  5001. * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
  5002. * SENSE KEY values from include/scsi/scsi.h
  5003. */
  5004. switch (reason) {
  5005. case TCM_NON_EXISTENT_LUN:
  5006. case TCM_UNSUPPORTED_SCSI_OPCODE:
  5007. case TCM_SECTOR_COUNT_TOO_MANY:
  5008. /* CURRENT ERROR */
  5009. buffer[offset] = 0x70;
  5010. /* ILLEGAL REQUEST */
  5011. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  5012. /* INVALID COMMAND OPERATION CODE */
  5013. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
  5014. break;
  5015. case TCM_UNKNOWN_MODE_PAGE:
  5016. /* CURRENT ERROR */
  5017. buffer[offset] = 0x70;
  5018. /* ILLEGAL REQUEST */
  5019. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  5020. /* INVALID FIELD IN CDB */
  5021. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  5022. break;
  5023. case TCM_CHECK_CONDITION_ABORT_CMD:
  5024. /* CURRENT ERROR */
  5025. buffer[offset] = 0x70;
  5026. /* ABORTED COMMAND */
  5027. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5028. /* BUS DEVICE RESET FUNCTION OCCURRED */
  5029. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
  5030. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
  5031. break;
  5032. case TCM_INCORRECT_AMOUNT_OF_DATA:
  5033. /* CURRENT ERROR */
  5034. buffer[offset] = 0x70;
  5035. /* ABORTED COMMAND */
  5036. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5037. /* WRITE ERROR */
  5038. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  5039. /* NOT ENOUGH UNSOLICITED DATA */
  5040. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
  5041. break;
  5042. case TCM_INVALID_CDB_FIELD:
  5043. /* CURRENT ERROR */
  5044. buffer[offset] = 0x70;
  5045. /* ABORTED COMMAND */
  5046. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5047. /* INVALID FIELD IN CDB */
  5048. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  5049. break;
  5050. case TCM_INVALID_PARAMETER_LIST:
  5051. /* CURRENT ERROR */
  5052. buffer[offset] = 0x70;
  5053. /* ABORTED COMMAND */
  5054. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5055. /* INVALID FIELD IN PARAMETER LIST */
  5056. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
  5057. break;
  5058. case TCM_UNEXPECTED_UNSOLICITED_DATA:
  5059. /* CURRENT ERROR */
  5060. buffer[offset] = 0x70;
  5061. /* ABORTED COMMAND */
  5062. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5063. /* WRITE ERROR */
  5064. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  5065. /* UNEXPECTED_UNSOLICITED_DATA */
  5066. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
  5067. break;
  5068. case TCM_SERVICE_CRC_ERROR:
  5069. /* CURRENT ERROR */
  5070. buffer[offset] = 0x70;
  5071. /* ABORTED COMMAND */
  5072. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5073. /* PROTOCOL SERVICE CRC ERROR */
  5074. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
  5075. /* N/A */
  5076. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
  5077. break;
  5078. case TCM_SNACK_REJECTED:
  5079. /* CURRENT ERROR */
  5080. buffer[offset] = 0x70;
  5081. /* ABORTED COMMAND */
  5082. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5083. /* READ ERROR */
  5084. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
  5085. /* FAILED RETRANSMISSION REQUEST */
  5086. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
  5087. break;
  5088. case TCM_WRITE_PROTECTED:
  5089. /* CURRENT ERROR */
  5090. buffer[offset] = 0x70;
  5091. /* DATA PROTECT */
  5092. buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
  5093. /* WRITE PROTECTED */
  5094. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
  5095. break;
  5096. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  5097. /* CURRENT ERROR */
  5098. buffer[offset] = 0x70;
  5099. /* UNIT ATTENTION */
  5100. buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  5101. core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
  5102. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  5103. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  5104. break;
  5105. case TCM_CHECK_CONDITION_NOT_READY:
  5106. /* CURRENT ERROR */
  5107. buffer[offset] = 0x70;
  5108. /* Not Ready */
  5109. buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
  5110. transport_get_sense_codes(cmd, &asc, &ascq);
  5111. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  5112. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  5113. break;
  5114. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  5115. default:
  5116. /* CURRENT ERROR */
  5117. buffer[offset] = 0x70;
  5118. /* ILLEGAL REQUEST */
  5119. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  5120. /* LOGICAL UNIT COMMUNICATION FAILURE */
  5121. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
  5122. break;
  5123. }
  5124. /*
  5125. * This code uses linux/include/scsi/scsi.h SAM status codes!
  5126. */
  5127. cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
  5128. /*
  5129. * Automatically padded, this value is encoded in the fabric's
  5130. * data_length response PDU containing the SCSI defined sense data.
  5131. */
  5132. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
  5133. after_reason:
  5134. CMD_TFO(cmd)->queue_status(cmd);
  5135. return 0;
  5136. }
  5137. EXPORT_SYMBOL(transport_send_check_condition_and_sense);
  5138. int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
  5139. {
  5140. int ret = 0;
  5141. if (atomic_read(&T_TASK(cmd)->t_transport_aborted) != 0) {
  5142. if (!(send_status) ||
  5143. (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
  5144. return 1;
  5145. #if 0
  5146. printk(KERN_INFO "Sending delayed SAM_STAT_TASK_ABORTED"
  5147. " status for CDB: 0x%02x ITT: 0x%08x\n",
  5148. T_TASK(cmd)->t_task_cdb[0],
  5149. CMD_TFO(cmd)->get_task_tag(cmd));
  5150. #endif
  5151. cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
  5152. CMD_TFO(cmd)->queue_status(cmd);
  5153. ret = 1;
  5154. }
  5155. return ret;
  5156. }
  5157. EXPORT_SYMBOL(transport_check_aborted_status);
  5158. void transport_send_task_abort(struct se_cmd *cmd)
  5159. {
  5160. /*
  5161. * If there are still expected incoming fabric WRITEs, we wait
  5162. * until until they have completed before sending a TASK_ABORTED
  5163. * response. This response with TASK_ABORTED status will be
  5164. * queued back to fabric module by transport_check_aborted_status().
  5165. */
  5166. if (cmd->data_direction == DMA_TO_DEVICE) {
  5167. if (CMD_TFO(cmd)->write_pending_status(cmd) != 0) {
  5168. atomic_inc(&T_TASK(cmd)->t_transport_aborted);
  5169. smp_mb__after_atomic_inc();
  5170. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  5171. transport_new_cmd_failure(cmd);
  5172. return;
  5173. }
  5174. }
  5175. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  5176. #if 0
  5177. printk(KERN_INFO "Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
  5178. " ITT: 0x%08x\n", T_TASK(cmd)->t_task_cdb[0],
  5179. CMD_TFO(cmd)->get_task_tag(cmd));
  5180. #endif
  5181. CMD_TFO(cmd)->queue_status(cmd);
  5182. }
  5183. /* transport_generic_do_tmr():
  5184. *
  5185. *
  5186. */
  5187. int transport_generic_do_tmr(struct se_cmd *cmd)
  5188. {
  5189. struct se_cmd *ref_cmd;
  5190. struct se_device *dev = SE_DEV(cmd);
  5191. struct se_tmr_req *tmr = cmd->se_tmr_req;
  5192. int ret;
  5193. switch (tmr->function) {
  5194. case TMR_ABORT_TASK:
  5195. ref_cmd = tmr->ref_cmd;
  5196. tmr->response = TMR_FUNCTION_REJECTED;
  5197. break;
  5198. case TMR_ABORT_TASK_SET:
  5199. case TMR_CLEAR_ACA:
  5200. case TMR_CLEAR_TASK_SET:
  5201. tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  5202. break;
  5203. case TMR_LUN_RESET:
  5204. ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
  5205. tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
  5206. TMR_FUNCTION_REJECTED;
  5207. break;
  5208. case TMR_TARGET_WARM_RESET:
  5209. tmr->response = TMR_FUNCTION_REJECTED;
  5210. break;
  5211. case TMR_TARGET_COLD_RESET:
  5212. tmr->response = TMR_FUNCTION_REJECTED;
  5213. break;
  5214. default:
  5215. printk(KERN_ERR "Uknown TMR function: 0x%02x.\n",
  5216. tmr->function);
  5217. tmr->response = TMR_FUNCTION_REJECTED;
  5218. break;
  5219. }
  5220. cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
  5221. CMD_TFO(cmd)->queue_tm_rsp(cmd);
  5222. transport_cmd_check_stop(cmd, 2, 0);
  5223. return 0;
  5224. }
  5225. /*
  5226. * Called with spin_lock_irq(&dev->execute_task_lock); held
  5227. *
  5228. */
  5229. static struct se_task *
  5230. transport_get_task_from_state_list(struct se_device *dev)
  5231. {
  5232. struct se_task *task;
  5233. if (list_empty(&dev->state_task_list))
  5234. return NULL;
  5235. list_for_each_entry(task, &dev->state_task_list, t_state_list)
  5236. break;
  5237. list_del(&task->t_state_list);
  5238. atomic_set(&task->task_state_active, 0);
  5239. return task;
  5240. }
  5241. static void transport_processing_shutdown(struct se_device *dev)
  5242. {
  5243. struct se_cmd *cmd;
  5244. struct se_queue_req *qr;
  5245. struct se_task *task;
  5246. u8 state;
  5247. unsigned long flags;
  5248. /*
  5249. * Empty the struct se_device's struct se_task state list.
  5250. */
  5251. spin_lock_irqsave(&dev->execute_task_lock, flags);
  5252. while ((task = transport_get_task_from_state_list(dev))) {
  5253. if (!(TASK_CMD(task))) {
  5254. printk(KERN_ERR "TASK_CMD(task) is NULL!\n");
  5255. continue;
  5256. }
  5257. cmd = TASK_CMD(task);
  5258. if (!T_TASK(cmd)) {
  5259. printk(KERN_ERR "T_TASK(cmd) is NULL for task: %p cmd:"
  5260. " %p ITT: 0x%08x\n", task, cmd,
  5261. CMD_TFO(cmd)->get_task_tag(cmd));
  5262. continue;
  5263. }
  5264. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  5265. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  5266. DEBUG_DO("PT: cmd: %p task: %p ITT/CmdSN: 0x%08x/0x%08x,"
  5267. " i_state/def_i_state: %d/%d, t_state/def_t_state:"
  5268. " %d/%d cdb: 0x%02x\n", cmd, task,
  5269. CMD_TFO(cmd)->get_task_tag(cmd), cmd->cmd_sn,
  5270. CMD_TFO(cmd)->get_cmd_state(cmd), cmd->deferred_i_state,
  5271. cmd->t_state, cmd->deferred_t_state,
  5272. T_TASK(cmd)->t_task_cdb[0]);
  5273. DEBUG_DO("PT: ITT[0x%08x] - t_task_cdbs: %d t_task_cdbs_left:"
  5274. " %d t_task_cdbs_sent: %d -- t_transport_active: %d"
  5275. " t_transport_stop: %d t_transport_sent: %d\n",
  5276. CMD_TFO(cmd)->get_task_tag(cmd),
  5277. T_TASK(cmd)->t_task_cdbs,
  5278. atomic_read(&T_TASK(cmd)->t_task_cdbs_left),
  5279. atomic_read(&T_TASK(cmd)->t_task_cdbs_sent),
  5280. atomic_read(&T_TASK(cmd)->t_transport_active),
  5281. atomic_read(&T_TASK(cmd)->t_transport_stop),
  5282. atomic_read(&T_TASK(cmd)->t_transport_sent));
  5283. if (atomic_read(&task->task_active)) {
  5284. atomic_set(&task->task_stop, 1);
  5285. spin_unlock_irqrestore(
  5286. &T_TASK(cmd)->t_state_lock, flags);
  5287. DEBUG_DO("Waiting for task: %p to shutdown for dev:"
  5288. " %p\n", task, dev);
  5289. wait_for_completion(&task->task_stop_comp);
  5290. DEBUG_DO("Completed task: %p shutdown for dev: %p\n",
  5291. task, dev);
  5292. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  5293. atomic_dec(&T_TASK(cmd)->t_task_cdbs_left);
  5294. atomic_set(&task->task_active, 0);
  5295. atomic_set(&task->task_stop, 0);
  5296. } else {
  5297. if (atomic_read(&task->task_execute_queue) != 0)
  5298. transport_remove_task_from_execute_queue(task, dev);
  5299. }
  5300. __transport_stop_task_timer(task, &flags);
  5301. if (!(atomic_dec_and_test(&T_TASK(cmd)->t_task_cdbs_ex_left))) {
  5302. spin_unlock_irqrestore(
  5303. &T_TASK(cmd)->t_state_lock, flags);
  5304. DEBUG_DO("Skipping task: %p, dev: %p for"
  5305. " t_task_cdbs_ex_left: %d\n", task, dev,
  5306. atomic_read(&T_TASK(cmd)->t_task_cdbs_ex_left));
  5307. spin_lock_irqsave(&dev->execute_task_lock, flags);
  5308. continue;
  5309. }
  5310. if (atomic_read(&T_TASK(cmd)->t_transport_active)) {
  5311. DEBUG_DO("got t_transport_active = 1 for task: %p, dev:"
  5312. " %p\n", task, dev);
  5313. if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
  5314. spin_unlock_irqrestore(
  5315. &T_TASK(cmd)->t_state_lock, flags);
  5316. transport_send_check_condition_and_sense(
  5317. cmd, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE,
  5318. 0);
  5319. transport_remove_cmd_from_queue(cmd,
  5320. SE_DEV(cmd)->dev_queue_obj);
  5321. transport_lun_remove_cmd(cmd);
  5322. transport_cmd_check_stop(cmd, 1, 0);
  5323. } else {
  5324. spin_unlock_irqrestore(
  5325. &T_TASK(cmd)->t_state_lock, flags);
  5326. transport_remove_cmd_from_queue(cmd,
  5327. SE_DEV(cmd)->dev_queue_obj);
  5328. transport_lun_remove_cmd(cmd);
  5329. if (transport_cmd_check_stop(cmd, 1, 0))
  5330. transport_generic_remove(cmd, 0, 0);
  5331. }
  5332. spin_lock_irqsave(&dev->execute_task_lock, flags);
  5333. continue;
  5334. }
  5335. DEBUG_DO("Got t_transport_active = 0 for task: %p, dev: %p\n",
  5336. task, dev);
  5337. if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
  5338. spin_unlock_irqrestore(
  5339. &T_TASK(cmd)->t_state_lock, flags);
  5340. transport_send_check_condition_and_sense(cmd,
  5341. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  5342. transport_remove_cmd_from_queue(cmd,
  5343. SE_DEV(cmd)->dev_queue_obj);
  5344. transport_lun_remove_cmd(cmd);
  5345. transport_cmd_check_stop(cmd, 1, 0);
  5346. } else {
  5347. spin_unlock_irqrestore(
  5348. &T_TASK(cmd)->t_state_lock, flags);
  5349. transport_remove_cmd_from_queue(cmd,
  5350. SE_DEV(cmd)->dev_queue_obj);
  5351. transport_lun_remove_cmd(cmd);
  5352. if (transport_cmd_check_stop(cmd, 1, 0))
  5353. transport_generic_remove(cmd, 0, 0);
  5354. }
  5355. spin_lock_irqsave(&dev->execute_task_lock, flags);
  5356. }
  5357. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  5358. /*
  5359. * Empty the struct se_device's struct se_cmd list.
  5360. */
  5361. spin_lock_irqsave(&dev->dev_queue_obj->cmd_queue_lock, flags);
  5362. while ((qr = __transport_get_qr_from_queue(dev->dev_queue_obj))) {
  5363. spin_unlock_irqrestore(
  5364. &dev->dev_queue_obj->cmd_queue_lock, flags);
  5365. cmd = (struct se_cmd *)qr->cmd;
  5366. state = qr->state;
  5367. kfree(qr);
  5368. DEBUG_DO("From Device Queue: cmd: %p t_state: %d\n",
  5369. cmd, state);
  5370. if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
  5371. transport_send_check_condition_and_sense(cmd,
  5372. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  5373. transport_lun_remove_cmd(cmd);
  5374. transport_cmd_check_stop(cmd, 1, 0);
  5375. } else {
  5376. transport_lun_remove_cmd(cmd);
  5377. if (transport_cmd_check_stop(cmd, 1, 0))
  5378. transport_generic_remove(cmd, 0, 0);
  5379. }
  5380. spin_lock_irqsave(&dev->dev_queue_obj->cmd_queue_lock, flags);
  5381. }
  5382. spin_unlock_irqrestore(&dev->dev_queue_obj->cmd_queue_lock, flags);
  5383. }
  5384. /* transport_processing_thread():
  5385. *
  5386. *
  5387. */
  5388. static int transport_processing_thread(void *param)
  5389. {
  5390. int ret, t_state;
  5391. struct se_cmd *cmd;
  5392. struct se_device *dev = (struct se_device *) param;
  5393. struct se_queue_req *qr;
  5394. set_user_nice(current, -20);
  5395. while (!kthread_should_stop()) {
  5396. ret = wait_event_interruptible(dev->dev_queue_obj->thread_wq,
  5397. atomic_read(&dev->dev_queue_obj->queue_cnt) ||
  5398. kthread_should_stop());
  5399. if (ret < 0)
  5400. goto out;
  5401. spin_lock_irq(&dev->dev_status_lock);
  5402. if (dev->dev_status & TRANSPORT_DEVICE_SHUTDOWN) {
  5403. spin_unlock_irq(&dev->dev_status_lock);
  5404. transport_processing_shutdown(dev);
  5405. continue;
  5406. }
  5407. spin_unlock_irq(&dev->dev_status_lock);
  5408. get_cmd:
  5409. __transport_execute_tasks(dev);
  5410. qr = transport_get_qr_from_queue(dev->dev_queue_obj);
  5411. if (!(qr))
  5412. continue;
  5413. cmd = (struct se_cmd *)qr->cmd;
  5414. t_state = qr->state;
  5415. kfree(qr);
  5416. switch (t_state) {
  5417. case TRANSPORT_NEW_CMD_MAP:
  5418. if (!(CMD_TFO(cmd)->new_cmd_map)) {
  5419. printk(KERN_ERR "CMD_TFO(cmd)->new_cmd_map is"
  5420. " NULL for TRANSPORT_NEW_CMD_MAP\n");
  5421. BUG();
  5422. }
  5423. ret = CMD_TFO(cmd)->new_cmd_map(cmd);
  5424. if (ret < 0) {
  5425. cmd->transport_error_status = ret;
  5426. transport_generic_request_failure(cmd, NULL,
  5427. 0, (cmd->data_direction !=
  5428. DMA_TO_DEVICE));
  5429. break;
  5430. }
  5431. /* Fall through */
  5432. case TRANSPORT_NEW_CMD:
  5433. ret = transport_generic_new_cmd(cmd);
  5434. if (ret < 0) {
  5435. cmd->transport_error_status = ret;
  5436. transport_generic_request_failure(cmd, NULL,
  5437. 0, (cmd->data_direction !=
  5438. DMA_TO_DEVICE));
  5439. }
  5440. break;
  5441. case TRANSPORT_PROCESS_WRITE:
  5442. transport_generic_process_write(cmd);
  5443. break;
  5444. case TRANSPORT_COMPLETE_OK:
  5445. transport_stop_all_task_timers(cmd);
  5446. transport_generic_complete_ok(cmd);
  5447. break;
  5448. case TRANSPORT_REMOVE:
  5449. transport_generic_remove(cmd, 1, 0);
  5450. break;
  5451. case TRANSPORT_PROCESS_TMR:
  5452. transport_generic_do_tmr(cmd);
  5453. break;
  5454. case TRANSPORT_COMPLETE_FAILURE:
  5455. transport_generic_request_failure(cmd, NULL, 1, 1);
  5456. break;
  5457. case TRANSPORT_COMPLETE_TIMEOUT:
  5458. transport_stop_all_task_timers(cmd);
  5459. transport_generic_request_timeout(cmd);
  5460. break;
  5461. default:
  5462. printk(KERN_ERR "Unknown t_state: %d deferred_t_state:"
  5463. " %d for ITT: 0x%08x i_state: %d on SE LUN:"
  5464. " %u\n", t_state, cmd->deferred_t_state,
  5465. CMD_TFO(cmd)->get_task_tag(cmd),
  5466. CMD_TFO(cmd)->get_cmd_state(cmd),
  5467. SE_LUN(cmd)->unpacked_lun);
  5468. BUG();
  5469. }
  5470. goto get_cmd;
  5471. }
  5472. out:
  5473. transport_release_all_cmds(dev);
  5474. dev->process_thread = NULL;
  5475. return 0;
  5476. }