target_core_transport.c 166 KB

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