target_core_transport.c 168 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165
  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 ret = 0, 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. out:
  1533. if (!ret)
  1534. return dev;
  1535. kthread_stop(dev->process_thread);
  1536. spin_lock(&hba->device_lock);
  1537. list_del(&dev->dev_list);
  1538. hba->dev_count--;
  1539. spin_unlock(&hba->device_lock);
  1540. se_release_vpd_for_dev(dev);
  1541. kfree(dev->dev_status_queue_obj);
  1542. kfree(dev->dev_queue_obj);
  1543. kfree(dev);
  1544. return NULL;
  1545. }
  1546. EXPORT_SYMBOL(transport_add_device_to_core_hba);
  1547. /* transport_generic_prepare_cdb():
  1548. *
  1549. * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
  1550. * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
  1551. * The point of this is since we are mapping iSCSI LUNs to
  1552. * SCSI Target IDs having a non-zero LUN in the CDB will throw the
  1553. * devices and HBAs for a loop.
  1554. */
  1555. static inline void transport_generic_prepare_cdb(
  1556. unsigned char *cdb)
  1557. {
  1558. switch (cdb[0]) {
  1559. case READ_10: /* SBC - RDProtect */
  1560. case READ_12: /* SBC - RDProtect */
  1561. case READ_16: /* SBC - RDProtect */
  1562. case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
  1563. case VERIFY: /* SBC - VRProtect */
  1564. case VERIFY_16: /* SBC - VRProtect */
  1565. case WRITE_VERIFY: /* SBC - VRProtect */
  1566. case WRITE_VERIFY_12: /* SBC - VRProtect */
  1567. break;
  1568. default:
  1569. cdb[1] &= 0x1f; /* clear logical unit number */
  1570. break;
  1571. }
  1572. }
  1573. static struct se_task *
  1574. transport_generic_get_task(struct se_cmd *cmd,
  1575. enum dma_data_direction data_direction)
  1576. {
  1577. struct se_task *task;
  1578. struct se_device *dev = SE_DEV(cmd);
  1579. unsigned long flags;
  1580. task = dev->transport->alloc_task(cmd);
  1581. if (!task) {
  1582. printk(KERN_ERR "Unable to allocate struct se_task\n");
  1583. return NULL;
  1584. }
  1585. INIT_LIST_HEAD(&task->t_list);
  1586. INIT_LIST_HEAD(&task->t_execute_list);
  1587. INIT_LIST_HEAD(&task->t_state_list);
  1588. init_completion(&task->task_stop_comp);
  1589. task->task_no = T_TASK(cmd)->t_tasks_no++;
  1590. task->task_se_cmd = cmd;
  1591. task->se_dev = dev;
  1592. task->task_data_direction = data_direction;
  1593. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  1594. list_add_tail(&task->t_list, &T_TASK(cmd)->t_task_list);
  1595. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  1596. return task;
  1597. }
  1598. static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
  1599. void transport_device_setup_cmd(struct se_cmd *cmd)
  1600. {
  1601. cmd->se_dev = SE_LUN(cmd)->lun_se_dev;
  1602. }
  1603. EXPORT_SYMBOL(transport_device_setup_cmd);
  1604. /*
  1605. * Used by fabric modules containing a local struct se_cmd within their
  1606. * fabric dependent per I/O descriptor.
  1607. */
  1608. void transport_init_se_cmd(
  1609. struct se_cmd *cmd,
  1610. struct target_core_fabric_ops *tfo,
  1611. struct se_session *se_sess,
  1612. u32 data_length,
  1613. int data_direction,
  1614. int task_attr,
  1615. unsigned char *sense_buffer)
  1616. {
  1617. INIT_LIST_HEAD(&cmd->se_lun_list);
  1618. INIT_LIST_HEAD(&cmd->se_delayed_list);
  1619. INIT_LIST_HEAD(&cmd->se_ordered_list);
  1620. /*
  1621. * Setup t_task pointer to t_task_backstore
  1622. */
  1623. cmd->t_task = &cmd->t_task_backstore;
  1624. INIT_LIST_HEAD(&T_TASK(cmd)->t_task_list);
  1625. init_completion(&T_TASK(cmd)->transport_lun_fe_stop_comp);
  1626. init_completion(&T_TASK(cmd)->transport_lun_stop_comp);
  1627. init_completion(&T_TASK(cmd)->t_transport_stop_comp);
  1628. spin_lock_init(&T_TASK(cmd)->t_state_lock);
  1629. atomic_set(&T_TASK(cmd)->transport_dev_active, 1);
  1630. cmd->se_tfo = tfo;
  1631. cmd->se_sess = se_sess;
  1632. cmd->data_length = data_length;
  1633. cmd->data_direction = data_direction;
  1634. cmd->sam_task_attr = task_attr;
  1635. cmd->sense_buffer = sense_buffer;
  1636. }
  1637. EXPORT_SYMBOL(transport_init_se_cmd);
  1638. static int transport_check_alloc_task_attr(struct se_cmd *cmd)
  1639. {
  1640. /*
  1641. * Check if SAM Task Attribute emulation is enabled for this
  1642. * struct se_device storage object
  1643. */
  1644. if (SE_DEV(cmd)->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  1645. return 0;
  1646. if (cmd->sam_task_attr == TASK_ATTR_ACA) {
  1647. DEBUG_STA("SAM Task Attribute ACA"
  1648. " emulation is not supported\n");
  1649. return -1;
  1650. }
  1651. /*
  1652. * Used to determine when ORDERED commands should go from
  1653. * Dormant to Active status.
  1654. */
  1655. cmd->se_ordered_id = atomic_inc_return(&SE_DEV(cmd)->dev_ordered_id);
  1656. smp_mb__after_atomic_inc();
  1657. DEBUG_STA("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
  1658. cmd->se_ordered_id, cmd->sam_task_attr,
  1659. TRANSPORT(cmd->se_dev)->name);
  1660. return 0;
  1661. }
  1662. void transport_free_se_cmd(
  1663. struct se_cmd *se_cmd)
  1664. {
  1665. if (se_cmd->se_tmr_req)
  1666. core_tmr_release_req(se_cmd->se_tmr_req);
  1667. /*
  1668. * Check and free any extended CDB buffer that was allocated
  1669. */
  1670. if (T_TASK(se_cmd)->t_task_cdb != T_TASK(se_cmd)->__t_task_cdb)
  1671. kfree(T_TASK(se_cmd)->t_task_cdb);
  1672. }
  1673. EXPORT_SYMBOL(transport_free_se_cmd);
  1674. static void transport_generic_wait_for_tasks(struct se_cmd *, int, int);
  1675. /* transport_generic_allocate_tasks():
  1676. *
  1677. * Called from fabric RX Thread.
  1678. */
  1679. int transport_generic_allocate_tasks(
  1680. struct se_cmd *cmd,
  1681. unsigned char *cdb)
  1682. {
  1683. int ret;
  1684. transport_generic_prepare_cdb(cdb);
  1685. /*
  1686. * This is needed for early exceptions.
  1687. */
  1688. cmd->transport_wait_for_tasks = &transport_generic_wait_for_tasks;
  1689. transport_device_setup_cmd(cmd);
  1690. /*
  1691. * Ensure that the received CDB is less than the max (252 + 8) bytes
  1692. * for VARIABLE_LENGTH_CMD
  1693. */
  1694. if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
  1695. printk(KERN_ERR "Received SCSI CDB with command_size: %d that"
  1696. " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
  1697. scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
  1698. return -1;
  1699. }
  1700. /*
  1701. * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
  1702. * allocate the additional extended CDB buffer now.. Otherwise
  1703. * setup the pointer from __t_task_cdb to t_task_cdb.
  1704. */
  1705. if (scsi_command_size(cdb) > sizeof(T_TASK(cmd)->__t_task_cdb)) {
  1706. T_TASK(cmd)->t_task_cdb = kzalloc(scsi_command_size(cdb),
  1707. GFP_KERNEL);
  1708. if (!(T_TASK(cmd)->t_task_cdb)) {
  1709. printk(KERN_ERR "Unable to allocate T_TASK(cmd)->t_task_cdb"
  1710. " %u > sizeof(T_TASK(cmd)->__t_task_cdb): %lu ops\n",
  1711. scsi_command_size(cdb),
  1712. (unsigned long)sizeof(T_TASK(cmd)->__t_task_cdb));
  1713. return -1;
  1714. }
  1715. } else
  1716. T_TASK(cmd)->t_task_cdb = &T_TASK(cmd)->__t_task_cdb[0];
  1717. /*
  1718. * Copy the original CDB into T_TASK(cmd).
  1719. */
  1720. memcpy(T_TASK(cmd)->t_task_cdb, cdb, scsi_command_size(cdb));
  1721. /*
  1722. * Setup the received CDB based on SCSI defined opcodes and
  1723. * perform unit attention, persistent reservations and ALUA
  1724. * checks for virtual device backends. The T_TASK(cmd)->t_task_cdb
  1725. * pointer is expected to be setup before we reach this point.
  1726. */
  1727. ret = transport_generic_cmd_sequencer(cmd, cdb);
  1728. if (ret < 0)
  1729. return ret;
  1730. /*
  1731. * Check for SAM Task Attribute Emulation
  1732. */
  1733. if (transport_check_alloc_task_attr(cmd) < 0) {
  1734. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1735. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1736. return -2;
  1737. }
  1738. spin_lock(&cmd->se_lun->lun_sep_lock);
  1739. if (cmd->se_lun->lun_sep)
  1740. cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
  1741. spin_unlock(&cmd->se_lun->lun_sep_lock);
  1742. return 0;
  1743. }
  1744. EXPORT_SYMBOL(transport_generic_allocate_tasks);
  1745. /*
  1746. * Used by fabric module frontends not defining a TFO->new_cmd_map()
  1747. * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD statis
  1748. */
  1749. int transport_generic_handle_cdb(
  1750. struct se_cmd *cmd)
  1751. {
  1752. if (!SE_LUN(cmd)) {
  1753. dump_stack();
  1754. printk(KERN_ERR "SE_LUN(cmd) is NULL\n");
  1755. return -1;
  1756. }
  1757. transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD);
  1758. return 0;
  1759. }
  1760. EXPORT_SYMBOL(transport_generic_handle_cdb);
  1761. /*
  1762. * Used by fabric module frontends defining a TFO->new_cmd_map() caller
  1763. * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
  1764. * complete setup in TCM process context w/ TFO->new_cmd_map().
  1765. */
  1766. int transport_generic_handle_cdb_map(
  1767. struct se_cmd *cmd)
  1768. {
  1769. if (!SE_LUN(cmd)) {
  1770. dump_stack();
  1771. printk(KERN_ERR "SE_LUN(cmd) is NULL\n");
  1772. return -1;
  1773. }
  1774. transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP);
  1775. return 0;
  1776. }
  1777. EXPORT_SYMBOL(transport_generic_handle_cdb_map);
  1778. /* transport_generic_handle_data():
  1779. *
  1780. *
  1781. */
  1782. int transport_generic_handle_data(
  1783. struct se_cmd *cmd)
  1784. {
  1785. /*
  1786. * For the software fabric case, then we assume the nexus is being
  1787. * failed/shutdown when signals are pending from the kthread context
  1788. * caller, so we return a failure. For the HW target mode case running
  1789. * in interrupt code, the signal_pending() check is skipped.
  1790. */
  1791. if (!in_interrupt() && signal_pending(current))
  1792. return -1;
  1793. /*
  1794. * If the received CDB has aleady been ABORTED by the generic
  1795. * target engine, we now call transport_check_aborted_status()
  1796. * to queue any delated TASK_ABORTED status for the received CDB to the
  1797. * fabric module as we are expecting no futher incoming DATA OUT
  1798. * sequences at this point.
  1799. */
  1800. if (transport_check_aborted_status(cmd, 1) != 0)
  1801. return 0;
  1802. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE);
  1803. return 0;
  1804. }
  1805. EXPORT_SYMBOL(transport_generic_handle_data);
  1806. /* transport_generic_handle_tmr():
  1807. *
  1808. *
  1809. */
  1810. int transport_generic_handle_tmr(
  1811. struct se_cmd *cmd)
  1812. {
  1813. /*
  1814. * This is needed for early exceptions.
  1815. */
  1816. cmd->transport_wait_for_tasks = &transport_generic_wait_for_tasks;
  1817. transport_device_setup_cmd(cmd);
  1818. transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR);
  1819. return 0;
  1820. }
  1821. EXPORT_SYMBOL(transport_generic_handle_tmr);
  1822. static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
  1823. {
  1824. struct se_task *task, *task_tmp;
  1825. unsigned long flags;
  1826. int ret = 0;
  1827. DEBUG_TS("ITT[0x%08x] - Stopping tasks\n",
  1828. CMD_TFO(cmd)->get_task_tag(cmd));
  1829. /*
  1830. * No tasks remain in the execution queue
  1831. */
  1832. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  1833. list_for_each_entry_safe(task, task_tmp,
  1834. &T_TASK(cmd)->t_task_list, t_list) {
  1835. DEBUG_TS("task_no[%d] - Processing task %p\n",
  1836. task->task_no, task);
  1837. /*
  1838. * If the struct se_task has not been sent and is not active,
  1839. * remove the struct se_task from the execution queue.
  1840. */
  1841. if (!atomic_read(&task->task_sent) &&
  1842. !atomic_read(&task->task_active)) {
  1843. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  1844. flags);
  1845. transport_remove_task_from_execute_queue(task,
  1846. task->se_dev);
  1847. DEBUG_TS("task_no[%d] - Removed from execute queue\n",
  1848. task->task_no);
  1849. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  1850. continue;
  1851. }
  1852. /*
  1853. * If the struct se_task is active, sleep until it is returned
  1854. * from the plugin.
  1855. */
  1856. if (atomic_read(&task->task_active)) {
  1857. atomic_set(&task->task_stop, 1);
  1858. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  1859. flags);
  1860. DEBUG_TS("task_no[%d] - Waiting to complete\n",
  1861. task->task_no);
  1862. wait_for_completion(&task->task_stop_comp);
  1863. DEBUG_TS("task_no[%d] - Stopped successfully\n",
  1864. task->task_no);
  1865. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  1866. atomic_dec(&T_TASK(cmd)->t_task_cdbs_left);
  1867. atomic_set(&task->task_active, 0);
  1868. atomic_set(&task->task_stop, 0);
  1869. } else {
  1870. DEBUG_TS("task_no[%d] - Did nothing\n", task->task_no);
  1871. ret++;
  1872. }
  1873. __transport_stop_task_timer(task, &flags);
  1874. }
  1875. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  1876. return ret;
  1877. }
  1878. static void transport_failure_reset_queue_depth(struct se_device *dev)
  1879. {
  1880. unsigned long flags;
  1881. spin_lock_irqsave(&SE_HBA(dev)->hba_queue_lock, flags);;
  1882. atomic_inc(&dev->depth_left);
  1883. atomic_inc(&SE_HBA(dev)->left_queue_depth);
  1884. spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
  1885. }
  1886. /*
  1887. * Handle SAM-esque emulation for generic transport request failures.
  1888. */
  1889. static void transport_generic_request_failure(
  1890. struct se_cmd *cmd,
  1891. struct se_device *dev,
  1892. int complete,
  1893. int sc)
  1894. {
  1895. DEBUG_GRF("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
  1896. " CDB: 0x%02x\n", cmd, CMD_TFO(cmd)->get_task_tag(cmd),
  1897. T_TASK(cmd)->t_task_cdb[0]);
  1898. DEBUG_GRF("-----[ i_state: %d t_state/def_t_state:"
  1899. " %d/%d transport_error_status: %d\n",
  1900. CMD_TFO(cmd)->get_cmd_state(cmd),
  1901. cmd->t_state, cmd->deferred_t_state,
  1902. cmd->transport_error_status);
  1903. DEBUG_GRF("-----[ t_task_cdbs: %d t_task_cdbs_left: %d"
  1904. " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
  1905. " t_transport_active: %d t_transport_stop: %d"
  1906. " t_transport_sent: %d\n", T_TASK(cmd)->t_task_cdbs,
  1907. atomic_read(&T_TASK(cmd)->t_task_cdbs_left),
  1908. atomic_read(&T_TASK(cmd)->t_task_cdbs_sent),
  1909. atomic_read(&T_TASK(cmd)->t_task_cdbs_ex_left),
  1910. atomic_read(&T_TASK(cmd)->t_transport_active),
  1911. atomic_read(&T_TASK(cmd)->t_transport_stop),
  1912. atomic_read(&T_TASK(cmd)->t_transport_sent));
  1913. transport_stop_all_task_timers(cmd);
  1914. if (dev)
  1915. transport_failure_reset_queue_depth(dev);
  1916. /*
  1917. * For SAM Task Attribute emulation for failed struct se_cmd
  1918. */
  1919. if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  1920. transport_complete_task_attr(cmd);
  1921. if (complete) {
  1922. transport_direct_request_timeout(cmd);
  1923. cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
  1924. }
  1925. switch (cmd->transport_error_status) {
  1926. case PYX_TRANSPORT_UNKNOWN_SAM_OPCODE:
  1927. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1928. break;
  1929. case PYX_TRANSPORT_REQ_TOO_MANY_SECTORS:
  1930. cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY;
  1931. break;
  1932. case PYX_TRANSPORT_INVALID_CDB_FIELD:
  1933. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1934. break;
  1935. case PYX_TRANSPORT_INVALID_PARAMETER_LIST:
  1936. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1937. break;
  1938. case PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES:
  1939. if (!sc)
  1940. transport_new_cmd_failure(cmd);
  1941. /*
  1942. * Currently for PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES,
  1943. * we force this session to fall back to session
  1944. * recovery.
  1945. */
  1946. CMD_TFO(cmd)->fall_back_to_erl0(cmd->se_sess);
  1947. CMD_TFO(cmd)->stop_session(cmd->se_sess, 0, 0);
  1948. goto check_stop;
  1949. case PYX_TRANSPORT_LU_COMM_FAILURE:
  1950. case PYX_TRANSPORT_ILLEGAL_REQUEST:
  1951. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1952. break;
  1953. case PYX_TRANSPORT_UNKNOWN_MODE_PAGE:
  1954. cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
  1955. break;
  1956. case PYX_TRANSPORT_WRITE_PROTECTED:
  1957. cmd->scsi_sense_reason = TCM_WRITE_PROTECTED;
  1958. break;
  1959. case PYX_TRANSPORT_RESERVATION_CONFLICT:
  1960. /*
  1961. * No SENSE Data payload for this case, set SCSI Status
  1962. * and queue the response to $FABRIC_MOD.
  1963. *
  1964. * Uses linux/include/scsi/scsi.h SAM status codes defs
  1965. */
  1966. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  1967. /*
  1968. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  1969. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  1970. * CONFLICT STATUS.
  1971. *
  1972. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  1973. */
  1974. if (SE_SESS(cmd) &&
  1975. DEV_ATTRIB(cmd->se_dev)->emulate_ua_intlck_ctrl == 2)
  1976. core_scsi3_ua_allocate(SE_SESS(cmd)->se_node_acl,
  1977. cmd->orig_fe_lun, 0x2C,
  1978. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  1979. CMD_TFO(cmd)->queue_status(cmd);
  1980. goto check_stop;
  1981. case PYX_TRANSPORT_USE_SENSE_REASON:
  1982. /*
  1983. * struct se_cmd->scsi_sense_reason already set
  1984. */
  1985. break;
  1986. default:
  1987. printk(KERN_ERR "Unknown transport error for CDB 0x%02x: %d\n",
  1988. T_TASK(cmd)->t_task_cdb[0],
  1989. cmd->transport_error_status);
  1990. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1991. break;
  1992. }
  1993. if (!sc)
  1994. transport_new_cmd_failure(cmd);
  1995. else
  1996. transport_send_check_condition_and_sense(cmd,
  1997. cmd->scsi_sense_reason, 0);
  1998. check_stop:
  1999. transport_lun_remove_cmd(cmd);
  2000. if (!(transport_cmd_check_stop_to_fabric(cmd)))
  2001. ;
  2002. }
  2003. static void transport_direct_request_timeout(struct se_cmd *cmd)
  2004. {
  2005. unsigned long flags;
  2006. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  2007. if (!(atomic_read(&T_TASK(cmd)->t_transport_timeout))) {
  2008. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2009. return;
  2010. }
  2011. if (atomic_read(&T_TASK(cmd)->t_task_cdbs_timeout_left)) {
  2012. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2013. return;
  2014. }
  2015. atomic_sub(atomic_read(&T_TASK(cmd)->t_transport_timeout),
  2016. &T_TASK(cmd)->t_se_count);
  2017. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2018. }
  2019. static void transport_generic_request_timeout(struct se_cmd *cmd)
  2020. {
  2021. unsigned long flags;
  2022. /*
  2023. * Reset T_TASK(cmd)->t_se_count to allow transport_generic_remove()
  2024. * to allow last call to free memory resources.
  2025. */
  2026. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  2027. if (atomic_read(&T_TASK(cmd)->t_transport_timeout) > 1) {
  2028. int tmp = (atomic_read(&T_TASK(cmd)->t_transport_timeout) - 1);
  2029. atomic_sub(tmp, &T_TASK(cmd)->t_se_count);
  2030. }
  2031. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2032. transport_generic_remove(cmd, 0, 0);
  2033. }
  2034. static int
  2035. transport_generic_allocate_buf(struct se_cmd *cmd, u32 data_length)
  2036. {
  2037. unsigned char *buf;
  2038. buf = kzalloc(data_length, GFP_KERNEL);
  2039. if (!(buf)) {
  2040. printk(KERN_ERR "Unable to allocate memory for buffer\n");
  2041. return -1;
  2042. }
  2043. T_TASK(cmd)->t_tasks_se_num = 0;
  2044. T_TASK(cmd)->t_task_buf = buf;
  2045. return 0;
  2046. }
  2047. static inline u32 transport_lba_21(unsigned char *cdb)
  2048. {
  2049. return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
  2050. }
  2051. static inline u32 transport_lba_32(unsigned char *cdb)
  2052. {
  2053. return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  2054. }
  2055. static inline unsigned long long transport_lba_64(unsigned char *cdb)
  2056. {
  2057. unsigned int __v1, __v2;
  2058. __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  2059. __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  2060. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  2061. }
  2062. /*
  2063. * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
  2064. */
  2065. static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
  2066. {
  2067. unsigned int __v1, __v2;
  2068. __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
  2069. __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
  2070. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  2071. }
  2072. static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
  2073. {
  2074. unsigned long flags;
  2075. spin_lock_irqsave(&T_TASK(se_cmd)->t_state_lock, flags);
  2076. se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
  2077. spin_unlock_irqrestore(&T_TASK(se_cmd)->t_state_lock, flags);
  2078. }
  2079. /*
  2080. * Called from interrupt context.
  2081. */
  2082. static void transport_task_timeout_handler(unsigned long data)
  2083. {
  2084. struct se_task *task = (struct se_task *)data;
  2085. struct se_cmd *cmd = TASK_CMD(task);
  2086. unsigned long flags;
  2087. DEBUG_TT("transport task timeout fired! task: %p cmd: %p\n", task, cmd);
  2088. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  2089. if (task->task_flags & TF_STOP) {
  2090. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2091. return;
  2092. }
  2093. task->task_flags &= ~TF_RUNNING;
  2094. /*
  2095. * Determine if transport_complete_task() has already been called.
  2096. */
  2097. if (!(atomic_read(&task->task_active))) {
  2098. DEBUG_TT("transport task: %p cmd: %p timeout task_active"
  2099. " == 0\n", task, cmd);
  2100. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2101. return;
  2102. }
  2103. atomic_inc(&T_TASK(cmd)->t_se_count);
  2104. atomic_inc(&T_TASK(cmd)->t_transport_timeout);
  2105. T_TASK(cmd)->t_tasks_failed = 1;
  2106. atomic_set(&task->task_timeout, 1);
  2107. task->task_error_status = PYX_TRANSPORT_TASK_TIMEOUT;
  2108. task->task_scsi_status = 1;
  2109. if (atomic_read(&task->task_stop)) {
  2110. DEBUG_TT("transport task: %p cmd: %p timeout task_stop"
  2111. " == 1\n", task, cmd);
  2112. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2113. complete(&task->task_stop_comp);
  2114. return;
  2115. }
  2116. if (!(atomic_dec_and_test(&T_TASK(cmd)->t_task_cdbs_left))) {
  2117. DEBUG_TT("transport task: %p cmd: %p timeout non zero"
  2118. " t_task_cdbs_left\n", task, cmd);
  2119. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2120. return;
  2121. }
  2122. DEBUG_TT("transport task: %p cmd: %p timeout ZERO t_task_cdbs_left\n",
  2123. task, cmd);
  2124. cmd->t_state = TRANSPORT_COMPLETE_FAILURE;
  2125. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2126. transport_add_cmd_to_queue(cmd, TRANSPORT_COMPLETE_FAILURE);
  2127. }
  2128. /*
  2129. * Called with T_TASK(cmd)->t_state_lock held.
  2130. */
  2131. static void transport_start_task_timer(struct se_task *task)
  2132. {
  2133. struct se_device *dev = task->se_dev;
  2134. int timeout;
  2135. if (task->task_flags & TF_RUNNING)
  2136. return;
  2137. /*
  2138. * If the task_timeout is disabled, exit now.
  2139. */
  2140. timeout = DEV_ATTRIB(dev)->task_timeout;
  2141. if (!(timeout))
  2142. return;
  2143. init_timer(&task->task_timer);
  2144. task->task_timer.expires = (get_jiffies_64() + timeout * HZ);
  2145. task->task_timer.data = (unsigned long) task;
  2146. task->task_timer.function = transport_task_timeout_handler;
  2147. task->task_flags |= TF_RUNNING;
  2148. add_timer(&task->task_timer);
  2149. #if 0
  2150. printk(KERN_INFO "Starting task timer for cmd: %p task: %p seconds:"
  2151. " %d\n", task->task_se_cmd, task, timeout);
  2152. #endif
  2153. }
  2154. /*
  2155. * Called with spin_lock_irq(&T_TASK(cmd)->t_state_lock) held.
  2156. */
  2157. void __transport_stop_task_timer(struct se_task *task, unsigned long *flags)
  2158. {
  2159. struct se_cmd *cmd = TASK_CMD(task);
  2160. if (!(task->task_flags & TF_RUNNING))
  2161. return;
  2162. task->task_flags |= TF_STOP;
  2163. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, *flags);
  2164. del_timer_sync(&task->task_timer);
  2165. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, *flags);
  2166. task->task_flags &= ~TF_RUNNING;
  2167. task->task_flags &= ~TF_STOP;
  2168. }
  2169. static void transport_stop_all_task_timers(struct se_cmd *cmd)
  2170. {
  2171. struct se_task *task = NULL, *task_tmp;
  2172. unsigned long flags;
  2173. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  2174. list_for_each_entry_safe(task, task_tmp,
  2175. &T_TASK(cmd)->t_task_list, t_list)
  2176. __transport_stop_task_timer(task, &flags);
  2177. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2178. }
  2179. static inline int transport_tcq_window_closed(struct se_device *dev)
  2180. {
  2181. if (dev->dev_tcq_window_closed++ <
  2182. PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) {
  2183. msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT);
  2184. } else
  2185. msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG);
  2186. wake_up_interruptible(&dev->dev_queue_obj->thread_wq);
  2187. return 0;
  2188. }
  2189. /*
  2190. * Called from Fabric Module context from transport_execute_tasks()
  2191. *
  2192. * The return of this function determins if the tasks from struct se_cmd
  2193. * get added to the execution queue in transport_execute_tasks(),
  2194. * or are added to the delayed or ordered lists here.
  2195. */
  2196. static inline int transport_execute_task_attr(struct se_cmd *cmd)
  2197. {
  2198. if (SE_DEV(cmd)->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
  2199. return 1;
  2200. /*
  2201. * Check for the existance of HEAD_OF_QUEUE, and if true return 1
  2202. * to allow the passed struct se_cmd list of tasks to the front of the list.
  2203. */
  2204. if (cmd->sam_task_attr == TASK_ATTR_HOQ) {
  2205. atomic_inc(&SE_DEV(cmd)->dev_hoq_count);
  2206. smp_mb__after_atomic_inc();
  2207. DEBUG_STA("Added HEAD_OF_QUEUE for CDB:"
  2208. " 0x%02x, se_ordered_id: %u\n",
  2209. T_TASK(cmd)->t_task_cdb[0],
  2210. cmd->se_ordered_id);
  2211. return 1;
  2212. } else if (cmd->sam_task_attr == TASK_ATTR_ORDERED) {
  2213. spin_lock(&SE_DEV(cmd)->ordered_cmd_lock);
  2214. list_add_tail(&cmd->se_ordered_list,
  2215. &SE_DEV(cmd)->ordered_cmd_list);
  2216. spin_unlock(&SE_DEV(cmd)->ordered_cmd_lock);
  2217. atomic_inc(&SE_DEV(cmd)->dev_ordered_sync);
  2218. smp_mb__after_atomic_inc();
  2219. DEBUG_STA("Added ORDERED for CDB: 0x%02x to ordered"
  2220. " list, se_ordered_id: %u\n",
  2221. T_TASK(cmd)->t_task_cdb[0],
  2222. cmd->se_ordered_id);
  2223. /*
  2224. * Add ORDERED command to tail of execution queue if
  2225. * no other older commands exist that need to be
  2226. * completed first.
  2227. */
  2228. if (!(atomic_read(&SE_DEV(cmd)->simple_cmds)))
  2229. return 1;
  2230. } else {
  2231. /*
  2232. * For SIMPLE and UNTAGGED Task Attribute commands
  2233. */
  2234. atomic_inc(&SE_DEV(cmd)->simple_cmds);
  2235. smp_mb__after_atomic_inc();
  2236. }
  2237. /*
  2238. * Otherwise if one or more outstanding ORDERED task attribute exist,
  2239. * add the dormant task(s) built for the passed struct se_cmd to the
  2240. * execution queue and become in Active state for this struct se_device.
  2241. */
  2242. if (atomic_read(&SE_DEV(cmd)->dev_ordered_sync) != 0) {
  2243. /*
  2244. * Otherwise, add cmd w/ tasks to delayed cmd queue that
  2245. * will be drained upon competion of HEAD_OF_QUEUE task.
  2246. */
  2247. spin_lock(&SE_DEV(cmd)->delayed_cmd_lock);
  2248. cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
  2249. list_add_tail(&cmd->se_delayed_list,
  2250. &SE_DEV(cmd)->delayed_cmd_list);
  2251. spin_unlock(&SE_DEV(cmd)->delayed_cmd_lock);
  2252. DEBUG_STA("Added CDB: 0x%02x Task Attr: 0x%02x to"
  2253. " delayed CMD list, se_ordered_id: %u\n",
  2254. T_TASK(cmd)->t_task_cdb[0], cmd->sam_task_attr,
  2255. cmd->se_ordered_id);
  2256. /*
  2257. * Return zero to let transport_execute_tasks() know
  2258. * not to add the delayed tasks to the execution list.
  2259. */
  2260. return 0;
  2261. }
  2262. /*
  2263. * Otherwise, no ORDERED task attributes exist..
  2264. */
  2265. return 1;
  2266. }
  2267. /*
  2268. * Called from fabric module context in transport_generic_new_cmd() and
  2269. * transport_generic_process_write()
  2270. */
  2271. static int transport_execute_tasks(struct se_cmd *cmd)
  2272. {
  2273. int add_tasks;
  2274. if (!(cmd->se_cmd_flags & SCF_SE_DISABLE_ONLINE_CHECK)) {
  2275. if (se_dev_check_online(cmd->se_orig_obj_ptr) != 0) {
  2276. cmd->transport_error_status =
  2277. PYX_TRANSPORT_LU_COMM_FAILURE;
  2278. transport_generic_request_failure(cmd, NULL, 0, 1);
  2279. return 0;
  2280. }
  2281. }
  2282. /*
  2283. * Call transport_cmd_check_stop() to see if a fabric exception
  2284. * has occured that prevents execution.
  2285. */
  2286. if (!(transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING))) {
  2287. /*
  2288. * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
  2289. * attribute for the tasks of the received struct se_cmd CDB
  2290. */
  2291. add_tasks = transport_execute_task_attr(cmd);
  2292. if (add_tasks == 0)
  2293. goto execute_tasks;
  2294. /*
  2295. * This calls transport_add_tasks_from_cmd() to handle
  2296. * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
  2297. * (if enabled) in __transport_add_task_to_execute_queue() and
  2298. * transport_add_task_check_sam_attr().
  2299. */
  2300. transport_add_tasks_from_cmd(cmd);
  2301. }
  2302. /*
  2303. * Kick the execution queue for the cmd associated struct se_device
  2304. * storage object.
  2305. */
  2306. execute_tasks:
  2307. __transport_execute_tasks(SE_DEV(cmd));
  2308. return 0;
  2309. }
  2310. /*
  2311. * Called to check struct se_device tcq depth window, and once open pull struct se_task
  2312. * from struct se_device->execute_task_list and
  2313. *
  2314. * Called from transport_processing_thread()
  2315. */
  2316. static int __transport_execute_tasks(struct se_device *dev)
  2317. {
  2318. int error;
  2319. struct se_cmd *cmd = NULL;
  2320. struct se_task *task;
  2321. unsigned long flags;
  2322. /*
  2323. * Check if there is enough room in the device and HBA queue to send
  2324. * struct se_transport_task's to the selected transport.
  2325. */
  2326. check_depth:
  2327. spin_lock_irqsave(&SE_HBA(dev)->hba_queue_lock, flags);
  2328. if (!(atomic_read(&dev->depth_left)) ||
  2329. !(atomic_read(&SE_HBA(dev)->left_queue_depth))) {
  2330. spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
  2331. return transport_tcq_window_closed(dev);
  2332. }
  2333. dev->dev_tcq_window_closed = 0;
  2334. spin_lock(&dev->execute_task_lock);
  2335. task = transport_get_task_from_execute_queue(dev);
  2336. spin_unlock(&dev->execute_task_lock);
  2337. if (!task) {
  2338. spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
  2339. return 0;
  2340. }
  2341. atomic_dec(&dev->depth_left);
  2342. atomic_dec(&SE_HBA(dev)->left_queue_depth);
  2343. spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
  2344. cmd = TASK_CMD(task);
  2345. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  2346. atomic_set(&task->task_active, 1);
  2347. atomic_set(&task->task_sent, 1);
  2348. atomic_inc(&T_TASK(cmd)->t_task_cdbs_sent);
  2349. if (atomic_read(&T_TASK(cmd)->t_task_cdbs_sent) ==
  2350. T_TASK(cmd)->t_task_cdbs)
  2351. atomic_set(&cmd->transport_sent, 1);
  2352. transport_start_task_timer(task);
  2353. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2354. /*
  2355. * The struct se_cmd->transport_emulate_cdb() function pointer is used
  2356. * to grab REPORT_LUNS CDBs before they hit the
  2357. * struct se_subsystem_api->do_task() caller below.
  2358. */
  2359. if (cmd->transport_emulate_cdb) {
  2360. error = cmd->transport_emulate_cdb(cmd);
  2361. if (error != 0) {
  2362. cmd->transport_error_status = error;
  2363. atomic_set(&task->task_active, 0);
  2364. atomic_set(&cmd->transport_sent, 0);
  2365. transport_stop_tasks_for_cmd(cmd);
  2366. transport_generic_request_failure(cmd, dev, 0, 1);
  2367. goto check_depth;
  2368. }
  2369. /*
  2370. * Handle the successful completion for transport_emulate_cdb()
  2371. * for synchronous operation, following SCF_EMULATE_CDB_ASYNC
  2372. * Otherwise the caller is expected to complete the task with
  2373. * proper status.
  2374. */
  2375. if (!(cmd->se_cmd_flags & SCF_EMULATE_CDB_ASYNC)) {
  2376. cmd->scsi_status = SAM_STAT_GOOD;
  2377. task->task_scsi_status = GOOD;
  2378. transport_complete_task(task, 1);
  2379. }
  2380. } else {
  2381. /*
  2382. * Currently for all virtual TCM plugins including IBLOCK, FILEIO and
  2383. * RAMDISK we use the internal transport_emulate_control_cdb() logic
  2384. * with struct se_subsystem_api callers for the primary SPC-3 TYPE_DISK
  2385. * LUN emulation code.
  2386. *
  2387. * For TCM/pSCSI and all other SCF_SCSI_DATA_SG_IO_CDB I/O tasks we
  2388. * call ->do_task() directly and let the underlying TCM subsystem plugin
  2389. * code handle the CDB emulation.
  2390. */
  2391. if ((TRANSPORT(dev)->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) &&
  2392. (!(TASK_CMD(task)->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
  2393. error = transport_emulate_control_cdb(task);
  2394. else
  2395. error = TRANSPORT(dev)->do_task(task);
  2396. if (error != 0) {
  2397. cmd->transport_error_status = error;
  2398. atomic_set(&task->task_active, 0);
  2399. atomic_set(&cmd->transport_sent, 0);
  2400. transport_stop_tasks_for_cmd(cmd);
  2401. transport_generic_request_failure(cmd, dev, 0, 1);
  2402. }
  2403. }
  2404. goto check_depth;
  2405. return 0;
  2406. }
  2407. void transport_new_cmd_failure(struct se_cmd *se_cmd)
  2408. {
  2409. unsigned long flags;
  2410. /*
  2411. * Any unsolicited data will get dumped for failed command inside of
  2412. * the fabric plugin
  2413. */
  2414. spin_lock_irqsave(&T_TASK(se_cmd)->t_state_lock, flags);
  2415. se_cmd->se_cmd_flags |= SCF_SE_CMD_FAILED;
  2416. se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2417. spin_unlock_irqrestore(&T_TASK(se_cmd)->t_state_lock, flags);
  2418. CMD_TFO(se_cmd)->new_cmd_failure(se_cmd);
  2419. }
  2420. static void transport_nop_wait_for_tasks(struct se_cmd *, int, int);
  2421. static inline u32 transport_get_sectors_6(
  2422. unsigned char *cdb,
  2423. struct se_cmd *cmd,
  2424. int *ret)
  2425. {
  2426. struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
  2427. /*
  2428. * Assume TYPE_DISK for non struct se_device objects.
  2429. * Use 8-bit sector value.
  2430. */
  2431. if (!dev)
  2432. goto type_disk;
  2433. /*
  2434. * Use 24-bit allocation length for TYPE_TAPE.
  2435. */
  2436. if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE)
  2437. return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
  2438. /*
  2439. * Everything else assume TYPE_DISK Sector CDB location.
  2440. * Use 8-bit sector value.
  2441. */
  2442. type_disk:
  2443. return (u32)cdb[4];
  2444. }
  2445. static inline u32 transport_get_sectors_10(
  2446. unsigned char *cdb,
  2447. struct se_cmd *cmd,
  2448. int *ret)
  2449. {
  2450. struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
  2451. /*
  2452. * Assume TYPE_DISK for non struct se_device objects.
  2453. * Use 16-bit sector value.
  2454. */
  2455. if (!dev)
  2456. goto type_disk;
  2457. /*
  2458. * XXX_10 is not defined in SSC, throw an exception
  2459. */
  2460. if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE) {
  2461. *ret = -1;
  2462. return 0;
  2463. }
  2464. /*
  2465. * Everything else assume TYPE_DISK Sector CDB location.
  2466. * Use 16-bit sector value.
  2467. */
  2468. type_disk:
  2469. return (u32)(cdb[7] << 8) + cdb[8];
  2470. }
  2471. static inline u32 transport_get_sectors_12(
  2472. unsigned char *cdb,
  2473. struct se_cmd *cmd,
  2474. int *ret)
  2475. {
  2476. struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
  2477. /*
  2478. * Assume TYPE_DISK for non struct se_device objects.
  2479. * Use 32-bit sector value.
  2480. */
  2481. if (!dev)
  2482. goto type_disk;
  2483. /*
  2484. * XXX_12 is not defined in SSC, throw an exception
  2485. */
  2486. if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE) {
  2487. *ret = -1;
  2488. return 0;
  2489. }
  2490. /*
  2491. * Everything else assume TYPE_DISK Sector CDB location.
  2492. * Use 32-bit sector value.
  2493. */
  2494. type_disk:
  2495. return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
  2496. }
  2497. static inline u32 transport_get_sectors_16(
  2498. unsigned char *cdb,
  2499. struct se_cmd *cmd,
  2500. int *ret)
  2501. {
  2502. struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
  2503. /*
  2504. * Assume TYPE_DISK for non struct se_device objects.
  2505. * Use 32-bit sector value.
  2506. */
  2507. if (!dev)
  2508. goto type_disk;
  2509. /*
  2510. * Use 24-bit allocation length for TYPE_TAPE.
  2511. */
  2512. if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE)
  2513. return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
  2514. type_disk:
  2515. return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
  2516. (cdb[12] << 8) + cdb[13];
  2517. }
  2518. /*
  2519. * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
  2520. */
  2521. static inline u32 transport_get_sectors_32(
  2522. unsigned char *cdb,
  2523. struct se_cmd *cmd,
  2524. int *ret)
  2525. {
  2526. /*
  2527. * Assume TYPE_DISK for non struct se_device objects.
  2528. * Use 32-bit sector value.
  2529. */
  2530. return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
  2531. (cdb[30] << 8) + cdb[31];
  2532. }
  2533. static inline u32 transport_get_size(
  2534. u32 sectors,
  2535. unsigned char *cdb,
  2536. struct se_cmd *cmd)
  2537. {
  2538. struct se_device *dev = SE_DEV(cmd);
  2539. if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE) {
  2540. if (cdb[1] & 1) { /* sectors */
  2541. return DEV_ATTRIB(dev)->block_size * sectors;
  2542. } else /* bytes */
  2543. return sectors;
  2544. }
  2545. #if 0
  2546. printk(KERN_INFO "Returning block_size: %u, sectors: %u == %u for"
  2547. " %s object\n", DEV_ATTRIB(dev)->block_size, sectors,
  2548. DEV_ATTRIB(dev)->block_size * sectors,
  2549. TRANSPORT(dev)->name);
  2550. #endif
  2551. return DEV_ATTRIB(dev)->block_size * sectors;
  2552. }
  2553. unsigned char transport_asciihex_to_binaryhex(unsigned char val[2])
  2554. {
  2555. unsigned char result = 0;
  2556. /*
  2557. * MSB
  2558. */
  2559. if ((val[0] >= 'a') && (val[0] <= 'f'))
  2560. result = ((val[0] - 'a' + 10) & 0xf) << 4;
  2561. else
  2562. if ((val[0] >= 'A') && (val[0] <= 'F'))
  2563. result = ((val[0] - 'A' + 10) & 0xf) << 4;
  2564. else /* digit */
  2565. result = ((val[0] - '0') & 0xf) << 4;
  2566. /*
  2567. * LSB
  2568. */
  2569. if ((val[1] >= 'a') && (val[1] <= 'f'))
  2570. result |= ((val[1] - 'a' + 10) & 0xf);
  2571. else
  2572. if ((val[1] >= 'A') && (val[1] <= 'F'))
  2573. result |= ((val[1] - 'A' + 10) & 0xf);
  2574. else /* digit */
  2575. result |= ((val[1] - '0') & 0xf);
  2576. return result;
  2577. }
  2578. EXPORT_SYMBOL(transport_asciihex_to_binaryhex);
  2579. static void transport_xor_callback(struct se_cmd *cmd)
  2580. {
  2581. unsigned char *buf, *addr;
  2582. struct se_mem *se_mem;
  2583. unsigned int offset;
  2584. int i;
  2585. /*
  2586. * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
  2587. *
  2588. * 1) read the specified logical block(s);
  2589. * 2) transfer logical blocks from the data-out buffer;
  2590. * 3) XOR the logical blocks transferred from the data-out buffer with
  2591. * the logical blocks read, storing the resulting XOR data in a buffer;
  2592. * 4) if the DISABLE WRITE bit is set to zero, then write the logical
  2593. * blocks transferred from the data-out buffer; and
  2594. * 5) transfer the resulting XOR data to the data-in buffer.
  2595. */
  2596. buf = kmalloc(cmd->data_length, GFP_KERNEL);
  2597. if (!(buf)) {
  2598. printk(KERN_ERR "Unable to allocate xor_callback buf\n");
  2599. return;
  2600. }
  2601. /*
  2602. * Copy the scatterlist WRITE buffer located at T_TASK(cmd)->t_mem_list
  2603. * into the locally allocated *buf
  2604. */
  2605. transport_memcpy_se_mem_read_contig(cmd, buf, T_TASK(cmd)->t_mem_list);
  2606. /*
  2607. * Now perform the XOR against the BIDI read memory located at
  2608. * T_TASK(cmd)->t_mem_bidi_list
  2609. */
  2610. offset = 0;
  2611. list_for_each_entry(se_mem, T_TASK(cmd)->t_mem_bidi_list, se_list) {
  2612. addr = (unsigned char *)kmap_atomic(se_mem->se_page, KM_USER0);
  2613. if (!(addr))
  2614. goto out;
  2615. for (i = 0; i < se_mem->se_len; i++)
  2616. *(addr + se_mem->se_off + i) ^= *(buf + offset + i);
  2617. offset += se_mem->se_len;
  2618. kunmap_atomic(addr, KM_USER0);
  2619. }
  2620. out:
  2621. kfree(buf);
  2622. }
  2623. /*
  2624. * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
  2625. */
  2626. static int transport_get_sense_data(struct se_cmd *cmd)
  2627. {
  2628. unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
  2629. struct se_device *dev;
  2630. struct se_task *task = NULL, *task_tmp;
  2631. unsigned long flags;
  2632. u32 offset = 0;
  2633. if (!SE_LUN(cmd)) {
  2634. printk(KERN_ERR "SE_LUN(cmd) is NULL\n");
  2635. return -1;
  2636. }
  2637. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  2638. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  2639. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2640. return 0;
  2641. }
  2642. list_for_each_entry_safe(task, task_tmp,
  2643. &T_TASK(cmd)->t_task_list, t_list) {
  2644. if (!task->task_sense)
  2645. continue;
  2646. dev = task->se_dev;
  2647. if (!(dev))
  2648. continue;
  2649. if (!TRANSPORT(dev)->get_sense_buffer) {
  2650. printk(KERN_ERR "TRANSPORT(dev)->get_sense_buffer"
  2651. " is NULL\n");
  2652. continue;
  2653. }
  2654. sense_buffer = TRANSPORT(dev)->get_sense_buffer(task);
  2655. if (!(sense_buffer)) {
  2656. printk(KERN_ERR "ITT[0x%08x]_TASK[%d]: Unable to locate"
  2657. " sense buffer for task with sense\n",
  2658. CMD_TFO(cmd)->get_task_tag(cmd), task->task_no);
  2659. continue;
  2660. }
  2661. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2662. offset = CMD_TFO(cmd)->set_fabric_sense_len(cmd,
  2663. TRANSPORT_SENSE_BUFFER);
  2664. memcpy((void *)&buffer[offset], (void *)sense_buffer,
  2665. TRANSPORT_SENSE_BUFFER);
  2666. cmd->scsi_status = task->task_scsi_status;
  2667. /* Automatically padded */
  2668. cmd->scsi_sense_length =
  2669. (TRANSPORT_SENSE_BUFFER + offset);
  2670. printk(KERN_INFO "HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
  2671. " and sense\n",
  2672. dev->se_hba->hba_id, TRANSPORT(dev)->name,
  2673. cmd->scsi_status);
  2674. return 0;
  2675. }
  2676. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  2677. return -1;
  2678. }
  2679. static int transport_allocate_resources(struct se_cmd *cmd)
  2680. {
  2681. u32 length = cmd->data_length;
  2682. if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
  2683. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB))
  2684. return transport_generic_get_mem(cmd, length, PAGE_SIZE);
  2685. else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB)
  2686. return transport_generic_allocate_buf(cmd, length);
  2687. else
  2688. return 0;
  2689. }
  2690. static int
  2691. transport_handle_reservation_conflict(struct se_cmd *cmd)
  2692. {
  2693. cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
  2694. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2695. cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
  2696. cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
  2697. /*
  2698. * For UA Interlock Code 11b, a RESERVATION CONFLICT will
  2699. * establish a UNIT ATTENTION with PREVIOUS RESERVATION
  2700. * CONFLICT STATUS.
  2701. *
  2702. * See spc4r17, section 7.4.6 Control Mode Page, Table 349
  2703. */
  2704. if (SE_SESS(cmd) &&
  2705. DEV_ATTRIB(cmd->se_dev)->emulate_ua_intlck_ctrl == 2)
  2706. core_scsi3_ua_allocate(SE_SESS(cmd)->se_node_acl,
  2707. cmd->orig_fe_lun, 0x2C,
  2708. ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
  2709. return -2;
  2710. }
  2711. /* transport_generic_cmd_sequencer():
  2712. *
  2713. * Generic Command Sequencer that should work for most DAS transport
  2714. * drivers.
  2715. *
  2716. * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
  2717. * RX Thread.
  2718. *
  2719. * FIXME: Need to support other SCSI OPCODES where as well.
  2720. */
  2721. static int transport_generic_cmd_sequencer(
  2722. struct se_cmd *cmd,
  2723. unsigned char *cdb)
  2724. {
  2725. struct se_device *dev = SE_DEV(cmd);
  2726. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  2727. int ret = 0, sector_ret = 0, passthrough;
  2728. u32 sectors = 0, size = 0, pr_reg_type = 0;
  2729. u16 service_action;
  2730. u8 alua_ascq = 0;
  2731. /*
  2732. * Check for an existing UNIT ATTENTION condition
  2733. */
  2734. if (core_scsi3_ua_check(cmd, cdb) < 0) {
  2735. cmd->transport_wait_for_tasks =
  2736. &transport_nop_wait_for_tasks;
  2737. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2738. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
  2739. return -2;
  2740. }
  2741. /*
  2742. * Check status of Asymmetric Logical Unit Assignment port
  2743. */
  2744. ret = T10_ALUA(su_dev)->alua_state_check(cmd, cdb, &alua_ascq);
  2745. if (ret != 0) {
  2746. cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
  2747. /*
  2748. * Set SCSI additional sense code (ASC) to 'LUN Not Accessable';
  2749. * The ALUA additional sense code qualifier (ASCQ) is determined
  2750. * by the ALUA primary or secondary access state..
  2751. */
  2752. if (ret > 0) {
  2753. #if 0
  2754. printk(KERN_INFO "[%s]: ALUA TG Port not available,"
  2755. " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
  2756. CMD_TFO(cmd)->get_fabric_name(), alua_ascq);
  2757. #endif
  2758. transport_set_sense_codes(cmd, 0x04, alua_ascq);
  2759. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  2760. cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
  2761. return -2;
  2762. }
  2763. goto out_invalid_cdb_field;
  2764. }
  2765. /*
  2766. * Check status for SPC-3 Persistent Reservations
  2767. */
  2768. if (T10_PR_OPS(su_dev)->t10_reservation_check(cmd, &pr_reg_type) != 0) {
  2769. if (T10_PR_OPS(su_dev)->t10_seq_non_holder(
  2770. cmd, cdb, pr_reg_type) != 0)
  2771. return transport_handle_reservation_conflict(cmd);
  2772. /*
  2773. * This means the CDB is allowed for the SCSI Initiator port
  2774. * when said port is *NOT* holding the legacy SPC-2 or
  2775. * SPC-3 Persistent Reservation.
  2776. */
  2777. }
  2778. switch (cdb[0]) {
  2779. case READ_6:
  2780. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2781. if (sector_ret)
  2782. goto out_unsupported_cdb;
  2783. size = transport_get_size(sectors, cdb, cmd);
  2784. cmd->transport_split_cdb = &split_cdb_XX_6;
  2785. T_TASK(cmd)->t_task_lba = transport_lba_21(cdb);
  2786. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2787. break;
  2788. case READ_10:
  2789. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2790. if (sector_ret)
  2791. goto out_unsupported_cdb;
  2792. size = transport_get_size(sectors, cdb, cmd);
  2793. cmd->transport_split_cdb = &split_cdb_XX_10;
  2794. T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
  2795. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2796. break;
  2797. case READ_12:
  2798. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2799. if (sector_ret)
  2800. goto out_unsupported_cdb;
  2801. size = transport_get_size(sectors, cdb, cmd);
  2802. cmd->transport_split_cdb = &split_cdb_XX_12;
  2803. T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
  2804. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2805. break;
  2806. case READ_16:
  2807. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2808. if (sector_ret)
  2809. goto out_unsupported_cdb;
  2810. size = transport_get_size(sectors, cdb, cmd);
  2811. cmd->transport_split_cdb = &split_cdb_XX_16;
  2812. T_TASK(cmd)->t_task_lba = transport_lba_64(cdb);
  2813. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2814. break;
  2815. case WRITE_6:
  2816. sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
  2817. if (sector_ret)
  2818. goto out_unsupported_cdb;
  2819. size = transport_get_size(sectors, cdb, cmd);
  2820. cmd->transport_split_cdb = &split_cdb_XX_6;
  2821. T_TASK(cmd)->t_task_lba = transport_lba_21(cdb);
  2822. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2823. break;
  2824. case WRITE_10:
  2825. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2826. if (sector_ret)
  2827. goto out_unsupported_cdb;
  2828. size = transport_get_size(sectors, cdb, cmd);
  2829. cmd->transport_split_cdb = &split_cdb_XX_10;
  2830. T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
  2831. T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
  2832. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2833. break;
  2834. case WRITE_12:
  2835. sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
  2836. if (sector_ret)
  2837. goto out_unsupported_cdb;
  2838. size = transport_get_size(sectors, cdb, cmd);
  2839. cmd->transport_split_cdb = &split_cdb_XX_12;
  2840. T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
  2841. T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
  2842. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2843. break;
  2844. case WRITE_16:
  2845. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  2846. if (sector_ret)
  2847. goto out_unsupported_cdb;
  2848. size = transport_get_size(sectors, cdb, cmd);
  2849. cmd->transport_split_cdb = &split_cdb_XX_16;
  2850. T_TASK(cmd)->t_task_lba = transport_lba_64(cdb);
  2851. T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
  2852. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2853. break;
  2854. case XDWRITEREAD_10:
  2855. if ((cmd->data_direction != DMA_TO_DEVICE) ||
  2856. !(T_TASK(cmd)->t_tasks_bidi))
  2857. goto out_invalid_cdb_field;
  2858. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  2859. if (sector_ret)
  2860. goto out_unsupported_cdb;
  2861. size = transport_get_size(sectors, cdb, cmd);
  2862. cmd->transport_split_cdb = &split_cdb_XX_10;
  2863. T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
  2864. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2865. passthrough = (TRANSPORT(dev)->transport_type ==
  2866. TRANSPORT_PLUGIN_PHBA_PDEV);
  2867. /*
  2868. * Skip the remaining assignments for TCM/PSCSI passthrough
  2869. */
  2870. if (passthrough)
  2871. break;
  2872. /*
  2873. * Setup BIDI XOR callback to be run during transport_generic_complete_ok()
  2874. */
  2875. cmd->transport_complete_callback = &transport_xor_callback;
  2876. T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
  2877. break;
  2878. case VARIABLE_LENGTH_CMD:
  2879. service_action = get_unaligned_be16(&cdb[8]);
  2880. /*
  2881. * Determine if this is TCM/PSCSI device and we should disable
  2882. * internal emulation for this CDB.
  2883. */
  2884. passthrough = (TRANSPORT(dev)->transport_type ==
  2885. TRANSPORT_PLUGIN_PHBA_PDEV);
  2886. switch (service_action) {
  2887. case XDWRITEREAD_32:
  2888. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2889. if (sector_ret)
  2890. goto out_unsupported_cdb;
  2891. size = transport_get_size(sectors, cdb, cmd);
  2892. /*
  2893. * Use WRITE_32 and READ_32 opcodes for the emulated
  2894. * XDWRITE_READ_32 logic.
  2895. */
  2896. cmd->transport_split_cdb = &split_cdb_XX_32;
  2897. T_TASK(cmd)->t_task_lba = transport_lba_64_ext(cdb);
  2898. cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
  2899. /*
  2900. * Skip the remaining assignments for TCM/PSCSI passthrough
  2901. */
  2902. if (passthrough)
  2903. break;
  2904. /*
  2905. * Setup BIDI XOR callback to be run during
  2906. * transport_generic_complete_ok()
  2907. */
  2908. cmd->transport_complete_callback = &transport_xor_callback;
  2909. T_TASK(cmd)->t_tasks_fua = (cdb[10] & 0x8);
  2910. break;
  2911. case WRITE_SAME_32:
  2912. sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
  2913. if (sector_ret)
  2914. goto out_unsupported_cdb;
  2915. size = transport_get_size(sectors, cdb, cmd);
  2916. T_TASK(cmd)->t_task_lba = get_unaligned_be64(&cdb[12]);
  2917. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2918. /*
  2919. * Skip the remaining assignments for TCM/PSCSI passthrough
  2920. */
  2921. if (passthrough)
  2922. break;
  2923. if ((cdb[10] & 0x04) || (cdb[10] & 0x02)) {
  2924. printk(KERN_ERR "WRITE_SAME PBDATA and LBDATA"
  2925. " bits not supported for Block Discard"
  2926. " Emulation\n");
  2927. goto out_invalid_cdb_field;
  2928. }
  2929. /*
  2930. * Currently for the emulated case we only accept
  2931. * tpws with the UNMAP=1 bit set.
  2932. */
  2933. if (!(cdb[10] & 0x08)) {
  2934. printk(KERN_ERR "WRITE_SAME w/o UNMAP bit not"
  2935. " supported for Block Discard Emulation\n");
  2936. goto out_invalid_cdb_field;
  2937. }
  2938. break;
  2939. default:
  2940. printk(KERN_ERR "VARIABLE_LENGTH_CMD service action"
  2941. " 0x%04x not supported\n", service_action);
  2942. goto out_unsupported_cdb;
  2943. }
  2944. break;
  2945. case 0xa3:
  2946. if (TRANSPORT(dev)->get_device_type(dev) != TYPE_ROM) {
  2947. /* MAINTENANCE_IN from SCC-2 */
  2948. /*
  2949. * Check for emulated MI_REPORT_TARGET_PGS.
  2950. */
  2951. if (cdb[1] == MI_REPORT_TARGET_PGS) {
  2952. cmd->transport_emulate_cdb =
  2953. (T10_ALUA(su_dev)->alua_type ==
  2954. SPC3_ALUA_EMULATED) ?
  2955. &core_emulate_report_target_port_groups :
  2956. NULL;
  2957. }
  2958. size = (cdb[6] << 24) | (cdb[7] << 16) |
  2959. (cdb[8] << 8) | cdb[9];
  2960. } else {
  2961. /* GPCMD_SEND_KEY from multi media commands */
  2962. size = (cdb[8] << 8) + cdb[9];
  2963. }
  2964. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2965. break;
  2966. case MODE_SELECT:
  2967. size = cdb[4];
  2968. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2969. break;
  2970. case MODE_SELECT_10:
  2971. size = (cdb[7] << 8) + cdb[8];
  2972. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2973. break;
  2974. case MODE_SENSE:
  2975. size = cdb[4];
  2976. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2977. break;
  2978. case MODE_SENSE_10:
  2979. case GPCMD_READ_BUFFER_CAPACITY:
  2980. case GPCMD_SEND_OPC:
  2981. case LOG_SELECT:
  2982. case LOG_SENSE:
  2983. size = (cdb[7] << 8) + cdb[8];
  2984. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2985. break;
  2986. case READ_BLOCK_LIMITS:
  2987. size = READ_BLOCK_LEN;
  2988. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  2989. break;
  2990. case GPCMD_GET_CONFIGURATION:
  2991. case GPCMD_READ_FORMAT_CAPACITIES:
  2992. case GPCMD_READ_DISC_INFO:
  2993. case GPCMD_READ_TRACK_RZONE_INFO:
  2994. size = (cdb[7] << 8) + cdb[8];
  2995. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  2996. break;
  2997. case PERSISTENT_RESERVE_IN:
  2998. case PERSISTENT_RESERVE_OUT:
  2999. cmd->transport_emulate_cdb =
  3000. (T10_RES(su_dev)->res_type ==
  3001. SPC3_PERSISTENT_RESERVATIONS) ?
  3002. &core_scsi3_emulate_pr : NULL;
  3003. size = (cdb[7] << 8) + cdb[8];
  3004. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3005. break;
  3006. case GPCMD_MECHANISM_STATUS:
  3007. case GPCMD_READ_DVD_STRUCTURE:
  3008. size = (cdb[8] << 8) + cdb[9];
  3009. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  3010. break;
  3011. case READ_POSITION:
  3012. size = READ_POSITION_LEN;
  3013. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3014. break;
  3015. case 0xa4:
  3016. if (TRANSPORT(dev)->get_device_type(dev) != TYPE_ROM) {
  3017. /* MAINTENANCE_OUT from SCC-2
  3018. *
  3019. * Check for emulated MO_SET_TARGET_PGS.
  3020. */
  3021. if (cdb[1] == MO_SET_TARGET_PGS) {
  3022. cmd->transport_emulate_cdb =
  3023. (T10_ALUA(su_dev)->alua_type ==
  3024. SPC3_ALUA_EMULATED) ?
  3025. &core_emulate_set_target_port_groups :
  3026. NULL;
  3027. }
  3028. size = (cdb[6] << 24) | (cdb[7] << 16) |
  3029. (cdb[8] << 8) | cdb[9];
  3030. } else {
  3031. /* GPCMD_REPORT_KEY from multi media commands */
  3032. size = (cdb[8] << 8) + cdb[9];
  3033. }
  3034. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3035. break;
  3036. case INQUIRY:
  3037. size = (cdb[3] << 8) + cdb[4];
  3038. /*
  3039. * Do implict HEAD_OF_QUEUE processing for INQUIRY.
  3040. * See spc4r17 section 5.3
  3041. */
  3042. if (SE_DEV(cmd)->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  3043. cmd->sam_task_attr = TASK_ATTR_HOQ;
  3044. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3045. break;
  3046. case READ_BUFFER:
  3047. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  3048. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3049. break;
  3050. case READ_CAPACITY:
  3051. size = READ_CAP_LEN;
  3052. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3053. break;
  3054. case READ_MEDIA_SERIAL_NUMBER:
  3055. case SECURITY_PROTOCOL_IN:
  3056. case SECURITY_PROTOCOL_OUT:
  3057. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  3058. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3059. break;
  3060. case SERVICE_ACTION_IN:
  3061. case ACCESS_CONTROL_IN:
  3062. case ACCESS_CONTROL_OUT:
  3063. case EXTENDED_COPY:
  3064. case READ_ATTRIBUTE:
  3065. case RECEIVE_COPY_RESULTS:
  3066. case WRITE_ATTRIBUTE:
  3067. size = (cdb[10] << 24) | (cdb[11] << 16) |
  3068. (cdb[12] << 8) | cdb[13];
  3069. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3070. break;
  3071. case RECEIVE_DIAGNOSTIC:
  3072. case SEND_DIAGNOSTIC:
  3073. size = (cdb[3] << 8) | cdb[4];
  3074. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3075. break;
  3076. /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
  3077. #if 0
  3078. case GPCMD_READ_CD:
  3079. sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  3080. size = (2336 * sectors);
  3081. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3082. break;
  3083. #endif
  3084. case READ_TOC:
  3085. size = cdb[8];
  3086. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3087. break;
  3088. case REQUEST_SENSE:
  3089. size = cdb[4];
  3090. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3091. break;
  3092. case READ_ELEMENT_STATUS:
  3093. size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
  3094. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3095. break;
  3096. case WRITE_BUFFER:
  3097. size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  3098. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3099. break;
  3100. case RESERVE:
  3101. case RESERVE_10:
  3102. /*
  3103. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  3104. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  3105. */
  3106. if (cdb[0] == RESERVE_10)
  3107. size = (cdb[7] << 8) | cdb[8];
  3108. else
  3109. size = cmd->data_length;
  3110. /*
  3111. * Setup the legacy emulated handler for SPC-2 and
  3112. * >= SPC-3 compatible reservation handling (CRH=1)
  3113. * Otherwise, we assume the underlying SCSI logic is
  3114. * is running in SPC_PASSTHROUGH, and wants reservations
  3115. * emulation disabled.
  3116. */
  3117. cmd->transport_emulate_cdb =
  3118. (T10_RES(su_dev)->res_type !=
  3119. SPC_PASSTHROUGH) ?
  3120. &core_scsi2_emulate_crh : NULL;
  3121. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  3122. break;
  3123. case RELEASE:
  3124. case RELEASE_10:
  3125. /*
  3126. * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
  3127. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  3128. */
  3129. if (cdb[0] == RELEASE_10)
  3130. size = (cdb[7] << 8) | cdb[8];
  3131. else
  3132. size = cmd->data_length;
  3133. cmd->transport_emulate_cdb =
  3134. (T10_RES(su_dev)->res_type !=
  3135. SPC_PASSTHROUGH) ?
  3136. &core_scsi2_emulate_crh : NULL;
  3137. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  3138. break;
  3139. case SYNCHRONIZE_CACHE:
  3140. case 0x91: /* SYNCHRONIZE_CACHE_16: */
  3141. /*
  3142. * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
  3143. */
  3144. if (cdb[0] == SYNCHRONIZE_CACHE) {
  3145. sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
  3146. T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
  3147. } else {
  3148. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  3149. T_TASK(cmd)->t_task_lba = transport_lba_64(cdb);
  3150. }
  3151. if (sector_ret)
  3152. goto out_unsupported_cdb;
  3153. size = transport_get_size(sectors, cdb, cmd);
  3154. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  3155. /*
  3156. * For TCM/pSCSI passthrough, skip cmd->transport_emulate_cdb()
  3157. */
  3158. if (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
  3159. break;
  3160. /*
  3161. * Set SCF_EMULATE_CDB_ASYNC to ensure asynchronous operation
  3162. * for SYNCHRONIZE_CACHE* Immed=1 case in __transport_execute_tasks()
  3163. */
  3164. cmd->se_cmd_flags |= SCF_EMULATE_CDB_ASYNC;
  3165. /*
  3166. * Check to ensure that LBA + Range does not exceed past end of
  3167. * device.
  3168. */
  3169. if (transport_get_sectors(cmd) < 0)
  3170. goto out_invalid_cdb_field;
  3171. break;
  3172. case UNMAP:
  3173. size = get_unaligned_be16(&cdb[7]);
  3174. passthrough = (TRANSPORT(dev)->transport_type ==
  3175. TRANSPORT_PLUGIN_PHBA_PDEV);
  3176. /*
  3177. * Determine if the received UNMAP used to for direct passthrough
  3178. * into Linux/SCSI with struct request via TCM/pSCSI or we are
  3179. * signaling the use of internal transport_generic_unmap() emulation
  3180. * for UNMAP -> Linux/BLOCK disbard with TCM/IBLOCK and TCM/FILEIO
  3181. * subsystem plugin backstores.
  3182. */
  3183. if (!(passthrough))
  3184. cmd->se_cmd_flags |= SCF_EMULATE_SYNC_UNMAP;
  3185. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3186. break;
  3187. case WRITE_SAME_16:
  3188. sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
  3189. if (sector_ret)
  3190. goto out_unsupported_cdb;
  3191. size = transport_get_size(sectors, cdb, cmd);
  3192. T_TASK(cmd)->t_task_lba = get_unaligned_be16(&cdb[2]);
  3193. passthrough = (TRANSPORT(dev)->transport_type ==
  3194. TRANSPORT_PLUGIN_PHBA_PDEV);
  3195. /*
  3196. * Determine if the received WRITE_SAME_16 is used to for direct
  3197. * passthrough into Linux/SCSI with struct request via TCM/pSCSI
  3198. * or we are signaling the use of internal WRITE_SAME + UNMAP=1
  3199. * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK and
  3200. * TCM/FILEIO subsystem plugin backstores.
  3201. */
  3202. if (!(passthrough)) {
  3203. if ((cdb[1] & 0x04) || (cdb[1] & 0x02)) {
  3204. printk(KERN_ERR "WRITE_SAME PBDATA and LBDATA"
  3205. " bits not supported for Block Discard"
  3206. " Emulation\n");
  3207. goto out_invalid_cdb_field;
  3208. }
  3209. /*
  3210. * Currently for the emulated case we only accept
  3211. * tpws with the UNMAP=1 bit set.
  3212. */
  3213. if (!(cdb[1] & 0x08)) {
  3214. printk(KERN_ERR "WRITE_SAME w/o UNMAP bit not "
  3215. " supported for Block Discard Emulation\n");
  3216. goto out_invalid_cdb_field;
  3217. }
  3218. }
  3219. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
  3220. break;
  3221. case ALLOW_MEDIUM_REMOVAL:
  3222. case GPCMD_CLOSE_TRACK:
  3223. case ERASE:
  3224. case INITIALIZE_ELEMENT_STATUS:
  3225. case GPCMD_LOAD_UNLOAD:
  3226. case REZERO_UNIT:
  3227. case SEEK_10:
  3228. case GPCMD_SET_SPEED:
  3229. case SPACE:
  3230. case START_STOP:
  3231. case TEST_UNIT_READY:
  3232. case VERIFY:
  3233. case WRITE_FILEMARKS:
  3234. case MOVE_MEDIUM:
  3235. cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
  3236. break;
  3237. case REPORT_LUNS:
  3238. cmd->transport_emulate_cdb =
  3239. &transport_core_report_lun_response;
  3240. size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  3241. /*
  3242. * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
  3243. * See spc4r17 section 5.3
  3244. */
  3245. if (SE_DEV(cmd)->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  3246. cmd->sam_task_attr = TASK_ATTR_HOQ;
  3247. cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
  3248. break;
  3249. default:
  3250. printk(KERN_WARNING "TARGET_CORE[%s]: Unsupported SCSI Opcode"
  3251. " 0x%02x, sending CHECK_CONDITION.\n",
  3252. CMD_TFO(cmd)->get_fabric_name(), cdb[0]);
  3253. cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
  3254. goto out_unsupported_cdb;
  3255. }
  3256. if (size != cmd->data_length) {
  3257. printk(KERN_WARNING "TARGET_CORE[%s]: Expected Transfer Length:"
  3258. " %u does not match SCSI CDB Length: %u for SAM Opcode:"
  3259. " 0x%02x\n", CMD_TFO(cmd)->get_fabric_name(),
  3260. cmd->data_length, size, cdb[0]);
  3261. cmd->cmd_spdtl = size;
  3262. if (cmd->data_direction == DMA_TO_DEVICE) {
  3263. printk(KERN_ERR "Rejecting underflow/overflow"
  3264. " WRITE data\n");
  3265. goto out_invalid_cdb_field;
  3266. }
  3267. /*
  3268. * Reject READ_* or WRITE_* with overflow/underflow for
  3269. * type SCF_SCSI_DATA_SG_IO_CDB.
  3270. */
  3271. if (!(ret) && (DEV_ATTRIB(dev)->block_size != 512)) {
  3272. printk(KERN_ERR "Failing OVERFLOW/UNDERFLOW for LBA op"
  3273. " CDB on non 512-byte sector setup subsystem"
  3274. " plugin: %s\n", TRANSPORT(dev)->name);
  3275. /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
  3276. goto out_invalid_cdb_field;
  3277. }
  3278. if (size > cmd->data_length) {
  3279. cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
  3280. cmd->residual_count = (size - cmd->data_length);
  3281. } else {
  3282. cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  3283. cmd->residual_count = (cmd->data_length - size);
  3284. }
  3285. cmd->data_length = size;
  3286. }
  3287. transport_set_supported_SAM_opcode(cmd);
  3288. return ret;
  3289. out_unsupported_cdb:
  3290. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3291. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  3292. return -2;
  3293. out_invalid_cdb_field:
  3294. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3295. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3296. return -2;
  3297. }
  3298. static inline void transport_release_tasks(struct se_cmd *);
  3299. /*
  3300. * This function will copy a contiguous *src buffer into a destination
  3301. * struct scatterlist array.
  3302. */
  3303. static void transport_memcpy_write_contig(
  3304. struct se_cmd *cmd,
  3305. struct scatterlist *sg_d,
  3306. unsigned char *src)
  3307. {
  3308. u32 i = 0, length = 0, total_length = cmd->data_length;
  3309. void *dst;
  3310. while (total_length) {
  3311. length = sg_d[i].length;
  3312. if (length > total_length)
  3313. length = total_length;
  3314. dst = sg_virt(&sg_d[i]);
  3315. memcpy(dst, src, length);
  3316. if (!(total_length -= length))
  3317. return;
  3318. src += length;
  3319. i++;
  3320. }
  3321. }
  3322. /*
  3323. * This function will copy a struct scatterlist array *sg_s into a destination
  3324. * contiguous *dst buffer.
  3325. */
  3326. static void transport_memcpy_read_contig(
  3327. struct se_cmd *cmd,
  3328. unsigned char *dst,
  3329. struct scatterlist *sg_s)
  3330. {
  3331. u32 i = 0, length = 0, total_length = cmd->data_length;
  3332. void *src;
  3333. while (total_length) {
  3334. length = sg_s[i].length;
  3335. if (length > total_length)
  3336. length = total_length;
  3337. src = sg_virt(&sg_s[i]);
  3338. memcpy(dst, src, length);
  3339. if (!(total_length -= length))
  3340. return;
  3341. dst += length;
  3342. i++;
  3343. }
  3344. }
  3345. static void transport_memcpy_se_mem_read_contig(
  3346. struct se_cmd *cmd,
  3347. unsigned char *dst,
  3348. struct list_head *se_mem_list)
  3349. {
  3350. struct se_mem *se_mem;
  3351. void *src;
  3352. u32 length = 0, total_length = cmd->data_length;
  3353. list_for_each_entry(se_mem, se_mem_list, se_list) {
  3354. length = se_mem->se_len;
  3355. if (length > total_length)
  3356. length = total_length;
  3357. src = page_address(se_mem->se_page) + se_mem->se_off;
  3358. memcpy(dst, src, length);
  3359. if (!(total_length -= length))
  3360. return;
  3361. dst += length;
  3362. }
  3363. }
  3364. /*
  3365. * Called from transport_generic_complete_ok() and
  3366. * transport_generic_request_failure() to determine which dormant/delayed
  3367. * and ordered cmds need to have their tasks added to the execution queue.
  3368. */
  3369. static void transport_complete_task_attr(struct se_cmd *cmd)
  3370. {
  3371. struct se_device *dev = SE_DEV(cmd);
  3372. struct se_cmd *cmd_p, *cmd_tmp;
  3373. int new_active_tasks = 0;
  3374. if (cmd->sam_task_attr == TASK_ATTR_SIMPLE) {
  3375. atomic_dec(&dev->simple_cmds);
  3376. smp_mb__after_atomic_dec();
  3377. dev->dev_cur_ordered_id++;
  3378. DEBUG_STA("Incremented dev->dev_cur_ordered_id: %u for"
  3379. " SIMPLE: %u\n", dev->dev_cur_ordered_id,
  3380. cmd->se_ordered_id);
  3381. } else if (cmd->sam_task_attr == TASK_ATTR_HOQ) {
  3382. atomic_dec(&dev->dev_hoq_count);
  3383. smp_mb__after_atomic_dec();
  3384. dev->dev_cur_ordered_id++;
  3385. DEBUG_STA("Incremented dev_cur_ordered_id: %u for"
  3386. " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
  3387. cmd->se_ordered_id);
  3388. } else if (cmd->sam_task_attr == TASK_ATTR_ORDERED) {
  3389. spin_lock(&dev->ordered_cmd_lock);
  3390. list_del(&cmd->se_ordered_list);
  3391. atomic_dec(&dev->dev_ordered_sync);
  3392. smp_mb__after_atomic_dec();
  3393. spin_unlock(&dev->ordered_cmd_lock);
  3394. dev->dev_cur_ordered_id++;
  3395. DEBUG_STA("Incremented dev_cur_ordered_id: %u for ORDERED:"
  3396. " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
  3397. }
  3398. /*
  3399. * Process all commands up to the last received
  3400. * ORDERED task attribute which requires another blocking
  3401. * boundary
  3402. */
  3403. spin_lock(&dev->delayed_cmd_lock);
  3404. list_for_each_entry_safe(cmd_p, cmd_tmp,
  3405. &dev->delayed_cmd_list, se_delayed_list) {
  3406. list_del(&cmd_p->se_delayed_list);
  3407. spin_unlock(&dev->delayed_cmd_lock);
  3408. DEBUG_STA("Calling add_tasks() for"
  3409. " cmd_p: 0x%02x Task Attr: 0x%02x"
  3410. " Dormant -> Active, se_ordered_id: %u\n",
  3411. T_TASK(cmd_p)->t_task_cdb[0],
  3412. cmd_p->sam_task_attr, cmd_p->se_ordered_id);
  3413. transport_add_tasks_from_cmd(cmd_p);
  3414. new_active_tasks++;
  3415. spin_lock(&dev->delayed_cmd_lock);
  3416. if (cmd_p->sam_task_attr == TASK_ATTR_ORDERED)
  3417. break;
  3418. }
  3419. spin_unlock(&dev->delayed_cmd_lock);
  3420. /*
  3421. * If new tasks have become active, wake up the transport thread
  3422. * to do the processing of the Active tasks.
  3423. */
  3424. if (new_active_tasks != 0)
  3425. wake_up_interruptible(&dev->dev_queue_obj->thread_wq);
  3426. }
  3427. static void transport_generic_complete_ok(struct se_cmd *cmd)
  3428. {
  3429. int reason = 0;
  3430. /*
  3431. * Check if we need to move delayed/dormant tasks from cmds on the
  3432. * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
  3433. * Attribute.
  3434. */
  3435. if (SE_DEV(cmd)->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
  3436. transport_complete_task_attr(cmd);
  3437. /*
  3438. * Check if we need to retrieve a sense buffer from
  3439. * the struct se_cmd in question.
  3440. */
  3441. if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
  3442. if (transport_get_sense_data(cmd) < 0)
  3443. reason = TCM_NON_EXISTENT_LUN;
  3444. /*
  3445. * Only set when an struct se_task->task_scsi_status returned
  3446. * a non GOOD status.
  3447. */
  3448. if (cmd->scsi_status) {
  3449. transport_send_check_condition_and_sense(
  3450. cmd, reason, 1);
  3451. transport_lun_remove_cmd(cmd);
  3452. transport_cmd_check_stop_to_fabric(cmd);
  3453. return;
  3454. }
  3455. }
  3456. /*
  3457. * Check for a callback, used by amoungst other things
  3458. * XDWRITE_READ_10 emulation.
  3459. */
  3460. if (cmd->transport_complete_callback)
  3461. cmd->transport_complete_callback(cmd);
  3462. switch (cmd->data_direction) {
  3463. case DMA_FROM_DEVICE:
  3464. spin_lock(&cmd->se_lun->lun_sep_lock);
  3465. if (SE_LUN(cmd)->lun_sep) {
  3466. SE_LUN(cmd)->lun_sep->sep_stats.tx_data_octets +=
  3467. cmd->data_length;
  3468. }
  3469. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3470. /*
  3471. * If enabled by TCM fabirc module pre-registered SGL
  3472. * memory, perform the memcpy() from the TCM internal
  3473. * contigious buffer back to the original SGL.
  3474. */
  3475. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_CONTIG_TO_SG)
  3476. transport_memcpy_write_contig(cmd,
  3477. T_TASK(cmd)->t_task_pt_sgl,
  3478. T_TASK(cmd)->t_task_buf);
  3479. CMD_TFO(cmd)->queue_data_in(cmd);
  3480. break;
  3481. case DMA_TO_DEVICE:
  3482. spin_lock(&cmd->se_lun->lun_sep_lock);
  3483. if (SE_LUN(cmd)->lun_sep) {
  3484. SE_LUN(cmd)->lun_sep->sep_stats.rx_data_octets +=
  3485. cmd->data_length;
  3486. }
  3487. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3488. /*
  3489. * Check if we need to send READ payload for BIDI-COMMAND
  3490. */
  3491. if (T_TASK(cmd)->t_mem_bidi_list != NULL) {
  3492. spin_lock(&cmd->se_lun->lun_sep_lock);
  3493. if (SE_LUN(cmd)->lun_sep) {
  3494. SE_LUN(cmd)->lun_sep->sep_stats.tx_data_octets +=
  3495. cmd->data_length;
  3496. }
  3497. spin_unlock(&cmd->se_lun->lun_sep_lock);
  3498. CMD_TFO(cmd)->queue_data_in(cmd);
  3499. break;
  3500. }
  3501. /* Fall through for DMA_TO_DEVICE */
  3502. case DMA_NONE:
  3503. CMD_TFO(cmd)->queue_status(cmd);
  3504. break;
  3505. default:
  3506. break;
  3507. }
  3508. transport_lun_remove_cmd(cmd);
  3509. transport_cmd_check_stop_to_fabric(cmd);
  3510. }
  3511. static void transport_free_dev_tasks(struct se_cmd *cmd)
  3512. {
  3513. struct se_task *task, *task_tmp;
  3514. unsigned long flags;
  3515. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  3516. list_for_each_entry_safe(task, task_tmp,
  3517. &T_TASK(cmd)->t_task_list, t_list) {
  3518. if (atomic_read(&task->task_active))
  3519. continue;
  3520. kfree(task->task_sg_bidi);
  3521. kfree(task->task_sg);
  3522. list_del(&task->t_list);
  3523. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3524. if (task->se_dev)
  3525. TRANSPORT(task->se_dev)->free_task(task);
  3526. else
  3527. printk(KERN_ERR "task[%u] - task->se_dev is NULL\n",
  3528. task->task_no);
  3529. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  3530. }
  3531. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3532. }
  3533. static inline void transport_free_pages(struct se_cmd *cmd)
  3534. {
  3535. struct se_mem *se_mem, *se_mem_tmp;
  3536. int free_page = 1;
  3537. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
  3538. free_page = 0;
  3539. if (cmd->se_dev->transport->do_se_mem_map)
  3540. free_page = 0;
  3541. if (T_TASK(cmd)->t_task_buf) {
  3542. kfree(T_TASK(cmd)->t_task_buf);
  3543. T_TASK(cmd)->t_task_buf = NULL;
  3544. return;
  3545. }
  3546. /*
  3547. * Caller will handle releasing of struct se_mem.
  3548. */
  3549. if (cmd->se_cmd_flags & SCF_CMD_PASSTHROUGH_NOALLOC)
  3550. return;
  3551. if (!(T_TASK(cmd)->t_tasks_se_num))
  3552. return;
  3553. list_for_each_entry_safe(se_mem, se_mem_tmp,
  3554. T_TASK(cmd)->t_mem_list, se_list) {
  3555. /*
  3556. * We only release call __free_page(struct se_mem->se_page) when
  3557. * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
  3558. */
  3559. if (free_page)
  3560. __free_page(se_mem->se_page);
  3561. list_del(&se_mem->se_list);
  3562. kmem_cache_free(se_mem_cache, se_mem);
  3563. }
  3564. if (T_TASK(cmd)->t_mem_bidi_list && T_TASK(cmd)->t_tasks_se_bidi_num) {
  3565. list_for_each_entry_safe(se_mem, se_mem_tmp,
  3566. T_TASK(cmd)->t_mem_bidi_list, se_list) {
  3567. /*
  3568. * We only release call __free_page(struct se_mem->se_page) when
  3569. * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
  3570. */
  3571. if (free_page)
  3572. __free_page(se_mem->se_page);
  3573. list_del(&se_mem->se_list);
  3574. kmem_cache_free(se_mem_cache, se_mem);
  3575. }
  3576. }
  3577. kfree(T_TASK(cmd)->t_mem_bidi_list);
  3578. T_TASK(cmd)->t_mem_bidi_list = NULL;
  3579. kfree(T_TASK(cmd)->t_mem_list);
  3580. T_TASK(cmd)->t_mem_list = NULL;
  3581. T_TASK(cmd)->t_tasks_se_num = 0;
  3582. }
  3583. static inline void transport_release_tasks(struct se_cmd *cmd)
  3584. {
  3585. transport_free_dev_tasks(cmd);
  3586. }
  3587. static inline int transport_dec_and_check(struct se_cmd *cmd)
  3588. {
  3589. unsigned long flags;
  3590. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  3591. if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
  3592. if (!(atomic_dec_and_test(&T_TASK(cmd)->t_fe_count))) {
  3593. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  3594. flags);
  3595. return 1;
  3596. }
  3597. }
  3598. if (atomic_read(&T_TASK(cmd)->t_se_count)) {
  3599. if (!(atomic_dec_and_test(&T_TASK(cmd)->t_se_count))) {
  3600. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  3601. flags);
  3602. return 1;
  3603. }
  3604. }
  3605. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3606. return 0;
  3607. }
  3608. static void transport_release_fe_cmd(struct se_cmd *cmd)
  3609. {
  3610. unsigned long flags;
  3611. if (transport_dec_and_check(cmd))
  3612. return;
  3613. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  3614. if (!(atomic_read(&T_TASK(cmd)->transport_dev_active))) {
  3615. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3616. goto free_pages;
  3617. }
  3618. atomic_set(&T_TASK(cmd)->transport_dev_active, 0);
  3619. transport_all_task_dev_remove_state(cmd);
  3620. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3621. transport_release_tasks(cmd);
  3622. free_pages:
  3623. transport_free_pages(cmd);
  3624. transport_free_se_cmd(cmd);
  3625. CMD_TFO(cmd)->release_cmd_direct(cmd);
  3626. }
  3627. static int transport_generic_remove(
  3628. struct se_cmd *cmd,
  3629. int release_to_pool,
  3630. int session_reinstatement)
  3631. {
  3632. unsigned long flags;
  3633. if (!(T_TASK(cmd)))
  3634. goto release_cmd;
  3635. if (transport_dec_and_check(cmd)) {
  3636. if (session_reinstatement) {
  3637. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  3638. transport_all_task_dev_remove_state(cmd);
  3639. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  3640. flags);
  3641. }
  3642. return 1;
  3643. }
  3644. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  3645. if (!(atomic_read(&T_TASK(cmd)->transport_dev_active))) {
  3646. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3647. goto free_pages;
  3648. }
  3649. atomic_set(&T_TASK(cmd)->transport_dev_active, 0);
  3650. transport_all_task_dev_remove_state(cmd);
  3651. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  3652. transport_release_tasks(cmd);
  3653. free_pages:
  3654. transport_free_pages(cmd);
  3655. release_cmd:
  3656. if (release_to_pool) {
  3657. transport_release_cmd_to_pool(cmd);
  3658. } else {
  3659. transport_free_se_cmd(cmd);
  3660. CMD_TFO(cmd)->release_cmd_direct(cmd);
  3661. }
  3662. return 0;
  3663. }
  3664. /*
  3665. * transport_generic_map_mem_to_cmd - Perform SGL -> struct se_mem map
  3666. * @cmd: Associated se_cmd descriptor
  3667. * @mem: SGL style memory for TCM WRITE / READ
  3668. * @sg_mem_num: Number of SGL elements
  3669. * @mem_bidi_in: SGL style memory for TCM BIDI READ
  3670. * @sg_mem_bidi_num: Number of BIDI READ SGL elements
  3671. *
  3672. * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
  3673. * of parameters.
  3674. */
  3675. int transport_generic_map_mem_to_cmd(
  3676. struct se_cmd *cmd,
  3677. struct scatterlist *mem,
  3678. u32 sg_mem_num,
  3679. struct scatterlist *mem_bidi_in,
  3680. u32 sg_mem_bidi_num)
  3681. {
  3682. u32 se_mem_cnt_out = 0;
  3683. int ret;
  3684. if (!(mem) || !(sg_mem_num))
  3685. return 0;
  3686. /*
  3687. * Passed *mem will contain a list_head containing preformatted
  3688. * struct se_mem elements...
  3689. */
  3690. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM)) {
  3691. if ((mem_bidi_in) || (sg_mem_bidi_num)) {
  3692. printk(KERN_ERR "SCF_CMD_PASSTHROUGH_NOALLOC not supported"
  3693. " with BIDI-COMMAND\n");
  3694. return -ENOSYS;
  3695. }
  3696. T_TASK(cmd)->t_mem_list = (struct list_head *)mem;
  3697. T_TASK(cmd)->t_tasks_se_num = sg_mem_num;
  3698. cmd->se_cmd_flags |= SCF_CMD_PASSTHROUGH_NOALLOC;
  3699. return 0;
  3700. }
  3701. /*
  3702. * Otherwise, assume the caller is passing a struct scatterlist
  3703. * array from include/linux/scatterlist.h
  3704. */
  3705. if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
  3706. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
  3707. /*
  3708. * For CDB using TCM struct se_mem linked list scatterlist memory
  3709. * processed into a TCM struct se_subsystem_dev, we do the mapping
  3710. * from the passed physical memory to struct se_mem->se_page here.
  3711. */
  3712. T_TASK(cmd)->t_mem_list = transport_init_se_mem_list();
  3713. if (!(T_TASK(cmd)->t_mem_list))
  3714. return -ENOMEM;
  3715. ret = transport_map_sg_to_mem(cmd,
  3716. T_TASK(cmd)->t_mem_list, mem, &se_mem_cnt_out);
  3717. if (ret < 0)
  3718. return -ENOMEM;
  3719. T_TASK(cmd)->t_tasks_se_num = se_mem_cnt_out;
  3720. /*
  3721. * Setup BIDI READ list of struct se_mem elements
  3722. */
  3723. if ((mem_bidi_in) && (sg_mem_bidi_num)) {
  3724. T_TASK(cmd)->t_mem_bidi_list = transport_init_se_mem_list();
  3725. if (!(T_TASK(cmd)->t_mem_bidi_list)) {
  3726. kfree(T_TASK(cmd)->t_mem_list);
  3727. return -ENOMEM;
  3728. }
  3729. se_mem_cnt_out = 0;
  3730. ret = transport_map_sg_to_mem(cmd,
  3731. T_TASK(cmd)->t_mem_bidi_list, mem_bidi_in,
  3732. &se_mem_cnt_out);
  3733. if (ret < 0) {
  3734. kfree(T_TASK(cmd)->t_mem_list);
  3735. return -ENOMEM;
  3736. }
  3737. T_TASK(cmd)->t_tasks_se_bidi_num = se_mem_cnt_out;
  3738. }
  3739. cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  3740. } else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB) {
  3741. if (mem_bidi_in || sg_mem_bidi_num) {
  3742. printk(KERN_ERR "BIDI-Commands not supported using "
  3743. "SCF_SCSI_CONTROL_NONSG_IO_CDB\n");
  3744. return -ENOSYS;
  3745. }
  3746. /*
  3747. * For incoming CDBs using a contiguous buffer internall with TCM,
  3748. * save the passed struct scatterlist memory. After TCM storage object
  3749. * processing has completed for this struct se_cmd, TCM core will call
  3750. * transport_memcpy_[write,read]_contig() as necessary from
  3751. * transport_generic_complete_ok() and transport_write_pending() in order
  3752. * to copy the TCM buffer to/from the original passed *mem in SGL ->
  3753. * struct scatterlist format.
  3754. */
  3755. cmd->se_cmd_flags |= SCF_PASSTHROUGH_CONTIG_TO_SG;
  3756. T_TASK(cmd)->t_task_pt_sgl = mem;
  3757. }
  3758. return 0;
  3759. }
  3760. EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
  3761. static inline long long transport_dev_end_lba(struct se_device *dev)
  3762. {
  3763. return dev->transport->get_blocks(dev) + 1;
  3764. }
  3765. static int transport_get_sectors(struct se_cmd *cmd)
  3766. {
  3767. struct se_device *dev = SE_DEV(cmd);
  3768. T_TASK(cmd)->t_tasks_sectors =
  3769. (cmd->data_length / DEV_ATTRIB(dev)->block_size);
  3770. if (!(T_TASK(cmd)->t_tasks_sectors))
  3771. T_TASK(cmd)->t_tasks_sectors = 1;
  3772. if (TRANSPORT(dev)->get_device_type(dev) != TYPE_DISK)
  3773. return 0;
  3774. if ((T_TASK(cmd)->t_task_lba + T_TASK(cmd)->t_tasks_sectors) >
  3775. transport_dev_end_lba(dev)) {
  3776. printk(KERN_ERR "LBA: %llu Sectors: %u exceeds"
  3777. " transport_dev_end_lba(): %llu\n",
  3778. T_TASK(cmd)->t_task_lba, T_TASK(cmd)->t_tasks_sectors,
  3779. transport_dev_end_lba(dev));
  3780. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3781. cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY;
  3782. return PYX_TRANSPORT_REQ_TOO_MANY_SECTORS;
  3783. }
  3784. return 0;
  3785. }
  3786. static int transport_new_cmd_obj(struct se_cmd *cmd)
  3787. {
  3788. struct se_device *dev = SE_DEV(cmd);
  3789. u32 task_cdbs = 0, rc;
  3790. if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)) {
  3791. task_cdbs++;
  3792. T_TASK(cmd)->t_task_cdbs++;
  3793. } else {
  3794. int set_counts = 1;
  3795. /*
  3796. * Setup any BIDI READ tasks and memory from
  3797. * T_TASK(cmd)->t_mem_bidi_list so the READ struct se_tasks
  3798. * are queued first for the non pSCSI passthrough case.
  3799. */
  3800. if ((T_TASK(cmd)->t_mem_bidi_list != NULL) &&
  3801. (TRANSPORT(dev)->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV)) {
  3802. rc = transport_generic_get_cdb_count(cmd,
  3803. T_TASK(cmd)->t_task_lba,
  3804. T_TASK(cmd)->t_tasks_sectors,
  3805. DMA_FROM_DEVICE, T_TASK(cmd)->t_mem_bidi_list,
  3806. set_counts);
  3807. if (!(rc)) {
  3808. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3809. cmd->scsi_sense_reason =
  3810. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3811. return PYX_TRANSPORT_LU_COMM_FAILURE;
  3812. }
  3813. set_counts = 0;
  3814. }
  3815. /*
  3816. * Setup the tasks and memory from T_TASK(cmd)->t_mem_list
  3817. * Note for BIDI transfers this will contain the WRITE payload
  3818. */
  3819. task_cdbs = transport_generic_get_cdb_count(cmd,
  3820. T_TASK(cmd)->t_task_lba,
  3821. T_TASK(cmd)->t_tasks_sectors,
  3822. cmd->data_direction, T_TASK(cmd)->t_mem_list,
  3823. set_counts);
  3824. if (!(task_cdbs)) {
  3825. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  3826. cmd->scsi_sense_reason =
  3827. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3828. return PYX_TRANSPORT_LU_COMM_FAILURE;
  3829. }
  3830. T_TASK(cmd)->t_task_cdbs += task_cdbs;
  3831. #if 0
  3832. printk(KERN_INFO "data_length: %u, LBA: %llu t_tasks_sectors:"
  3833. " %u, t_task_cdbs: %u\n", obj_ptr, cmd->data_length,
  3834. T_TASK(cmd)->t_task_lba, T_TASK(cmd)->t_tasks_sectors,
  3835. T_TASK(cmd)->t_task_cdbs);
  3836. #endif
  3837. }
  3838. atomic_set(&T_TASK(cmd)->t_task_cdbs_left, task_cdbs);
  3839. atomic_set(&T_TASK(cmd)->t_task_cdbs_ex_left, task_cdbs);
  3840. atomic_set(&T_TASK(cmd)->t_task_cdbs_timeout_left, task_cdbs);
  3841. return 0;
  3842. }
  3843. static struct list_head *transport_init_se_mem_list(void)
  3844. {
  3845. struct list_head *se_mem_list;
  3846. se_mem_list = kzalloc(sizeof(struct list_head), GFP_KERNEL);
  3847. if (!(se_mem_list)) {
  3848. printk(KERN_ERR "Unable to allocate memory for se_mem_list\n");
  3849. return NULL;
  3850. }
  3851. INIT_LIST_HEAD(se_mem_list);
  3852. return se_mem_list;
  3853. }
  3854. static int
  3855. transport_generic_get_mem(struct se_cmd *cmd, u32 length, u32 dma_size)
  3856. {
  3857. unsigned char *buf;
  3858. struct se_mem *se_mem;
  3859. T_TASK(cmd)->t_mem_list = transport_init_se_mem_list();
  3860. if (!(T_TASK(cmd)->t_mem_list))
  3861. return -ENOMEM;
  3862. /*
  3863. * If the device uses memory mapping this is enough.
  3864. */
  3865. if (cmd->se_dev->transport->do_se_mem_map)
  3866. return 0;
  3867. /*
  3868. * Setup BIDI-COMMAND READ list of struct se_mem elements
  3869. */
  3870. if (T_TASK(cmd)->t_tasks_bidi) {
  3871. T_TASK(cmd)->t_mem_bidi_list = transport_init_se_mem_list();
  3872. if (!(T_TASK(cmd)->t_mem_bidi_list)) {
  3873. kfree(T_TASK(cmd)->t_mem_list);
  3874. return -ENOMEM;
  3875. }
  3876. }
  3877. while (length) {
  3878. se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
  3879. if (!(se_mem)) {
  3880. printk(KERN_ERR "Unable to allocate struct se_mem\n");
  3881. goto out;
  3882. }
  3883. INIT_LIST_HEAD(&se_mem->se_list);
  3884. se_mem->se_len = (length > dma_size) ? dma_size : length;
  3885. /* #warning FIXME Allocate contigous pages for struct se_mem elements */
  3886. se_mem->se_page = (struct page *) alloc_pages(GFP_KERNEL, 0);
  3887. if (!(se_mem->se_page)) {
  3888. printk(KERN_ERR "alloc_pages() failed\n");
  3889. goto out;
  3890. }
  3891. buf = kmap_atomic(se_mem->se_page, KM_IRQ0);
  3892. if (!(buf)) {
  3893. printk(KERN_ERR "kmap_atomic() failed\n");
  3894. goto out;
  3895. }
  3896. memset(buf, 0, se_mem->se_len);
  3897. kunmap_atomic(buf, KM_IRQ0);
  3898. list_add_tail(&se_mem->se_list, T_TASK(cmd)->t_mem_list);
  3899. T_TASK(cmd)->t_tasks_se_num++;
  3900. DEBUG_MEM("Allocated struct se_mem page(%p) Length(%u)"
  3901. " Offset(%u)\n", se_mem->se_page, se_mem->se_len,
  3902. se_mem->se_off);
  3903. length -= se_mem->se_len;
  3904. }
  3905. DEBUG_MEM("Allocated total struct se_mem elements(%u)\n",
  3906. T_TASK(cmd)->t_tasks_se_num);
  3907. return 0;
  3908. out:
  3909. return -1;
  3910. }
  3911. u32 transport_calc_sg_num(
  3912. struct se_task *task,
  3913. struct se_mem *in_se_mem,
  3914. u32 task_offset)
  3915. {
  3916. struct se_cmd *se_cmd = task->task_se_cmd;
  3917. struct se_device *se_dev = SE_DEV(se_cmd);
  3918. struct se_mem *se_mem = in_se_mem;
  3919. struct target_core_fabric_ops *tfo = CMD_TFO(se_cmd);
  3920. u32 sg_length, task_size = task->task_size, task_sg_num_padded;
  3921. while (task_size != 0) {
  3922. DEBUG_SC("se_mem->se_page(%p) se_mem->se_len(%u)"
  3923. " se_mem->se_off(%u) task_offset(%u)\n",
  3924. se_mem->se_page, se_mem->se_len,
  3925. se_mem->se_off, task_offset);
  3926. if (task_offset == 0) {
  3927. if (task_size >= se_mem->se_len) {
  3928. sg_length = se_mem->se_len;
  3929. if (!(list_is_last(&se_mem->se_list,
  3930. T_TASK(se_cmd)->t_mem_list)))
  3931. se_mem = list_entry(se_mem->se_list.next,
  3932. struct se_mem, se_list);
  3933. } else {
  3934. sg_length = task_size;
  3935. task_size -= sg_length;
  3936. goto next;
  3937. }
  3938. DEBUG_SC("sg_length(%u) task_size(%u)\n",
  3939. sg_length, task_size);
  3940. } else {
  3941. if ((se_mem->se_len - task_offset) > task_size) {
  3942. sg_length = task_size;
  3943. task_size -= sg_length;
  3944. goto next;
  3945. } else {
  3946. sg_length = (se_mem->se_len - task_offset);
  3947. if (!(list_is_last(&se_mem->se_list,
  3948. T_TASK(se_cmd)->t_mem_list)))
  3949. se_mem = list_entry(se_mem->se_list.next,
  3950. struct se_mem, se_list);
  3951. }
  3952. DEBUG_SC("sg_length(%u) task_size(%u)\n",
  3953. sg_length, task_size);
  3954. task_offset = 0;
  3955. }
  3956. task_size -= sg_length;
  3957. next:
  3958. DEBUG_SC("task[%u] - Reducing task_size to(%u)\n",
  3959. task->task_no, task_size);
  3960. task->task_sg_num++;
  3961. }
  3962. /*
  3963. * Check if the fabric module driver is requesting that all
  3964. * struct se_task->task_sg[] be chained together.. If so,
  3965. * then allocate an extra padding SG entry for linking and
  3966. * marking the end of the chained SGL.
  3967. */
  3968. if (tfo->task_sg_chaining) {
  3969. task_sg_num_padded = (task->task_sg_num + 1);
  3970. task->task_padded_sg = 1;
  3971. } else
  3972. task_sg_num_padded = task->task_sg_num;
  3973. task->task_sg = kzalloc(task_sg_num_padded *
  3974. sizeof(struct scatterlist), GFP_KERNEL);
  3975. if (!(task->task_sg)) {
  3976. printk(KERN_ERR "Unable to allocate memory for"
  3977. " task->task_sg\n");
  3978. return 0;
  3979. }
  3980. sg_init_table(&task->task_sg[0], task_sg_num_padded);
  3981. /*
  3982. * Setup task->task_sg_bidi for SCSI READ payload for
  3983. * TCM/pSCSI passthrough if present for BIDI-COMMAND
  3984. */
  3985. if ((T_TASK(se_cmd)->t_mem_bidi_list != NULL) &&
  3986. (TRANSPORT(se_dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)) {
  3987. task->task_sg_bidi = kzalloc(task_sg_num_padded *
  3988. sizeof(struct scatterlist), GFP_KERNEL);
  3989. if (!(task->task_sg_bidi)) {
  3990. printk(KERN_ERR "Unable to allocate memory for"
  3991. " task->task_sg_bidi\n");
  3992. return 0;
  3993. }
  3994. sg_init_table(&task->task_sg_bidi[0], task_sg_num_padded);
  3995. }
  3996. /*
  3997. * For the chaining case, setup the proper end of SGL for the
  3998. * initial submission struct task into struct se_subsystem_api.
  3999. * This will be cleared later by transport_do_task_sg_chain()
  4000. */
  4001. if (task->task_padded_sg) {
  4002. sg_mark_end(&task->task_sg[task->task_sg_num - 1]);
  4003. /*
  4004. * Added the 'if' check before marking end of bi-directional
  4005. * scatterlist (which gets created only in case of request
  4006. * (RD + WR).
  4007. */
  4008. if (task->task_sg_bidi)
  4009. sg_mark_end(&task->task_sg_bidi[task->task_sg_num - 1]);
  4010. }
  4011. DEBUG_SC("Successfully allocated task->task_sg_num(%u),"
  4012. " task_sg_num_padded(%u)\n", task->task_sg_num,
  4013. task_sg_num_padded);
  4014. return task->task_sg_num;
  4015. }
  4016. static inline int transport_set_tasks_sectors_disk(
  4017. struct se_task *task,
  4018. struct se_device *dev,
  4019. unsigned long long lba,
  4020. u32 sectors,
  4021. int *max_sectors_set)
  4022. {
  4023. if ((lba + sectors) > transport_dev_end_lba(dev)) {
  4024. task->task_sectors = ((transport_dev_end_lba(dev) - lba) + 1);
  4025. if (task->task_sectors > DEV_ATTRIB(dev)->max_sectors) {
  4026. task->task_sectors = DEV_ATTRIB(dev)->max_sectors;
  4027. *max_sectors_set = 1;
  4028. }
  4029. } else {
  4030. if (sectors > DEV_ATTRIB(dev)->max_sectors) {
  4031. task->task_sectors = DEV_ATTRIB(dev)->max_sectors;
  4032. *max_sectors_set = 1;
  4033. } else
  4034. task->task_sectors = sectors;
  4035. }
  4036. return 0;
  4037. }
  4038. static inline int transport_set_tasks_sectors_non_disk(
  4039. struct se_task *task,
  4040. struct se_device *dev,
  4041. unsigned long long lba,
  4042. u32 sectors,
  4043. int *max_sectors_set)
  4044. {
  4045. if (sectors > DEV_ATTRIB(dev)->max_sectors) {
  4046. task->task_sectors = DEV_ATTRIB(dev)->max_sectors;
  4047. *max_sectors_set = 1;
  4048. } else
  4049. task->task_sectors = sectors;
  4050. return 0;
  4051. }
  4052. static inline int transport_set_tasks_sectors(
  4053. struct se_task *task,
  4054. struct se_device *dev,
  4055. unsigned long long lba,
  4056. u32 sectors,
  4057. int *max_sectors_set)
  4058. {
  4059. return (TRANSPORT(dev)->get_device_type(dev) == TYPE_DISK) ?
  4060. transport_set_tasks_sectors_disk(task, dev, lba, sectors,
  4061. max_sectors_set) :
  4062. transport_set_tasks_sectors_non_disk(task, dev, lba, sectors,
  4063. max_sectors_set);
  4064. }
  4065. static int transport_map_sg_to_mem(
  4066. struct se_cmd *cmd,
  4067. struct list_head *se_mem_list,
  4068. void *in_mem,
  4069. u32 *se_mem_cnt)
  4070. {
  4071. struct se_mem *se_mem;
  4072. struct scatterlist *sg;
  4073. u32 sg_count = 1, cmd_size = cmd->data_length;
  4074. if (!in_mem) {
  4075. printk(KERN_ERR "No source scatterlist\n");
  4076. return -1;
  4077. }
  4078. sg = (struct scatterlist *)in_mem;
  4079. while (cmd_size) {
  4080. se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
  4081. if (!(se_mem)) {
  4082. printk(KERN_ERR "Unable to allocate struct se_mem\n");
  4083. return -1;
  4084. }
  4085. INIT_LIST_HEAD(&se_mem->se_list);
  4086. DEBUG_MEM("sg_to_mem: Starting loop with cmd_size: %u"
  4087. " sg_page: %p offset: %d length: %d\n", cmd_size,
  4088. sg_page(sg), sg->offset, sg->length);
  4089. se_mem->se_page = sg_page(sg);
  4090. se_mem->se_off = sg->offset;
  4091. if (cmd_size > sg->length) {
  4092. se_mem->se_len = sg->length;
  4093. sg = sg_next(sg);
  4094. sg_count++;
  4095. } else
  4096. se_mem->se_len = cmd_size;
  4097. cmd_size -= se_mem->se_len;
  4098. DEBUG_MEM("sg_to_mem: *se_mem_cnt: %u cmd_size: %u\n",
  4099. *se_mem_cnt, cmd_size);
  4100. DEBUG_MEM("sg_to_mem: Final se_page: %p se_off: %d se_len: %d\n",
  4101. se_mem->se_page, se_mem->se_off, se_mem->se_len);
  4102. list_add_tail(&se_mem->se_list, se_mem_list);
  4103. (*se_mem_cnt)++;
  4104. }
  4105. DEBUG_MEM("task[0] - Mapped(%u) struct scatterlist segments to(%u)"
  4106. " struct se_mem\n", sg_count, *se_mem_cnt);
  4107. if (sg_count != *se_mem_cnt)
  4108. BUG();
  4109. return 0;
  4110. }
  4111. /* transport_map_mem_to_sg():
  4112. *
  4113. *
  4114. */
  4115. int transport_map_mem_to_sg(
  4116. struct se_task *task,
  4117. struct list_head *se_mem_list,
  4118. void *in_mem,
  4119. struct se_mem *in_se_mem,
  4120. struct se_mem **out_se_mem,
  4121. u32 *se_mem_cnt,
  4122. u32 *task_offset)
  4123. {
  4124. struct se_cmd *se_cmd = task->task_se_cmd;
  4125. struct se_mem *se_mem = in_se_mem;
  4126. struct scatterlist *sg = (struct scatterlist *)in_mem;
  4127. u32 task_size = task->task_size, sg_no = 0;
  4128. if (!sg) {
  4129. printk(KERN_ERR "Unable to locate valid struct"
  4130. " scatterlist pointer\n");
  4131. return -1;
  4132. }
  4133. while (task_size != 0) {
  4134. /*
  4135. * Setup the contigious array of scatterlists for
  4136. * this struct se_task.
  4137. */
  4138. sg_assign_page(sg, se_mem->se_page);
  4139. if (*task_offset == 0) {
  4140. sg->offset = se_mem->se_off;
  4141. if (task_size >= se_mem->se_len) {
  4142. sg->length = se_mem->se_len;
  4143. if (!(list_is_last(&se_mem->se_list,
  4144. T_TASK(se_cmd)->t_mem_list))) {
  4145. se_mem = list_entry(se_mem->se_list.next,
  4146. struct se_mem, se_list);
  4147. (*se_mem_cnt)++;
  4148. }
  4149. } else {
  4150. sg->length = task_size;
  4151. /*
  4152. * Determine if we need to calculate an offset
  4153. * into the struct se_mem on the next go around..
  4154. */
  4155. task_size -= sg->length;
  4156. if (!(task_size))
  4157. *task_offset = sg->length;
  4158. goto next;
  4159. }
  4160. } else {
  4161. sg->offset = (*task_offset + se_mem->se_off);
  4162. if ((se_mem->se_len - *task_offset) > task_size) {
  4163. sg->length = task_size;
  4164. /*
  4165. * Determine if we need to calculate an offset
  4166. * into the struct se_mem on the next go around..
  4167. */
  4168. task_size -= sg->length;
  4169. if (!(task_size))
  4170. *task_offset += sg->length;
  4171. goto next;
  4172. } else {
  4173. sg->length = (se_mem->se_len - *task_offset);
  4174. if (!(list_is_last(&se_mem->se_list,
  4175. T_TASK(se_cmd)->t_mem_list))) {
  4176. se_mem = list_entry(se_mem->se_list.next,
  4177. struct se_mem, se_list);
  4178. (*se_mem_cnt)++;
  4179. }
  4180. }
  4181. *task_offset = 0;
  4182. }
  4183. task_size -= sg->length;
  4184. next:
  4185. DEBUG_MEM("task[%u] mem_to_sg - sg[%u](%p)(%u)(%u) - Reducing"
  4186. " task_size to(%u), task_offset: %u\n", task->task_no, sg_no,
  4187. sg_page(sg), sg->length, sg->offset, task_size, *task_offset);
  4188. sg_no++;
  4189. if (!(task_size))
  4190. break;
  4191. sg = sg_next(sg);
  4192. if (task_size > se_cmd->data_length)
  4193. BUG();
  4194. }
  4195. *out_se_mem = se_mem;
  4196. DEBUG_MEM("task[%u] - Mapped(%u) struct se_mem segments to total(%u)"
  4197. " SGs\n", task->task_no, *se_mem_cnt, sg_no);
  4198. return 0;
  4199. }
  4200. /*
  4201. * This function can be used by HW target mode drivers to create a linked
  4202. * scatterlist from all contiguously allocated struct se_task->task_sg[].
  4203. * This is intended to be called during the completion path by TCM Core
  4204. * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
  4205. */
  4206. void transport_do_task_sg_chain(struct se_cmd *cmd)
  4207. {
  4208. struct scatterlist *sg_head = NULL, *sg_link = NULL, *sg_first = NULL;
  4209. struct scatterlist *sg_head_cur = NULL, *sg_link_cur = NULL;
  4210. struct scatterlist *sg, *sg_end = NULL, *sg_end_cur = NULL;
  4211. struct se_task *task;
  4212. struct target_core_fabric_ops *tfo = CMD_TFO(cmd);
  4213. u32 task_sg_num = 0, sg_count = 0;
  4214. int i;
  4215. if (tfo->task_sg_chaining == 0) {
  4216. printk(KERN_ERR "task_sg_chaining is diabled for fabric module:"
  4217. " %s\n", tfo->get_fabric_name());
  4218. dump_stack();
  4219. return;
  4220. }
  4221. /*
  4222. * Walk the struct se_task list and setup scatterlist chains
  4223. * for each contiguosly allocated struct se_task->task_sg[].
  4224. */
  4225. list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
  4226. if (!(task->task_sg) || !(task->task_padded_sg))
  4227. continue;
  4228. if (sg_head && sg_link) {
  4229. sg_head_cur = &task->task_sg[0];
  4230. sg_link_cur = &task->task_sg[task->task_sg_num];
  4231. /*
  4232. * Either add chain or mark end of scatterlist
  4233. */
  4234. if (!(list_is_last(&task->t_list,
  4235. &T_TASK(cmd)->t_task_list))) {
  4236. /*
  4237. * Clear existing SGL termination bit set in
  4238. * transport_calc_sg_num(), see sg_mark_end()
  4239. */
  4240. sg_end_cur = &task->task_sg[task->task_sg_num - 1];
  4241. sg_end_cur->page_link &= ~0x02;
  4242. sg_chain(sg_head, task_sg_num, sg_head_cur);
  4243. sg_count += (task->task_sg_num + 1);
  4244. } else
  4245. sg_count += task->task_sg_num;
  4246. sg_head = sg_head_cur;
  4247. sg_link = sg_link_cur;
  4248. task_sg_num = task->task_sg_num;
  4249. continue;
  4250. }
  4251. sg_head = sg_first = &task->task_sg[0];
  4252. sg_link = &task->task_sg[task->task_sg_num];
  4253. task_sg_num = task->task_sg_num;
  4254. /*
  4255. * Check for single task..
  4256. */
  4257. if (!(list_is_last(&task->t_list, &T_TASK(cmd)->t_task_list))) {
  4258. /*
  4259. * Clear existing SGL termination bit set in
  4260. * transport_calc_sg_num(), see sg_mark_end()
  4261. */
  4262. sg_end = &task->task_sg[task->task_sg_num - 1];
  4263. sg_end->page_link &= ~0x02;
  4264. sg_count += (task->task_sg_num + 1);
  4265. } else
  4266. sg_count += task->task_sg_num;
  4267. }
  4268. /*
  4269. * Setup the starting pointer and total t_tasks_sg_linked_no including
  4270. * padding SGs for linking and to mark the end.
  4271. */
  4272. T_TASK(cmd)->t_tasks_sg_chained = sg_first;
  4273. T_TASK(cmd)->t_tasks_sg_chained_no = sg_count;
  4274. DEBUG_CMD_M("Setup T_TASK(cmd)->t_tasks_sg_chained: %p and"
  4275. " t_tasks_sg_chained_no: %u\n", T_TASK(cmd)->t_tasks_sg_chained,
  4276. T_TASK(cmd)->t_tasks_sg_chained_no);
  4277. for_each_sg(T_TASK(cmd)->t_tasks_sg_chained, sg,
  4278. T_TASK(cmd)->t_tasks_sg_chained_no, i) {
  4279. DEBUG_CMD_M("SG: %p page: %p length: %d offset: %d\n",
  4280. sg, sg_page(sg), sg->length, sg->offset);
  4281. if (sg_is_chain(sg))
  4282. DEBUG_CMD_M("SG: %p sg_is_chain=1\n", sg);
  4283. if (sg_is_last(sg))
  4284. DEBUG_CMD_M("SG: %p sg_is_last=1\n", sg);
  4285. }
  4286. }
  4287. EXPORT_SYMBOL(transport_do_task_sg_chain);
  4288. static int transport_do_se_mem_map(
  4289. struct se_device *dev,
  4290. struct se_task *task,
  4291. struct list_head *se_mem_list,
  4292. void *in_mem,
  4293. struct se_mem *in_se_mem,
  4294. struct se_mem **out_se_mem,
  4295. u32 *se_mem_cnt,
  4296. u32 *task_offset_in)
  4297. {
  4298. u32 task_offset = *task_offset_in;
  4299. int ret = 0;
  4300. /*
  4301. * se_subsystem_api_t->do_se_mem_map is used when internal allocation
  4302. * has been done by the transport plugin.
  4303. */
  4304. if (TRANSPORT(dev)->do_se_mem_map) {
  4305. ret = TRANSPORT(dev)->do_se_mem_map(task, se_mem_list,
  4306. in_mem, in_se_mem, out_se_mem, se_mem_cnt,
  4307. task_offset_in);
  4308. if (ret == 0)
  4309. T_TASK(task->task_se_cmd)->t_tasks_se_num += *se_mem_cnt;
  4310. return ret;
  4311. }
  4312. BUG_ON(list_empty(se_mem_list));
  4313. /*
  4314. * This is the normal path for all normal non BIDI and BIDI-COMMAND
  4315. * WRITE payloads.. If we need to do BIDI READ passthrough for
  4316. * TCM/pSCSI the first call to transport_do_se_mem_map ->
  4317. * transport_calc_sg_num() -> transport_map_mem_to_sg() will do the
  4318. * allocation for task->task_sg_bidi, and the subsequent call to
  4319. * transport_do_se_mem_map() from transport_generic_get_cdb_count()
  4320. */
  4321. if (!(task->task_sg_bidi)) {
  4322. /*
  4323. * Assume default that transport plugin speaks preallocated
  4324. * scatterlists.
  4325. */
  4326. if (!(transport_calc_sg_num(task, in_se_mem, task_offset)))
  4327. return -1;
  4328. /*
  4329. * struct se_task->task_sg now contains the struct scatterlist array.
  4330. */
  4331. return transport_map_mem_to_sg(task, se_mem_list, task->task_sg,
  4332. in_se_mem, out_se_mem, se_mem_cnt,
  4333. task_offset_in);
  4334. }
  4335. /*
  4336. * Handle the se_mem_list -> struct task->task_sg_bidi
  4337. * memory map for the extra BIDI READ payload
  4338. */
  4339. return transport_map_mem_to_sg(task, se_mem_list, task->task_sg_bidi,
  4340. in_se_mem, out_se_mem, se_mem_cnt,
  4341. task_offset_in);
  4342. }
  4343. static u32 transport_generic_get_cdb_count(
  4344. struct se_cmd *cmd,
  4345. unsigned long long lba,
  4346. u32 sectors,
  4347. enum dma_data_direction data_direction,
  4348. struct list_head *mem_list,
  4349. int set_counts)
  4350. {
  4351. unsigned char *cdb = NULL;
  4352. struct se_task *task;
  4353. struct se_mem *se_mem = NULL, *se_mem_lout = NULL;
  4354. struct se_mem *se_mem_bidi = NULL, *se_mem_bidi_lout = NULL;
  4355. struct se_device *dev = SE_DEV(cmd);
  4356. int max_sectors_set = 0, ret;
  4357. u32 task_offset_in = 0, se_mem_cnt = 0, se_mem_bidi_cnt = 0, task_cdbs = 0;
  4358. if (!mem_list) {
  4359. printk(KERN_ERR "mem_list is NULL in transport_generic_get"
  4360. "_cdb_count()\n");
  4361. return 0;
  4362. }
  4363. /*
  4364. * While using RAMDISK_DR backstores is the only case where
  4365. * mem_list will ever be empty at this point.
  4366. */
  4367. if (!(list_empty(mem_list)))
  4368. se_mem = list_entry(mem_list->next, struct se_mem, se_list);
  4369. /*
  4370. * Check for extra se_mem_bidi mapping for BIDI-COMMANDs to
  4371. * struct se_task->task_sg_bidi for TCM/pSCSI passthrough operation
  4372. */
  4373. if ((T_TASK(cmd)->t_mem_bidi_list != NULL) &&
  4374. !(list_empty(T_TASK(cmd)->t_mem_bidi_list)) &&
  4375. (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV))
  4376. se_mem_bidi = list_entry(T_TASK(cmd)->t_mem_bidi_list->next,
  4377. struct se_mem, se_list);
  4378. while (sectors) {
  4379. DEBUG_VOL("ITT[0x%08x] LBA(%llu) SectorsLeft(%u) EOBJ(%llu)\n",
  4380. CMD_TFO(cmd)->get_task_tag(cmd), lba, sectors,
  4381. transport_dev_end_lba(dev));
  4382. task = transport_generic_get_task(cmd, data_direction);
  4383. if (!(task))
  4384. goto out;
  4385. transport_set_tasks_sectors(task, dev, lba, sectors,
  4386. &max_sectors_set);
  4387. task->task_lba = lba;
  4388. lba += task->task_sectors;
  4389. sectors -= task->task_sectors;
  4390. task->task_size = (task->task_sectors *
  4391. DEV_ATTRIB(dev)->block_size);
  4392. cdb = TRANSPORT(dev)->get_cdb(task);
  4393. if ((cdb)) {
  4394. memcpy(cdb, T_TASK(cmd)->t_task_cdb,
  4395. scsi_command_size(T_TASK(cmd)->t_task_cdb));
  4396. cmd->transport_split_cdb(task->task_lba,
  4397. &task->task_sectors, cdb);
  4398. }
  4399. /*
  4400. * Perform the SE OBJ plugin and/or Transport plugin specific
  4401. * mapping for T_TASK(cmd)->t_mem_list. And setup the
  4402. * task->task_sg and if necessary task->task_sg_bidi
  4403. */
  4404. ret = transport_do_se_mem_map(dev, task, mem_list,
  4405. NULL, se_mem, &se_mem_lout, &se_mem_cnt,
  4406. &task_offset_in);
  4407. if (ret < 0)
  4408. goto out;
  4409. se_mem = se_mem_lout;
  4410. /*
  4411. * Setup the T_TASK(cmd)->t_mem_bidi_list -> task->task_sg_bidi
  4412. * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI
  4413. *
  4414. * Note that the first call to transport_do_se_mem_map() above will
  4415. * allocate struct se_task->task_sg_bidi in transport_do_se_mem_map()
  4416. * -> transport_calc_sg_num(), and the second here will do the
  4417. * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI.
  4418. */
  4419. if (task->task_sg_bidi != NULL) {
  4420. ret = transport_do_se_mem_map(dev, task,
  4421. T_TASK(cmd)->t_mem_bidi_list, NULL,
  4422. se_mem_bidi, &se_mem_bidi_lout, &se_mem_bidi_cnt,
  4423. &task_offset_in);
  4424. if (ret < 0)
  4425. goto out;
  4426. se_mem_bidi = se_mem_bidi_lout;
  4427. }
  4428. task_cdbs++;
  4429. DEBUG_VOL("Incremented task_cdbs(%u) task->task_sg_num(%u)\n",
  4430. task_cdbs, task->task_sg_num);
  4431. if (max_sectors_set) {
  4432. max_sectors_set = 0;
  4433. continue;
  4434. }
  4435. if (!sectors)
  4436. break;
  4437. }
  4438. if (set_counts) {
  4439. atomic_inc(&T_TASK(cmd)->t_fe_count);
  4440. atomic_inc(&T_TASK(cmd)->t_se_count);
  4441. }
  4442. DEBUG_VOL("ITT[0x%08x] total %s cdbs(%u)\n",
  4443. CMD_TFO(cmd)->get_task_tag(cmd), (data_direction == DMA_TO_DEVICE)
  4444. ? "DMA_TO_DEVICE" : "DMA_FROM_DEVICE", task_cdbs);
  4445. return task_cdbs;
  4446. out:
  4447. return 0;
  4448. }
  4449. static int
  4450. transport_map_control_cmd_to_task(struct se_cmd *cmd)
  4451. {
  4452. struct se_device *dev = SE_DEV(cmd);
  4453. unsigned char *cdb;
  4454. struct se_task *task;
  4455. int ret;
  4456. task = transport_generic_get_task(cmd, cmd->data_direction);
  4457. if (!task)
  4458. return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  4459. cdb = TRANSPORT(dev)->get_cdb(task);
  4460. if (cdb)
  4461. memcpy(cdb, cmd->t_task->t_task_cdb,
  4462. scsi_command_size(cmd->t_task->t_task_cdb));
  4463. task->task_size = cmd->data_length;
  4464. task->task_sg_num =
  4465. (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) ? 1 : 0;
  4466. atomic_inc(&cmd->t_task->t_fe_count);
  4467. atomic_inc(&cmd->t_task->t_se_count);
  4468. if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) {
  4469. struct se_mem *se_mem = NULL, *se_mem_lout = NULL;
  4470. u32 se_mem_cnt = 0, task_offset = 0;
  4471. if (!list_empty(T_TASK(cmd)->t_mem_list))
  4472. se_mem = list_entry(T_TASK(cmd)->t_mem_list->next,
  4473. struct se_mem, se_list);
  4474. ret = transport_do_se_mem_map(dev, task,
  4475. cmd->t_task->t_mem_list, NULL, se_mem,
  4476. &se_mem_lout, &se_mem_cnt, &task_offset);
  4477. if (ret < 0)
  4478. return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  4479. if (dev->transport->map_task_SG)
  4480. return dev->transport->map_task_SG(task);
  4481. return 0;
  4482. } else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB) {
  4483. if (dev->transport->map_task_non_SG)
  4484. return dev->transport->map_task_non_SG(task);
  4485. return 0;
  4486. } else if (cmd->se_cmd_flags & SCF_SCSI_NON_DATA_CDB) {
  4487. if (dev->transport->cdb_none)
  4488. return dev->transport->cdb_none(task);
  4489. return 0;
  4490. } else {
  4491. BUG();
  4492. return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  4493. }
  4494. }
  4495. /* transport_generic_new_cmd(): Called from transport_processing_thread()
  4496. *
  4497. * Allocate storage transport resources from a set of values predefined
  4498. * by transport_generic_cmd_sequencer() from the iSCSI Target RX process.
  4499. * Any non zero return here is treated as an "out of resource' op here.
  4500. */
  4501. /*
  4502. * Generate struct se_task(s) and/or their payloads for this CDB.
  4503. */
  4504. static int transport_generic_new_cmd(struct se_cmd *cmd)
  4505. {
  4506. struct se_portal_group *se_tpg;
  4507. struct se_task *task;
  4508. struct se_device *dev = SE_DEV(cmd);
  4509. int ret = 0;
  4510. /*
  4511. * Determine is the TCM fabric module has already allocated physical
  4512. * memory, and is directly calling transport_generic_map_mem_to_cmd()
  4513. * to setup beforehand the linked list of physical memory at
  4514. * T_TASK(cmd)->t_mem_list of struct se_mem->se_page
  4515. */
  4516. if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)) {
  4517. ret = transport_allocate_resources(cmd);
  4518. if (ret < 0)
  4519. return ret;
  4520. }
  4521. ret = transport_get_sectors(cmd);
  4522. if (ret < 0)
  4523. return ret;
  4524. ret = transport_new_cmd_obj(cmd);
  4525. if (ret < 0)
  4526. return ret;
  4527. /*
  4528. * Determine if the calling TCM fabric module is talking to
  4529. * Linux/NET via kernel sockets and needs to allocate a
  4530. * struct iovec array to complete the struct se_cmd
  4531. */
  4532. se_tpg = SE_LUN(cmd)->lun_sep->sep_tpg;
  4533. if (TPG_TFO(se_tpg)->alloc_cmd_iovecs != NULL) {
  4534. ret = TPG_TFO(se_tpg)->alloc_cmd_iovecs(cmd);
  4535. if (ret < 0)
  4536. return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  4537. }
  4538. if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
  4539. list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
  4540. if (atomic_read(&task->task_sent))
  4541. continue;
  4542. if (!dev->transport->map_task_SG)
  4543. continue;
  4544. ret = dev->transport->map_task_SG(task);
  4545. if (ret < 0)
  4546. return ret;
  4547. }
  4548. } else {
  4549. ret = transport_map_control_cmd_to_task(cmd);
  4550. if (ret < 0)
  4551. return ret;
  4552. }
  4553. /*
  4554. * For WRITEs, let the iSCSI Target RX Thread know its buffer is ready..
  4555. * This WRITE struct se_cmd (and all of its associated struct se_task's)
  4556. * will be added to the struct se_device execution queue after its WRITE
  4557. * data has arrived. (ie: It gets handled by the transport processing
  4558. * thread a second time)
  4559. */
  4560. if (cmd->data_direction == DMA_TO_DEVICE) {
  4561. transport_add_tasks_to_state_queue(cmd);
  4562. return transport_generic_write_pending(cmd);
  4563. }
  4564. /*
  4565. * Everything else but a WRITE, add the struct se_cmd's struct se_task's
  4566. * to the execution queue.
  4567. */
  4568. transport_execute_tasks(cmd);
  4569. return 0;
  4570. }
  4571. /* transport_generic_process_write():
  4572. *
  4573. *
  4574. */
  4575. void transport_generic_process_write(struct se_cmd *cmd)
  4576. {
  4577. #if 0
  4578. /*
  4579. * Copy SCSI Presented DTL sector(s) from received buffers allocated to
  4580. * original EDTL
  4581. */
  4582. if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
  4583. if (!T_TASK(cmd)->t_tasks_se_num) {
  4584. unsigned char *dst, *buf =
  4585. (unsigned char *)T_TASK(cmd)->t_task_buf;
  4586. dst = kzalloc(cmd->cmd_spdtl), GFP_KERNEL);
  4587. if (!(dst)) {
  4588. printk(KERN_ERR "Unable to allocate memory for"
  4589. " WRITE underflow\n");
  4590. transport_generic_request_failure(cmd, NULL,
  4591. PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
  4592. return;
  4593. }
  4594. memcpy(dst, buf, cmd->cmd_spdtl);
  4595. kfree(T_TASK(cmd)->t_task_buf);
  4596. T_TASK(cmd)->t_task_buf = dst;
  4597. } else {
  4598. struct scatterlist *sg =
  4599. (struct scatterlist *sg)T_TASK(cmd)->t_task_buf;
  4600. struct scatterlist *orig_sg;
  4601. orig_sg = kzalloc(sizeof(struct scatterlist) *
  4602. T_TASK(cmd)->t_tasks_se_num,
  4603. GFP_KERNEL))) {
  4604. if (!(orig_sg)) {
  4605. printk(KERN_ERR "Unable to allocate memory"
  4606. " for WRITE underflow\n");
  4607. transport_generic_request_failure(cmd, NULL,
  4608. PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
  4609. return;
  4610. }
  4611. memcpy(orig_sg, T_TASK(cmd)->t_task_buf,
  4612. sizeof(struct scatterlist) *
  4613. T_TASK(cmd)->t_tasks_se_num);
  4614. cmd->data_length = cmd->cmd_spdtl;
  4615. /*
  4616. * FIXME, clear out original struct se_task and state
  4617. * information.
  4618. */
  4619. if (transport_generic_new_cmd(cmd) < 0) {
  4620. transport_generic_request_failure(cmd, NULL,
  4621. PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
  4622. kfree(orig_sg);
  4623. return;
  4624. }
  4625. transport_memcpy_write_sg(cmd, orig_sg);
  4626. }
  4627. }
  4628. #endif
  4629. transport_execute_tasks(cmd);
  4630. }
  4631. EXPORT_SYMBOL(transport_generic_process_write);
  4632. /* transport_generic_write_pending():
  4633. *
  4634. *
  4635. */
  4636. static int transport_generic_write_pending(struct se_cmd *cmd)
  4637. {
  4638. unsigned long flags;
  4639. int ret;
  4640. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  4641. cmd->t_state = TRANSPORT_WRITE_PENDING;
  4642. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4643. /*
  4644. * For the TCM control CDBs using a contiguous buffer, do the memcpy
  4645. * from the passed Linux/SCSI struct scatterlist located at
  4646. * T_TASK(se_cmd)->t_task_pt_buf to the contiguous buffer at
  4647. * T_TASK(se_cmd)->t_task_buf.
  4648. */
  4649. if (cmd->se_cmd_flags & SCF_PASSTHROUGH_CONTIG_TO_SG)
  4650. transport_memcpy_read_contig(cmd,
  4651. T_TASK(cmd)->t_task_buf,
  4652. T_TASK(cmd)->t_task_pt_sgl);
  4653. /*
  4654. * Clear the se_cmd for WRITE_PENDING status in order to set
  4655. * T_TASK(cmd)->t_transport_active=0 so that transport_generic_handle_data
  4656. * can be called from HW target mode interrupt code. This is safe
  4657. * to be called with transport_off=1 before the CMD_TFO(cmd)->write_pending
  4658. * because the se_cmd->se_lun pointer is not being cleared.
  4659. */
  4660. transport_cmd_check_stop(cmd, 1, 0);
  4661. /*
  4662. * Call the fabric write_pending function here to let the
  4663. * frontend know that WRITE buffers are ready.
  4664. */
  4665. ret = CMD_TFO(cmd)->write_pending(cmd);
  4666. if (ret < 0)
  4667. return ret;
  4668. return PYX_TRANSPORT_WRITE_PENDING;
  4669. }
  4670. /* transport_release_cmd_to_pool():
  4671. *
  4672. *
  4673. */
  4674. void transport_release_cmd_to_pool(struct se_cmd *cmd)
  4675. {
  4676. BUG_ON(!T_TASK(cmd));
  4677. BUG_ON(!CMD_TFO(cmd));
  4678. transport_free_se_cmd(cmd);
  4679. CMD_TFO(cmd)->release_cmd_to_pool(cmd);
  4680. }
  4681. EXPORT_SYMBOL(transport_release_cmd_to_pool);
  4682. /* transport_generic_free_cmd():
  4683. *
  4684. * Called from processing frontend to release storage engine resources
  4685. */
  4686. void transport_generic_free_cmd(
  4687. struct se_cmd *cmd,
  4688. int wait_for_tasks,
  4689. int release_to_pool,
  4690. int session_reinstatement)
  4691. {
  4692. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) || !T_TASK(cmd))
  4693. transport_release_cmd_to_pool(cmd);
  4694. else {
  4695. core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
  4696. if (SE_LUN(cmd)) {
  4697. #if 0
  4698. printk(KERN_INFO "cmd: %p ITT: 0x%08x contains"
  4699. " SE_LUN(cmd)\n", cmd,
  4700. CMD_TFO(cmd)->get_task_tag(cmd));
  4701. #endif
  4702. transport_lun_remove_cmd(cmd);
  4703. }
  4704. if (wait_for_tasks && cmd->transport_wait_for_tasks)
  4705. cmd->transport_wait_for_tasks(cmd, 0, 0);
  4706. transport_generic_remove(cmd, release_to_pool,
  4707. session_reinstatement);
  4708. }
  4709. }
  4710. EXPORT_SYMBOL(transport_generic_free_cmd);
  4711. static void transport_nop_wait_for_tasks(
  4712. struct se_cmd *cmd,
  4713. int remove_cmd,
  4714. int session_reinstatement)
  4715. {
  4716. return;
  4717. }
  4718. /* transport_lun_wait_for_tasks():
  4719. *
  4720. * Called from ConfigFS context to stop the passed struct se_cmd to allow
  4721. * an struct se_lun to be successfully shutdown.
  4722. */
  4723. static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
  4724. {
  4725. unsigned long flags;
  4726. int ret;
  4727. /*
  4728. * If the frontend has already requested this struct se_cmd to
  4729. * be stopped, we can safely ignore this struct se_cmd.
  4730. */
  4731. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  4732. if (atomic_read(&T_TASK(cmd)->t_transport_stop)) {
  4733. atomic_set(&T_TASK(cmd)->transport_lun_stop, 0);
  4734. DEBUG_TRANSPORT_S("ConfigFS ITT[0x%08x] - t_transport_stop =="
  4735. " TRUE, skipping\n", CMD_TFO(cmd)->get_task_tag(cmd));
  4736. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4737. transport_cmd_check_stop(cmd, 1, 0);
  4738. return -1;
  4739. }
  4740. atomic_set(&T_TASK(cmd)->transport_lun_fe_stop, 1);
  4741. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4742. wake_up_interruptible(&SE_DEV(cmd)->dev_queue_obj->thread_wq);
  4743. ret = transport_stop_tasks_for_cmd(cmd);
  4744. DEBUG_TRANSPORT_S("ConfigFS: cmd: %p t_task_cdbs: %d stop tasks ret:"
  4745. " %d\n", cmd, T_TASK(cmd)->t_task_cdbs, ret);
  4746. if (!ret) {
  4747. DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
  4748. CMD_TFO(cmd)->get_task_tag(cmd));
  4749. wait_for_completion(&T_TASK(cmd)->transport_lun_stop_comp);
  4750. DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
  4751. CMD_TFO(cmd)->get_task_tag(cmd));
  4752. }
  4753. transport_remove_cmd_from_queue(cmd, SE_DEV(cmd)->dev_queue_obj);
  4754. return 0;
  4755. }
  4756. /* #define DEBUG_CLEAR_LUN */
  4757. #ifdef DEBUG_CLEAR_LUN
  4758. #define DEBUG_CLEAR_L(x...) printk(KERN_INFO x)
  4759. #else
  4760. #define DEBUG_CLEAR_L(x...)
  4761. #endif
  4762. static void __transport_clear_lun_from_sessions(struct se_lun *lun)
  4763. {
  4764. struct se_cmd *cmd = NULL;
  4765. unsigned long lun_flags, cmd_flags;
  4766. /*
  4767. * Do exception processing and return CHECK_CONDITION status to the
  4768. * Initiator Port.
  4769. */
  4770. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4771. while (!list_empty_careful(&lun->lun_cmd_list)) {
  4772. cmd = list_entry(lun->lun_cmd_list.next,
  4773. struct se_cmd, se_lun_list);
  4774. list_del(&cmd->se_lun_list);
  4775. if (!(T_TASK(cmd))) {
  4776. printk(KERN_ERR "ITT: 0x%08x, T_TASK(cmd) = NULL"
  4777. "[i,t]_state: %u/%u\n",
  4778. CMD_TFO(cmd)->get_task_tag(cmd),
  4779. CMD_TFO(cmd)->get_cmd_state(cmd), cmd->t_state);
  4780. BUG();
  4781. }
  4782. atomic_set(&T_TASK(cmd)->transport_lun_active, 0);
  4783. /*
  4784. * This will notify iscsi_target_transport.c:
  4785. * transport_cmd_check_stop() that a LUN shutdown is in
  4786. * progress for the iscsi_cmd_t.
  4787. */
  4788. spin_lock(&T_TASK(cmd)->t_state_lock);
  4789. DEBUG_CLEAR_L("SE_LUN[%d] - Setting T_TASK(cmd)->transport"
  4790. "_lun_stop for ITT: 0x%08x\n",
  4791. SE_LUN(cmd)->unpacked_lun,
  4792. CMD_TFO(cmd)->get_task_tag(cmd));
  4793. atomic_set(&T_TASK(cmd)->transport_lun_stop, 1);
  4794. spin_unlock(&T_TASK(cmd)->t_state_lock);
  4795. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  4796. if (!(SE_LUN(cmd))) {
  4797. printk(KERN_ERR "ITT: 0x%08x, [i,t]_state: %u/%u\n",
  4798. CMD_TFO(cmd)->get_task_tag(cmd),
  4799. CMD_TFO(cmd)->get_cmd_state(cmd), cmd->t_state);
  4800. BUG();
  4801. }
  4802. /*
  4803. * If the Storage engine still owns the iscsi_cmd_t, determine
  4804. * and/or stop its context.
  4805. */
  4806. DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x before transport"
  4807. "_lun_wait_for_tasks()\n", SE_LUN(cmd)->unpacked_lun,
  4808. CMD_TFO(cmd)->get_task_tag(cmd));
  4809. if (transport_lun_wait_for_tasks(cmd, SE_LUN(cmd)) < 0) {
  4810. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4811. continue;
  4812. }
  4813. DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
  4814. "_wait_for_tasks(): SUCCESS\n",
  4815. SE_LUN(cmd)->unpacked_lun,
  4816. CMD_TFO(cmd)->get_task_tag(cmd));
  4817. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, cmd_flags);
  4818. if (!(atomic_read(&T_TASK(cmd)->transport_dev_active))) {
  4819. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, cmd_flags);
  4820. goto check_cond;
  4821. }
  4822. atomic_set(&T_TASK(cmd)->transport_dev_active, 0);
  4823. transport_all_task_dev_remove_state(cmd);
  4824. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, cmd_flags);
  4825. transport_free_dev_tasks(cmd);
  4826. /*
  4827. * The Storage engine stopped this struct se_cmd before it was
  4828. * send to the fabric frontend for delivery back to the
  4829. * Initiator Node. Return this SCSI CDB back with an
  4830. * CHECK_CONDITION status.
  4831. */
  4832. check_cond:
  4833. transport_send_check_condition_and_sense(cmd,
  4834. TCM_NON_EXISTENT_LUN, 0);
  4835. /*
  4836. * If the fabric frontend is waiting for this iscsi_cmd_t to
  4837. * be released, notify the waiting thread now that LU has
  4838. * finished accessing it.
  4839. */
  4840. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, cmd_flags);
  4841. if (atomic_read(&T_TASK(cmd)->transport_lun_fe_stop)) {
  4842. DEBUG_CLEAR_L("SE_LUN[%d] - Detected FE stop for"
  4843. " struct se_cmd: %p ITT: 0x%08x\n",
  4844. lun->unpacked_lun,
  4845. cmd, CMD_TFO(cmd)->get_task_tag(cmd));
  4846. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  4847. cmd_flags);
  4848. transport_cmd_check_stop(cmd, 1, 0);
  4849. complete(&T_TASK(cmd)->transport_lun_fe_stop_comp);
  4850. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4851. continue;
  4852. }
  4853. DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
  4854. lun->unpacked_lun, CMD_TFO(cmd)->get_task_tag(cmd));
  4855. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, cmd_flags);
  4856. spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
  4857. }
  4858. spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
  4859. }
  4860. static int transport_clear_lun_thread(void *p)
  4861. {
  4862. struct se_lun *lun = (struct se_lun *)p;
  4863. __transport_clear_lun_from_sessions(lun);
  4864. complete(&lun->lun_shutdown_comp);
  4865. return 0;
  4866. }
  4867. int transport_clear_lun_from_sessions(struct se_lun *lun)
  4868. {
  4869. struct task_struct *kt;
  4870. kt = kthread_run(transport_clear_lun_thread, (void *)lun,
  4871. "tcm_cl_%u", lun->unpacked_lun);
  4872. if (IS_ERR(kt)) {
  4873. printk(KERN_ERR "Unable to start clear_lun thread\n");
  4874. return -1;
  4875. }
  4876. wait_for_completion(&lun->lun_shutdown_comp);
  4877. return 0;
  4878. }
  4879. /* transport_generic_wait_for_tasks():
  4880. *
  4881. * Called from frontend or passthrough context to wait for storage engine
  4882. * to pause and/or release frontend generated struct se_cmd.
  4883. */
  4884. static void transport_generic_wait_for_tasks(
  4885. struct se_cmd *cmd,
  4886. int remove_cmd,
  4887. int session_reinstatement)
  4888. {
  4889. unsigned long flags;
  4890. if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req))
  4891. return;
  4892. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  4893. /*
  4894. * If we are already stopped due to an external event (ie: LUN shutdown)
  4895. * sleep until the connection can have the passed struct se_cmd back.
  4896. * The T_TASK(cmd)->transport_lun_stopped_sem will be upped by
  4897. * transport_clear_lun_from_sessions() once the ConfigFS context caller
  4898. * has completed its operation on the struct se_cmd.
  4899. */
  4900. if (atomic_read(&T_TASK(cmd)->transport_lun_stop)) {
  4901. DEBUG_TRANSPORT_S("wait_for_tasks: Stopping"
  4902. " wait_for_completion(&T_TASK(cmd)transport_lun_fe"
  4903. "_stop_comp); for ITT: 0x%08x\n",
  4904. CMD_TFO(cmd)->get_task_tag(cmd));
  4905. /*
  4906. * There is a special case for WRITES where a FE exception +
  4907. * LUN shutdown means ConfigFS context is still sleeping on
  4908. * transport_lun_stop_comp in transport_lun_wait_for_tasks().
  4909. * We go ahead and up transport_lun_stop_comp just to be sure
  4910. * here.
  4911. */
  4912. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4913. complete(&T_TASK(cmd)->transport_lun_stop_comp);
  4914. wait_for_completion(&T_TASK(cmd)->transport_lun_fe_stop_comp);
  4915. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  4916. transport_all_task_dev_remove_state(cmd);
  4917. /*
  4918. * At this point, the frontend who was the originator of this
  4919. * struct se_cmd, now owns the structure and can be released through
  4920. * normal means below.
  4921. */
  4922. DEBUG_TRANSPORT_S("wait_for_tasks: Stopped"
  4923. " wait_for_completion(&T_TASK(cmd)transport_lun_fe_"
  4924. "stop_comp); for ITT: 0x%08x\n",
  4925. CMD_TFO(cmd)->get_task_tag(cmd));
  4926. atomic_set(&T_TASK(cmd)->transport_lun_stop, 0);
  4927. }
  4928. if (!atomic_read(&T_TASK(cmd)->t_transport_active) ||
  4929. atomic_read(&T_TASK(cmd)->t_transport_aborted))
  4930. goto remove;
  4931. atomic_set(&T_TASK(cmd)->t_transport_stop, 1);
  4932. DEBUG_TRANSPORT_S("wait_for_tasks: Stopping %p ITT: 0x%08x"
  4933. " i_state: %d, t_state/def_t_state: %d/%d, t_transport_stop"
  4934. " = TRUE\n", cmd, CMD_TFO(cmd)->get_task_tag(cmd),
  4935. CMD_TFO(cmd)->get_cmd_state(cmd), cmd->t_state,
  4936. cmd->deferred_t_state);
  4937. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4938. wake_up_interruptible(&SE_DEV(cmd)->dev_queue_obj->thread_wq);
  4939. wait_for_completion(&T_TASK(cmd)->t_transport_stop_comp);
  4940. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  4941. atomic_set(&T_TASK(cmd)->t_transport_active, 0);
  4942. atomic_set(&T_TASK(cmd)->t_transport_stop, 0);
  4943. DEBUG_TRANSPORT_S("wait_for_tasks: Stopped wait_for_compltion("
  4944. "&T_TASK(cmd)->t_transport_stop_comp) for ITT: 0x%08x\n",
  4945. CMD_TFO(cmd)->get_task_tag(cmd));
  4946. remove:
  4947. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4948. if (!remove_cmd)
  4949. return;
  4950. transport_generic_free_cmd(cmd, 0, 0, session_reinstatement);
  4951. }
  4952. static int transport_get_sense_codes(
  4953. struct se_cmd *cmd,
  4954. u8 *asc,
  4955. u8 *ascq)
  4956. {
  4957. *asc = cmd->scsi_asc;
  4958. *ascq = cmd->scsi_ascq;
  4959. return 0;
  4960. }
  4961. static int transport_set_sense_codes(
  4962. struct se_cmd *cmd,
  4963. u8 asc,
  4964. u8 ascq)
  4965. {
  4966. cmd->scsi_asc = asc;
  4967. cmd->scsi_ascq = ascq;
  4968. return 0;
  4969. }
  4970. int transport_send_check_condition_and_sense(
  4971. struct se_cmd *cmd,
  4972. u8 reason,
  4973. int from_transport)
  4974. {
  4975. unsigned char *buffer = cmd->sense_buffer;
  4976. unsigned long flags;
  4977. int offset;
  4978. u8 asc = 0, ascq = 0;
  4979. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  4980. if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  4981. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4982. return 0;
  4983. }
  4984. cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
  4985. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  4986. if (!reason && from_transport)
  4987. goto after_reason;
  4988. if (!from_transport)
  4989. cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
  4990. /*
  4991. * Data Segment and SenseLength of the fabric response PDU.
  4992. *
  4993. * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
  4994. * from include/scsi/scsi_cmnd.h
  4995. */
  4996. offset = CMD_TFO(cmd)->set_fabric_sense_len(cmd,
  4997. TRANSPORT_SENSE_BUFFER);
  4998. /*
  4999. * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
  5000. * SENSE KEY values from include/scsi/scsi.h
  5001. */
  5002. switch (reason) {
  5003. case TCM_NON_EXISTENT_LUN:
  5004. case TCM_UNSUPPORTED_SCSI_OPCODE:
  5005. case TCM_SECTOR_COUNT_TOO_MANY:
  5006. /* CURRENT ERROR */
  5007. buffer[offset] = 0x70;
  5008. /* ILLEGAL REQUEST */
  5009. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  5010. /* INVALID COMMAND OPERATION CODE */
  5011. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
  5012. break;
  5013. case TCM_UNKNOWN_MODE_PAGE:
  5014. /* CURRENT ERROR */
  5015. buffer[offset] = 0x70;
  5016. /* ILLEGAL REQUEST */
  5017. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  5018. /* INVALID FIELD IN CDB */
  5019. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  5020. break;
  5021. case TCM_CHECK_CONDITION_ABORT_CMD:
  5022. /* CURRENT ERROR */
  5023. buffer[offset] = 0x70;
  5024. /* ABORTED COMMAND */
  5025. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5026. /* BUS DEVICE RESET FUNCTION OCCURRED */
  5027. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
  5028. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
  5029. break;
  5030. case TCM_INCORRECT_AMOUNT_OF_DATA:
  5031. /* CURRENT ERROR */
  5032. buffer[offset] = 0x70;
  5033. /* ABORTED COMMAND */
  5034. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5035. /* WRITE ERROR */
  5036. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  5037. /* NOT ENOUGH UNSOLICITED DATA */
  5038. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
  5039. break;
  5040. case TCM_INVALID_CDB_FIELD:
  5041. /* CURRENT ERROR */
  5042. buffer[offset] = 0x70;
  5043. /* ABORTED COMMAND */
  5044. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5045. /* INVALID FIELD IN CDB */
  5046. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
  5047. break;
  5048. case TCM_INVALID_PARAMETER_LIST:
  5049. /* CURRENT ERROR */
  5050. buffer[offset] = 0x70;
  5051. /* ABORTED COMMAND */
  5052. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5053. /* INVALID FIELD IN PARAMETER LIST */
  5054. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
  5055. break;
  5056. case TCM_UNEXPECTED_UNSOLICITED_DATA:
  5057. /* CURRENT ERROR */
  5058. buffer[offset] = 0x70;
  5059. /* ABORTED COMMAND */
  5060. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5061. /* WRITE ERROR */
  5062. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
  5063. /* UNEXPECTED_UNSOLICITED_DATA */
  5064. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
  5065. break;
  5066. case TCM_SERVICE_CRC_ERROR:
  5067. /* CURRENT ERROR */
  5068. buffer[offset] = 0x70;
  5069. /* ABORTED COMMAND */
  5070. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5071. /* PROTOCOL SERVICE CRC ERROR */
  5072. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
  5073. /* N/A */
  5074. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
  5075. break;
  5076. case TCM_SNACK_REJECTED:
  5077. /* CURRENT ERROR */
  5078. buffer[offset] = 0x70;
  5079. /* ABORTED COMMAND */
  5080. buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
  5081. /* READ ERROR */
  5082. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
  5083. /* FAILED RETRANSMISSION REQUEST */
  5084. buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
  5085. break;
  5086. case TCM_WRITE_PROTECTED:
  5087. /* CURRENT ERROR */
  5088. buffer[offset] = 0x70;
  5089. /* DATA PROTECT */
  5090. buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
  5091. /* WRITE PROTECTED */
  5092. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
  5093. break;
  5094. case TCM_CHECK_CONDITION_UNIT_ATTENTION:
  5095. /* CURRENT ERROR */
  5096. buffer[offset] = 0x70;
  5097. /* UNIT ATTENTION */
  5098. buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  5099. core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
  5100. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  5101. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  5102. break;
  5103. case TCM_CHECK_CONDITION_NOT_READY:
  5104. /* CURRENT ERROR */
  5105. buffer[offset] = 0x70;
  5106. /* Not Ready */
  5107. buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
  5108. transport_get_sense_codes(cmd, &asc, &ascq);
  5109. buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
  5110. buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
  5111. break;
  5112. case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
  5113. default:
  5114. /* CURRENT ERROR */
  5115. buffer[offset] = 0x70;
  5116. /* ILLEGAL REQUEST */
  5117. buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
  5118. /* LOGICAL UNIT COMMUNICATION FAILURE */
  5119. buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
  5120. break;
  5121. }
  5122. /*
  5123. * This code uses linux/include/scsi/scsi.h SAM status codes!
  5124. */
  5125. cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
  5126. /*
  5127. * Automatically padded, this value is encoded in the fabric's
  5128. * data_length response PDU containing the SCSI defined sense data.
  5129. */
  5130. cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
  5131. after_reason:
  5132. CMD_TFO(cmd)->queue_status(cmd);
  5133. return 0;
  5134. }
  5135. EXPORT_SYMBOL(transport_send_check_condition_and_sense);
  5136. int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
  5137. {
  5138. int ret = 0;
  5139. if (atomic_read(&T_TASK(cmd)->t_transport_aborted) != 0) {
  5140. if (!(send_status) ||
  5141. (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
  5142. return 1;
  5143. #if 0
  5144. printk(KERN_INFO "Sending delayed SAM_STAT_TASK_ABORTED"
  5145. " status for CDB: 0x%02x ITT: 0x%08x\n",
  5146. T_TASK(cmd)->t_task_cdb[0],
  5147. CMD_TFO(cmd)->get_task_tag(cmd));
  5148. #endif
  5149. cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
  5150. CMD_TFO(cmd)->queue_status(cmd);
  5151. ret = 1;
  5152. }
  5153. return ret;
  5154. }
  5155. EXPORT_SYMBOL(transport_check_aborted_status);
  5156. void transport_send_task_abort(struct se_cmd *cmd)
  5157. {
  5158. /*
  5159. * If there are still expected incoming fabric WRITEs, we wait
  5160. * until until they have completed before sending a TASK_ABORTED
  5161. * response. This response with TASK_ABORTED status will be
  5162. * queued back to fabric module by transport_check_aborted_status().
  5163. */
  5164. if (cmd->data_direction == DMA_TO_DEVICE) {
  5165. if (CMD_TFO(cmd)->write_pending_status(cmd) != 0) {
  5166. atomic_inc(&T_TASK(cmd)->t_transport_aborted);
  5167. smp_mb__after_atomic_inc();
  5168. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  5169. transport_new_cmd_failure(cmd);
  5170. return;
  5171. }
  5172. }
  5173. cmd->scsi_status = SAM_STAT_TASK_ABORTED;
  5174. #if 0
  5175. printk(KERN_INFO "Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
  5176. " ITT: 0x%08x\n", T_TASK(cmd)->t_task_cdb[0],
  5177. CMD_TFO(cmd)->get_task_tag(cmd));
  5178. #endif
  5179. CMD_TFO(cmd)->queue_status(cmd);
  5180. }
  5181. /* transport_generic_do_tmr():
  5182. *
  5183. *
  5184. */
  5185. int transport_generic_do_tmr(struct se_cmd *cmd)
  5186. {
  5187. struct se_cmd *ref_cmd;
  5188. struct se_device *dev = SE_DEV(cmd);
  5189. struct se_tmr_req *tmr = cmd->se_tmr_req;
  5190. int ret;
  5191. switch (tmr->function) {
  5192. case ABORT_TASK:
  5193. ref_cmd = tmr->ref_cmd;
  5194. tmr->response = TMR_FUNCTION_REJECTED;
  5195. break;
  5196. case ABORT_TASK_SET:
  5197. case CLEAR_ACA:
  5198. case CLEAR_TASK_SET:
  5199. tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  5200. break;
  5201. case LUN_RESET:
  5202. ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
  5203. tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
  5204. TMR_FUNCTION_REJECTED;
  5205. break;
  5206. #if 0
  5207. case TARGET_WARM_RESET:
  5208. transport_generic_host_reset(dev->se_hba);
  5209. tmr->response = TMR_FUNCTION_REJECTED;
  5210. break;
  5211. case TARGET_COLD_RESET:
  5212. transport_generic_host_reset(dev->se_hba);
  5213. transport_generic_cold_reset(dev->se_hba);
  5214. tmr->response = TMR_FUNCTION_REJECTED;
  5215. break;
  5216. #endif
  5217. default:
  5218. printk(KERN_ERR "Uknown TMR function: 0x%02x.\n",
  5219. tmr->function);
  5220. tmr->response = TMR_FUNCTION_REJECTED;
  5221. break;
  5222. }
  5223. cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
  5224. CMD_TFO(cmd)->queue_tm_rsp(cmd);
  5225. transport_cmd_check_stop(cmd, 2, 0);
  5226. return 0;
  5227. }
  5228. /*
  5229. * Called with spin_lock_irq(&dev->execute_task_lock); held
  5230. *
  5231. */
  5232. static struct se_task *
  5233. transport_get_task_from_state_list(struct se_device *dev)
  5234. {
  5235. struct se_task *task;
  5236. if (list_empty(&dev->state_task_list))
  5237. return NULL;
  5238. list_for_each_entry(task, &dev->state_task_list, t_state_list)
  5239. break;
  5240. list_del(&task->t_state_list);
  5241. atomic_set(&task->task_state_active, 0);
  5242. return task;
  5243. }
  5244. static void transport_processing_shutdown(struct se_device *dev)
  5245. {
  5246. struct se_cmd *cmd;
  5247. struct se_queue_req *qr;
  5248. struct se_task *task;
  5249. u8 state;
  5250. unsigned long flags;
  5251. /*
  5252. * Empty the struct se_device's struct se_task state list.
  5253. */
  5254. spin_lock_irqsave(&dev->execute_task_lock, flags);
  5255. while ((task = transport_get_task_from_state_list(dev))) {
  5256. if (!(TASK_CMD(task))) {
  5257. printk(KERN_ERR "TASK_CMD(task) is NULL!\n");
  5258. continue;
  5259. }
  5260. cmd = TASK_CMD(task);
  5261. if (!T_TASK(cmd)) {
  5262. printk(KERN_ERR "T_TASK(cmd) is NULL for task: %p cmd:"
  5263. " %p ITT: 0x%08x\n", task, cmd,
  5264. CMD_TFO(cmd)->get_task_tag(cmd));
  5265. continue;
  5266. }
  5267. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  5268. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  5269. DEBUG_DO("PT: cmd: %p task: %p ITT/CmdSN: 0x%08x/0x%08x,"
  5270. " i_state/def_i_state: %d/%d, t_state/def_t_state:"
  5271. " %d/%d cdb: 0x%02x\n", cmd, task,
  5272. CMD_TFO(cmd)->get_task_tag(cmd), cmd->cmd_sn,
  5273. CMD_TFO(cmd)->get_cmd_state(cmd), cmd->deferred_i_state,
  5274. cmd->t_state, cmd->deferred_t_state,
  5275. T_TASK(cmd)->t_task_cdb[0]);
  5276. DEBUG_DO("PT: ITT[0x%08x] - t_task_cdbs: %d t_task_cdbs_left:"
  5277. " %d t_task_cdbs_sent: %d -- t_transport_active: %d"
  5278. " t_transport_stop: %d t_transport_sent: %d\n",
  5279. CMD_TFO(cmd)->get_task_tag(cmd),
  5280. T_TASK(cmd)->t_task_cdbs,
  5281. atomic_read(&T_TASK(cmd)->t_task_cdbs_left),
  5282. atomic_read(&T_TASK(cmd)->t_task_cdbs_sent),
  5283. atomic_read(&T_TASK(cmd)->t_transport_active),
  5284. atomic_read(&T_TASK(cmd)->t_transport_stop),
  5285. atomic_read(&T_TASK(cmd)->t_transport_sent));
  5286. if (atomic_read(&task->task_active)) {
  5287. atomic_set(&task->task_stop, 1);
  5288. spin_unlock_irqrestore(
  5289. &T_TASK(cmd)->t_state_lock, flags);
  5290. DEBUG_DO("Waiting for task: %p to shutdown for dev:"
  5291. " %p\n", task, dev);
  5292. wait_for_completion(&task->task_stop_comp);
  5293. DEBUG_DO("Completed task: %p shutdown for dev: %p\n",
  5294. task, dev);
  5295. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  5296. atomic_dec(&T_TASK(cmd)->t_task_cdbs_left);
  5297. atomic_set(&task->task_active, 0);
  5298. atomic_set(&task->task_stop, 0);
  5299. } else {
  5300. if (atomic_read(&task->task_execute_queue) != 0)
  5301. transport_remove_task_from_execute_queue(task, dev);
  5302. }
  5303. __transport_stop_task_timer(task, &flags);
  5304. if (!(atomic_dec_and_test(&T_TASK(cmd)->t_task_cdbs_ex_left))) {
  5305. spin_unlock_irqrestore(
  5306. &T_TASK(cmd)->t_state_lock, flags);
  5307. DEBUG_DO("Skipping task: %p, dev: %p for"
  5308. " t_task_cdbs_ex_left: %d\n", task, dev,
  5309. atomic_read(&T_TASK(cmd)->t_task_cdbs_ex_left));
  5310. spin_lock_irqsave(&dev->execute_task_lock, flags);
  5311. continue;
  5312. }
  5313. if (atomic_read(&T_TASK(cmd)->t_transport_active)) {
  5314. DEBUG_DO("got t_transport_active = 1 for task: %p, dev:"
  5315. " %p\n", task, dev);
  5316. if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
  5317. spin_unlock_irqrestore(
  5318. &T_TASK(cmd)->t_state_lock, flags);
  5319. transport_send_check_condition_and_sense(
  5320. cmd, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE,
  5321. 0);
  5322. transport_remove_cmd_from_queue(cmd,
  5323. SE_DEV(cmd)->dev_queue_obj);
  5324. transport_lun_remove_cmd(cmd);
  5325. transport_cmd_check_stop(cmd, 1, 0);
  5326. } else {
  5327. spin_unlock_irqrestore(
  5328. &T_TASK(cmd)->t_state_lock, flags);
  5329. transport_remove_cmd_from_queue(cmd,
  5330. SE_DEV(cmd)->dev_queue_obj);
  5331. transport_lun_remove_cmd(cmd);
  5332. if (transport_cmd_check_stop(cmd, 1, 0))
  5333. transport_generic_remove(cmd, 0, 0);
  5334. }
  5335. spin_lock_irqsave(&dev->execute_task_lock, flags);
  5336. continue;
  5337. }
  5338. DEBUG_DO("Got t_transport_active = 0 for task: %p, dev: %p\n",
  5339. task, dev);
  5340. if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
  5341. spin_unlock_irqrestore(
  5342. &T_TASK(cmd)->t_state_lock, flags);
  5343. transport_send_check_condition_and_sense(cmd,
  5344. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  5345. transport_remove_cmd_from_queue(cmd,
  5346. SE_DEV(cmd)->dev_queue_obj);
  5347. transport_lun_remove_cmd(cmd);
  5348. transport_cmd_check_stop(cmd, 1, 0);
  5349. } else {
  5350. spin_unlock_irqrestore(
  5351. &T_TASK(cmd)->t_state_lock, flags);
  5352. transport_remove_cmd_from_queue(cmd,
  5353. SE_DEV(cmd)->dev_queue_obj);
  5354. transport_lun_remove_cmd(cmd);
  5355. if (transport_cmd_check_stop(cmd, 1, 0))
  5356. transport_generic_remove(cmd, 0, 0);
  5357. }
  5358. spin_lock_irqsave(&dev->execute_task_lock, flags);
  5359. }
  5360. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  5361. /*
  5362. * Empty the struct se_device's struct se_cmd list.
  5363. */
  5364. spin_lock_irqsave(&dev->dev_queue_obj->cmd_queue_lock, flags);
  5365. while ((qr = __transport_get_qr_from_queue(dev->dev_queue_obj))) {
  5366. spin_unlock_irqrestore(
  5367. &dev->dev_queue_obj->cmd_queue_lock, flags);
  5368. cmd = (struct se_cmd *)qr->cmd;
  5369. state = qr->state;
  5370. kfree(qr);
  5371. DEBUG_DO("From Device Queue: cmd: %p t_state: %d\n",
  5372. cmd, state);
  5373. if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
  5374. transport_send_check_condition_and_sense(cmd,
  5375. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  5376. transport_lun_remove_cmd(cmd);
  5377. transport_cmd_check_stop(cmd, 1, 0);
  5378. } else {
  5379. transport_lun_remove_cmd(cmd);
  5380. if (transport_cmd_check_stop(cmd, 1, 0))
  5381. transport_generic_remove(cmd, 0, 0);
  5382. }
  5383. spin_lock_irqsave(&dev->dev_queue_obj->cmd_queue_lock, flags);
  5384. }
  5385. spin_unlock_irqrestore(&dev->dev_queue_obj->cmd_queue_lock, flags);
  5386. }
  5387. /* transport_processing_thread():
  5388. *
  5389. *
  5390. */
  5391. static int transport_processing_thread(void *param)
  5392. {
  5393. int ret, t_state;
  5394. struct se_cmd *cmd;
  5395. struct se_device *dev = (struct se_device *) param;
  5396. struct se_queue_req *qr;
  5397. set_user_nice(current, -20);
  5398. while (!kthread_should_stop()) {
  5399. ret = wait_event_interruptible(dev->dev_queue_obj->thread_wq,
  5400. atomic_read(&dev->dev_queue_obj->queue_cnt) ||
  5401. kthread_should_stop());
  5402. if (ret < 0)
  5403. goto out;
  5404. spin_lock_irq(&dev->dev_status_lock);
  5405. if (dev->dev_status & TRANSPORT_DEVICE_SHUTDOWN) {
  5406. spin_unlock_irq(&dev->dev_status_lock);
  5407. transport_processing_shutdown(dev);
  5408. continue;
  5409. }
  5410. spin_unlock_irq(&dev->dev_status_lock);
  5411. get_cmd:
  5412. __transport_execute_tasks(dev);
  5413. qr = transport_get_qr_from_queue(dev->dev_queue_obj);
  5414. if (!(qr))
  5415. continue;
  5416. cmd = (struct se_cmd *)qr->cmd;
  5417. t_state = qr->state;
  5418. kfree(qr);
  5419. switch (t_state) {
  5420. case TRANSPORT_NEW_CMD_MAP:
  5421. if (!(CMD_TFO(cmd)->new_cmd_map)) {
  5422. printk(KERN_ERR "CMD_TFO(cmd)->new_cmd_map is"
  5423. " NULL for TRANSPORT_NEW_CMD_MAP\n");
  5424. BUG();
  5425. }
  5426. ret = CMD_TFO(cmd)->new_cmd_map(cmd);
  5427. if (ret < 0) {
  5428. cmd->transport_error_status = ret;
  5429. transport_generic_request_failure(cmd, NULL,
  5430. 0, (cmd->data_direction !=
  5431. DMA_TO_DEVICE));
  5432. break;
  5433. }
  5434. /* Fall through */
  5435. case TRANSPORT_NEW_CMD:
  5436. ret = transport_generic_new_cmd(cmd);
  5437. if (ret < 0) {
  5438. cmd->transport_error_status = ret;
  5439. transport_generic_request_failure(cmd, NULL,
  5440. 0, (cmd->data_direction !=
  5441. DMA_TO_DEVICE));
  5442. }
  5443. break;
  5444. case TRANSPORT_PROCESS_WRITE:
  5445. transport_generic_process_write(cmd);
  5446. break;
  5447. case TRANSPORT_COMPLETE_OK:
  5448. transport_stop_all_task_timers(cmd);
  5449. transport_generic_complete_ok(cmd);
  5450. break;
  5451. case TRANSPORT_REMOVE:
  5452. transport_generic_remove(cmd, 1, 0);
  5453. break;
  5454. case TRANSPORT_PROCESS_TMR:
  5455. transport_generic_do_tmr(cmd);
  5456. break;
  5457. case TRANSPORT_COMPLETE_FAILURE:
  5458. transport_generic_request_failure(cmd, NULL, 1, 1);
  5459. break;
  5460. case TRANSPORT_COMPLETE_TIMEOUT:
  5461. transport_stop_all_task_timers(cmd);
  5462. transport_generic_request_timeout(cmd);
  5463. break;
  5464. default:
  5465. printk(KERN_ERR "Unknown t_state: %d deferred_t_state:"
  5466. " %d for ITT: 0x%08x i_state: %d on SE LUN:"
  5467. " %u\n", t_state, cmd->deferred_t_state,
  5468. CMD_TFO(cmd)->get_task_tag(cmd),
  5469. CMD_TFO(cmd)->get_cmd_state(cmd),
  5470. SE_LUN(cmd)->unpacked_lun);
  5471. BUG();
  5472. }
  5473. goto get_cmd;
  5474. }
  5475. out:
  5476. transport_release_all_cmds(dev);
  5477. dev->process_thread = NULL;
  5478. return 0;
  5479. }