target_core_transport.c 168 KB

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