target_core_transport.c 162 KB

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