target_core_transport.c 168 KB

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