target_core_transport.c 167 KB

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