target_core_transport.c 169 KB

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